import org.collectionspace.services.common.ServiceMain;
import org.collectionspace.services.common.ServiceMessages;
import org.collectionspace.services.common.blob.BlobInput;
+import org.collectionspace.services.common.blob.BlobUtil;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentHandler;
+import org.collectionspace.services.blob.nuxeo.BlobDocumentModelHandler; //FIXEME: A resource class should not have a dependency on a specific DocumentHandler
import org.collectionspace.services.blob.BlobsCommon;
import org.collectionspace.services.blob.BlobsCommonList;
@Produces("multipart/mixed")
public class BlobResource extends ResourceBase {
- @Override
+ @Override
public String getServiceName(){
- return "blobs";
- };
+ return BlobUtil.BLOB_RESOURCE_NAME;
+ }
@Override
return (BlobsCommonList) super.search(queryParams, keywords);
}
- private BlobsCommonList getDerivativeList(String csid) throws Exception {
+ private BlobsCommonList getDerivativeList(ServiceContext<MultipartInput, MultipartOutput> ctx,
+ String csid) throws Exception {
BlobsCommonList result = null;
- ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
- ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, Boolean.TRUE);
+ BlobInput blobInput = new BlobInput();
+ blobInput.setDerivativeListRequested(true);
+ BlobUtil.setBlobInput(ctx, blobInput);
+
MultipartOutput response = this.get(csid, ctx);
if (logger.isDebugEnabled() == true) {
logger.debug(response.toString());
}
- result = (BlobsCommonList)ctx.getProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY);
+ //
+ // The result of a successful get should have put the results in the
+ // blobInput instance
+ //
+ result = BlobUtil.getBlobInput(ctx).getDerivativeList();
return result;
}
try {
ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
- ctx.setProperty(BlobInput.BLOB_DERIVATIVE_TERM_KEY, derivativeTerm);
- ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, Boolean.TRUE);
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ blobInput.setDerivativeTerm(derivativeTerm);
+ blobInput.setContentRequested(true);
+
MultipartOutput response = this.get(csid, ctx);
if (logger.isDebugEnabled() == true) {
logger.debug(response.toString());
}
- result = (InputStream)ctx.getProperty(BlobInput.BLOB_CONTENT_KEY);
+ //
+ // The result of a successful get should have put the results in the
+ // blobInput instance
+ //
+ result = BlobUtil.getBlobInput(ctx).getContentStream();
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
}
return result;
}
-
- private BlobInput setBlobInput(ServiceContext<MultipartInput, MultipartOutput> ctx,
- HttpServletRequest req,
- String blobUri) {
- File tmpFile = FileUtils.createTmpFile(req);
- BlobInput blobInput = new BlobInput(tmpFile, blobUri);
- ctx.setProperty(BlobInput.class.getName(), blobInput);
- return blobInput;
- }
-
+
@POST
@Consumes("multipart/form-data")
@Produces("application/xml")
Response response = null;
try {
ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
- setBlobInput(ctx, req, blobUri);
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ blobInput.createBlobFile(req, blobUri);
response = this.create(null, ctx);
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
}
@GET
- @Path("{csid}/derivatives/{derivative_term}/content")
+ @Path("{csid}/derivatives/{derivativeTerm}/content")
@Produces({"image/jpeg", "image/png", "image/tiff"})
public InputStream getDerivativeContent(
@PathParam("csid") String csid,
- @PathParam("derivative_term") String derivative_term) {
+ @PathParam("derivativeTerm") String derivativeTerm) {
InputStream result = null;
- result = getBlobContent(csid, derivative_term);
+ result = getBlobContent(csid, derivativeTerm);
return result;
}
@GET
- @Path("{csid}/derivatives/{derivative_term}")
+ @Path("{csid}/derivatives/{derivativeTerm}")
public MultipartOutput getDerivative(@PathParam("csid") String csid,
- @PathParam("derivative_term") String derivative_term) {
+ @PathParam("derivativeTerm") String derivativeTerm) {
MultipartOutput result = null;
ensureCSID(csid, READ);
try {
ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
- ctx.setProperty(BlobInput.BLOB_DERIVATIVE_TERM_KEY, derivative_term);
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ blobInput.setDerivativeTerm(derivativeTerm);
result = get(csid, ctx);
if (result == null) {
Response response = Response.status(Response.Status.NOT_FOUND).entity(
ensureCSID(csid, READ);
try {
ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
- ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, true);
- result = this.getDerivativeList(csid);
+ result = this.getDerivativeList(ctx, csid);
if (result == null) {
Response response = Response.status(Response.Status.NOT_FOUND).entity(
ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
import org.collectionspace.services.common.blob.BlobInput;
import org.collectionspace.services.common.blob.BlobOutput;
+import org.collectionspace.services.common.blob.BlobUtil;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentWrapper;
import org.collectionspace.services.common.document.DocumentHandler.Action;
import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
import org.collectionspace.services.nuxeo.util.NuxeoUtils;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
/** The logger. */
private final Logger logger = LoggerFactory.getLogger(BlobDocumentModelHandler.class);
-
+
public final String getNuxeoSchemaName(){
return "blobs";
}
List list = ((BlobsCommonList)commonList).getBlobListItem();
return list;
}
-
+
private String getDerivativePathBase(DocumentModel docModel) {
return getServiceContextPath() + docModel.getName() + "/" +
BlobInput.URI_DERIVATIVES_PATH + "/";
public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
throws Exception {
ServiceContext ctx = this.getServiceContext();
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
RepositoryInstance repoSession = this.getRepositorySession();
DocumentModel docModel = wrapDoc.getWrappedObject();
BlobsCommon blobsCommon = this.getCommonPartProperties(docModel);
String blobRepositoryId = blobsCommon.getRepositoryId(); //cache the value to pass to the blob retriever
- if (ctx.getProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY) != null) {
+ if (blobInput.isDerivativeListRequested() == true) {
BlobsCommonList blobsCommonList = NuxeoImageUtils.getBlobDerivatives(
repoSession, blobRepositoryId, getDerivativePathBase(docModel));
- ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, blobsCommonList);
+// ctx.setProperty(BlobInput.BLOB_DERIVATIVE_LIST_KEY, blobsCommonList);
+ blobInput.setDerivativeList(blobsCommonList);
return; //FIXME: Don't like this exit point. Perhaps derivatives should be a sub-resource?
}
- String derivativeTerm = (String)ctx.getProperty(BlobInput.BLOB_DERIVATIVE_TERM_KEY);
- Boolean getContentFlag = ctx.getProperty(BlobInput.BLOB_CONTENT_KEY) != null ? true : false;
+ String derivativeTerm = blobInput.getDerivativeTerm();
+ Boolean getContentFlag = blobInput.isContentRequested();
BlobOutput blobOutput = NuxeoImageUtils.getBlobOutput(ctx, repoSession,
blobRepositoryId, derivativeTerm, getContentFlag);
if (getContentFlag == true) {
- ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, blobOutput.getBlobInputStream());
+ blobInput.setContentStream(blobOutput.getBlobInputStream());
+// ctx.setProperty(BlobInput.BLOB_CONTENT_KEY, blobOutput.getBlobInputStream());
}
if (derivativeTerm != null) {
@Override
public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
ServiceContext ctx = this.getServiceContext();
- BlobInput blobInput = (BlobInput)ctx.getProperty(BlobInput.class.getName());
- if (blobInput == null) {
- super.fillAllParts(wrapDoc, action);
- } else {
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ if (blobInput.getBlobFile() != null) {
//
- // If blobInput is set then we just received a multipart/form-data file post
+ // If blobInput has a file then we just received a multipart/form-data file post
//
DocumentModel documentModel = wrapDoc.getWrappedObject();
RepositoryInstance repoSession = this.getRepositorySession();
BlobsCommon blobsCommon = NuxeoImageUtils.createPicture(ctx, repoSession, blobInput);
this.setCommonPartProperties(documentModel, blobsCommon);
+ blobInput.setBlobCsid(documentModel.getName());
+ } else {
+ super.fillAllParts(wrapDoc, action);
}
}
}
*/\r
package org.collectionspace.services.common;\r
\r
+import java.util.List;\r
+\r
import javax.ws.rs.GET;\r
import javax.ws.rs.Path;\r
import javax.ws.rs.Produces;\r
import javax.ws.rs.core.MultivaluedMap;\r
+import javax.ws.rs.core.Response;\r
\r
import org.collectionspace.services.common.context.ServiceContext;\r
import org.collectionspace.services.common.context.ServiceContextProperties;\r
import org.collectionspace.services.common.repository.RepositoryClientFactory;\r
import org.collectionspace.services.common.storage.StorageClient;\r
import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;\r
+import org.jboss.resteasy.client.ClientResponse;\r
\r
/**\r
* The Class AbstractCollectionSpaceResourceImpl.\r
/** The storage client. */\r
private StorageClient storageClient;\r
\r
+ /**\r
+ * Extract id.\r
+ *\r
+ * @param res the res\r
+ * @return the string\r
+ */\r
+ protected static String extractId(Response res) {\r
+ MultivaluedMap<String, Object> mvm = res.getMetadata();\r
+ String uri = (String) ((List<Object>) mvm.get("Location")).get(0);\r
+ String[] segments = uri.split("/");\r
+ String id = segments[segments.length - 1];\r
+ return id;\r
+ } \r
+ \r
/**\r
* Instantiates a new abstract collection space resource.\r
*/\r
return bigReThrow(e, serviceMsg, "");\r
}\r
\r
- protected WebApplicationException bigReThrow(Exception e, String serviceMsg, String csid)\r
- throws WebApplicationException {\r
- Response response;\r
- if (logger.isDebugEnabled()) {\r
- logger.debug(getClass().getName(), e);\r
- }\r
- if (e instanceof UnauthorizedException) {\r
- response = Response.status(Response.Status.UNAUTHORIZED)\r
- .entity(serviceMsg + e.getMessage())\r
- .type("text/plain")\r
- .build();\r
- return new WebApplicationException(response);\r
- } else if (e instanceof DocumentNotFoundException) {\r
- response = Response.status(Response.Status.NOT_FOUND)\r
- .entity(serviceMsg + " on "+getClass().getName()+" csid=" + csid)\r
- .type("text/plain")\r
- .build();\r
- return new WebApplicationException(response);\r
- } else { //e is now instanceof Exception\r
- response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)\r
- .entity(serviceMsg)\r
- .type("text/plain")\r
- .build();\r
- return new WebApplicationException(response);\r
- }\r
- }\r
+ protected WebApplicationException bigReThrow(Exception e,\r
+ String serviceMsg, String csid) throws WebApplicationException {\r
+ Response response;\r
+ if (logger.isDebugEnabled()) {\r
+ logger.debug(getClass().getName(), e);\r
+ }\r
+ if (e instanceof UnauthorizedException) {\r
+ response = Response.status(Response.Status.UNAUTHORIZED)\r
+ .entity(serviceMsg + e.getMessage()).type("text/plain")\r
+ .build();\r
+ return new WebApplicationException(response);\r
+ } else if (e instanceof DocumentNotFoundException) {\r
+ response = Response\r
+ .status(Response.Status.NOT_FOUND)\r
+ .entity(serviceMsg + " on " + getClass().getName()\r
+ + " csid=" + csid).type("text/plain").build();\r
+ return new WebApplicationException(response);\r
+ } else if (e instanceof WebApplicationException) {\r
+ //\r
+ // subresource may have already thrown this exception\r
+ // so just pass it on\r
+ return (WebApplicationException)e;\r
+ } else { // e is now instanceof Exception\r
+ response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)\r
+ .entity(serviceMsg).type("text/plain").build();\r
+ return new WebApplicationException(response);\r
+ }\r
+ }\r
\r
//======================= CREATE ====================================================\r
\r
ensureCSID(csid, UPDATE);\r
try {\r
ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);\r
- DocumentHandler handler = createDocumentHandler(ctx);\r
- return update(csid, theUpdate, ctx, handler); //==> CALL implementation method, which subclasses may override.\r
+ return update(csid, theUpdate, ctx); //==> CALL implementation method, which subclasses may override.\r
} catch (Exception e) {\r
throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid);\r
}\r
}\r
\r
+ /** Subclasses may override this overload, which gets called from #udpate(String,MultipartInput) */\r
+ protected MultipartOutput update(String csid,\r
+ MultipartInput theUpdate,\r
+ ServiceContext<MultipartInput, MultipartOutput> ctx)\r
+ throws Exception {\r
+ DocumentHandler handler = createDocumentHandler(ctx);\r
+ getRepositoryClient(ctx).update(ctx, csid, handler);\r
+ return (MultipartOutput) ctx.getOutput();\r
+ }\r
+\r
/** Subclasses may override this overload, which gets called from #udpate(String,MultipartInput) */\r
protected MultipartOutput update(String csid,\r
MultipartInput theUpdate,\r
package org.collectionspace.services.common.blob;\r
\r
import java.io.File;\r
+import java.io.InputStream;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+\r
+import org.collectionspace.services.blob.BlobsCommonList; \r
+import org.collectionspace.services.common.FileUtils;\r
\r
public class BlobInput {\r
- private String mediaCsid;\r
- private File blobFile;\r
- private String blobUri;\r
+ private String blobCsid = null;\r
+ private File blobFile = null;\r
+ private String blobUri = null;\r
\r
- public static final String URI_CONTENT_PATH = "content";\r
- public static final String URI_DERIVATIVES_PATH = "derivatives";\r
+ private String derivativeTerm;\r
+ private boolean derivativeListRequested = false;\r
+ private BlobsCommonList derivativeList;\r
+ \r
+ private boolean contentRequested = false;\r
+ private InputStream contentStream;\r
\r
- public static final String BLOB_DERIVATIVE_TERM_KEY = "derivative";\r
- public static final String BLOB_DERIVATIVE_LIST_KEY = "derivative.list";\r
- public static final String BLOB_CONTENT_KEY = "derivative.content.stream";\r
+ private boolean schemaRequested = false;\r
\r
+ public static final String URI_CONTENT_PATH = "content";\r
+ public static final String URI_DERIVATIVES_PATH = "derivatives";\r
+\r
+ /*\r
+ * Constructors\r
+ */\r
+ public BlobInput() {\r
+ /* Empty constructor */\r
+ }\r
+ \r
public BlobInput(File blobFile, String blobUri) {\r
this.blobFile = blobFile;\r
this.blobUri = blobUri;\r
}\r
\r
- public String getMediaCsid() {\r
- return mediaCsid;\r
+ /*\r
+ * Getters and Setters\r
+ */\r
+ public boolean isSchemaRequested() {\r
+ return schemaRequested;\r
}\r
- \r
+\r
+ public void setSchemaRequested(boolean schemaRequested) {\r
+ this.schemaRequested = schemaRequested;\r
+ }\r
+\r
+ public String getBlobCsid() {\r
+ return blobCsid;\r
+ }\r
+\r
+ public void setBlobCsid(String blobCsid) {\r
+ this.blobCsid = blobCsid;\r
+ }\r
+\r
public File getBlobFile() {\r
return blobFile;\r
}\r
- \r
+\r
+ public void setBlobFile(File blobFile) {\r
+ this.blobFile = blobFile;\r
+ }\r
+\r
public String getBlobUri() {\r
return blobUri;\r
}\r
+\r
+ public void setBlobUri(String blobUri) {\r
+ this.blobUri = blobUri;\r
+ }\r
+\r
+ public String getDerivativeTerm() {\r
+ return derivativeTerm;\r
+ }\r
+\r
+ public void setDerivativeTerm(String derivativeTerm) {\r
+ this.derivativeTerm = derivativeTerm;\r
+ }\r
+\r
+ public boolean isDerivativeListRequested() {\r
+ return derivativeListRequested;\r
+ }\r
+\r
+ public void setDerivativeListRequested(boolean derivativesRequested) {\r
+ this.derivativeListRequested = derivativesRequested;\r
+ }\r
+\r
+ public BlobsCommonList getDerivativeList() {\r
+ return derivativeList;\r
+ }\r
+\r
+ public void setDerivativeList(BlobsCommonList derivativeList) {\r
+ this.derivativeList = derivativeList;\r
+ }\r
+\r
+ public InputStream getContentStream() {\r
+ return contentStream;\r
+ }\r
+\r
+ public void setContentStream(InputStream contentStream) {\r
+ this.contentStream = contentStream;\r
+ }\r
+\r
+ public boolean isContentRequested() {\r
+ return contentRequested;\r
+ }\r
+\r
+ public void setContentRequested(boolean contentRequested) {\r
+ this.contentRequested = contentRequested;\r
+ } \r
+ /*\r
+ * End of setters and getters\r
+ */\r
+ \r
+ public void createBlobFile(HttpServletRequest req, String blobUri) {\r
+ File tmpFile = FileUtils.createTmpFile(req);\r
+ this.setBlobFile(tmpFile);\r
+ this.setBlobUri(blobUri);\r
+ } \r
}\r
\r
package org.collectionspace.services.common.blob;\r
\r
-public class BlobUtil {\r
+import org.collectionspace.services.common.context.ServiceContext;\r
+import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
+import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
\r
+public class BlobUtil {\r
+ //FIXME: REM - We should have a class/interface in common that has constant defs for the names of all the services.\r
+ public static String BLOB_RESOURCE_NAME = "blobs";\r
+ private static final Logger logger = LoggerFactory.getLogger(BlobUtil.class);\r
+ \r
+ public static BlobInput getBlobInput(ServiceContext<MultipartInput, MultipartOutput> ctx) {\r
+ BlobInput result = (BlobInput)ctx.getProperty(BlobInput.class.getName());\r
+ if (result == null) {\r
+ result = new BlobInput();\r
+ setBlobInput(ctx, result);\r
+ }\r
+ return result;\r
+ }\r
+ \r
+ public static BlobInput resetBlobInput(ServiceContext<MultipartInput, MultipartOutput> ctx) {\r
+ BlobInput blobInput = new BlobInput();\r
+ setBlobInput(ctx, blobInput);\r
+ return blobInput;\r
+ }\r
+ \r
+ public static void setBlobInput(ServiceContext<MultipartInput, MultipartOutput> ctx,\r
+ BlobInput blobInput) {\r
+ ctx.setProperty(BlobInput.class.getName(), blobInput); \r
+ } \r
+ \r
}\r
//return at least those document part(s) that were received
Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
- List<InputPart> inputParts = ctx.getInput().getParts();
- for (InputPart part : inputParts) {
- String partLabel = part.getHeaders().getFirst("label");
- ObjectPartType partMeta = partsMetaMap.get(partLabel);
-// extractPart(docModel, partLabel, partMeta);
- Map<String, Object> unQObjectProperties = extractPart(docModel, partLabel, partMeta);
- addOutputPart(unQObjectProperties, partLabel, partMeta);
+ MultipartInput input = ctx.getInput();
+ if (input != null) {
+ List<InputPart> inputParts = ctx.getInput().getParts();
+ for (InputPart part : inputParts) {
+ String partLabel = part.getHeaders().getFirst("label");
+ ObjectPartType partMeta = partsMetaMap.get(partLabel);
+ // extractPart(docModel, partLabel, partMeta);
+ Map<String, Object> unQObjectProperties = extractPart(docModel, partLabel, partMeta);
+ addOutputPart(unQObjectProperties, partLabel, partMeta);
+ }
+ } else {
+ if (logger.isWarnEnabled() == true) {
+ logger.warn("MultipartInput part was null for document id = " +
+ docModel.getName());
+ }
}
}
<xs:element name="title" type="xs:string"/>\r
<xs:element name="type" type="xs:string"/>\r
<xs:element name="uri" type="xs:string" />\r
+ <xs:element name="blobCsid" type="xs:string" />\r
\r
</xs:schema>\r
final static String title = "title";
final static String type = "type";
final static String uri = "uri";
+ final static String blobCsid = "blobCsid";
}
<xs:element name="title" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="uri" type="xs:string" />
+ <xs:element name="blobCsid" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<artifactId>org.collectionspace.services.common</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.collectionspace.services</groupId>
+ <artifactId>org.collectionspace.services.jaxb</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.collectionspace.services</groupId>
+ <artifactId>org.collectionspace.services.blob.service</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.collectionspace.services</groupId>
<artifactId>org.collectionspace.services.media.jaxb</artifactId>
import org.collectionspace.services.common.ResourceBase;
import org.collectionspace.services.common.ClientType;
import org.collectionspace.services.common.ServiceMain;
+import org.collectionspace.services.common.ServiceMessages;
+import org.collectionspace.services.common.blob.BlobInput;
+import org.collectionspace.services.common.blob.BlobUtil;
+import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.document.DocumentHandler;
+import org.collectionspace.services.blob.BlobsCommon;
+import org.collectionspace.services.blob.BlobsCommonList;
+import org.collectionspace.services.blob.nuxeo.BlobDocumentModelHandler;
+import org.collectionspace.services.blob.BlobResource;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
+
+import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import java.io.InputStream;
import java.util.List;
@Path("/media")
@Override
public String getServiceName(){
return "media";
- };
+ }
+
+ private BlobResource blobResource = new BlobResource();
+ BlobResource getBlobResource() {
+ return blobResource;
+ }
+
+// /*
+// * This member is used to get and set context for the blob document handler
+// */
+// private BlobInput blobInput = new BlobInput();
+//
+// public BlobInput getBlobInput(ServiceContext<MultipartInput, MultipartOutput> ctx) {
+// //
+// // Publish the blobInput to the current context on every get. Even though
+// // it might already be published.
+// //
+// BlobUtil.setBlobInput(ctx, blobInput);
+// return blobInput;
+// }
+ private String getBlobCsid(String mediaCsid) throws Exception {
+ String result = null;
+
+ ServiceContext<MultipartInput, MultipartOutput> mediaContext = createServiceContext();
+ BlobInput blobInput = BlobUtil.getBlobInput(mediaContext);
+ blobInput.setSchemaRequested(true);
+ get(mediaCsid, mediaContext); //this call sets the blobInput.blobCsid field for us
+ result = blobInput.getBlobCsid();
+ ensureCSID(result, READ);
+
+ return result;
+ }
+
+
//FIXME retrieve client type from configuration
final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
return MediaCommon.class;
}
+ @POST
+ @Path("{csid}")
+ @Consumes("multipart/form-data")
+ @Produces("application/xml")
+ public Response createBlob(@Context HttpServletRequest req,
+ @QueryParam("blobUri") String blobUri,
+ @PathParam("csid") String csid) {
+ MultipartInput input = null;
+ Response response = null;
+ try {
+ //
+ // First, create the blob
+ //
+ ServiceContext<MultipartInput, MultipartOutput> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input);
+ BlobInput blobInput = BlobUtil.getBlobInput(blobContext);
+ blobInput.createBlobFile(req, blobUri);
+ response = this.create(input, blobContext);
+ //
+ // Next, update the Media record to be linked to the blob
+ //
+ ServiceContext<MultipartInput, MultipartOutput> mediaContext = createServiceContext();
+ BlobUtil.setBlobInput(mediaContext, blobInput); //and put the blobInput into the Media context
+ this.update(csid, input, mediaContext);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
+ }
+
+ return response;
+ }
+
+ @GET
+ @Path("{csid}/blob")
+ public MultipartOutput getBlobInfo(@PathParam("csid") String csid) {
+ MultipartOutput result = null;
+
+ try {
+ String blobCsid = this.getBlobCsid(csid);
+ ServiceContext<MultipartInput, MultipartOutput> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+ result = this.get(blobCsid, blobContext);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
+ }
+
+ return result;
+ }
+ @GET
+ @Path("{csid}/blob/content")
+ @Produces({"image/jpeg", "image/png", "image/tiff"})
+ public InputStream getBlobContent(
+ @PathParam("csid") String csid) {
+ InputStream result = null;
+
+ try {
+ ensureCSID(csid, READ);
+ String blobCsid = this.getBlobCsid(csid);
+ result = getBlobResource().getBlobContent(blobCsid);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
+ }
+
+ return result;
+ }
+
+ @GET
+ @Path("{csid}/blob/derivatives/{derivativeTerm}/content")
+ @Produces({"image/jpeg", "image/png", "image/tiff"})
+ public InputStream getDerivativeContent(
+ @PathParam("csid") String csid,
+ @PathParam("derivativeTerm") String derivativeTerm) {
+ InputStream result = null;
+
+ try {
+ ensureCSID(csid, READ);
+ String blobCsid = this.getBlobCsid(csid);
+ result = getBlobResource().getDerivativeContent(blobCsid, derivativeTerm);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
+ }
+
+ return result;
+ }
+
+ @GET
+ @Path("{csid}/blob/derivatives/{derivativeTerm}")
+ public MultipartOutput getDerivative(@PathParam("csid") String csid,
+ @PathParam("derivativeTerm") String derivativeTerm) {
+ MultipartOutput result = null;
+
+ try {
+ ensureCSID(csid, READ);
+ String blobCsid = this.getBlobCsid(csid);
+ ServiceContext<MultipartInput, MultipartOutput> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+ result = getBlobResource().getDerivative(blobCsid, derivativeTerm);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
+ }
+
+ return result;
+ }
+
+ @GET
+ @Path("{csid}/blob/derivatives")
+ @Produces("application/xml")
+ public BlobsCommonList getDerivatives(
+ @PathParam("csid") String csid) {
+ BlobsCommonList result = null;
+
+ try {
+ ensureCSID(csid, READ);
+ String blobCsid = this.getBlobCsid(csid);
+ ServiceContext<MultipartInput, MultipartOutput> blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME);
+ result = getBlobResource().getDerivatives(blobCsid);
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
+ }
+
+ return result;
+ }
}
import java.util.List;
import org.collectionspace.services.MediaJAXBSchema;
+import org.collectionspace.services.blob.BlobsCommon;
import org.collectionspace.services.common.DocHandlerBase;
+import org.collectionspace.services.common.blob.BlobInput;
+import org.collectionspace.services.common.blob.BlobUtil;
+import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.document.DocumentWrapper;
+import org.collectionspace.services.common.document.DocumentHandler.Action;
+import org.collectionspace.services.common.imaging.nuxeo.NuxeoImageUtils;
import org.collectionspace.services.media.MediaCommon;
import org.collectionspace.services.media.MediaCommonList;
import org.collectionspace.services.media.MediaCommonList.MediaListItem;
import org.collectionspace.services.jaxb.AbstractCommonList;
+import org.collectionspace.services.jaxb.BlobJAXBSchema;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
import org.nuxeo.ecm.core.api.DocumentModel;
+import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
/**
* The Class MediaDocumentModelHandler.
*/
public class MediaDocumentModelHandler
extends DocHandlerBase<MediaCommon, AbstractCommonList> {
-
+
public final String getNuxeoSchemaName(){
return "media";
}
List list = ((MediaCommonList)commonList).getMediaListItem();
return list;
}
-
+
+ private MediaCommon getCommonPartProperties(DocumentModel docModel) throws Exception {
+ String label = getServiceContext().getCommonPartLabel();
+ MediaCommon result = new MediaCommon();
+
+ result.setBlobCsid((String)
+ docModel.getProperty(label, MediaJAXBSchema.blobCsid));
+
+ return result;
+ }
+
public Object createItemForCommonList(DocumentModel docModel, String label, String id) throws Exception {
MediaListItem item = new MediaListItem();
item.setTitle((String) docModel.getProperty(label, MediaJAXBSchema.title));
item.setCsid(id);
return item;
}
+
+ @Override
+ public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
+ throws Exception {
+ ServiceContext ctx = this.getServiceContext();
+
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ if (blobInput != null && blobInput.isSchemaRequested()) { //Extract the blob info instead of the media info
+ DocumentModel docModel = wrapDoc.getWrappedObject();
+ MediaCommon mediaCommon = this.getCommonPartProperties(docModel);
+ String blobCsid = mediaCommon.getBlobCsid(); //cache the value to pass to the blob retriever
+ blobInput.setBlobCsid(blobCsid);
+ } else {
+ super.extractAllParts(wrapDoc);
+ }
+ }
+
+ @Override
+ public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
+ ServiceContext ctx = this.getServiceContext();
+ BlobInput blobInput = BlobUtil.getBlobInput(ctx);
+ if (blobInput != null && blobInput.getBlobCsid() != null) {
+ String blobCsid = blobInput.getBlobCsid();
+ //
+ // If getBlobCsid has a value then we just received a multipart/form-data file post
+ //
+ DocumentModel documentModel = wrapDoc.getWrappedObject();
+ documentModel.setProperty(ctx.getCommonPartLabel(), MediaJAXBSchema.blobCsid, blobCsid);
+ } else {
+ super.fillAllParts(wrapDoc, action);
+ }
+ }
+
}