From 301e44ecbb70dc4ee5f384f46355cf182dc89562 Mon Sep 17 00:00:00 2001 From: ulrich <undisclosed> Date: Tue, 01 Aug 2017 12:34:15 +0000 Subject: [PATCH] Redundanz entfernt --- src/java/de/uhilger/filecms/api/Api.java | 102 ++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/java/de/uhilger/filecms/api/Api.java b/src/java/de/uhilger/filecms/api/Api.java index 3da69f3..507c2ae 100644 --- a/src/java/de/uhilger/filecms/api/Api.java +++ b/src/java/de/uhilger/filecms/api/Api.java @@ -22,6 +22,7 @@ import de.uhilger.filecms.data.FileRef; import de.uhilger.filecms.web.Initialiser; +import de.uhilger.transit.Access; import de.uhilger.transit.web.RequestKontext; import de.uhilger.transit.web.WebKontext; import java.io.File; @@ -35,6 +36,11 @@ */ public abstract class Api implements WebKontext, RequestKontext { + protected ServletContext ctx; + + /** Zeiger zum Request, der zur Ausfuehrung fuehrte */ + protected HttpServletRequest request; + private static final Logger logger = Logger.getLogger(Api.class.getName()); public static final String WBX_DATA_PATH = "daten/"; @@ -47,14 +53,7 @@ public static final String WBX_BASE = "$basis"; public static final String WBX_DATA = "$daten"; - - /** Zeiger zum Servlet-Kontext dieser Anwendung */ - private ServletContext ctx; - - /** Zeiger zum Request, der zur Ausfuehrung fuehrte */ - private HttpServletRequest request; - - + /** * Einen relativen Pfad in einen absoluten Pfad der WebBox * aufloesen. @@ -83,10 +82,10 @@ logger.fine("in admin role"); if(relPath.startsWith(WBX_BASE)) { logger.fine("is base"); - targetPath = getCatalinaBase(); + targetPath = getCatalinaBase(ctx); targetDir = new File(targetPath, relPath.substring(WBX_BASE.length())); } else if(relPath.startsWith(WBX_DATA)) { - targetPath = getWbxDataDir(); + targetPath = getWbxDataDir(ctx); logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length())); targetDir = new File(targetPath, relPath.substring(WBX_DATA.length())); } else { @@ -108,16 +107,69 @@ protected FileRef getBase() { FileRef base = null; - Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); - if(o instanceof String) { - String baseStr = (String) o; - logger.fine(baseStr); - File file = new File(baseStr); + Object o = getFileBase(ctx); + if(o instanceof File) { + File file = (File) o; base = new FileRef(file.getAbsolutePath(), file.isDirectory()); } return base; } + /* -------------- Hilfsfunktionen --------------- */ + + /** + * Das Datenverzeichnis der WebBox ermitteln + * @return Ordner $wbx/daten + */ + protected File getFileBase(ServletContext ctx) { + File file = null; + Object o = ctx.getAttribute(Initialiser.FILE_BASE); + if(o instanceof String) { + String baseStr = (String) o; + logger.fine(baseStr); + file = new File(baseStr); + } + return file; + } + + /** + * Den absoluten Pfad zum Verzeichnis ermitteln das gemaess der + * Tomcat-Doku als CATALINA_BASE der WebBox gilt + * @return absoluter Pfad zu $wbx/sys/base + */ + protected String getCatalinaBase(ServletContext ctx) { + String path = ctx.getRealPath("/"); + logger.fine("getRealPath: " + path); // file-cms in webapps + File file = new File(path); + file = file.getParentFile().getParentFile(); + return file.getAbsolutePath(); + } + + /** + * Den absoluten Pfad zum Datenverzeichnis der WebBox ermitteln + * @return absoluter Pfad zu $wbx/daten + */ + protected String getWbxDataDir(ServletContext ctx) { + return getFileBase(ctx).getAbsolutePath(); + } + + /** + * Das Verzeichnis ermitteln, in dem die WebBox laeuft + * @return der Ordner $wbx + */ + protected File getWbxDir(ServletContext ctx) { + String path = ctx.getRealPath("/"); + logger.fine("getRealPath: " + path); + File file = new File(path); + file = file.getParentFile().getParentFile().getParentFile().getParentFile(); + logger.fine("WebBox: " + file.getAbsolutePath()); + return file; + } + + /** + * den Namen des angemeldeten Benutzers ermitteln + * @return Name des angemeldeten Benutzers oder null, wenn keiner angemeldet ist + */ protected String getUserName() { String userName = null; Object p = getRequest().getUserPrincipal(); @@ -125,29 +177,19 @@ userName = ((Principal) p).getName(); } return userName; - } + } + - protected String getCatalinaBase() { - String path = getServletContext().getRealPath("/"); - logger.fine("getRealPath: " + path); // file-cms in webapps - File file = new File(path); - file = file.getParentFile().getParentFile(); - return file.getAbsolutePath(); - } - - protected String getWbxDataDir() { - String wbxBase = getBase().getAbsolutePath(); - File file = new File(wbxBase); - return file.getAbsolutePath(); - } /* ------------- Implementierung WebKontext ------------- */ @Override + @Access(type = Access.Type.RESTRICT) public ServletContext getServletContext() { return ctx; } @Override + @Access(type = Access.Type.RESTRICT) public void setServletContext(ServletContext servletContext) { this.ctx = servletContext; } @@ -155,11 +197,13 @@ /* ------------- Implementierung RequestKontext ------------- */ @Override + @Access(type = Access.Type.RESTRICT) public HttpServletRequest getRequest() { return request; } @Override + @Access(type = Access.Type.RESTRICT) public void setRequest(HttpServletRequest r) { this.request = r; } -- Gitblit v1.9.3