1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.resolver;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.jcr.Node;
26 import javax.jcr.Session;
27
28 import org.exoplatform.resolver.ResourceKey;
29 import org.exoplatform.resolver.ResourceResolver;
30 import org.exoplatform.services.cms.templates.TemplateService;
31 import org.exoplatform.services.jcr.ext.common.SessionProvider;
32 import org.exoplatform.services.log.ExoLogger;
33 import org.exoplatform.services.log.Log;
34 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
35
36
37
38
39
40 public class JCRResourceResolver extends ResourceResolver {
41 protected String repository ;
42 protected String workspace ;
43 protected String propertyName ;
44
45 private static final Log LOG = ExoLogger.getLogger(JCRResourceResolver.class.getName());
46 private TemplateService templateService;
47
48
49
50
51
52
53
54 public JCRResourceResolver(String workspace) {
55 this.workspace = workspace;
56 templateService = WCMCoreUtils.getService(TemplateService.class);
57 }
58
59
60
61
62 public URL getResource(String url) throws Exception {
63 throw new Exception("This method is not supported") ;
64 }
65
66
67
68
69
70 public InputStream getInputStream(String url) throws Exception {
71 SessionProvider provider = WCMCoreUtils.getSystemSessionProvider();
72 Session session = provider.getSession(workspace, WCMCoreUtils.getRepository());
73 ByteArrayInputStream inputStream = null;
74 try {
75 Node template = (Node)session.getItem(removeScheme(url));
76 inputStream = new ByteArrayInputStream(templateService.getTemplate(template).getBytes());
77 } catch(Exception e) {
78 if (LOG.isErrorEnabled()) {
79 LOG.error("Unexpected problem happen when try to process with url");
80 }
81 } finally {
82 session.logout();
83 }
84 return inputStream;
85 }
86
87
88
89
90 public List<URL> getResources(String url) throws Exception {
91 throw new Exception("This method is not supported") ;
92 }
93
94
95
96
97 public List<InputStream> getInputStreams(String url) throws Exception {
98 ArrayList<InputStream> inputStreams = new ArrayList<InputStream>(1) ;
99 inputStreams.add(getInputStream(url)) ;
100 return inputStreams ;
101 }
102
103
104
105
106 public boolean isModified(String url, long lastAccess) { return false ; }
107
108
109
110
111 public String createResourceId(String url) { return url ; }
112
113 @Override
114 public ResourceKey createResourceKey(String url) {
115 return new ResourceKey(url.hashCode(), url);
116 }
117
118
119
120
121 public String getResourceScheme() { return "jcr:" ; }
122
123 }