1 17 package org.alfresco.repo.webservice.content; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.InputStream ; 21 import java.io.UnsupportedEncodingException ; 22 import java.net.URLEncoder ; 23 import java.rmi.RemoteException ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.transaction.UserTransaction ; 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 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 BROWSER_URL = "{0}://{1}{2}/download/direct/{3}/{4}/{5}/{6}"; 58 59 62 public Content[] read(Predicate items, String property) 63 throws RemoteException , ContentFault 64 { 65 UserTransaction tx = null; 66 67 try 68 { 69 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 70 tx.begin(); 71 72 List <NodeRef> nodes = Utils.resolvePredicate(items, this.nodeService, this.searchService, this.namespaceService); 74 List <Content> results = new ArrayList <Content>(nodes.size()); 75 for (NodeRef nodeRef : nodes) 76 { 77 results.add(createContent(nodeRef, property)); 79 } 80 81 tx.commit(); 83 84 return results.toArray(new Content[results.size()]); 85 } 86 catch (Throwable e) 87 { 88 try 90 { 91 if (tx != null) 92 { 93 tx.rollback(); 94 } 95 } 96 catch (Exception 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 117 private Content createContent(NodeRef nodeRef, String property) 118 throws UnsupportedEncodingException 119 { 120 Content content = null; 121 122 ContentReader contentReader = this.contentService.getReader(nodeRef, QName.createQName(property)); 124 125 if (contentReader != null) 126 { 127 HttpServletRequest req = (HttpServletRequest )MessageContext.getCurrentContext().getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); 129 130 String address = req.getLocalName(); 131 if (req.getLocalPort() != 80) 132 { 133 address = address + ":" + req.getLocalPort(); 134 } 135 136 String filename = (String )this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME); 138 139 String downloadUrl = MessageFormat.format(BROWSER_URL, 141 new Object [] { 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 ContentFormat format = new ContentFormat(contentReader.getMimetype(), contentReader.getEncoding()); 150 content = new Content(Utils.convertToReference(nodeRef), property, contentReader.getSize(), format, downloadUrl); 151 152 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 content = new Content(Utils.convertToReference(nodeRef), property, 0, null, null); 167 168 if (logger.isDebugEnabled()) 170 { 171 logger.debug("No content found: " + nodeRef.getId()); 172 } 173 } 174 175 return content; 176 } 177 178 182 public Content write(Reference node, String property, byte[] content, ContentFormat format) 183 throws RemoteException , ContentFault 184 { 185 UserTransaction tx = null; 186 187 try 188 { 189 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 190 tx.begin(); 191 192 NodeRef nodeRef = Utils.convertToNodeRef(node, this.nodeService, 194 this.searchService, this.namespaceService); 195 196 ContentWriter writer = this.contentService.getWriter(nodeRef, QName.createQName(property), true); 198 199 if (format != null) 201 { 202 writer.setEncoding(format.getEncoding()); 203 writer.setMimetype(format.getMimetype()); 204 } 205 206 InputStream is = new ByteArrayInputStream (content); 208 writer.putContent(is); 209 210 if (logger.isDebugEnabled()) 212 { 213 logger.debug("Updated content for node with id: " + nodeRef.getId()); 214 } 215 216 tx.commit(); 218 219 return createContent(nodeRef, property); 221 } 222 catch (Throwable e) 223 { 224 try 226 { 227 if (tx != null) 228 { 229 tx.rollback(); 230 } 231 } catch (Exception 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 248 public Content[] clear(Predicate items, String property) 249 throws RemoteException , ContentFault 250 { 251 UserTransaction tx = null; 252 253 try 254 { 255 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 256 tx.begin(); 257 258 List <NodeRef> nodes = Utils.resolvePredicate(items, this.nodeService,this.searchService, this.namespaceService); 259 Content[] contents = new Content[nodes.size()]; 260 261 for (int x = 0; x < nodes.size(); x++) 263 { 264 NodeRef nodeRef = nodes.get(x); 265 266 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 tx.commit(); 279 280 return contents; 281 } 282 catch (Throwable e) 283 { 284 try 286 { 287 if (tx != null) 288 { 289 tx.rollback(); 290 } 291 } catch (Exception 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 |