View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.resource.support;
20  
21  import java.io.IOException;
22  import java.util.StringTokenizer;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.json.JSONObject;
27  import org.springframework.extensions.surf.resource.AbstractResource;
28  import org.springframework.extensions.surf.resource.ResourceContent;
29  import org.springframework.extensions.surf.resource.ResourceContentImpl;
30  import org.springframework.extensions.surf.resource.ResourceJSONContent;
31  import org.springframework.extensions.surf.resource.ResourceJSONContentImpl;
32  
33  /**
34   * Alfresco resource
35   * 
36   * Content = heavy asset
37   * Metadata = Alfresco information
38   * 
39   * Object ids are of the following format:
40   *
41   *    alfresco://<endpointId>/<objectId>
42   *    alfresco://<endpointId>/workspace/SpacesStore/39627322-c892-49fe-bcbb-a5c72bdc2a9b
43   *    
44   *    where <objectId> is <repoProtocol>/<repoStore>/<repoNodeRefId>
45   *    
46   * Example:
47   *    
48   *    alfresco://<endpointId>/workspace/SpacesStore/790ccbc3-a3ee-45ba-b169-0926ad77c2c8 
49   *     
50   * @author muzquiano
51   */
52  public class AlfrescoResource extends AbstractResource
53  {
54      private static Log logger = LogFactory.getLog(AlfrescoResource.class);
55      
56      private String objectTypeId = null;
57      
58      public AlfrescoResource(String protocolId, String endpointId, String objectId)
59      {
60          super(protocolId, endpointId, objectId);
61      }
62      
63      /* (non-Javadoc)
64       * @see org.alfresco.web.framework.resource.Resource#getMetadata()
65       */
66      public ResourceContent getMetadata() throws IOException
67      {
68          return new ResourceJSONContentImpl(this, getMetadataURL());
69      }
70      
71      /* (non-Javadoc)
72       * @see org.alfresco.web.framework.resource.Resource#getMetadataURL()
73       */
74      public String getMetadataURL()
75      {
76          String metadataURL = "/webframework/content/metadata";
77          
78          if (getObjectId() != null)
79          {
80              metadataURL = "/webframework/content/metadata?id=" + this.getNodeRefId();
81          }
82          
83          if (logger.isDebugEnabled())
84              logger.debug("Alfresco Resource metadata URL: " + metadataURL);
85          
86          return metadataURL;
87      }
88  
89      /* (non-Javadoc)
90       * @see org.alfresco.web.framework.resource.Resource#getContent()
91       */
92      public ResourceContent getContent() throws IOException
93      {
94          return new ResourceContentImpl(this, getContentURL());        
95      }
96  
97      /* (non-Javadoc)
98       * @see org.alfresco.web.framework.resource.Resource#getContentURL()
99       */
100     public String getContentURL()
101     {      
102         String contentURL = null;
103         
104         if (getObjectId() != null)
105         {
106             contentURL = "/api/node/" + getObjectId() + "/content";
107         }
108         
109         if (logger.isDebugEnabled())
110             logger.debug("Alfresco Resource content URL: " + contentURL);
111         
112         return contentURL;
113     }
114     
115     /* (non-Javadoc)
116      * @see org.alfresco.web.framework.resource.Resource#getObjectTypeId()
117      */
118     public synchronized String getObjectTypeId()
119     {
120         if (objectTypeId == null)
121         {
122             JSONObject json = null;
123 
124             try
125             {
126                 json = ((ResourceJSONContent) getMetadata()).getJSON();
127                 objectTypeId = json.getString("type");
128             }
129             catch (Exception e)
130             {
131                 e.printStackTrace();
132             }
133         }
134         
135         return objectTypeId;
136     }
137     
138     /* (non-Javadoc)
139      * @see org.alfresco.web.framework.resource.AbstractResource#isContainer()
140      */
141     public boolean isContainer()
142     {
143         boolean isContainer = false;
144         
145         try
146         {
147             ResourceJSONContent jsonContent = (ResourceJSONContent) getMetadata();
148             isContainer = jsonContent.getJSON().getBoolean("isContainer");
149         }
150         catch (Exception e) { }
151         
152         return isContainer;
153     }
154     
155     
156     /*
157      * Gets the node ref version of the object id
158      */
159     protected String getNodeRefId()
160     {
161         String nodeRefId = null;
162         
163         String objectId = getObjectId();
164         if (objectId != null)
165         {        
166             String workspaceId = null;
167             String storeId = null;
168             String nodeId = null;
169             
170             StringTokenizer tokenizer = new StringTokenizer(objectId, "/");
171             workspaceId = tokenizer.nextToken();
172             storeId = tokenizer.nextToken();
173             nodeId = tokenizer.nextToken();
174         
175             nodeRefId = workspaceId + "://" + storeId + "/" + nodeId;
176         }
177         
178         return nodeRefId;
179     }
180 }