]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3e86596f8710a3e7bc85c728fa64bea558d139c7
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.nuxeo.client.java;
25
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
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;
45
46 /**
47  * RemoteDocumentModelHandler
48  *
49  * $LastChangedRevision: $
50  * $LastChangedDate: $
51  */
52 public abstract class RemoteDocumentModelHandlerImpl<T, TL>
53         extends DocumentModelHandler<T, TL> {
54
55     private final Logger logger = LoggerFactory.getLogger(RemoteDocumentModelHandlerImpl.class);
56
57     @Override
58     public void setServiceContext(ServiceContext ctx) {
59         if(ctx instanceof MultipartServiceContext){
60             super.setServiceContext(ctx);
61         }else{
62             throw new IllegalArgumentException("setServiceContext requires instance of " +
63                     MultipartServiceContext.class.getName());
64         }
65     }
66
67     @Override
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);
78         }
79     }
80
81     @Override
82     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
83
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);
89             if(partMeta == null){
90                 continue; //unknown part, ignore
91             }
92             extractPart(docModel, schema, partMeta);
93         }
94     }
95
96     @Override
97     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
98
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);
109         }
110
111         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
112
113         //iterate over parts received and fill those parts
114         List<InputPart> inputParts = input.getParts();
115         for(InputPart part : inputParts){
116
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);
122             }
123             
124             //skip if the part is not in metadata
125             if(!partsMetaMap.containsKey(partLabel)){
126                 continue;
127             }
128             ObjectPartType partMeta = partsMetaMap.get(partLabel);
129             fillPart(part, docModel, partMeta);
130         }//rof
131
132     }
133
134     /**
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
139      * @throws Exception
140      */
141     protected void fillPart(InputPart part, DocumentModel docModel, ObjectPartType partMeta)
142             throws Exception {
143         InputStream payload = part.getBody(InputStream.class, null);
144
145         //check if this is an xml part
146         if(part.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)){
147             if(payload != null){
148                 Document document = DocumentUtils.parseDocument(payload);
149                 //TODO: callback to handler if registered to validate the
150                 //document
151                 Map<String, Object> objectProps = DocumentUtils.parseProperties(document);
152                 docModel.setProperties(partMeta.getLabel(), objectProps);
153             }
154         }
155     }
156
157     /**
158      * extractPart extracts an XML object from given DocumentModel
159      * @param docModel
160      * @param schema of the object to extract
161      * @param partMeta metadata for the object to extract
162      * @throws Exception
163      */
164     protected void extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
165             throws Exception {
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());
176             }
177             Document doc = DocumentUtils.buildDocument(partMeta, schema, unQObjectProperties);
178             if(logger.isDebugEnabled()){
179                 DocumentUtils.writeDocument(doc, System.out);
180             }
181             MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
182             ctx.addOutputPart(schema, doc, partMeta.getContent().getContentType());
183         } //TODO: handle other media types
184     }
185 }