View Javadoc

1   /*
2    * Copyright 2007 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.repository;
17  
18  import java.util.StringTokenizer;
19  
20  import nl.hippo.client.api.content.Document;
21  import nl.hippo.client.api.content.DocumentPath;
22  import nl.hippo.portal.cms.CMSService;
23  import nl.hippo.portal.cms.source.AbstractEventCacheableSourceFactory;
24  import nl.hippo.portal.cms.source.EventCacheableSource;
25  import nl.hippo.portal.cms.source.Source;
26  import nl.hippo.portal.cms.source.SourceException;
27  
28  import org.apache.commons.lang.StringUtils;
29  
30  /***
31   * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
32   *
33   * @version $Id: RepositorySourceFactory.java 10619 2008-02-27 21:04:43Z ddam $
34   */
35  public class RepositorySourceFactory extends AbstractEventCacheableSourceFactory {
36  
37      protected EventCacheableSource getEventCacheableSource(Document doc) throws SourceException {
38          if (doc==null || doc.getContent()== null || doc.getContent().getResponseAsStream() == null){
39              throw new SourceException("Unable to load JAXB source using this factory ("+getName()+").");
40          }
41          return this.getEventCacheableSource(getSite(), doc.getContent().getResponseAsStream(), new DocumentPath[]{doc.getPath()});
42      }
43      
44      private String getServiceName(String url){
45          StringTokenizer tokenizer = new StringTokenizer(url,":");
46          if (tokenizer.countTokens() >= 2){
47              tokenizer.nextToken();
48              return tokenizer.nextToken();
49          }
50          return CMSService.DEFAULT;
51      }
52      
53      private String getDocPath(String url){
54          return StringUtils.substringAfter(url, "://");
55      }
56      
57      public Source getSource(String url) throws SourceException {
58          String serviceName = getServiceName(url);
59          if (serviceName != null){
60              CMSService service = getSite().getCMSService(serviceName);
61              if (service != null){
62                  String docPath = getDocPath(url);
63                  Document d = service.get(service.createRelativePath(docPath));
64                  if (d != null){
65                      return getEventCacheableSource(d);
66                  } 
67              }
68          }
69          
70          return null;
71      }
72      
73  }