From ca7db43764bd0b775e8e5831605134bf55af41d2 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Mon, 2 May 2011 22:34:46 +0000 Subject: [PATCH] CSPACE-1598: Reformatted indentation to better understand program flow when investigating this issue. --- ...tMultiPartCollectionSpaceResourceImpl.java | 145 ++++++++------- .../services/common/ResourceBase.java | 167 ++++++++---------- 2 files changed, 145 insertions(+), 167 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/AbstractMultiPartCollectionSpaceResourceImpl.java b/services/common/src/main/java/org/collectionspace/services/common/AbstractMultiPartCollectionSpaceResourceImpl.java index efa1c75ed..cc4976b9a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/AbstractMultiPartCollectionSpaceResourceImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/AbstractMultiPartCollectionSpaceResourceImpl.java @@ -57,58 +57,52 @@ import org.slf4j.LoggerFactory; /** * The Class AbstractMultiPartCollectionSpaceResourceImpl. */ -public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends - AbstractCollectionSpaceResourceImpl { +public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends AbstractCollectionSpaceResourceImpl { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public ServiceContextFactory getServiceContextFactory() { - return MultipartServiceContextFactory.get(); + return MultipartServiceContextFactory.get(); } protected WebApplicationException bigReThrow(Exception e, String serviceMsg) - throws WebApplicationException { + throws WebApplicationException { return bigReThrow(e, serviceMsg, ""); } - protected WebApplicationException bigReThrow(Exception e, - String serviceMsg, String csid) throws WebApplicationException { - Response response; - if (logger.isDebugEnabled()) { - logger.debug(getClass().getName(), e); - } - if (e instanceof UnauthorizedException) { - response = Response.status(Response.Status.UNAUTHORIZED) - .entity(serviceMsg + e.getMessage()).type("text/plain") - .build(); - return new WebApplicationException(response); - } else if (e instanceof DocumentNotFoundException) { - response = Response - .status(Response.Status.NOT_FOUND) - .entity(serviceMsg + " on " + getClass().getName() - + " csid=" + csid).type("text/plain").build(); - return new WebApplicationException(response); - } else if (e instanceof BadRequestException) { + protected WebApplicationException bigReThrow(Exception e, + String serviceMsg, String csid) throws WebApplicationException { + Response response; + if (logger.isDebugEnabled()) { + logger.debug(getClass().getName(), e); + } + if (e instanceof UnauthorizedException) { + response = Response.status(Response.Status.UNAUTHORIZED).entity(serviceMsg + e.getMessage()).type("text/plain").build(); + return new WebApplicationException(response); + } else if (e instanceof DocumentNotFoundException) { + response = Response.status(Response.Status.NOT_FOUND).entity(serviceMsg + " on " + getClass().getName() + + " csid=" + csid).type("text/plain").build(); + return new WebApplicationException(response); + } else if (e instanceof BadRequestException) { return new WebApplicationException(e, ((BadRequestException) e).getErrorCode()); - } else if (e instanceof WebApplicationException) { - // - // subresource may have already thrown this exception - // so just pass it on - return (WebApplicationException)e; - } else { // e is now instanceof Exception + } else if (e instanceof WebApplicationException) { + // + // subresource may have already thrown this exception + // so just pass it on + return (WebApplicationException) e; + } else { // e is now instanceof Exception String detail = Tools.errorToString(e, true); - response = Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(serviceMsg+" detail: "+detail).type("text/plain").build(); - return new WebApplicationException(response); - } - } - + response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(serviceMsg + " detail: " + detail).type("text/plain").build(); + return new WebApplicationException(response); + } + } + @Override public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { - return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass()); + return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass()); } - + /** * Creates the document handler. * @@ -121,20 +115,20 @@ public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends * @throws Exception the exception */ public DocumentHandler createDocumentHandler(ServiceContext serviceContext, - String schemaName, - Class commonClass) throws Exception { - DocumentHandler result = null; - - MultipartServiceContext ctx = (MultipartServiceContext)serviceContext; - Object commonPart = null; - if (ctx.getInput() != null) { - commonPart = ctx.getInputPart(schemaName); + String schemaName, + Class commonClass) throws Exception { + DocumentHandler result = null; + + MultipartServiceContext ctx = (MultipartServiceContext) serviceContext; + Object commonPart = null; + if (ctx.getInput() != null) { + commonPart = ctx.getInputPart(schemaName); } result = super.createDocumentHandler(ctx, commonPart); - + return result; } - + /** * Creates the document handler. * @@ -146,11 +140,11 @@ public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends * @throws Exception the exception */ public DocumentHandler createDocumentHandler( - ServiceContext ctx, - Class commonClass) throws Exception { - return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass); + ServiceContext ctx, + Class commonClass) throws Exception { + return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass); } - + /** * Creates the contact document handler. * @@ -163,58 +157,57 @@ public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends * @throws Exception the exception */ protected WorkflowDocumentModelHandler createWorkflowDocumentHandler( - ServiceContext ctx) throws Exception { - - WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler)createDocumentHandler(ctx, - WorkflowClient.SERVICE_COMMONPART_NAME, - WorkflowCommon.class); - + ServiceContext ctx) throws Exception { + + WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx, + WorkflowClient.SERVICE_COMMONPART_NAME, + WorkflowCommon.class); + return docHandler; } - + /* * JAX-RS Annotated methods */ - @GET @Path("{csid}" + WorkflowClient.SERVICE_PATH) public byte[] getWorkflow( @PathParam("csid") String csid) { PoxPayloadOut result = null; - try { + try { ServiceContext parentCtx = createServiceContext(); String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName(); - - MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME); - WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx); - ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace + + MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME); + WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx); + ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace getRepositoryClient(ctx).get(ctx, csid, handler); result = ctx.getOutput(); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid); } - + return result.getBytes(); } - + @PUT @Path("{csid}" + WorkflowClient.SERVICE_PATH) public byte[] updateWorkflow(@PathParam("csid") String csid, String xmlPayload) { PoxPayloadOut result = null; - try { - ServiceContext parentCtx = createServiceContext(); - String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName(); - - PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload); - MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate); - WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx); - ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace - getRepositoryClient(ctx).update(ctx, csid, handler); - result = ctx.getOutput(); + try { + ServiceContext parentCtx = createServiceContext(); + String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName(); + + PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload); + MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate); + WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx); + ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace + getRepositoryClient(ctx).update(ctx, csid, handler); + result = ctx.getOutput(); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid); } return result.getBytes(); - } + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java index e9831fa57..08dfa1c4a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java @@ -21,7 +21,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.collectionspace.services.common; import org.collectionspace.services.client.IQueryManager; @@ -55,58 +54,53 @@ import java.util.List; * Author: laramie */ public abstract class ResourceBase -extends AbstractMultiPartCollectionSpaceResourceImpl { + extends AbstractMultiPartCollectionSpaceResourceImpl { public static final String CREATE = "create"; - public static final String READ = "get"; + public static final String READ = "get"; public static final String UPDATE = "update"; public static final String DELETE = "delete"; - public static final String LIST = "list"; - + public static final String LIST = "list"; //FIXME retrieve client type from configuration static ClientType CLIENT_TYPE; + static { - try { - // I put this in a try-catch static block instead of file-level static var initializer so that static methods of - // *Resource classes may be called statically from test cases. - // Without this catch, you can't even access static methods of a *Resource class for testing. - CLIENT_TYPE = ServiceMain.getInstance().getClientType(); - //System.out.println("Static initializer in ResourceBase. CLIENT_TYPE:"+CLIENT_TYPE); - } catch (Throwable t){ - System.out.println("Static initializer failed in ResourceBase because not running from deployment. OK to use Resource classes statically for tests."); - } + try { + // I put this in a try-catch static block instead of file-level static var initializer so that static methods of + // *Resource classes may be called statically from test cases. + // Without this catch, you can't even access static methods of a *Resource class for testing. + CLIENT_TYPE = ServiceMain.getInstance().getClientType(); + //System.out.println("Static initializer in ResourceBase. CLIENT_TYPE:"+CLIENT_TYPE); + } catch (Throwable t) { + System.out.println("Static initializer failed in ResourceBase because not running from deployment. OK to use Resource classes statically for tests."); + } } protected void ensureCSID(String csid, String crudType) throws WebApplicationException { if (logger.isDebugEnabled()) { - logger.debug(crudType+" for "+getClass().getName()+" with csid=" + csid); + logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid); } if (csid == null || "".equals(csid)) { - logger.error(crudType+" for " + getClass().getName() + " missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST) - .entity("update failed on " + getClass().getName() + " csid=" + csid) - .type("text/plain") - .build(); + logger.error(crudType + " for " + getClass().getName() + " missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on " + getClass().getName() + " csid=" + csid).type("text/plain").build(); throw new WebApplicationException(response); } } - //======================= CREATE ==================================================== - @POST public Response create(@Context UriInfo ui, - String xmlPayload) { + String xmlPayload) { try { - PoxPayloadIn input = new PoxPayloadIn(xmlPayload); + PoxPayloadIn input = new PoxPayloadIn(xmlPayload); //System.out.println("\r\n\r\n==============================\r\nxmlPayload:\r\n"+xmlPayload); - ServiceContext ctx = createServiceContext(input); + ServiceContext ctx = createServiceContext(input); return create(input, ctx); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.CREATE_FAILED); } } - + protected Response create(PoxPayloadIn input, ServiceContext ctx) { try { DocumentHandler handler = createDocumentHandler(ctx); @@ -119,10 +113,10 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { /** Subclasses may override this overload, which gets called from @see #create(MultipartInput) */ protected Response create(PoxPayloadIn input, - ServiceContext ctx, - DocumentHandler handler, - UriBuilder path) - throws Exception { + ServiceContext ctx, + DocumentHandler handler, + UriBuilder path) + throws Exception { String csid = getRepositoryClient(ctx).create(ctx, handler); path.path("" + csid); Response response = Response.created(path.build()).build(); @@ -130,15 +124,14 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { } //======================= UPDATE ==================================================== - @PUT @Path("{csid}") public byte[] update(@PathParam("csid") String csid, String xmlPayload) { PoxPayloadOut result = null; ensureCSID(csid, UPDATE); try { - PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload); - ServiceContext ctx = createServiceContext(theUpdate); + PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload); + ServiceContext ctx = createServiceContext(theUpdate); result = update(csid, theUpdate, ctx); //==> CALL implementation method, which subclasses may override. } catch (Exception e) { throw bigReThrow(e, ServiceMessages.UPDATE_FAILED, csid); @@ -148,9 +141,9 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { /** Subclasses may override this overload, which gets called from #udpate(String,MultipartInput) */ protected PoxPayloadOut update(String csid, - PoxPayloadIn theUpdate, + PoxPayloadIn theUpdate, ServiceContext ctx) - throws Exception { + throws Exception { DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).update(ctx, csid, handler); return ctx.getOutput(); @@ -158,22 +151,21 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { /** Subclasses may override this overload, which gets called from #udpate(String,MultipartInput) */ protected PoxPayloadOut update(String csid, - MultipartInput theUpdate, - ServiceContext ctx, - DocumentHandler handler) - throws Exception { + MultipartInput theUpdate, + ServiceContext ctx, + DocumentHandler handler) + throws Exception { getRepositoryClient(ctx).update(ctx, csid, handler); return ctx.getOutput(); } //======================= DELETE ==================================================== - @DELETE @Path("{csid}") public Response delete(@PathParam("csid") String csid) { ensureCSID(csid, DELETE); try { - ServiceContext ctx = createServiceContext(); + ServiceContext ctx = createServiceContext(); return delete(csid, ctx); //==> CALL implementation method, which subclasses may override. } catch (Exception e) { throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid); @@ -183,68 +175,64 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { /** subclasses may override this method, which is called from #delete(String) * which handles setup of ServiceContext, and does Exception handling. */ protected Response delete(String csid, ServiceContext ctx) - throws Exception { + throws Exception { getRepositoryClient(ctx).delete(ctx, csid); - return Response.status(HttpResponseCodes.SC_OK).build(); + return Response.status(HttpResponseCodes.SC_OK).build(); } - //======================= GET ==================================================== - @GET @Path("{csid}") public byte[] get( - @Context UriInfo ui, - @PathParam("csid") String csid) { - PoxPayloadOut result = null; + @Context UriInfo ui, + @PathParam("csid") String csid) { + PoxPayloadOut result = null; ensureCSID(csid, READ); try { MultivaluedMap queryParams = ui.getQueryParameters(); - ServiceContext ctx = createServiceContext(queryParams); - result = get(csid, ctx);// ==> CALL implementation method, which subclasses may override. + ServiceContext ctx = createServiceContext(queryParams); + result = get(csid, ctx);// ==> CALL implementation method, which subclasses may override. if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND).entity( - ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build(); + ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build(); throw new WebApplicationException(response); } } catch (Exception e) { throw bigReThrow(e, ServiceMessages.READ_FAILED, csid); } - + return result.getBytes(); } - + protected PoxPayloadOut get(@PathParam("csid") String csid, - ServiceContext ctx) throws Exception { - PoxPayloadOut result = null; - - ensureCSID(csid, READ); + ServiceContext ctx) throws Exception { + PoxPayloadOut result = null; + + ensureCSID(csid, READ); DocumentHandler handler = createDocumentHandler(ctx); result = get(csid, ctx, handler); if (result == null) { String msg = "Could not find document with id = " + csid; if (logger.isErrorEnabled() == true) { - logger.error(msg); + logger.error(msg); } throw new DocumentNotFoundException(msg); } - + return result; } - /** subclasses may override this method, which is called from #get(String) * which handles setup of ServiceContext and DocumentHandler, and Exception handling.*/ protected PoxPayloadOut get(String csid, - ServiceContext ctx, - DocumentHandler handler) - throws Exception { + ServiceContext ctx, + DocumentHandler handler) + throws Exception { getRepositoryClient(ctx).get(ctx, csid, handler); return ctx.getOutput(); } //======================= GET without csid. List, search, etc. ===================================== - @GET public AbstractCommonList getList(@Context UriInfo ui) { MultivaluedMap queryParams = ui.getQueryParameters(); @@ -263,33 +251,34 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { ServiceContext ctx = createServiceContext(queryParams); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).getFiltered(ctx, handler); - AbstractCommonList list = (AbstractCommonList)handler.getCommonPartList(); + AbstractCommonList list = (AbstractCommonList) handler.getCommonPartList(); return list; } catch (Exception e) { throw bigReThrow(e, ServiceMessages.LIST_FAILED); } } - protected AbstractCommonList finish_getList(ServiceContext ctx, DocumentHandler handler){ - try{ + protected AbstractCommonList finish_getList(ServiceContext ctx, DocumentHandler handler) { + try { getRepositoryClient(ctx).getFiltered(ctx, handler); - return (AbstractCommonList)handler.getCommonPartList(); - } catch (Exception e) { + return (AbstractCommonList) handler.getCommonPartList(); + } catch (Exception e) { throw bigReThrow(e, ServiceMessages.LIST_FAILED); - } + } } + protected AbstractCommonList search(MultivaluedMap queryParams, String keywords) { try { - ServiceContext ctx = createServiceContext(queryParams); + ServiceContext ctx = createServiceContext(queryParams); DocumentHandler handler = createDocumentHandler(ctx); // perform a keyword search if (keywords != null && !keywords.isEmpty()) { - String whereClause = QueryManager.createWhereClauseFromKeywords(keywords); - DocumentFilter documentFilter = handler.getDocumentFilter(); - documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND); - if (logger.isDebugEnabled()) { - logger.debug("The WHERE clause is: " + documentFilter.getWhereClause()); - } + String whereClause = QueryManager.createWhereClauseFromKeywords(keywords); + DocumentFilter documentFilter = handler.getDocumentFilter(); + documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND); + if (logger.isDebugEnabled()) { + logger.debug("The WHERE clause is: " + documentFilter.getWhereClause()); + } } getRepositoryClient(ctx).getFiltered(ctx, handler); return (AbstractCommonList) handler.getCommonPartList(); @@ -302,39 +291,35 @@ extends AbstractMultiPartCollectionSpaceResourceImpl { @Deprecated public AbstractCommonList getList(List csidList) { try { - ServiceContext ctx = createServiceContext(); + ServiceContext ctx = createServiceContext(); DocumentHandler handler = createDocumentHandler(ctx); getRepositoryClient(ctx).get(ctx, csidList, handler); - return (AbstractCommonList)handler.getCommonPartList(); + return (AbstractCommonList) handler.getCommonPartList(); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.LIST_FAILED); } } - - //======================== GET : getAuthorityRefs ======================================== - @GET @Path("{csid}/authorityrefs") @Produces("application/xml") public AuthorityRefList getAuthorityRefs( - @PathParam("csid") String csid, - @Context UriInfo ui) { - AuthorityRefList authRefList = null; + @PathParam("csid") String csid, + @Context UriInfo ui) { + AuthorityRefList authRefList = null; try { - MultivaluedMap queryParams = ui.getQueryParameters(); - ServiceContext ctx = createServiceContext(queryParams); + MultivaluedMap queryParams = ui.getQueryParameters(); + ServiceContext ctx = createServiceContext(queryParams); DocumentWrapper docWrapper = getRepositoryClient(ctx).getDoc(ctx, csid); - DocumentModelHandler handler = (DocumentModelHandler)createDocumentHandler(ctx); + DocumentModelHandler handler = (DocumentModelHandler) createDocumentHandler(ctx); List authRefFields = - ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues( - ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES); + ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues( + ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES); authRefList = handler.getAuthorityRefs(docWrapper, authRefFields); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.AUTH_REFS_FAILED, csid); } return authRefList; } - } -- 2.47.3