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.export.nuxeo;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.sql.Connection;
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
41 import javax.ws.rs.core.MediaType;
42 import javax.naming.NamingException;
43 import javax.ws.rs.core.Response;
45 import org.apache.commons.lang.StringUtils;
46 import org.collectionspace.authentication.AuthN;
47 import org.collectionspace.services.ExportJAXBSchema;
48 import org.collectionspace.services.account.AccountResource;
49 import org.collectionspace.services.authorization.AuthZ;
50 import org.collectionspace.services.authorization.CSpaceResource;
51 import org.collectionspace.services.authorization.PermissionException;
52 import org.collectionspace.services.authorization.URIResourceImpl;
53 import org.collectionspace.services.authorization.perms.ActionType;
54 import org.collectionspace.services.client.PoxPayloadIn;
55 import org.collectionspace.services.client.PoxPayloadOut;
56 import org.collectionspace.services.client.ExportClient;
57 import org.collectionspace.services.common.CSWebApplicationException;
58 import org.collectionspace.services.common.NuxeoBasedResource;
59 import org.collectionspace.services.common.ResourceMap;
60 import org.collectionspace.services.common.ServiceMain;
61 import org.collectionspace.services.common.api.JEEServerDeployment;
62 import org.collectionspace.services.common.api.FileTools;
63 import org.collectionspace.services.common.api.Tools;
64 import org.collectionspace.services.common.authorization_mgt.ActionGroup;
65 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
66 import org.collectionspace.services.common.context.ServiceBindingUtils;
67 import org.collectionspace.services.common.context.ServiceContext;
68 import org.collectionspace.services.common.document.BadRequestException;
69 import org.collectionspace.services.common.document.DocumentException;
70 import org.collectionspace.services.common.document.DocumentWrapper;
71 import org.collectionspace.services.common.invocable.Invocable;
72 import org.collectionspace.services.common.invocable.InvocationContext;
73 import org.collectionspace.services.common.invocable.InvocationContext.ListCSIDs;
74 import org.collectionspace.services.common.storage.JDBCTools;
75 import org.collectionspace.services.config.service.ServiceBindingType;
76 import org.collectionspace.services.config.types.PropertyItemType;
77 import org.collectionspace.services.export.ExportsCommon;
78 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
79 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
80 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
81 import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
82 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
84 import org.nuxeo.ecm.core.api.model.PropertyException;
85 import org.nuxeo.ecm.core.api.DocumentModel;
87 import org.slf4j.Logger;
88 import org.slf4j.LoggerFactory;
91 * ExportDocumentModelHandler
93 * $LastChangedRevision: $
96 public class ExportDocumentModelHandler extends NuxeoDocumentModelHandler<ExportsCommon> {
97 private final Logger logger = LoggerFactory.getLogger(ExportDocumentModelHandler.class);
99 public InputStream invokeExport(
100 TenantBindingConfigReaderImpl tenantBindingReader,
101 ResourceMap resourceMap,
102 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
103 InvocationContext invocationContext,
104 StringBuffer outputMimeType,
105 StringBuffer outputFileName) throws Exception {
107 NuxeoRepositoryClientImpl repoClient = (NuxeoRepositoryClientImpl) this.getRepositoryClient(serviceContext);
108 boolean releaseRepoSession = false;
109 CoreSessionInterface repoSession = this.getRepositorySession();
111 if (repoSession == null) {
112 repoSession = repoClient.getRepositorySession(serviceContext);
113 releaseRepoSession = true;
117 Iterator<DocumentModel> documents = findDocuments(tenantBindingReader, resourceMap, serviceContext, repoSession, invocationContext);
121 if (releaseRepoSession && repoSession != null) {
122 repoClient.releaseRepositorySession(serviceContext, repoSession);
127 // return buildExportResult(csid, params, exportFileNameProperty, outMimeType.toString(), outExportFileName);
130 private Iterator<DocumentModel> findDocuments(
131 TenantBindingConfigReaderImpl tenantBindingReader,
132 ResourceMap resourceMap,
133 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
134 CoreSessionInterface repoSession,
135 InvocationContext invocationContext) throws Exception {
137 String docType = invocationContext.getDocType();
138 ServiceBindingType binding = tenantBindingReader.getServiceBindingForDocType(serviceContext.getTenantId(), docType);
139 String serviceName = binding.getName();
141 switch (invocationContext.getMode().toLowerCase()) {
142 case Invocable.INVOCATION_MODE_SINGLE:
143 return findDocumentByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getSingleCSID());
144 case Invocable.INVOCATION_MODE_LIST:
145 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getListCSIDs().getCsid());
146 case Invocable.INVOCATION_MODE_GROUP:
147 return findDocumentsByGroup(resourceMap, serviceContext, repoSession, invocationContext.getGroupCSID());
148 case Invocable.INVOCATION_MODE_NO_CONTEXT:
149 return findDocumentsByType(resourceMap, serviceContext, repoSession, invocationContext.getDocType());
155 private Iterator<DocumentModel> findDocumentByCsid(
156 ResourceMap resourceMap,
157 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
158 CoreSessionInterface repoSession,
160 String csid) throws Exception {
162 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, Arrays.asList(csid));
165 private Iterator<DocumentModel> findDocumentsByCsid(
166 ResourceMap resourceMap,
167 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
168 CoreSessionInterface repoSession,
170 List<String> csids) throws Exception {
172 return new DocumentsByCsidIterator(resourceMap, serviceContext, repoSession, serviceName, csids);
175 private Iterator<DocumentModel> findDocumentsByType(
176 ResourceMap resourceMap,
177 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
178 CoreSessionInterface repoSession,
184 private Iterator<DocumentModel> findDocumentsByGroup(
185 ResourceMap resourceMap,
186 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
187 CoreSessionInterface repoSession,
193 // private InputStream buildExportResult(String exportCSID,
194 // HashMap<String, Object> params,
195 // String exportFileName,
196 // String outputMimeType,
197 // StringBuffer outExportFileName) throws Exception {
198 // Connection conn = null;
199 // InputStream result = null;
204 private class DocumentsByCsidIterator implements Iterator<DocumentModel> {
205 private NuxeoBasedResource resource;
206 private ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext;
207 private CoreSessionInterface repoSession;
208 private Iterator<String> csidIterator;
210 DocumentsByCsidIterator(
211 ResourceMap resourceMap,
212 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
213 CoreSessionInterface repoSession,
215 List<String> csids) throws Exception {
217 NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(serviceName.toLowerCase());
219 if (resource == null) {
220 throw new Exception("Resource not found for service name " + serviceName);
223 this.resource = resource;
224 this.serviceContext = serviceContext;
225 this.repoSession = repoSession;
226 this.csidIterator = csids.iterator();
230 public boolean hasNext() {
231 return csidIterator.hasNext();
235 public DocumentModel next() {
236 String csid = csidIterator.next();
239 // PoxPayloadOut payload = resource.getWithParentCtx(serviceContext, csid);
242 catch (Exception e) {
243 logger.warn("Could not get document with csid " + csid, e);