View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.core;
35  
36  import info.magnolia.cms.util.SelectorUtil;
37  import info.magnolia.init.MagnoliaConfigurationProperties;
38  import info.magnolia.jcr.util.NodeNameHelper;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.objectfactory.Components;
41  
42  import java.io.File;
43  import java.nio.file.Paths;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  import javax.jcr.Session;
48  
49  import org.safehaus.uuid.UUIDGenerator;
50  
51  /**
52   * Utility class to retrieve files or directory used by Magnolia. Examples: cache directory, tmp files, ..
53   *
54   * @deprecated since 5.5.3, please see methods description to find out what to use instead
55   */
56  @Deprecated
57  public final class Path {
58  
59      /**
60       * @deprecated since 5.5.3, use {@link info.magnolia.cms.util.SelectorUtil#SELECTOR_DELIMITER} instead.
61       */
62      public static final String SELECTOR_DELIMITER = SelectorUtil.SELECTOR_DELIMITER;
63  
64      /**
65       * Utility class, don't instantiate.
66       */
67      private Path() {
68          // unused
69      }
70  
71      /**
72       * @deprecated since 5.5.3 without replacement.
73       */
74      public static String getCacheDirectoryPath() {
75          return getCacheDirectory().getAbsolutePath();
76      }
77  
78      /**
79       * @deprecated since 5.5.3 without replacement.
80       */
81      public static File getCacheDirectory() {
82          String path = SystemProperty.getProperty(MagnoliaConfigurationProperties.MAGNOLIA_CACHE_STARTDIR);
83          File dir = isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
84          dir.mkdirs();
85          return dir;
86      }
87  
88      /**
89       * @deprecated since 5.5.3, use {@link FileSystemHelper#getTempDirectory()} and {@link java.io.File#getAbsolutePath()} instead.
90       */
91      public static String getTempDirectoryPath() {
92          return Components.getComponent(FileSystemHelper.class).getTempDirectory().getAbsolutePath();
93      }
94  
95      /**
96       @deprecated since 5.5.3, use {@link FileSystemHelper#getTempDirectory()} instead.
97       */
98      public static File getTempDirectory() {
99          return Components.getComponent(FileSystemHelper.class).getTempDirectory();
100     }
101 
102     /**
103      * Gets {@link info.magnolia.init.MagnoliaConfigurationProperties#MAGNOLIA_EXCHANGE_HISTORY} file set in magnolia properties.
104      *
105      * @return exchange history file location
106      * @deprecated since 5.5.3 without replacement
107      */
108     public static String getHistoryFilePath() {
109         return getHistoryFile().getAbsolutePath();
110     }
111 
112     /**
113      * @deprecated since 5.5.3 without replacement
114      */
115     public static File getHistoryFile() {
116         String path = SystemProperty.getProperty(MagnoliaConfigurationProperties.MAGNOLIA_EXCHANGE_HISTORY);
117         return isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
118     }
119 
120     /**
121      * @deprecated since 5.5.3 without replacement {@see info.magnolia.repository.DefaultRepositoryManager#loadRepositories()} and {@see info.magnolia.cms.util.ConfigUtil}
122      */
123     public static String getRepositoriesConfigFilePath() {
124         return getRepositoriesConfigFile().getAbsolutePath();
125     }
126 
127     /**
128      * @deprecated since 5.5.3 without replacement {@see info.magnolia.repository.DefaultRepositoryManager#loadRepositories()} and {@see info.magnolia.cms.util.ConfigUtil}
129      */
130     public static File getRepositoriesConfigFile() {
131         String path = SystemProperty.getProperty(MagnoliaConfigurationProperties.MAGNOLIA_REPOSITORIES_CONFIG);
132         return isAbsolute(path) ? new File(path) : new File(getAppRootDir(), path);
133     }
134 
135     /**
136      * @deprecated since 5.5.3, use {@link FileSystemHelper#getAppRootDir}
137      */
138     public static File getAppRootDir() {
139         return Components.getComponent(FileSystemHelper.class).getAppRootDir();
140     }
141 
142     /**
143      * @deprecated since 5.5.3, use {@link FileSystemHelper#getAppRootDir}
144      */
145     public static String getAbsoluteFileSystemPath(String path) {
146         return Components.getComponent(FileSystemHelper.class).getAbsoluteFileSystemPath(path);
147     }
148 
149     /**
150      * @deprecated since 5.5.3, use {@link NodeNameHelper#getUniqueName(Session, String, String)} instead
151      */
152     public static String getUniqueLabel(Session session, String parentPath, String label) throws RepositoryException {
153         return Components.getComponent(NodeNameHelper.class).getUniqueName(session, parentPath, label);
154     }
155 
156     /**
157      * @deprecated since 5.5.3, use {@link NodeNameHelper#getUniqueName(Session, String, String, String)}
158      */
159     public static String getUniqueLabel(Session session, String parentPath, String label, String extension) throws RepositoryException {
160         return Components.getComponent(NodeNameHelper.class).getUniqueName(session, parentPath, label, extension);
161     }
162 
163     /**
164      * @deprecated since 5.5.3, use {@link NodeNameHelper#getUniqueName(Node, String)}
165      */
166     // When removing this deprecated method please check if dependency to jug.2.0.0-asl can be removed. This seems to be the only usage of it.
167     public static String getUniqueLabel(Node parent, String label) {
168         try {
169             return Components.getComponent(NodeNameHelper.class).getUniqueName(parent, label);
170         } catch (RepositoryException e) {
171             return UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
172         }
173     }
174 
175     /**
176      * @deprecated since 5.5.3
177      * for file system operation use for example {@link java.nio.file.Path#isAbsolute()}
178      * jcr just simply check if path starts with "/"
179      */
180     public static boolean isAbsolute(String path) {
181         if (path == null) {
182             return false;
183         }
184 
185         return path.startsWith("/") || Paths.get(path).isAbsolute();
186     }
187 
188     /**
189      * @deprecated since 5.5.3, use {@link NodeNameHelper#getValidatedName(String)}
190      */
191     public static String getValidatedLabel(String label) {
192         return Components.getComponent(NodeNameHelper.class).getValidatedName(label);
193     }
194 
195     /**
196      * @deprecated since 5.5.3, use {@link NodeNameHelper#getValidatedName(String, String)}
197      */
198     public static String getValidatedLabel(String label, String charset) {
199         return Components.getComponent(NodeNameHelper.class).getValidatedName(label, charset);
200     }
201 
202     /**
203      * @deprecated since 5.5.3, without replacement (this method never meant to be public)
204      */
205     public static boolean isCharValid(int charCode, String charset) {
206         // TODO fgrilli: we now allow dots (.) in JR local names but actually in JR 2.0 other chars could be allowed as well
207         // (see http://www.day.com/specs/jcr/2.0/3_Repository_Model.html paragraph 2.2 and org.apache.jackrabbit.util.XMLChar.isValid()).
208         // Also, now that we're on java 6 and JR 2.0 should the check for the charset be dropped?
209 
210         // http://www.ietf.org/rfc/rfc1738.txt
211         // safe = "$" | "-" | "_" | "." | "+"
212         // extra = "!" | "*" | "'" | "(" | ")" | ","
213         // national = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`"
214         // punctuation = "<" | ">" | "#" | "%" | <">
215         // reserved = ";" | "/" | "?" | ":" | "@" | "&" | "="
216 
217         if ("UTF-8".equals(charset)) {
218             // jackrabbit not allowed 32: [ ] 91: [[] 93: []] 42: [*] 34: ["] 46: [.] 58 [:] 92: [\] 39 :[']
219             // url not valid 59: [;] 47: [/] 63: [?] 43: [+] 37: [%] 33: [!] 35:[#]
220             if (charCode != 32
221                     && charCode != '['
222                     && charCode != ']'
223                     && charCode != '*'
224                     && charCode != '"'
225                     && charCode != ':'
226                     && charCode != 92
227                     && charCode != 39
228                     && charCode != ';'
229                     && charCode != '/'
230                     && charCode != '?'
231                     && charCode != '+'
232                     && charCode != '%'
233                     && charCode != '!'
234                     && charCode != '#'
235                     && charCode != '@'
236                     && charCode != '='
237                     && charCode != '&'
238                     && charCode != SelectorUtil.SELECTOR_DELIMITER.charAt(0)) {
239                 return true;
240             }
241         } else {
242             // charCodes: 48-57: [0-9]; 65-90: [A-Z]; 97-122: [a-z]; 45: [-]; 95:[_]
243             if (((charCode >= 48) && (charCode <= 57))
244                     || ((charCode >= 65) && (charCode <= 90))
245                     || ((charCode >= 97) && (charCode <= 122))
246                     || charCode == 45
247                     || charCode == 46
248                     || charCode == 95) {
249                 return true;
250             }
251 
252         }
253         return false;
254 
255     }
256 
257     /**
258      * @deprecated since 5.5.3, use {@link info.magnolia.jcr.util.NodeUtil#getAbsolutePath(String, String)}
259      */
260     public static String getAbsolutePath(String path, String label) {
261         return NodeUtil.getAbsolutePath(path, label);
262     }
263 
264     /**
265      * @deprecated since 5.5.3 without replacement
266      */
267     public static String getAbsolutePath(String path) {
268         if (!path.startsWith("/")) {
269             return "/" + path;
270         }
271         return path;
272     }
273 }