View Javadoc

1   /**
2    * Standard Portlet Collection Project 7 
3    * open source portlets every community needs
4    *
5    * Copyright (C) 2007  Phillip Merensky (phillip23@users.sourceforge.net)
6    *
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package spcp7.imagegallery.impl.alfresco.utils;
21  
22  import java.util.Map;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.alfresco.webservice.util.ISO9075;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import spcp7.imagegallery.abstractionlayer.face.persistence.PropertyModelFace;
31  import spcp7.imagegallery.impl.alfresco.AlfrescoContentRetrievalImpl;
32  
33  /**
34   * @author Phillip Merensky
35   * 
36   */
37  public class AlfrescoUtils {
38      private final Pattern replaceAppPattern = Pattern
39  	    .compile(AlfrescoDefaultValues.ALFRESCO_SHORTEN_APP_NAMESPACE_REGEX_VALUE);
40      private final Pattern replaceCmPattern = Pattern
41  	    .compile(AlfrescoDefaultValues.ALFRESCO_SHORTEN_CM_SPACE_REGEX_VALUE);
42      private Log log = LogFactory.getLog(AlfrescoContentRetrievalImpl.class);
43  
44      public AlfrescoUtils() {
45  
46      }
47  
48  
49      /**
50       * This method replaces the full path of alfresco to the short one. The
51       * replacements can be defined in
52       * {@link AlfrescoDefaultProperties#ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT}
53       * and
54       * {@link AlfrescoDefaultProperties#ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT}.
55       * The regex replace pattern is defined in
56       * {@link AlfrescoDefaultProperties#ALFRESCO_SHORTEN_APP_NAMESPACE_REGEX}
57       * and {@link AlfrescoDefaultProperties#ALFRESCO_SHORTEN_CM_NAMESPACE_REGEX}.
58       * 
59       * @param inputPath
60       * @return The shortened path or the input path if the properties
61       *         ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT and
62       *         ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT could not be found
63       */
64      public String normalizePathNamespace(String inputPath,
65  	    Map<String, PropertyModelFace> properties) {
66  	if (properties != null
67  		&& properties
68  			.containsKey(AlfrescoDefaultProperties.ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT
69  				.toString())
70  		&& properties
71  			.containsKey(AlfrescoDefaultProperties.ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT
72  				.toString())) {
73  	    String appReplace = properties
74  		    .get(
75  			    AlfrescoDefaultProperties.ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT
76  				    .toString()).getWert();
77  	    String cmReplace = properties
78  		    .get(
79  			    AlfrescoDefaultProperties.ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT
80  				    .toString()).getWert();
81  	    if (appReplace != null && cmReplace != null) {
82  		Matcher appMatcher = replaceAppPattern.matcher(inputPath);
83  		inputPath = appMatcher.replaceAll(appReplace);
84  		Matcher cmMatcher = replaceCmPattern.matcher(inputPath);
85  		inputPath = cmMatcher.replaceAll(cmReplace);
86  	    }
87  	} else {
88  	    log.error("We could not normalize path namespace"
89  		    + " because no properties were provided!");
90  	}
91  	return inputPath;
92      }
93  
94      /**
95       * This method encodes the elements of an alfresco path conforming to
96       * {@link ISO9075#encode(String)}. Elements of a path are only names. That
97       * is no slashes or content prefixes like (app:). Slashes and content
98       * prefixes remain unchanged.
99       * 
100      * @param path
101      * @return The path with ISO9075 encoded elements.
102      */
103     public String encodeAlfrescoAbbreviatedPathISO9075(String path) {
104 	// first we decode the whole path because we need a fresh start and want
105 	// to prevent "double" encoding.
106 	path = ISO9075.decode(path);
107 	String slashSplit[] = path.split("/");
108 	boolean slashPrefix = path.startsWith("/");
109 	StringBuffer retVal = new StringBuffer();
110 	String colonSplit[] = null;
111 	for (int i = 0; i < slashSplit.length; i++) {
112 	    if (!slashPrefix) {
113 		colonSplit = slashSplit[i].split(":");
114 	    } else {
115 		if (i > 0) {
116 		    colonSplit = slashSplit[i].split(":");
117 		}
118 	    }
119 	    if (colonSplit != null && colonSplit.length > 0) {
120 		if (colonSplit.length == 1) {
121 		    // we only have one whole string without app: or cm: prefix
122 		    // so we encode it
123 		    retVal.append(ISO9075.encode(colonSplit[0]));
124 		} else {
125 		    // otherwise we only append it as prefixes should not be
126 		    // encoded
127 		    retVal.append(colonSplit[0]);
128 		}
129 		if (colonSplit.length == 2) {
130 		    retVal.append(":");
131 		    retVal.append(ISO9075.encode(colonSplit[1]));
132 		} else if (colonSplit.length > 2) {
133 		    log
134 			    .error("Path could not be correctly encoded"
135 				    + " because we found two colons within a path element"
136 				    + " (eg app:testname.jpg. This is not allowed."
137 				    + " So please specify a name without a colon!");
138 		}
139 	    }
140 	    if (i < slashSplit.length - 1) {
141 		retVal.append("/");
142 	    }
143 	}
144 	return retVal.toString();
145     }
146 
147 }