From: Sanjay Dalal Date: Thu, 19 Nov 2009 03:20:22 +0000 (+0000) Subject: NOJIRA added jpastorage. refactored context and dochandler to isolate multipart proce... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=a5286770d0b55bafccfc511b79db0314cead11f1;p=tmp%2Fjakarta-migration.git NOJIRA added jpastorage. refactored context and dochandler to isolate multipart processing introduced servicecontext factory to free up abstractresource from assumption that it is used only to process multipart remote requests. affected services are fixed. mvn test passes. account is not yet plugged into service build. account build fails. M services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java M services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java M services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResource.java A services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextFactory.java M services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContext.java M services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContextImpl.java M services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java A services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContext.java M services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java A services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextImpl.java A services/common/src/main/java/org/collectionspace/services/common/context/ServiceContextFactory.java A services/common/src/main/java/org/collectionspace/services/common/storage/jpa A services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java M services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandler.java A services/common/src/main/java/org/collectionspace/services/common/document/AbstractMultipartDocumentHandler.java M services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java A services/common/src/main/java/org/collectionspace/services/common/document/MultipartDocumentHandler.java M services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java M services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandler.java M services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java M services/common/pom.xml M services/acquisition/service/src/main/java/org/collectionspace/services/acquisition/AcquisitionResource.java _M services/account/service M services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java M services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java M services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java M services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java M services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java --- diff --git a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java index 6665462c8..014022c69 100644 --- a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java +++ b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java @@ -38,19 +38,18 @@ import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.collectionspace.services.common.AbstractCollectionSpaceResource; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.RemoteServiceContextImpl; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; import org.jboss.resteasy.util.HttpResponseCodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Path("/accounts") -@Consumes("multipart/mixed") -@Produces("multipart/mixed") +@Consumes("application/xml") +@Produces("application/xml") public class AccountResource extends AbstractCollectionSpaceResource { @@ -62,15 +61,21 @@ public class AccountResource return serviceName; } + private ServiceContext createServiceContext(T obj) { + ServiceContext ctx = new RemoteServiceContextImpl(getServiceName()); + ctx.setInput(obj); + return ctx; + } + @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { throw new IllegalStateException(); } @POST - public Response createAccount(MultipartInput input) { + public Response createAccount(AccountsCommon input) { try { - RemoteServiceContext ctx = createServiceContext(input); + ServiceContext ctx = createServiceContext(input); String csid = ""; UriBuilder path = UriBuilder.fromResource(AccountResource.class); path.path("" + csid); @@ -88,7 +93,7 @@ public class AccountResource @GET @Path("{csid}") - public MultipartOutput getAccount( + public AccountsCommon getAccount( @PathParam("csid") String csid) { if (logger.isDebugEnabled()) { logger.debug("getAccount with csid=" + csid); @@ -100,11 +105,11 @@ public class AccountResource "text/plain").build(); throw new WebApplicationException(response); } - MultipartOutput result = null; + AccountsCommon result = null; try { - RemoteServiceContext ctx = createServiceContext(null); + ServiceContext ctx = createServiceContext((AccountsCommon)null); - result = ctx.getOutput(); + result = (AccountsCommon) ctx.getOutput(); } catch (DocumentNotFoundException dnfe) { if (logger.isDebugEnabled()) { logger.debug("getAccount", dnfe); @@ -136,7 +141,7 @@ public class AccountResource public AccountsCommonList getAccountList(@Context UriInfo ui) { AccountsCommonList accountList = new AccountsCommonList(); try { - RemoteServiceContext ctx = createServiceContext(null); + ServiceContext ctx = createServiceContext((AccountsCommonList)null); accountList = null; } catch (Exception e) { @@ -152,9 +157,9 @@ public class AccountResource @PUT @Path("{csid}") - public MultipartOutput updateAccount( + public AccountsCommon updateAccount( @PathParam("csid") String csid, - MultipartInput theUpdate) { + AccountsCommon theUpdate) { if (logger.isDebugEnabled()) { logger.debug("updateAccount with csid=" + csid); } @@ -165,11 +170,11 @@ public class AccountResource "text/plain").build(); throw new WebApplicationException(response); } - MultipartOutput result = null; + AccountsCommon result = null; try { - RemoteServiceContext ctx = createServiceContext(theUpdate); + ServiceContext ctx = createServiceContext(theUpdate); - result = ctx.getOutput(); + result = (AccountsCommon)ctx.getOutput(); } catch (DocumentNotFoundException dnfe) { if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateAccount", dnfe); @@ -201,7 +206,7 @@ public class AccountResource throw new WebApplicationException(response); } try { - ServiceContext ctx = createServiceContext(null); + ServiceContext ctx = createServiceContext((AccountsCommon)null); return Response.status(HttpResponseCodes.SC_OK).build(); } catch (DocumentNotFoundException dnfe) { diff --git a/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java b/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java index d76d73473..cc554b9eb 100644 --- a/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java +++ b/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java @@ -23,6 +23,7 @@ */ package org.collectionspace.services.account.storage; +import java.util.UUID; import org.collectionspace.services.account.AccountsCommon; import org.collectionspace.services.account.AccountsCommonList; import org.collectionspace.services.common.document.AbstractDocumentHandler; @@ -34,47 +35,40 @@ import org.collectionspace.services.common.document.DocumentWrapper; * @author */ public class AccountDocumentHandler - extends AbstractDocumentHandler { + extends AbstractDocumentHandler { @Override - public void handleCreate(DocumentWrapper wrapDoc) throws Exception { + public void handleCreate(DocumentWrapper wrapDoc) throws Exception { + String id = UUID.randomUUID().toString(); + AccountsCommon account = wrapDoc.getWrappedObject(); + account.setCsid(id); } @Override - public void handleUpdate(DocumentWrapper wrapDoc) throws Exception { + public void handleUpdate(DocumentWrapper wrapDoc) throws Exception { } @Override - public void handleGet(DocumentWrapper wrapDoc) throws Exception { + public void handleGet(DocumentWrapper wrapDoc) throws Exception { } @Override - public void handleGetAll(DocumentWrapper wrapDoc) throws Exception { + public void handleGetAll(DocumentWrapper< AccountsCommonList> wrapDoc) throws Exception { } @Override - public void extractAllParts(DocumentWrapper wrapDoc) - throws Exception { - } - - @Override - public void fillAllParts(DocumentWrapper wrapDoc) - throws Exception { - } - - @Override - public AccountsCommon extractCommonPart(DocumentWrapper wrapDoc) + public AccountsCommon extractCommonPart(DocumentWrapper wrapDoc) throws Exception { return null; } @Override - public void fillCommonPart(AccountsCommon obj, DocumentWrapper wrapDoc) + public void fillCommonPart(AccountsCommon obj, DocumentWrapper wrapDoc) throws Exception { } @Override - public AccountsCommonList extractCommonPartList(DocumentWrapper wrapDoc) + public AccountsCommonList extractCommonPartList(DocumentWrapper wrapDoc) throws Exception { return null; } diff --git a/services/acquisition/service/src/main/java/org/collectionspace/services/acquisition/AcquisitionResource.java b/services/acquisition/service/src/main/java/org/collectionspace/services/acquisition/AcquisitionResource.java index 8e606825f..26ef178a1 100644 --- a/services/acquisition/service/src/main/java/org/collectionspace/services/acquisition/AcquisitionResource.java +++ b/services/acquisition/service/src/main/java/org/collectionspace/services/acquisition/AcquisitionResource.java @@ -39,7 +39,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.acquisition.nuxeo.AcquisitionHandlerFactory; import org.collectionspace.services.common.AbstractCollectionSpaceResource; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; @@ -64,13 +65,13 @@ public class AcquisitionResource } @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { DocumentHandler docHandler = AcquisitionHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class); - if(obj != null){ + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), AcquisitionsCommon.class); + if (obj != null) { docHandler.setCommonPart((AcquisitionsCommon) obj); } } @@ -84,16 +85,16 @@ public class AcquisitionResource @POST public Response createAcquisition(MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); String csid = getRepositoryClient(ctx).create(ctx, handler); UriBuilder path = UriBuilder.fromResource(AcquisitionResource.class); path.path("" + csid); Response response = Response.created(path.build()).build(); return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in createAcquisition", e); } Response response = Response.status( @@ -106,10 +107,10 @@ public class AcquisitionResource @Path("{csid}") public MultipartOutput getAcquisition( @PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("getAcquisition with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("getAcquisition: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "get failed on Acquisition csid=" + csid).type( @@ -117,21 +118,21 @@ public class AcquisitionResource throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).get(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("getAcquisition", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed on Acquisition csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("getAcquisition", e); } Response response = Response.status( @@ -139,7 +140,7 @@ public class AcquisitionResource throw new WebApplicationException(response); } - if(result == null){ + if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed, the requested Acquisition CSID:" + csid + ": was not found.").type( "text/plain").build(); @@ -152,13 +153,13 @@ public class AcquisitionResource @Produces("application/xml") public AcquisitionsCommonList getAcquisitionList(@Context UriInfo ui) { AcquisitionsCommonList acquisitionObjectList = new AcquisitionsCommonList(); - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).getAll(ctx, handler); acquisitionObjectList = (AcquisitionsCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in getAcquisitionList", e); } Response response = Response.status( @@ -173,34 +174,34 @@ public class AcquisitionResource public MultipartOutput updateAcquisition( @PathParam("csid") String csid, MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateAcquisition with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("updateAcquisition: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "update failed on Acquisition csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateAcquisition with input: ", theUpdate); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(theUpdate); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateAcquisition", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Update failed on Acquisition csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); throw new WebApplicationException(response); @@ -212,34 +213,33 @@ public class AcquisitionResource @Path("{csid}") public Response deleteAcquisition(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("deleteAcquisition with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("deleteAcquisition: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "delete failed on Acquisition csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - try{ - ServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caught exception in deleteAcquisition", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Delete failed on Acquisition csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); throw new WebApplicationException(response); } } - } diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java index 1d5fd147f..efc67f77d 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java @@ -40,7 +40,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectHandlerFactory; import org.collectionspace.services.common.AbstractCollectionSpaceResource; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; @@ -65,13 +66,13 @@ public class CollectionObjectResource } @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { DocumentHandler docHandler = CollectionObjectHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class); - if(obj != null){ + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), CollectionobjectsCommon.class); + if (obj != null) { docHandler.setCommonPart((CollectionobjectsCommon) obj); } } @@ -80,16 +81,16 @@ public class CollectionObjectResource @POST public Response createCollectionObject(MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); String csid = getRepositoryClient(ctx).create(ctx, handler); UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class); path.path("" + csid); Response response = Response.created(path.build()).build(); return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in createCollectionObject", e); } Response response = Response.status( @@ -102,10 +103,10 @@ public class CollectionObjectResource @Path("{csid}") public MultipartOutput getCollectionObject( @PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("getCollectionObject with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("getCollectionObject: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "get failed on CollectionObject csid=" + csid).type( @@ -113,21 +114,21 @@ public class CollectionObjectResource throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).get(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("getCollectionObject", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed on CollectionObject csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("getCollectionObject", e); } Response response = Response.status( @@ -135,7 +136,7 @@ public class CollectionObjectResource throw new WebApplicationException(response); } - if(result == null){ + if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed, the requested CollectionObject CSID:" + csid + ": was not found.").type( "text/plain").build(); @@ -148,13 +149,13 @@ public class CollectionObjectResource @Produces("application/xml") public CollectionobjectsCommonList getCollectionObjectList(@Context UriInfo ui) { CollectionobjectsCommonList collectionObjectList = new CollectionobjectsCommonList(); - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).getAll(ctx, handler); collectionObjectList = (CollectionobjectsCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in getCollectionObjectList", e); } Response response = Response.status( @@ -169,10 +170,10 @@ public class CollectionObjectResource public MultipartOutput updateCollectionObject( @PathParam("csid") String csid, MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateCollectionObject with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("updateCollectionObject: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "update failed on CollectionObject csid=" + csid).type( @@ -180,20 +181,20 @@ public class CollectionObjectResource throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(theUpdate); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateCollectionObject", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Update failed on CollectionObject csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); throw new WebApplicationException(response); @@ -205,29 +206,29 @@ public class CollectionObjectResource @Path("{csid}") public Response deleteCollectionObject(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("deleteCollectionObject with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("deleteCollectionObject: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "delete failed on CollectionObject csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - try{ - ServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caught exception in deleteCollectionObject", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Delete failed on CollectionObject csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); throw new WebApplicationException(response); diff --git a/services/common/pom.xml b/services/common/pom.xml index 5aaefcd9a..9cd0d834c 100644 --- a/services/common/pom.xml +++ b/services/common/pom.xml @@ -16,7 +16,7 @@ com.sun.xml.bind jaxb-impl - + org.jvnet.jaxb2-commons property-listener-injector @@ -85,7 +85,14 @@ provided --> - + + javax.persistence + persistence-api + + + org.hibernate + hibernate-entitymanager + jboss diff --git a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResource.java b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResource.java index 55041d1ff..67b916489 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResource.java +++ b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResource.java @@ -23,13 +23,12 @@ */ package org.collectionspace.services.common; -import org.collectionspace.services.common.context.RemoteServiceContext; import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.context.RemoteServiceContextImpl; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.repository.RepositoryClient; import org.collectionspace.services.common.repository.RepositoryClientFactory; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.collectionspace.services.common.storage.StorageClient; +import org.collectionspace.services.common.storage.jpa.JpaStorageClient; public abstract class AbstractCollectionSpaceResource implements CollectionSpaceResource { @@ -37,6 +36,7 @@ public abstract class AbstractCollectionSpaceResource // Fields for default client factory and client private RepositoryClientFactory repositoryClientFactory; private RepositoryClient repositoryClient; + private StorageClient storageClient; public AbstractCollectionSpaceResource() { repositoryClientFactory = RepositoryClientFactory.getInstance(); @@ -45,10 +45,6 @@ public abstract class AbstractCollectionSpaceResource @Override abstract public String getServiceName(); - @Override - public RepositoryClientFactory getRepositoryClientFactory() { - return repositoryClientFactory; - } @Override synchronized public RepositoryClient getRepositoryClient(ServiceContext ctx) { @@ -60,17 +56,14 @@ public abstract class AbstractCollectionSpaceResource } @Override - public RemoteServiceContext createServiceContext(MultipartInput input) throws Exception { - return createServiceContext(input,getServiceName()); - } - - @Override - public RemoteServiceContext createServiceContext(MultipartInput input, String serviceName) throws Exception { - RemoteServiceContext ctx = new RemoteServiceContextImpl(serviceName); - ctx.setInput(input); - return ctx; + synchronized public StorageClient getStorageClient(ServiceContext ctx) { + if(storageClient != null) { + return storageClient; + } + storageClient = new JpaStorageClient(); + return storageClient; } @Override - abstract public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception ; + abstract public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception ; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java b/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java index 1bba85063..1e6dd4a9c 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java +++ b/services/common/src/main/java/org/collectionspace/services/common/CollectionSpaceResource.java @@ -21,15 +21,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.collectionspace.services.common; -import org.collectionspace.services.common.context.RemoteServiceContext; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.repository.RepositoryClient; -import org.collectionspace.services.common.repository.RepositoryClientFactory; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.collectionspace.services.common.storage.StorageClient; /** * CollectionSpaceResource is a resource interface implemented by every @@ -41,12 +38,7 @@ public interface CollectionSpaceResource { * getServiceName returns the name of the service */ public String getServiceName(); - - /** - * getRepositoryClientFactory - * @return - */ - public RepositoryClientFactory getRepositoryClientFactory(); + /** * getRepositoryClient @@ -55,23 +47,11 @@ public interface CollectionSpaceResource { public RepositoryClient getRepositoryClient(ServiceContext ctx); /** - * createServiceContext is a factory method to create a service context - * a service context is created on every service request call - * This form uses the serviceName as the default context - * @param input - * @return - */ - public RemoteServiceContext createServiceContext(MultipartInput input) throws Exception; - - /** - * createServiceContext is a factory method to create a service context - * a service context is created on every service request call - * @param input - * @param serviceName which service/repository context to use - * @return + * getStorageClient + * @param ctx service context */ - public RemoteServiceContext createServiceContext(MultipartInput input, String serviceName) throws Exception; - + public StorageClient getStorageClient(ServiceContext ctx); + /** * createDocumentHandler creates a document handler and populates it with given * service context. document handler should never be used @@ -80,5 +60,5 @@ public interface CollectionSpaceResource { * @param ctx * @return */ - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception ; + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java index 78b1b106a..48fd89583 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java @@ -181,13 +181,13 @@ public abstract class AbstractServiceContext public abstract IT getInput(); @Override - public abstract void setInput(IT input) throws Exception; + public abstract void setInput(IT input); @Override public abstract OT getOutput(); @Override - public abstract void setOutput(OT output) throws Exception; + public abstract void setOutput(OT output); @Override public String toString() { diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContext.java new file mode 100644 index 000000000..e17a5abdc --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContext.java @@ -0,0 +1,82 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.context; + +import java.io.IOException; +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; +import org.w3c.dom.Document; + +/** + * RemoteServiceContext is used to encapsulate the service context of a + * remotely invokable service + */ +public interface MultipartServiceContext + extends RemoteServiceContext { + + /** + * Get input parts as received over the wire from service consumer + * @return the input + */ + @Override + public MultipartInput getInput(); + + /** + * setInput is used to set request input before starting to + * process input data + * @param input + */ + @Override + public void setInput(MultipartInput input); + + /** + * Get output parts to send over the wire to service consumer + * @return the output + */ + @Override + public MultipartOutput getOutput(); + + /** + * Set output parts to send over the wire to service consumer + * @return the output + */ + @Override + public void setOutput(MultipartOutput output); + + /** + * getInputPart returns part for given label from input + * @param label + * @param clazz class of the object + * @return part + */ + public Object getInputPart(String label, Class clazz) throws IOException; + + /** + * addOutputPart adds given XML part with given label and content type to output + * @param label + * @param document + * @param contentType media type + */ + public void addOutputPart(String label, Document doc, String contentType) throws Exception; +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextFactory.java b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextFactory.java new file mode 100644 index 000000000..98277c254 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextFactory.java @@ -0,0 +1,59 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.context; + +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; + +/** + * + * MultipartRemoteServiceContextFactory creates a service context for + * a service processing multipart messages + * + */ +public class MultipartServiceContextFactory + implements ServiceContextFactory { + + final private static MultipartServiceContextFactory self = new MultipartServiceContextFactory(); + + private MultipartServiceContextFactory() { + } + + public static MultipartServiceContextFactory get() { + return self; + } + + /** + * createServiceContext is a factory method to create a service context + * a service context is created on every service request call + * @param input + * @param serviceName which service/repository context to use + * @return + */ + @Override + public ServiceContext createServiceContext(MultipartInput input, String serviceName) throws Exception { + MultipartServiceContext ctx = new MultipartServiceContextImpl(serviceName); + ctx.setInput(input); + return ctx; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextImpl.java b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextImpl.java new file mode 100644 index 000000000..a204f06cd --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/context/MultipartServiceContextImpl.java @@ -0,0 +1,110 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.context; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import javax.ws.rs.core.MediaType; +import org.collectionspace.services.common.document.DocumentUtils; +import org.jboss.resteasy.plugins.providers.multipart.InputPart; +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; +import org.jboss.resteasy.plugins.providers.multipart.OutputPart; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +/** + * RemoteServiceContextImpl + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class MultipartServiceContextImpl + extends RemoteServiceContextImpl + implements MultipartServiceContext { + + final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class); + + public MultipartServiceContextImpl(String serviceName) { + super(serviceName); + setOutput(new MultipartOutput()); + } + + + @Override + public Object getInputPart(String label, Class clazz) throws IOException { + Object obj = null; + if(getInput() != null){ + MultipartInput fdip = getInput(); + + for(InputPart part : fdip.getParts()){ + String partLabel = part.getHeaders().getFirst("label"); + if(label.equalsIgnoreCase(partLabel)){ + if(logger.isDebugEnabled()){ + logger.debug("received part label=" + partLabel + + "\npayload=" + part.getBodyAsString()); + } + obj = part.getBody(clazz, null); + break; + } + } + } + return obj; + } + + @Override + public void addOutputPart(String label, Document doc, String contentType) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try{ + DocumentUtils.writeDocument(doc, baos); + baos.close(); + OutputPart part = getOutput().addPart(new String(baos.toByteArray()), + MediaType.valueOf(contentType)); + part.getHeaders().add("label", label); + }finally{ + if(baos != null){ + try{ + baos.close(); + }catch(Exception e){ + } + } + } + } + + @Override + public ServiceContext getLocalContext(String localContextClassName) throws Exception { + ClassLoader cloader = Thread.currentThread().getContextClassLoader(); + Class ctxClass = cloader.loadClass(localContextClassName); + if(!ServiceContext.class.isAssignableFrom(ctxClass)) { + throw new IllegalArgumentException("getLocalContext requires " + + " implementation of " + ServiceContext.class.getName()); + } + + Constructor ctor = ctxClass.getConstructor(java.lang.String.class); + ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName()); + return ctx; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContext.java index 9ec071da8..01fed3c86 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContext.java @@ -23,24 +23,19 @@ */ package org.collectionspace.services.common.context; -import java.io.IOException; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; -import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; -import org.w3c.dom.Document; - /** * RemoteServiceContext is used to encapsulate the service context of a * remotely invokable service */ -public interface RemoteServiceContext - extends ServiceContext { +public interface RemoteServiceContext + extends ServiceContext { /** * Get input parts as received over the wire from service consumer * @return the input */ @Override - public MultipartInput getInput(); + public IT getInput(); /** * setInput is used to set request input before starting to @@ -49,37 +44,22 @@ public interface RemoteServiceContext * @exception IOException */ @Override - public void setInput(MultipartInput input) throws Exception; + public void setInput(IT input); /** * Get output parts to send over the wire to service consumer * @return the output */ @Override - public MultipartOutput getOutput(); + public OT getOutput(); /** * Set output parts to send over the wire to service consumer * @return the output */ @Override - public void setOutput(MultipartOutput output) throws Exception; - - /** - * getInputPart returns part for given label from input - * @param label - * @param clazz class of the object - * @return part - */ - public Object getInputPart(String label, Class clazz) throws IOException; + public void setOutput(OT output); - /** - * addOutputPart adds given XML part with given label and content type to output - * @param label - * @param document - * @param contentType media type - */ - public void addOutputPart(String label, Document doc, String contentType) throws Exception; /** * getLocalContext clones the remote context minus remote messaging data parts diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContextImpl.java b/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContextImpl.java index 4d6be9049..a5bdb2f4e 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContextImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/RemoteServiceContextImpl.java @@ -23,18 +23,9 @@ */ package org.collectionspace.services.common.context; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.lang.reflect.Constructor; -import javax.ws.rs.core.MediaType; -import org.collectionspace.services.common.document.DocumentUtils; -import org.jboss.resteasy.plugins.providers.multipart.InputPart; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; -import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; -import org.jboss.resteasy.plugins.providers.multipart.OutputPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; /** * RemoteServiceContextImpl @@ -42,79 +33,39 @@ import org.w3c.dom.Document; * $LastChangedRevision: $ * $LastChangedDate: $ */ -public class RemoteServiceContextImpl - extends AbstractServiceContext - implements RemoteServiceContext { +public class RemoteServiceContextImpl + extends AbstractServiceContext + implements RemoteServiceContext { final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class); //input stores original content as received over the wire - private MultipartInput input; - private MultipartOutput output; + private IT input; + private OT output; public RemoteServiceContextImpl(String serviceName) { super(serviceName); - output = new MultipartOutput(); } @Override - public MultipartInput getInput() { + public IT getInput() { return input; } @Override - public void setInput(MultipartInput input) throws IOException { + public void setInput(IT input){ this.input = input; } @Override - public MultipartOutput getOutput() { + public OT getOutput() { return output; } @Override - public void setOutput(MultipartOutput output) throws Exception { + public void setOutput(OT output){ this.output = output; } - @Override - public Object getInputPart(String label, Class clazz) throws IOException { - Object obj = null; - if(getInput() != null){ - MultipartInput fdip = getInput(); - - for(InputPart part : fdip.getParts()){ - String partLabel = part.getHeaders().getFirst("label"); - if(label.equalsIgnoreCase(partLabel)){ - if(logger.isDebugEnabled()){ - logger.debug("received part label=" + partLabel + - "\npayload=" + part.getBodyAsString()); - } - obj = part.getBody(clazz, null); - break; - } - } - } - return obj; - } - - @Override - public void addOutputPart(String label, Document doc, String contentType) throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try{ - DocumentUtils.writeDocument(doc, baos); - baos.close(); - OutputPart part = output.addPart(new String(baos.toByteArray()), - MediaType.valueOf(contentType)); - part.getHeaders().add("label", label); - }finally{ - if(baos != null){ - try{ - baos.close(); - }catch(Exception e){ - } - } - } - } @Override public ServiceContext getLocalContext(String localContextClassName) throws Exception { diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java index 5b209ef8a..c3723c06d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java @@ -134,9 +134,8 @@ public interface ServiceContext { * setInput is used to set request input before starting to * process input data * @param input - * @exception Exception */ - public void setInput(IT input) throws Exception; + public void setInput(IT input); /** * Get output parts to send over the wire to service consumer @@ -148,7 +147,7 @@ public interface ServiceContext { * setOutput set output * @param output */ - public void setOutput(OT output) throws Exception; + public void setOutput(OT output); /** * getPartsMetadata returns metadata for object parts used by the service diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContextFactory.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContextFactory.java new file mode 100644 index 000000000..ae76e6dd3 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContextFactory.java @@ -0,0 +1,43 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.context; + +/** + * + * ServiceContextFactory creates a service context + * + */ +public interface ServiceContextFactory { + + + /** + * createServiceContext is a factory method to create a service context + * a service context is created on every service request call + * @param input + * @param serviceName which service/repository context to use + * @return + */ + public ServiceContext createServiceContext(T input, String serviceName) throws Exception; + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandler.java index bb9690a1b..c3c4c9aa4 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandler.java @@ -145,14 +145,6 @@ public abstract class AbstractDocumentHandler //no specific action needed } - @Override - public abstract void extractAllParts(DocumentWrapper wrapDoc) - throws Exception; - - @Override - public abstract void fillAllParts(DocumentWrapper wrapDoc) - throws Exception; - @Override public abstract T extractCommonPart(DocumentWrapper wrapDoc) throws Exception; diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractMultipartDocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractMultipartDocumentHandler.java new file mode 100644 index 000000000..fef1b5df3 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractMultipartDocumentHandler.java @@ -0,0 +1,97 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.common.document; + +import java.util.HashMap; +import java.util.Map; + +import java.util.StringTokenizer; +import org.collectionspace.services.common.context.ServiceContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * AbstractMultipartDocumentHandler + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public abstract class AbstractMultipartDocumentHandler + extends AbstractDocumentHandler + implements MultipartDocumentHandler { + + private final Logger logger = LoggerFactory.getLogger(AbstractMultipartDocumentHandler.class); + + public AbstractMultipartDocumentHandler() { + } + + @Override + public abstract void handleCreate(DocumentWrapper wrapDoc) throws Exception; + + @Override + public abstract void handleUpdate(DocumentWrapper wrapDoc) throws Exception; + + @Override + public abstract void handleGet(DocumentWrapper wrapDoc) throws Exception; + + @Override + public abstract void handleGetAll(DocumentWrapper wrapDoc) throws Exception; + + @Override + public abstract void extractAllParts(DocumentWrapper wrapDoc) + throws Exception; + + @Override + public abstract void fillAllParts(DocumentWrapper wrapDoc) + throws Exception; + + @Override + public abstract T extractCommonPart(DocumentWrapper wrapDoc) + throws Exception; + + @Override + public abstract void fillCommonPart(T obj, DocumentWrapper wrapDoc) + throws Exception; + + @Override + public abstract TL extractCommonPartList(DocumentWrapper wrapDoc) + throws Exception; + + @Override + public abstract T getCommonPart(); + + @Override + public abstract void setCommonPart(T obj); + + @Override + public abstract TL getCommonPartList(); + + @Override + public abstract void setCommonPartList(TL obj); + + @Override + public abstract String getQProperty(String prop); + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java index 5fbda3564..804c7ed45 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java @@ -32,6 +32,11 @@ import org.collectionspace.services.common.context.ServiceContext; * repository client calls prepare on the handler * The repository client then calls handle on the handler * + * T - Entity Type (e.g. CollectionObjectsCommon) + * TL - Entity List Type (e.g. CollectionObjectsCommonList) + * WT - Wrapped Type (e.g. DocumentModel) + * WTL - Wrapped List Type (e.g. DocumentModelList) + * */ public interface DocumentHandler { @@ -120,25 +125,6 @@ public interface DocumentHandler { */ public void completeUpdate(DocumentWrapper wrapDoc) throws Exception; - /** - * extractAllParts extracts all parts of a CS object from given document. - * this is usually called AFTER the get operation is invoked on the repository - * Called in handle GET/GET_ALL actions. - * @param docWrap document - * @throws Exception - */ - public void extractAllParts(DocumentWrapper docWrap) throws Exception; - - /** - * fillAllParts sets parts of CS object into given document - * this is usually called BEFORE create/update operations are invoked on the - * repository. Called in handle CREATE/UPDATE actions. - * @param obj input object - * @param docWrap target document - * @throws Exception - */ - public void fillAllParts(DocumentWrapper docWrap) throws Exception; - /** * extractCommonPart extracts common part of a CS object from given document. * this is usually called AFTER the get operation is invoked on the repository. diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/MultipartDocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/MultipartDocumentHandler.java new file mode 100644 index 000000000..2a84a045f --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/document/MultipartDocumentHandler.java @@ -0,0 +1,51 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services.common.document; + +/** + * MultipartDocumentHandler is a DocumentHandler provides document processing + * methods for entities using schema extension that are mapped to multipart + * request and response. + * @author + * @param + * @param + * @param + * @param + */ +public interface MultipartDocumentHandler + extends DocumentHandler { + + /** + * extractAllParts extracts all parts of a CS object from given document. + * this is usually called AFTER the get operation is invoked on the repository + * Called in handle GET/GET_ALL actions. + * @param docWrap document + * @throws Exception + */ + public void extractAllParts(DocumentWrapper docWrap) throws Exception; + + /** + * fillAllParts sets parts of CS object into given document + * this is usually called BEFORE create/update operations are invoked on the + * repository. Called in handle CREATE/UPDATE actions. + * @param obj input object + * @param docWrap target document + * @throws Exception + */ + public void fillAllParts(DocumentWrapper docWrap) throws Exception; +} \ No newline at end of file diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java new file mode 100644 index 000000000..32a326728 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java @@ -0,0 +1,382 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services.common.storage.jpa; + +import java.lang.reflect.Method; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.Query; +import org.collectionspace.services.common.context.ServiceContext; +import org.collectionspace.services.common.document.BadRequestException; +import org.collectionspace.services.common.document.DocumentException; +import org.collectionspace.services.common.document.DocumentFilter; +import org.collectionspace.services.common.document.DocumentHandler; +import org.collectionspace.services.common.document.DocumentNotFoundException; +import org.collectionspace.services.common.document.DocumentHandler.Action; +import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.document.DocumentWrapperImpl; +import org.collectionspace.services.common.storage.StorageClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * JpaStorageClient is used to perform CRUD operations on documents in Nuxeo + * repository using Remote Java APIs. It uses @see DocumentHandler as IOHandler + * with the client. + * + * $LastChangedRevision: $ $LastChangedDate: $ + */ +public class JpaStorageClient implements StorageClient { + + private final Logger logger = LoggerFactory.getLogger(JpaStorageClient.class); + + public JpaStorageClient() { + } + + @Override + public String create(ServiceContext ctx, + DocumentHandler handler) throws BadRequestException, + DocumentException { + + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + if (handler == null) { + throw new IllegalArgumentException( + "JpaStorageClient.create: handler is missing"); + } + EntityManagerFactory emf = null; + EntityManager em = null; + try { + handler.prepare(Action.CREATE); + Object po = handler.getCommonPart(); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(po); + handler.handle(Action.CREATE, wrapDoc); + emf = getEntityManagerFactory(docType); + em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(po); + em.getTransaction().commit(); + handler.complete(Action.CREATE, wrapDoc); + return getCsid(po); + } catch (Exception e) { + if (em != null) { + em.getTransaction().rollback(); + } + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (em != null) { + releaseEntityManagerFactory(emf); + } + } + + } + + @Override + public void get(ServiceContext ctx, String id, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + if (handler == null) { + throw new IllegalArgumentException( + "JpaStorageClient.get: handler is missing"); + } + DocumentFilter docFilter = handler.getDocumentFilter(); + if (docFilter == null) { + throw new IllegalArgumentException( + "JpaStorageClient.get: handler has no Filter specified"); + } + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + EntityManagerFactory emf = null; + EntityManager em = null; + try { + handler.prepare(Action.GET); + StringBuilder queryStr = new StringBuilder("SELECT * FROM "); + queryStr.append(docType); + queryStr.append(" WHERE csid = :csid"); + //TODO: add tenant id + String where = docFilter.getWhereClause(); + if ((null != where) && (where.length() > 0)) { + queryStr.append(" AND " + where); + } + emf = getEntityManagerFactory(docType); + em = emf.createEntityManager(); + Query q = em.createQuery(queryStr.toString()); + q.setParameter("csid", id); + //TODO: add tenant id + + //TODO: get page + if ((docFilter.getOffset() > 0) || (docFilter.getPageSize() > 0)) { + } else { + } + //require transaction for get? + em.getTransaction().begin(); + Object o = q.getSingleResult(); + em.getTransaction().commit(); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(o); + handler.handle(Action.GET, wrapDoc); + handler.complete(Action.GET, wrapDoc); + } catch (IllegalArgumentException iae) { + throw iae; + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (emf != null) { + releaseEntityManagerFactory(emf); + } + } + } + + @Override + public void getAll(ServiceContext ctx, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + throw new UnsupportedOperationException("use getFiltered instead"); + } + + /** + * getFiltered get all documents for an entity service from the Document repository, + * given filter parameters specified by the handler. + * @param ctx service context under which this method is invoked + * @param handler should be used by the caller to provide and transform the document + * @throws DocumentNotFoundException if workspace not found + * @throws DocumentException + */ + public void getFiltered(ServiceContext ctx, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + if (handler == null) { + throw new IllegalArgumentException( + "JpaStorageClient.getFiltered: handler is missing"); + } + DocumentFilter docFilter = handler.getDocumentFilter(); + if (docFilter == null) { + throw new IllegalArgumentException( + "JpaStorageClient.getFiltered: handler has no Filter specified"); + } + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + + EntityManagerFactory emf = null; + EntityManager em = null; + try { + handler.prepare(Action.GET_ALL); + + StringBuilder queryStr = new StringBuilder("SELECT * FROM "); + queryStr.append(docType); + //TODO: add tenant id + String where = docFilter.getWhereClause(); + if ((null != where) && (where.length() > 0)) { + queryStr.append(" AND " + where); + } + emf = getEntityManagerFactory(docType); + em = emf.createEntityManager(); + Query q = em.createQuery(queryStr.toString()); + //TODO: add tenant id + //TODO: get page + if ((docFilter.getOffset() > 0) || (docFilter.getPageSize() > 0)) { + } else { + } + //FIXME is transaction required for get? + em.getTransaction().begin(); + List list = q.getResultList(); + em.getTransaction().commit(); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(list); + handler.handle(Action.GET_ALL, wrapDoc); + handler.complete(Action.GET_ALL, wrapDoc); + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (emf != null) { + releaseEntityManagerFactory(emf); + } + } + } + + @Override + public void update(ServiceContext ctx, String id, DocumentHandler handler) + throws BadRequestException, DocumentNotFoundException, + DocumentException { + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + if (handler == null) { + throw new IllegalArgumentException( + "JpaStorageClient.update: handler is missing"); + } + EntityManagerFactory emf = null; + EntityManager em = null; + try { + handler.prepare(Action.UPDATE); + Object po = handler.getCommonPart(); + setCsid(po, id); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(po); + handler.handle(Action.UPDATE, wrapDoc); + emf = getEntityManagerFactory(docType); + em = emf.createEntityManager(); + em.getTransaction().begin(); + em.merge(po); + em.getTransaction().commit(); + handler.complete(Action.UPDATE, wrapDoc); + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + } + } + + /** + * delete a document from the Nuxeo repository + * @param ctx service context under which this method is invoked + * @param id + * of the document + * @throws DocumentException + */ + @Override + public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException, + DocumentException { + + if (logger.isDebugEnabled()) { + logger.debug("deleting document with id=" + id); + } + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + EntityManagerFactory emf = null; + EntityManager em = null; + try { + StringBuilder deleteStr = new StringBuilder("DELETE FROM "); + deleteStr.append(docType); + deleteStr.append(" WHERE csid = :csid"); + //TODO: add tenant id + + emf = getEntityManagerFactory(docType); + em = emf.createEntityManager(); + Query q = em.createQuery(deleteStr.toString()); + q.setParameter("csid", id); + //TODO: add tenant id + em.getTransaction().begin(); + int rcount = q.executeUpdate(); + em.getTransaction().commit(); + if (rcount != 1) { + throw new DocumentException("failed to delete " + docType + + " csid=" + id); + } + } catch (DocumentException de) { + if (em != null) { + em.getTransaction().rollback(); + } + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + if (em != null) { + em.getTransaction().rollback(); + } + throw new DocumentException(e); + } finally { + if (emf != null) { + releaseEntityManagerFactory(emf); + } + } + } + + private EntityManagerFactory getEntityManagerFactory(String persistenceUnit) { + return Persistence.createEntityManagerFactory(persistenceUnit); + + } + + private void releaseEntityManagerFactory(EntityManagerFactory emf) { + if (emf != null) { + emf.close(); + } + } + + private String getCsid(Object o) throws Exception { + Class c = o.getClass(); + Method m = c.getMethod("getCsid"); + if (m == null) { + String msg = "Could not find csid in object of class=" + o.getClass().getCanonicalName(); + logger.error(msg); + throw new IllegalArgumentException(msg); + } + Object r = m.invoke(o); + if (logger.isDebugEnabled()) { + logger.debug("getCsid returned csid=" + r.toString() + + " for " + c.getName()); + } + return (String) r; + } + + private void setCsid(Object o, String csid) throws Exception { + //verify csid + String id = getCsid(o); + if (id != null) { + if (!id.equals(csid)) { + String msg = "Csids do not match!"; + logger.error(msg); + throw new BadRequestException(msg); + } else { + //no need to set + return; + } + } + //set csid + Class c = o.getClass(); + Method m = c.getMethod("setCsid"); + if (m == null) { + String msg = "Could not find csid in object of class=" + o.getClass().getCanonicalName(); + logger.error(msg); + throw new IllegalArgumentException(msg); + } + Object r = m.invoke(o, csid); + if (logger.isDebugEnabled()) { + logger.debug("setCsid returned csid=" + r.toString() + + " for " + c.getName()); + } + } +} \ No newline at end of file diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java index d929813aa..ff1d0c603 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocumentModelHandler.java @@ -23,8 +23,8 @@ */ package org.collectionspace.services.nuxeo.client.java; +import org.collectionspace.services.common.document.AbstractMultipartDocumentHandler; import org.collectionspace.services.common.document.DocumentWrapper; -import org.collectionspace.services.common.document.AbstractDocumentHandler; import org.collectionspace.services.nuxeo.client.*; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; @@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory; * $LastChangedDate: $ */ public abstract class DocumentModelHandler - extends AbstractDocumentHandler { + extends AbstractMultipartDocumentHandler { private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class); private RepositoryInstance repositorySession; diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandler.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandler.java index af09b2427..31e42a2a3 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandler.java @@ -30,7 +30,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import javax.ws.rs.core.MediaType; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.BadRequestException; import org.collectionspace.services.common.document.DocumentUtils; @@ -56,11 +56,11 @@ public abstract class RemoteDocumentModelHandler @Override public void setServiceContext(ServiceContext ctx) { - if(ctx instanceof RemoteServiceContext){ + if(ctx instanceof MultipartServiceContext){ super.setServiceContext(ctx); }else{ throw new IllegalArgumentException("setServiceContext requires instance of " + - RemoteServiceContext.class.getName()); + MultipartServiceContext.class.getName()); } } @@ -72,7 +72,7 @@ public abstract class RemoteDocumentModelHandler DocumentModel docModel = wrapDoc.getWrappedObject(); //return at least those document part(s) that were received Map partsMetaMap = getServiceContext().getPartsMetadata(); - RemoteServiceContext ctx = (RemoteServiceContext) getServiceContext(); + MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext(); List inputParts = ctx.getInput().getParts(); for(InputPart part : inputParts){ String partLabel = part.getHeaders().getFirst("label"); @@ -103,7 +103,7 @@ public abstract class RemoteDocumentModelHandler //Nuxeo APIs lack to support stream/byte[] input, get/setting properties is //not an ideal way of populating objects. DocumentModel docModel = wrapDoc.getWrappedObject(); - RemoteServiceContext ctx = (RemoteServiceContext) getServiceContext(); + MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext(); MultipartInput input = ctx.getInput(); if(input.getParts().isEmpty()){ String msg = "No payload found!"; @@ -181,7 +181,7 @@ public abstract class RemoteDocumentModelHandler if(logger.isDebugEnabled()){ DocumentUtils.writeDocument(doc, System.out); } - RemoteServiceContext ctx = (RemoteServiceContext) getServiceContext(); + MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext(); ctx.addOutputPart(schema, doc, partMeta.getContent().getContentType()); } //TODO: handle other media types } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java index 9ccb51be1..0b23a002b 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java @@ -75,11 +75,11 @@ public class RepositoryJavaClient implements RepositoryClient { if (ctx.getDocumentType() == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.create: docType is missing"); + "RepositoryJavaClient.create: docType is missing"); } if (handler == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.create: handler is missing"); + "RepositoryJavaClient.create: handler is missing"); } String nuxeoWspaceId = ctx.getRepositoryWorkspaceId(); if (nuxeoWspaceId == null) { @@ -136,7 +136,7 @@ public class RepositoryJavaClient implements RepositoryClient { if (handler == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.get: handler is missing"); + "RepositoryJavaClient.get: handler is missing"); } RepositoryInstance repoSession = null; @@ -188,7 +188,7 @@ public class RepositoryJavaClient implements RepositoryClient { throws DocumentNotFoundException, DocumentException { if (handler == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.getAll: handler is missing"); + "RepositoryJavaClient.getAll: handler is missing"); } String nuxeoWspaceId = ctx.getRepositoryWorkspaceId(); if (nuxeoWspaceId == null) { @@ -235,12 +235,12 @@ public class RepositoryJavaClient implements RepositoryClient { throws DocumentNotFoundException, DocumentException { if (handler == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.getFiltered: handler is missing"); + "RepositoryJavaClient.getFiltered: handler is missing"); } DocumentFilter docFilter = handler.getDocumentFilter(); if (docFilter == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.getFiltered: handler has no Filter specified"); + "RepositoryJavaClient.getFiltered: handler has no Filter specified"); } String docType = ctx.getDocumentType(); if (docType == null) { @@ -306,13 +306,9 @@ public class RepositoryJavaClient implements RepositoryClient { public void update(ServiceContext ctx, String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, DocumentException { - if (id == null) { - throw new BadRequestException( - "RemoteRepositoryClient.update: id is missing"); - } if (handler == null) { throw new IllegalArgumentException( - "RemoteRepositoryClient.update: handler is missing"); + "RepositoryJavaClient.update: handler is missing"); } RepositoryInstance repoSession = null; try { diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java index fd936e728..e1a965c41 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java @@ -43,7 +43,8 @@ import org.collectionspace.services.intake.IntakesCommonList.*; import org.collectionspace.services.intake.nuxeo.IntakeHandlerFactory; import org.collectionspace.services.common.ClientType; import org.collectionspace.services.common.ServiceMain; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; @@ -73,13 +74,13 @@ public class IntakeResource extends AbstractCollectionSpaceResource { } @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { DocumentHandler docHandler = IntakeHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), IntakesCommon.class); - if(obj != null){ + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), IntakesCommon.class); + if (obj != null) { docHandler.setCommonPart((IntakesCommon) obj); } } @@ -88,8 +89,8 @@ public class IntakeResource extends AbstractCollectionSpaceResource { @POST public Response createIntake(MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); String csid = getRepositoryClient(ctx).create(ctx, handler); //intakeObject.setCsid(csid); @@ -97,8 +98,8 @@ public class IntakeResource extends AbstractCollectionSpaceResource { path.path("" + csid); Response response = Response.created(path.build()).build(); return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in createIntake", e); } Response response = Response.status( @@ -111,10 +112,10 @@ public class IntakeResource extends AbstractCollectionSpaceResource { @Path("{csid}") public MultipartOutput getIntake( @PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("getIntake with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("getIntake: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "get failed on Intake csid=" + csid).type( @@ -122,28 +123,28 @@ public class IntakeResource extends AbstractCollectionSpaceResource { throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).get(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("getIntake", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed on Intake csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("getIntake", e); } Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); throw new WebApplicationException(response); } - if(result == null){ + if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed, the requested Intake CSID:" + csid + ": was not found.").type( "text/plain").build(); @@ -156,13 +157,13 @@ public class IntakeResource extends AbstractCollectionSpaceResource { @Produces("application/xml") public IntakesCommonList getIntakeList(@Context UriInfo ui) { IntakesCommonList intakeObjectList = new IntakesCommonList(); - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).getAll(ctx, handler); intakeObjectList = (IntakesCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in getIntakeList", e); } Response response = Response.status( @@ -177,10 +178,10 @@ public class IntakeResource extends AbstractCollectionSpaceResource { public MultipartOutput updateIntake( @PathParam("csid") String csid, MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateIntake with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("updateIntake: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "update failed on Intake csid=" + csid).type( @@ -188,20 +189,20 @@ public class IntakeResource extends AbstractCollectionSpaceResource { throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(theUpdate); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateIntake", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Update failed on Intake csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); throw new WebApplicationException(response); @@ -213,29 +214,29 @@ public class IntakeResource extends AbstractCollectionSpaceResource { @Path("{csid}") public Response deleteIntake(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("deleteIntake with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("deleteIntake: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "delete failed on Intake csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - try{ - ServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caught exception in deleteIntake", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Delete failed on Intake csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); throw new WebApplicationException(response); diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java index 35ce07a8f..552a34210 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java @@ -44,7 +44,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.common.AbstractCollectionSpaceResource; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.relation.IRelationsManager; import org.collectionspace.services.common.document.DocumentNotFoundException; @@ -70,13 +71,13 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { } @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { DocumentHandler docHandler = RelationHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class); - if(obj != null){ + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class); + if (obj != null) { docHandler.setCommonPart((RelationsCommon) obj); } } @@ -86,16 +87,16 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { @POST public Response createRelation(MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); String csid = getRepositoryClient(ctx).create(ctx, handler); UriBuilder path = UriBuilder.fromResource(NewRelationResource.class); path.path("" + csid); Response response = Response.created(path.build()).build(); return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in createRelation", e); } Response response = Response.status( @@ -108,30 +109,30 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { @GET @Path("{csid}") public MultipartOutput getRelation(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("getRelation with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("getRelation: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).get(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("getRelation", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("getRelation", e); } Response response = Response.status( @@ -139,7 +140,7 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { throw new WebApplicationException(response); } - if(result == null){ + if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build(); throw new WebApplicationException(response); @@ -243,29 +244,29 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { @Path("{csid}") public MultipartOutput updateRelation(@PathParam("csid") String csid, MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateRelation with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("updateRelation: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(theUpdate); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateRelation", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity( "Update failed").type("text/plain").build(); @@ -278,27 +279,27 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { @Path("{csid}") public Response deleteRelation(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("deleteRelation with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("deleteRelation: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - try{ - ServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caught exception in deleteRelation", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity( "Delete failed").type("text/plain").build(); @@ -322,8 +323,8 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { String objectCsid) throws WebApplicationException { RelationsCommonList relationList = new RelationsCommonList(); - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); Map propsFromPath = handler.getProperties(); propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid); @@ -331,8 +332,8 @@ public class NewRelationResource extends AbstractCollectionSpaceResource { propsFromPath.put(IRelationsManager.OBJECT, objectCsid); getRepositoryClient(ctx).getAll(ctx, handler); relationList = (RelationsCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in getRelationList", e); } Response response = Response.status( diff --git a/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java b/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java index 66220a8e7..f627bb892 100644 --- a/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java +++ b/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java @@ -23,9 +23,6 @@ */ package org.collectionspace.services.vocabulary; -import java.util.List; -import java.util.Map; - import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -34,7 +31,6 @@ import javax.ws.rs.PUT; 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; @@ -45,7 +41,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.common.AbstractCollectionSpaceResource; import org.collectionspace.services.common.ClientType; import org.collectionspace.services.common.ServiceMain; -import org.collectionspace.services.common.context.RemoteServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextFactory; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentHandler; @@ -85,37 +82,36 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { /* public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception { - RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName()); - ctx.setInput(input); - return ctx; + RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName()); + ctx.setInput(input); + return ctx; } - */ - + */ @Override - public DocumentHandler createDocumentHandler(RemoteServiceContext ctx) throws Exception { + public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { DocumentHandler docHandler = VocabularyHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class); - if(obj != null){ + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), VocabulariesCommon.class); + if (obj != null) { docHandler.setCommonPart((VocabulariesCommon) obj); } } return docHandler; } - + private DocumentHandler createItemDocumentHandler( - RemoteServiceContext ctx, - String inVocabulary) throws Exception { + ServiceContext ctx, + String inVocabulary) throws Exception { DocumentHandler docHandler = VocabularyItemHandlerFactory.getInstance().getHandler( ctx.getRepositoryClientType().toString()); docHandler.setServiceContext(ctx); - ((VocabularyItemDocumentModelHandler)docHandler).setInVocabulary(inVocabulary); - if(ctx.getInput() != null){ - Object obj = ctx.getInputPart(ctx.getCommonPartLabel(getItemServiceName()), - VocabularyitemsCommon.class); - if(obj != null){ + ((VocabularyItemDocumentModelHandler) docHandler).setInVocabulary(inVocabulary); + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()), + VocabularyitemsCommon.class); + if (obj != null) { docHandler.setCommonPart((VocabularyitemsCommon) obj); } } @@ -124,8 +120,8 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { @POST public Response createVocabulary(MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); String csid = getRepositoryClient(ctx).create(ctx, handler); //vocabularyObject.setCsid(csid); @@ -133,8 +129,8 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { path.path("" + csid); Response response = Response.created(path.build()).build(); return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in createVocabulary", e); } Response response = Response.status( @@ -146,40 +142,40 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { @GET @Path("{csid}") public MultipartOutput getVocabulary(@PathParam("csid") String csid) { - String idValue = null; - if(csid == null){ + String idValue = null; + if (csid == null) { logger.error("getVocabulary: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "get failed on Vocabulary csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("getVocabulary with path(id)=" + csid); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(null); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); - getRepositoryClient(ctx).get(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + getRepositoryClient(ctx).get(ctx, csid, handler); + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("getVocabulary", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed on Vocabulary csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("getVocabulary", e); } Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); throw new WebApplicationException(response); } - if(result == null){ + if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( "Get failed, the requested Vocabulary CSID:" + csid + ": was not found.").type( "text/plain").build(); @@ -192,9 +188,9 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { @Produces("application/xml") public VocabulariesCommonList getVocabularyList(@Context UriInfo ui) { VocabulariesCommonList vocabularyObjectList = new VocabulariesCommonList(); - try{ - RemoteServiceContext ctx = createServiceContext(null); - MultivaluedMap queryParams = ui.getQueryParameters(); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); + MultivaluedMap queryParams = ui.getQueryParameters(); DocumentHandler handler = createDocumentHandler(ctx); DocumentFilter myFilter = DocumentFilter.CreatePaginatedDocumentFilter(queryParams); @@ -205,8 +201,8 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { handler.setDocumentFilter(myFilter); getRepositoryClient(ctx).getFiltered(ctx, handler); vocabularyObjectList = (VocabulariesCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ + } catch (Exception e) { + if (logger.isDebugEnabled()) { logger.debug("Caught exception in getVocabularyList", e); } Response response = Response.status( @@ -221,10 +217,10 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { public MultipartOutput updateVocabulary( @PathParam("csid") String csid, MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("updateVocabulary with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("updateVocabulary: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "update failed on Vocabulary csid=" + csid).type( @@ -232,20 +228,20 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { throw new WebApplicationException(response); } MultipartOutput result = null; - try{ - RemoteServiceContext ctx = createServiceContext(theUpdate); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + result = (MultipartOutput) ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateVocabulary", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Update failed on Vocabulary csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); throw new WebApplicationException(response); @@ -257,29 +253,29 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { @Path("{csid}") public Response deleteVocabulary(@PathParam("csid") String csid) { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("deleteVocabulary with csid=" + csid); } - if(csid == null || "".equals(csid)){ + if (csid == null || "".equals(csid)) { logger.error("deleteVocabulary: missing csid!"); Response response = Response.status(Response.Status.BAD_REQUEST).entity( "delete failed on Vocabulary csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); } - try{ - ServiceContext ctx = createServiceContext(null); + try { +ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { logger.debug("caught exception in deleteVocabulary", dnfe); } Response response = Response.status(Response.Status.NOT_FOUND).entity( "Delete failed on Vocabulary csid=" + csid).type( "text/plain").build(); throw new WebApplicationException(response); - }catch(Exception e){ + } catch (Exception e) { Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); throw new WebApplicationException(response); @@ -290,197 +286,195 @@ public class VocabularyResource extends AbstractCollectionSpaceResource { /************************************************************************* * VocabularyItem parts - this is a sub-resource of Vocabulary *************************************************************************/ + @POST + @Path("{csid}/items") + public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) { + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName()); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); + String itemcsid = getRepositoryClient(ctx).create(ctx, handler); + UriBuilder path = UriBuilder.fromResource(VocabularyResource.class); + path.path(parentcsid + "/items/" + itemcsid); + Response response = Response.created(path.build()).build(); + return response; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in createVocabularyItem", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + } - @POST - @Path("{csid}/items") - public Response createVocabularyItem(@PathParam("csid") String parentcsid, MultipartInput input) { - try{ - RemoteServiceContext ctx = createServiceContext(input, getItemServiceName()); - DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); - String itemcsid = getRepositoryClient(ctx).create(ctx, handler); - UriBuilder path = UriBuilder.fromResource(VocabularyResource.class); - path.path(parentcsid + "/items/" + itemcsid); - Response response = Response.created(path.build()).build(); - return response; - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception in createVocabularyItem", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - } - - @GET - @Path("{csid}/items/{itemcsid}") - public MultipartOutput getVocabularyItem( - @PathParam("csid") String parentcsid, - @PathParam("itemcsid") String itemcsid) { - if(logger.isDebugEnabled()){ - logger.debug("getVocabularyItem with parentcsid=" - + parentcsid + " and itemcsid=" + itemcsid); - } - if(parentcsid == null || "".equals(parentcsid)){ - logger.error("getVocabularyItem: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "get failed on VocabularyItem csid=" + parentcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - if(itemcsid == null || "".equals(itemcsid)){ - logger.error("getVocabularyItem: missing itemcsid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "get failed on VocabularyItem itemcsid=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - MultipartOutput result = null; - try{ - // Note that we have to create the service context for the Items, not the main service - RemoteServiceContext ctx = createServiceContext(null, getItemServiceName()); - DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); - getRepositoryClient(ctx).get(ctx, itemcsid, handler); - // TODO should we assert that the item is in the passed vocab? - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ - logger.debug("getVocabularyItem", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Get failed on VocabularyItem csid=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("getVocabularyItem", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - if(result == null){ - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type( - "text/plain").build(); - throw new WebApplicationException(response); - } - return result; - } + @GET + @Path("{csid}/items/{itemcsid}") + public MultipartOutput getVocabularyItem( + @PathParam("csid") String parentcsid, + @PathParam("itemcsid") String itemcsid) { + if (logger.isDebugEnabled()) { + logger.debug("getVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid); + } + if (parentcsid == null || "".equals(parentcsid)) { + logger.error("getVocabularyItem: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "get failed on VocabularyItem csid=" + parentcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + if (itemcsid == null || "".equals(itemcsid)) { + logger.error("getVocabularyItem: missing itemcsid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "get failed on VocabularyItem itemcsid=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + MultipartOutput result = null; + try { + // Note that we have to create the service context for the Items, not the main service + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName()); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); + getRepositoryClient(ctx).get(ctx, itemcsid, handler); + // TODO should we assert that the item is in the passed vocab? + result = (MultipartOutput)ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("getVocabularyItem", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Get failed on VocabularyItem csid=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("getVocabularyItem", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + if (result == null) { + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Get failed, the requested VocabularyItem CSID:" + itemcsid + ": was not found.").type( + "text/plain").build(); + throw new WebApplicationException(response); + } + return result; + } - @GET - @Path("{csid}/items") - @Produces("application/xml") - public VocabularyitemsCommonList getVocabularyItemList( - @PathParam("csid") String parentcsid, - @Context UriInfo ui) { - VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList(); - try{ - // Note that docType defaults to the ServiceName, so we're fine with that. - RemoteServiceContext ctx = createServiceContext(null, getItemServiceName()); - DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); - DocumentFilter myFilter = new DocumentFilter( - "vocabularyitems_common:inVocabulary='"+parentcsid+"'", 0, 0); - handler.setDocumentFilter(myFilter); - getRepositoryClient(ctx).getFiltered(ctx, handler); - vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList(); - }catch(Exception e){ - if(logger.isDebugEnabled()){ - logger.debug("Caught exception in getVocabularyItemList", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - return vocabularyItemObjectList; - } + @GET + @Path("{csid}/items") + @Produces("application/xml") + public VocabularyitemsCommonList getVocabularyItemList( + @PathParam("csid") String parentcsid, + @Context UriInfo ui) { + VocabularyitemsCommonList vocabularyItemObjectList = new VocabularyitemsCommonList(); + try { + // Note that docType defaults to the ServiceName, so we're fine with that. + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName()); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); + DocumentFilter myFilter = new DocumentFilter( + "vocabularyitems_common:inVocabulary='" + parentcsid + "'", 0, 0); + handler.setDocumentFilter(myFilter); + getRepositoryClient(ctx).getFiltered(ctx, handler); + vocabularyItemObjectList = (VocabularyitemsCommonList) handler.getCommonPartList(); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getVocabularyItemList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return vocabularyItemObjectList; + } - @PUT - @Path("{csid}/items/{itemcsid}") - public MultipartOutput updateVocabularyItem( - @PathParam("csid") String parentcsid, - @PathParam("itemcsid") String itemcsid, - MultipartInput theUpdate) { - if(logger.isDebugEnabled()){ - logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid); - } - if(parentcsid == null || "".equals(parentcsid)){ - logger.error("updateVocabularyItem: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "update failed on VocabularyItem parentcsid=" + parentcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - if(itemcsid == null || "".equals(itemcsid)){ - logger.error("updateVocabularyItem: missing itemcsid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "update failed on VocabularyItem=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - MultipartOutput result = null; - try{ - // Note that we have to create the service context for the Items, not the main service - RemoteServiceContext ctx = createServiceContext(theUpdate, getItemServiceName()); - DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); - getRepositoryClient(ctx).update(ctx, itemcsid, handler); - result = ctx.getOutput(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ - logger.debug("caugth exception in updateVocabularyItem", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Update failed on VocabularyItem csid=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - }catch(Exception e){ - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - return result; - } + @PUT + @Path("{csid}/items/{itemcsid}") + public MultipartOutput updateVocabularyItem( + @PathParam("csid") String parentcsid, + @PathParam("itemcsid") String itemcsid, + MultipartInput theUpdate) { + if (logger.isDebugEnabled()) { + logger.debug("updateVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid); + } + if (parentcsid == null || "".equals(parentcsid)) { + logger.error("updateVocabularyItem: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "update failed on VocabularyItem parentcsid=" + parentcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + if (itemcsid == null || "".equals(itemcsid)) { + logger.error("updateVocabularyItem: missing itemcsid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "update failed on VocabularyItem=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + MultipartOutput result = null; + try { + // Note that we have to create the service context for the Items, not the main service + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName()); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); + getRepositoryClient(ctx).update(ctx, itemcsid, handler); + result = (MultipartOutput)ctx.getOutput(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("caugth exception in updateVocabularyItem", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Update failed on VocabularyItem csid=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return result; + } - @DELETE - @Path("{csid}/items/{itemcsid}") - public Response deleteVocabularyItem( - @PathParam("csid") String parentcsid, - @PathParam("itemcsid") String itemcsid) { - if(logger.isDebugEnabled()){ - logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid); - } - if(parentcsid == null || "".equals(parentcsid)){ - logger.error("deleteVocabularyItem: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "delete failed on VocabularyItem parentcsid=" + parentcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - if(itemcsid == null || "".equals(itemcsid)){ - logger.error("deleteVocabularyItem: missing itemcsid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity( - "delete failed on VocabularyItem=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - try{ - // Note that we have to create the service context for the Items, not the main service - RemoteServiceContext ctx = createServiceContext(null, getItemServiceName()); - getRepositoryClient(ctx).delete(ctx, itemcsid); - return Response.status(HttpResponseCodes.SC_OK).build(); - }catch(DocumentNotFoundException dnfe){ - if(logger.isDebugEnabled()){ - logger.debug("caught exception in deleteVocabulary", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Delete failed on VocabularyItem itemcsid=" + itemcsid).type( - "text/plain").build(); - throw new WebApplicationException(response); - }catch(Exception e){ - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); - throw new WebApplicationException(response); - } + @DELETE + @Path("{csid}/items/{itemcsid}") + public Response deleteVocabularyItem( + @PathParam("csid") String parentcsid, + @PathParam("itemcsid") String itemcsid) { + if (logger.isDebugEnabled()) { + logger.debug("deleteVocabularyItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid); + } + if (parentcsid == null || "".equals(parentcsid)) { + logger.error("deleteVocabularyItem: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "delete failed on VocabularyItem parentcsid=" + parentcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + if (itemcsid == null || "".equals(itemcsid)) { + logger.error("deleteVocabularyItem: missing itemcsid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "delete failed on VocabularyItem=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + try { + // Note that we have to create the service context for the Items, not the main service + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName()); + getRepositoryClient(ctx).delete(ctx, itemcsid); + return Response.status(HttpResponseCodes.SC_OK).build(); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("caught exception in deleteVocabulary", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Delete failed on VocabularyItem itemcsid=" + itemcsid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); + throw new WebApplicationException(response); + } - } + } }