1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }