View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SARL Author : Dang Van Minh
38   * minh.dang@exoplatform.com May 8, 2008 3:07:02 PM
39   */
40  public class JCRResourceResolver extends ResourceResolver {
41    protected String repository ;
42    protected String workspace ;
43    protected String propertyName ;
44    /** The log. */
45    private static final Log LOG = ExoLogger.getLogger(JCRResourceResolver.class.getName());
46    private TemplateService templateService;
47    
48    /**
49     * Instantiates a new jCR resource resolver to load template that stored as a
50     * property of node in jcr
51     * 
52     * @param workspace the workspace
53     */
54    public JCRResourceResolver(String workspace) {
55      this.workspace = workspace;
56      templateService = WCMCoreUtils.getService(TemplateService.class);
57    }
58  
59    /* (non-Javadoc)
60     * @see org.exoplatform.resolver.ResourceResolver#getResource(java.lang.String)
61     */
62    public URL getResource(String url) throws Exception {
63      throw new Exception("This method is not  supported") ;
64    }
65  
66    /**
67     * @param url URL must be like jcr:path with path is node path
68     * @see org.exoplatform.resolver.ResourceResolver#getInputStream(java.lang.String)
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    /* (non-Javadoc)
88     * @see org.exoplatform.resolver.ResourceResolver#getResources(java.lang.String)
89     */
90    public List<URL> getResources(String url) throws Exception {
91      throw new Exception("This method is not  supported") ;
92    }
93  
94    /* (non-Javadoc)
95     * @see org.exoplatform.resolver.ResourceResolver#getInputStreams(java.lang.String)
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   /* (non-Javadoc)
104    * @see org.exoplatform.resolver.ResourceResolver#isModified(java.lang.String, long)
105    */
106   public boolean isModified(String url, long lastAccess) {  return false ; }
107 
108   /* (non-Javadoc)
109    * @see org.exoplatform.resolver.ResourceResolver#createResourceId(java.lang.String)
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   /* (non-Javadoc)
119    * @see org.exoplatform.resolver.ResourceResolver#getResourceScheme()
120    */
121   public String getResourceScheme() {  return "jcr:" ; }
122 
123 }