Dateiverwaltung für die WebBox
ulrich
2017-03-19 72e43ddf5a01b28d57a41bdbd4b77a0519d92912
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 
    Dateiverwaltung - File management in your browser
    Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 
package de.uhilger.filecms.api;
 
import de.uhilger.filecms.data.FileRef;
import de.uhilger.filecms.web.Initialiser;
import de.uhilger.transit.web.RequestKontext;
import de.uhilger.transit.web.WebKontext;
import java.io.File;
import java.security.Principal;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
 
/**
 *
 */
public abstract class Api implements WebKontext, RequestKontext {
  
  private static final Logger logger = Logger.getLogger(Api.class.getName());
 
  public static final String WBX_DATA_PATH = "daten/";
  public static final String PUB_DIR_PATH = "www/";
  public static final String HOME_DIR_PATH = "home/";
  public static final String PUB_DIR_NAME = "Oeffentlich";
  //public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
  public static final String HOME_DIR_NAME = "Persoenlich";
  public static final String WBX_ADMIN_ROLE = "wbxAdmin";
  
  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.
   * 
   * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME 
   * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere 
   * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE.
   * 
   * D.h., es werden nur Pfade aufgeloest, die sich innerhalb 
   * des Ordners der WeBox befinden.
   * 
   * @param relPath
   * @return 
   */
  protected File getTargetDir(String relPath) {
    logger.fine(relPath);
    File targetDir;
    String targetPath = null;
    if(relPath.startsWith(PUB_DIR_NAME)) {
      targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
      targetDir = new File(getBase().getAbsolutePath(), targetPath);
    } else if(relPath.startsWith(HOME_DIR_NAME)) {
      targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
      targetDir = new File(getBase().getAbsolutePath(), targetPath);
    } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
      logger.fine("in admin role");
      if(relPath.startsWith(WBX_BASE)) {
        logger.fine("is base");
        targetPath = getCatalinaBase();
        targetDir = new File(targetPath, relPath.substring(WBX_BASE.length()));
      } else if(relPath.startsWith(WBX_DATA)) {
        targetPath = getWbxDataDir();
        logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length()));
        targetDir = new File(targetPath, relPath.substring(WBX_DATA.length()));
      } else {
        targetDir = getDefaultDir(relPath);
      }
    } else {
      // kann eigentlich nicht sein..
      targetDir = getDefaultDir(relPath);
    }
    logger.fine("returning targetDir " + targetDir.getAbsolutePath());
    //File targetDir = new File(getBase().getAbsolutePath(), targetPath);
    return targetDir;
  }
  
  protected File getDefaultDir(String relPath) {
    String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
    return new File(getBase().getAbsolutePath(), targetPath);
  }
  
  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);
      base = new FileRef(file.getAbsolutePath(), file.isDirectory());
    }
    return base;
  }
  
  protected String getUserName() {
    String userName = null;
    Object p = getRequest().getUserPrincipal();
    if(p instanceof Principal) {
      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
  public ServletContext getServletContext() {
    return ctx;
  }
 
  @Override
  public void setServletContext(ServletContext servletContext) {
    this.ctx = servletContext;
  }
  
  /* ------------- Implementierung RequestKontext ------------- */
 
  @Override
  public HttpServletRequest getRequest() {
    return request;
  }
 
  @Override
  public void setRequest(HttpServletRequest r) {
    this.request = r;
  }
  
}