2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.nuxeo.client.java;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import javax.ws.rs.core.MediaType;
33 import org.collectionspace.services.common.context.MultipartServiceContext;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.BadRequestException;
36 import org.collectionspace.services.common.document.DocumentUtils;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.common.service.ObjectPartType;
39 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.w3c.dom.Document;
47 * RemoteDocumentModelHandler
49 * $LastChangedRevision: $
52 public abstract class RemoteDocumentModelHandlerImpl<T, TL>
53 extends DocumentModelHandler<T, TL> {
55 private final Logger logger = LoggerFactory.getLogger(RemoteDocumentModelHandlerImpl.class);
58 public void setServiceContext(ServiceContext ctx) {
59 if(ctx instanceof MultipartServiceContext){
60 super.setServiceContext(ctx);
62 throw new IllegalArgumentException("setServiceContext requires instance of " +
63 MultipartServiceContext.class.getName());
68 public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
69 DocumentModel docModel = wrapDoc.getWrappedObject();
70 //return at least those document part(s) that were received
71 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
72 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
73 List<InputPart> inputParts = ctx.getInput().getParts();
74 for(InputPart part : inputParts){
75 String partLabel = part.getHeaders().getFirst("label");
76 ObjectPartType partMeta = partsMetaMap.get(partLabel);
77 extractPart(docModel, partLabel, partMeta);
82 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84 DocumentModel docModel = wrapDoc.getWrappedObject();
85 String[] schemas = docModel.getDeclaredSchemas();
86 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
87 for(String schema : schemas){
88 ObjectPartType partMeta = partsMetaMap.get(schema);
90 continue; //unknown part, ignore
92 extractPart(docModel, schema, partMeta);
97 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
99 //TODO filling extension parts should be dynamic
100 //Nuxeo APIs lack to support stream/byte[] input, get/setting properties is
101 //not an ideal way of populating objects.
102 DocumentModel docModel = wrapDoc.getWrappedObject();
103 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
104 MultipartInput input = ctx.getInput();
105 if(input.getParts().isEmpty()){
106 String msg = "No payload found!";
107 logger.error(msg + "Ctx=" + getServiceContext().toString());
108 throw new BadRequestException(msg);
111 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
113 //iterate over parts received and fill those parts
114 List<InputPart> inputParts = input.getParts();
115 for(InputPart part : inputParts){
117 String partLabel = part.getHeaders().getFirst("label");
118 if (partLabel == null) {
119 String msg = "Part label is missing or empty!";
120 logger.error(msg + "Ctx=" + getServiceContext().toString());
121 throw new BadRequestException(msg);
124 //skip if the part is not in metadata
125 if(!partsMetaMap.containsKey(partLabel)){
128 ObjectPartType partMeta = partsMetaMap.get(partLabel);
129 fillPart(part, docModel, partMeta);
135 * fillPart fills an XML part into given document model
136 * @param part to fill
137 * @param docModel for the given object
138 * @param partMeta metadata for the object to fill
141 protected void fillPart(InputPart part, DocumentModel docModel, ObjectPartType partMeta)
143 InputStream payload = part.getBody(InputStream.class, null);
145 //check if this is an xml part
146 if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
148 Document document = DocumentUtils.parseDocument(payload);
149 //TODO: callback to handler if registered to validate the
151 Map<String, Object> objectProps = DocumentUtils.parseProperties(document);
152 docModel.setProperties(partMeta.getLabel(), objectProps);
158 * extractPart extracts an XML object from given DocumentModel
160 * @param schema of the object to extract
161 * @param partMeta metadata for the object to extract
164 protected void extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
166 MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType());
167 if(mt.equals(MediaType.APPLICATION_XML_TYPE)){
168 Map<String, Object> objectProps = docModel.getProperties(schema);
169 //unqualify properties before sending the doc over the wire (to save bandwidh)
170 //FIXME: is there a better way to avoid duplication of a collection?
171 Map<String, Object> unQObjectProperties = new HashMap<String, Object>();
172 Set<Entry<String, Object>> qualifiedEntries = objectProps.entrySet();
173 for(Entry<String, Object> entry : qualifiedEntries){
174 String unqProp = getUnQProperty(entry.getKey());
175 unQObjectProperties.put(unqProp, entry.getValue());
177 Document doc = DocumentUtils.buildDocument(partMeta, schema, unQObjectProperties);
178 if(logger.isDebugEnabled()){
179 DocumentUtils.writeDocument(doc, System.out);
181 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
182 ctx.addOutputPart(schema, doc, partMeta.getContent().getContentType());
183 } //TODO: handle other media types