KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > content > ContentWebService


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webservice.content;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.rmi.RemoteException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.transaction.UserTransaction JavaDoc;
30
31 import org.alfresco.model.ContentModel;
32 import org.alfresco.repo.webservice.AbstractWebService;
33 import org.alfresco.repo.webservice.Utils;
34 import org.alfresco.repo.webservice.types.ContentFormat;
35 import org.alfresco.repo.webservice.types.Predicate;
36 import org.alfresco.repo.webservice.types.Reference;
37 import org.alfresco.service.cmr.repository.ContentReader;
38 import org.alfresco.service.cmr.repository.ContentWriter;
39 import org.alfresco.service.cmr.repository.NodeRef;
40 import org.alfresco.service.namespace.QName;
41 import org.apache.axis.MessageContext;
42 import org.apache.axis.transport.http.HTTPConstants;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 /**
47  * Web service implementation of the ContentService. The WSDL for this service
48  * can be accessed from http://localhost:8080/alfresco/wsdl/content-service.wsdl
49  *
50  * @author gavinc
51  */

52 public class ContentWebService extends AbstractWebService implements
53         ContentServiceSoapPort
54 {
55     private static Log logger = LogFactory.getLog(ContentWebService.class);
56
57     private static final String JavaDoc BROWSER_URL = "{0}://{1}{2}/download/direct/{3}/{4}/{5}/{6}";
58
59     /**
60      * @see org.alfresco.repo.webservice.content.ContentServiceSoapPort#read(org.alfresco.repo.webservice.types.Reference)
61      */

62     public Content[] read(Predicate items, String JavaDoc property)
63             throws RemoteException JavaDoc, ContentFault
64     {
65         UserTransaction JavaDoc tx = null;
66
67         try
68         {
69             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
70             tx.begin();
71
72             // resolve the predicates
73
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(items, this.nodeService, this.searchService, this.namespaceService);
74             List JavaDoc<Content> results = new ArrayList JavaDoc<Content>(nodes.size());
75             for (NodeRef nodeRef : nodes)
76             {
77                 // Add content to the result
78
results.add(createContent(nodeRef, property));
79             }
80
81             // commit the transaction
82
tx.commit();
83
84             return results.toArray(new Content[results.size()]);
85         }
86         catch (Throwable JavaDoc e)
87         {
88             // rollback the transaction
89
try
90             {
91                 if (tx != null)
92                 {
93                     tx.rollback();
94                 }
95             }
96             catch (Exception JavaDoc ex)
97             {
98             }
99
100             if (logger.isDebugEnabled())
101             {
102                 logger.error("Unexpected error occurred", e);
103             }
104
105             throw new ContentFault(0, e.getMessage());
106         }
107     }
108     
109     /**
110      * Create the content object
111      *
112      * @param nodeRef the node reference
113      * @param property the content property
114      * @return the content object
115      * @throws UnsupportedEncodingException
116      */

117     private Content createContent(NodeRef nodeRef, String JavaDoc property)
118         throws UnsupportedEncodingException JavaDoc
119     {
120         Content content = null;
121         
122         // Lets have a look and see if this node has any content on this node
123
ContentReader contentReader = this.contentService.getReader(nodeRef, QName.createQName(property));
124         
125         if (contentReader != null)
126         {
127             // Work out what the server, port and context path are
128
HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)MessageContext.getCurrentContext().getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
129     
130             String JavaDoc address = req.getLocalName();
131             if (req.getLocalPort() != 80)
132             {
133                 address = address + ":" + req.getLocalPort();
134             }
135     
136             // Get the file name
137
String JavaDoc filename = (String JavaDoc)this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
138             
139             // format the URL that can be used to download the content
140
String JavaDoc downloadUrl = MessageFormat.format(BROWSER_URL,
141                     new Object JavaDoc[] { req.getScheme(), address,
142                             req.getContextPath(),
143                             nodeRef.getStoreRef().getProtocol(),
144                             nodeRef.getStoreRef().getIdentifier(),
145                             nodeRef.getId(),
146                             URLEncoder.encode(filename, "UTF-8") });
147             
148             // Create the content object
149
ContentFormat format = new ContentFormat(contentReader.getMimetype(), contentReader.getEncoding());
150             content = new Content(Utils.convertToReference(nodeRef), property, contentReader.getSize(), format, downloadUrl);
151             
152             // Debug
153
if (logger.isDebugEnabled())
154             {
155                 logger.debug("Content: " + nodeRef.getId() + " name="
156                         + filename + " encoding="
157                         + content.getFormat().getEncoding() + " mimetype="
158                         + content.getFormat().getMimetype() + " size="
159                         + content.getLength() + " downloadURL="
160                         + content.getUrl());
161             }
162         }
163         else
164         {
165             // Create an empty content object
166
content = new Content(Utils.convertToReference(nodeRef), property, 0, null, null);
167             
168             // Debug
169
if (logger.isDebugEnabled())
170             {
171                 logger.debug("No content found: " + nodeRef.getId());
172             }
173         }
174         
175         return content;
176     }
177
178     /**
179      * @see org.alfresco.repo.webservice.content.ContentServiceSoapPort#write(org.alfresco.repo.webservice.types.Reference,
180      * byte[])
181      */

182     public Content write(Reference node, String JavaDoc property, byte[] content, ContentFormat format)
183         throws RemoteException JavaDoc, ContentFault
184     {
185         UserTransaction JavaDoc tx = null;
186
187         try
188         {
189             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
190             tx.begin();
191
192             // create a NodeRef from the parent reference
193
NodeRef nodeRef = Utils.convertToNodeRef(node, this.nodeService,
194                     this.searchService, this.namespaceService);
195
196             // Get the content writer
197
ContentWriter writer = this.contentService.getWriter(nodeRef, QName.createQName(property), true);
198             
199             // Set the content format details (if they have been specified)
200
if (format != null)
201             {
202                 writer.setEncoding(format.getEncoding());
203                 writer.setMimetype(format.getMimetype());
204             }
205             
206             // Write the content
207
InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(content);
208             writer.putContent(is);
209
210             // Debug
211
if (logger.isDebugEnabled())
212             {
213                 logger.debug("Updated content for node with id: " + nodeRef.getId());
214             }
215
216             // Commit the transaction
217
tx.commit();
218             
219             // Return the content object
220
return createContent(nodeRef, property);
221         }
222         catch (Throwable JavaDoc e)
223         {
224             // Rollback the transaction
225
try
226             {
227                 if (tx != null)
228                 {
229                     tx.rollback();
230                 }
231             } catch (Exception JavaDoc ex)
232             {
233             }
234
235             if (logger.isDebugEnabled())
236             {
237                 logger.error("Unexpected error occurred", e);
238             }
239
240             throw new ContentFault(0, e.getMessage());
241         }
242     }
243
244     /**
245      * @see org.alfresco.repo.webservice.content.ContentServiceSoapPort#clear(org.alfresco.repo.webservice.types.Predicate,
246      * java.lang.String)
247      */

248     public Content[] clear(Predicate items, String JavaDoc property)
249             throws RemoteException JavaDoc, ContentFault
250     {
251         UserTransaction JavaDoc tx = null;
252
253         try
254         {
255             tx = Utils.getUserTransaction(MessageContext.getCurrentContext());
256             tx.begin();
257
258             List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(items, this.nodeService,this.searchService, this.namespaceService);
259             Content[] contents = new Content[nodes.size()];
260
261             // delete each node in the predicate
262
for (int x = 0; x < nodes.size(); x++)
263             {
264                 NodeRef nodeRef = nodes.get(x);
265
266                 // Clear the content
267
this.nodeService.setProperty(nodeRef, QName.createQName(property), null);
268
269                 if (logger.isDebugEnabled())
270                 {
271                     logger.debug("Cleared content node with id: " + nodeRef.getId());
272                 }
273
274                 contents[x] = createContent(nodeRef, property);
275             }
276
277             // commit the transaction
278
tx.commit();
279
280             return contents;
281         }
282         catch (Throwable JavaDoc e)
283         {
284             // rollback the transaction
285
try
286             {
287                 if (tx != null)
288                 {
289                     tx.rollback();
290                 }
291             } catch (Exception JavaDoc ex)
292             {
293             }
294
295             if (logger.isDebugEnabled())
296             {
297                 logger.error("Unexpected error occurred", e);
298             }
299
300             throw new ContentFault(0, e.getMessage());
301         }
302     }
303 }
304
Popular Tags