View Javadoc

1   /*
2    * Copyright 2007-2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.portal.cms;
17  
18  import java.util.HashMap;
19  import java.util.Locale;
20  import java.util.Map;
21  import java.util.regex.Pattern;
22  
23  import javax.portlet.PortletRequest;
24  import javax.portlet.PortletResponse;
25  
26  import nl.hippo.client.api.content.DocumentMetadata;
27  import nl.hippo.client.api.content.DocumentPath;
28  import nl.hippo.portal.cms.site.CMSLinkFactory;
29  import nl.hippo.portal.cms.site.CMSLinkFactoryImpl;
30  import nl.hippo.portal.cms.site.SiteMapItem;
31  
32  /***
33   * @version $Id: CMSRequestContextImpl.java 13575 2008-09-19 14:15:15Z jhoffman $
34   * 
35   */
36  public class CMSRequestContextImpl implements CMSRequestContext
37  {
38      // default URL type
39  	private UrlType urlType = UrlType.NORMAL;
40  	
41  	// TODO Ate: below copied from DocumentPathImpl to use the fixPath(String) method
42      //           As soon as this method is made available through the (or a) api this should be removed again
43      // Regular expression matching two or more consecutive slashes
44      private static final Pattern multipleSlashes = Pattern.compile("/{2,}");
45  
46      /***
47       * Ensure that:
48       * <ol>
49       * <li>path is '/' rather than null or an empty String</li>
50       * <li>there are only single '/' occurences</li>
51       * <li>path starts with one and only one '/'</li>
52       * <li>path ends without a '/'</li>
53       * </ol>
54       */
55      private static String fixPath(String path) {
56          // rule number 1
57          if (path == null || path.length() == 0) {
58              return "/";
59          }
60  
61          // rule number 2
62          path = multipleSlashes.matcher(path).replaceAll("/");
63  
64          // rule number 3
65          if (!path.startsWith("/")) {
66              path = "/" + path;
67          }
68  
69          // rule number 4
70          if (path.endsWith("/")) {
71              path = path.substring(0, path.length() - 1);
72          }
73  
74          return path;
75      }
76      // TODO Ate: end todo
77      
78      private CMSSiteImpl site;
79  
80      private CMSUser user;
81  
82      private CMSPortalURL url;
83  
84      private CMSPortalURL originalURL;
85      
86      private SiteMapItem smi;
87  
88      private DocumentPath src;
89      
90      private DocumentMetadata metadata;
91  
92      private Locale locale;
93      
94      private CMSLinkFactory linkFactory;
95      
96      private Object attributesMutex = new Object();
97      private HashMap attributes = new HashMap();
98  
99      public CMSRequestContextImpl(CMSSiteImpl site, CMSUser user, CMSPortalURL url, SiteMapItem smi, Locale locale)
100     {
101         this.site = site;
102         this.user = user;
103         this.url = url;
104         this.smi = smi;
105         this.locale = new Locale("nl", "NL"); // constant for now
106         this.src = getSrc(smi, url, true);
107         if ( src != null && metadata == null )
108         {
109             metadata = getDefaultService().getMetaData(src);
110         }
111         linkFactory = new CMSLinkFactoryImpl(this);
112     }
113     
114     public CMSRequestContextImpl(CMSSiteImpl site, CMSUser user, CMSPortalURL url, CMSPortalURL originalURL, SiteMapItem smi, Locale locale)
115     {
116         this(site,user,url,smi,locale);
117         this.originalURL = originalURL;
118     }
119     
120     public SiteMapItem getSiteMapItem()
121     {
122         return smi;
123     }
124 
125     public String getSiteMapItemURL(String id)
126     {
127         return getSiteMapItemURL(getSiteMapItem().getSiteMap().getById(id));
128     }
129 
130     public String getSiteMapItemURL(SiteMapItem item)
131     {        
132         return item != null && item.isLinkable() ? url.getSitePath() + item.getPath() : null;
133     }
134 
135     public DocumentPath getSrc()
136     {
137         return src;
138     }
139     
140     public DocumentMetadata getMetadata()
141     {
142         return metadata;
143     }
144     
145     public DocumentPath getSrc(CMSPortalURL url)
146     {
147         DocumentPath documentPath = null;
148         if (url != null 
149                 && url.getHost().getPort() == this.url.getHost().getPort()
150                 && url.getHost().getName().equals(this.url.getHost().getName())
151                 && url.getSitePath().equals(this.url.getSitePath()))
152         {
153             SiteMapItem smi = getSiteMapItem().getSiteMap().getByPath(url.getSiteMapPath());
154             if ( smi != null )
155             {
156                 documentPath = getSrc(smi, url, false);
157             }
158         }
159         return documentPath;
160     }
161     
162     private DocumentPath getSrc(SiteMapItem smi, CMSPortalURL url, boolean requestSrc)
163     {
164         DocumentPath path = null;
165         if ( smi.getSrc() != null )
166         {
167             path = getDefaultService().createRelativePath(smi.getSrc());
168         }
169         String postfix = url.getSiteMapPathPostfix();
170         if (postfix != null)
171         {
172             if (postfix.startsWith(DOCUMENT_ID_PREFIX))
173             {
174                 DocumentMetadata metadata = getDefaultService().getMetaDataById(postfix.substring(DOCUMENT_ID_PREFIX.length()));
175                 if ( metadata != null )
176                 {
177                     path = metadata.getDocumentPath();
178                     if ( requestSrc )
179                     {
180                         this.metadata = metadata;
181                     }
182                 }
183             }
184             else if ( path != null )
185             {
186                 if (postfix.toLowerCase().endsWith(".html"))
187                 {
188                     postfix = postfix.substring(0, postfix.length() - 4) + "xml";
189                 }
190                 // TODO Ate: replace locally defined fixPath method copied from DocumentPathImpl by public provided utils or api
191                 path = getDefaultService().createRelativePath(path.getRelativePath()+fixPath(postfix));
192             }
193         }
194         return path;
195     }
196     
197     public String rewriteSrcUrl(PortletRequest request, PortletResponse response, Map urlParameters)
198     {
199         return url.createPortalURL(urlParameters);
200     }
201 
202     public CMSService getDefaultService()
203     {
204         return site.getCMSService(CMSService.DEFAULT);
205     }
206 
207     public CMSService getNoCacheService()
208     {
209         return site.getCMSService(CMSService.NOCACHE);
210     }
211 
212     public CMSService getResourceService()
213     {
214         return site.getCMSService(CMSService.RESOURCE);
215     }
216 
217     public Locale getLocale()
218     {
219         return locale;
220     }
221 
222     public CMSPortalURL getPortalURL()
223     {
224         return url;
225     }
226     
227     public CMSPortalURL getOriginalPortalURL()
228     {
229         return originalURL;
230     }
231 
232     public CMSSite getSite()
233     {
234         return site;
235     }
236 
237     public CMSUser getUser()
238     {
239         return user;
240     }
241 
242     public CMSLinkFactory getLinkFactory() {
243         return linkFactory;
244     }
245     
246     public Object getAttributesMutex() {
247         return attributesMutex;
248     }
249     
250     public Object getAttribute(String name) {
251         synchronized (attributesMutex) {
252             return attributes.get(name);
253         }
254     }
255     
256     public void setAttribute(String name, Object value) {
257         synchronized (attributesMutex) {
258             if ( value == null ) {
259                 attributes.remove(name);
260             }
261             else {
262                 attributes.put(name, value);
263             }
264         }
265     }
266 
267 	public UrlType getUrlType() {
268 		return urlType;
269 	}
270 
271 	public void setUrlType(UrlType urlType) {
272 		this.urlType = urlType;
273 	}
274     
275 }