1 13 package info.magnolia.cms.core; 14 15 import java.io.File ; 16 import java.io.UnsupportedEncodingException ; 17 import java.net.URLDecoder ; 18 19 import javax.jcr.RepositoryException; 20 import javax.servlet.http.HttpServletRequest ; 21 22 import org.apache.commons.lang.StringUtils; 23 import org.safehaus.uuid.UUIDGenerator; 24 25 26 30 public final class Path { 31 32 public static final String JAVAX_FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path"; 34 public static final String MGNL_REQUEST_URI_DECODED = "mgnl.request.uri.decoded"; 36 39 private static final String DEFAULT_UNTITLED_NODE_NAME = "untitled"; 40 41 private static final String ENCODING_DEFAULT = "UTF-8"; 43 46 private Path() { 47 } 49 50 54 public static String getCacheDirectoryPath() { 55 return getCacheDirectory().getAbsolutePath(); 56 } 57 58 public static File getCacheDirectory() { 59 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_CACHE_STARTDIR); 60 File dir = isAbsolute(path) ? new File (path) : new File (Path.getAppRootDir(), path); 61 dir.mkdirs(); 62 return dir; 63 } 64 65 69 public static String getTempDirectoryPath() { 70 return getTempDirectory().getAbsolutePath(); 71 } 72 73 public static File getTempDirectory() { 74 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_UPLOAD_TMPDIR); 75 File dir = isAbsolute(path) ? new File (path) : new File (Path.getAppRootDir(), path); 76 dir.mkdirs(); 77 return dir; 78 } 79 80 84 public static String getHistoryFilePath() { 85 return getHistoryFile().getAbsolutePath(); 86 } 87 88 public static File getHistoryFile() { 89 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_EXCHANGE_HISTORY); 90 return isAbsolute(path) ? new File (path) : new File (Path.getAppRootDir(), path); 91 } 92 93 97 public static String getRepositoriesConfigFilePath() { 98 return getRepositoriesConfigFile().getAbsolutePath(); 99 } 100 101 public static File getRepositoriesConfigFile() { 102 String path = SystemProperty.getProperty(SystemProperty.MAGNOLIA_REPOSITORIES_CONFIG); 103 return isAbsolute(path) ? new File (path) : new File (Path.getAppRootDir(), path); 104 } 105 106 110 public static File getAppRootDir() { 111 return new File (SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR)); 112 } 113 114 117 public static String getAbsoluteFileSystemPath(String path) { 118 if (Path.isAbsolute(path)) { 119 return path; 120 } 121 return new File (Path.getAppRootDir(), path).getAbsolutePath(); 123 } 124 125 130 public static String getURI(HttpServletRequest req) { 131 132 String uri = (String ) req.getAttribute(MGNL_REQUEST_URI_DECODED); 133 if (uri == null) { 134 uri = getDecodedURI(req); 135 req.setAttribute(MGNL_REQUEST_URI_DECODED, uri); 137 } 138 139 return uri; 140 } 141 142 public static void setURI(String uri, HttpServletRequest req) { 143 req.setAttribute(MGNL_REQUEST_URI_DECODED, uri); 144 } 145 146 151 public static String getHandle(HttpServletRequest req) { 152 return (String ) req.getAttribute(Aggregator.HANDLE); 153 } 154 155 159 public static String getOriginalURI(HttpServletRequest req) { 160 return (String ) req.getAttribute(JAVAX_FORWARD_SERVLET_PATH); 161 } 162 163 168 private static String getDecodedURI(HttpServletRequest req) { 169 String encoding = StringUtils.defaultString(req.getCharacterEncoding(), ENCODING_DEFAULT); 170 String decodedURL = null; 171 try { 172 decodedURL = URLDecoder.decode(req.getRequestURI(), encoding); 173 } 174 catch (UnsupportedEncodingException e) { 175 decodedURL = req.getRequestURI(); 176 } 177 return StringUtils.substringAfter(decodedURL, req.getContextPath()); 178 } 179 180 public static String getExtension(HttpServletRequest req) { 181 String ext = (String ) req.getAttribute(Aggregator.EXTENSION); 182 if (ext == null) { 183 ext = StringUtils.substringAfterLast(req.getRequestURI(), "."); 184 req.setAttribute(Aggregator.EXTENSION, ext); 185 } 186 return ext; 187 } 188 189 public static String getUniqueLabel(HierarchyManager hierarchyManager, String parent, String label) { 190 if (parent.equals("/")) { parent = StringUtils.EMPTY; 192 } 193 while (hierarchyManager.isExist(parent + "/" + label)) { label = createUniqueName(label); 195 } 196 return label; 197 } 198 199 public static String getUniqueLabel(Content parent, String label) { 200 try { 201 while (parent.hasContent(label) || parent.hasNodeData(label)) { label = createUniqueName(label); 203 } 204 } 205 catch (RepositoryException e) { 206 label = UUIDGenerator.getInstance().generateRandomBasedUUID().toString(); 207 } 208 return label; 209 } 210 211 protected static boolean isAbsolute(String path) { 212 213 if (path == null) { 214 return false; 215 } 216 217 if (path.startsWith("/") || path.startsWith(File.separator)) { return true; 219 } 220 221 if (path.length() >= 3 && Character.isLetter(path.charAt(0)) && path.charAt(1) == ':') { 223 return true; 224 } 225 226 return false; 227 } 228 229 236 public static String getValidatedLabel(String label) { 237 StringBuffer s = new StringBuffer (label); 238 StringBuffer newLabel = new StringBuffer (); 239 for (int i = 0; i < s.length(); i++) { 240 int charCode = s.charAt(i); 241 if (((charCode >= 48) && (charCode <= 57)) 243 || ((charCode >= 65) && (charCode <= 90)) 244 || ((charCode >= 97) && (charCode <= 122)) 245 || charCode == 45 246 || charCode == 95) { 247 newLabel.append(s.charAt(i)); 248 } 249 else { 250 newLabel.append("-"); } 252 } 253 if (newLabel.length() == 0) { 254 newLabel.append(DEFAULT_UNTITLED_NODE_NAME); 255 } 256 return newLabel.toString(); 257 } 258 259 262 private static String createUniqueName(String baseName) { 263 int pos; 264 for (pos = baseName.length() - 1; pos >= 0; pos--) { 265 char c = baseName.charAt(pos); 266 if (c < '0' || c > '9') { 267 break; 268 } 269 } 270 String base; 271 int cnt; 272 if (pos == -1) { 273 if (baseName.length() > 1) { 274 pos = baseName.length() - 2; 275 } 276 } 277 if (pos == -1) { 278 base = baseName; 279 cnt = -1; 280 } 281 else { 282 pos++; 283 base = baseName.substring(0, pos); 284 if (pos == baseName.length()) { 285 cnt = -1; 286 } 287 else { 288 cnt = new Integer (baseName.substring(pos)).intValue(); 289 } 290 } 291 return (base + ++cnt); 292 } 293 294 public static String getAbsolutePath(String path, String label) { 295 if (StringUtils.isEmpty(path) || (path.equals("/"))) { return "/" + label; } 298 299 return path + "/" + label; } 301 302 public static String getAbsolutePath(String path) { 303 if (!path.startsWith("/")) { return "/" + path; } 306 return path; 307 } 308 309 public static String getNodePath(String path, String label) { 310 if (StringUtils.isEmpty(path) || (path.equals("/"))) { return label; 312 } 313 return getNodePath(path + "/" + label); } 315 316 public static String getNodePath(String path) { 317 if (path.startsWith("/")) { return path.replaceFirst("/", StringUtils.EMPTY); } 320 return path; 321 } 322 323 public static String getParentPath(String path) { 324 int lastIndexOfSlash = path.lastIndexOf("/"); if (lastIndexOfSlash > 0) { 326 return StringUtils.substringBefore(path, "/"); } 328 return "/"; } 330 } 331 | Popular Tags |