From 63fade354acadc41ba86658a9501771d179aeb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Kov=C3=A1cs?= Date: Tue, 11 Sep 2018 21:14:26 +0200 Subject: [PATCH] #39 - remove requestBinaryTransiiton() --- .../java/org/cristalise/restapi/ItemRoot.java | 151 +++--------------- 1 file changed, 22 insertions(+), 129 deletions(-) diff --git a/src/main/java/org/cristalise/restapi/ItemRoot.java b/src/main/java/org/cristalise/restapi/ItemRoot.java index e57d0a6..1bf1b6f 100644 --- a/src/main/java/org/cristalise/restapi/ItemRoot.java +++ b/src/main/java/org/cristalise/restapi/ItemRoot.java @@ -376,11 +376,14 @@ else if (job != null) { } @POST - @Consumes( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) - @Produces( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) + @Consumes( MediaType.MULTIPART_FORM_DATA ) + @Produces( MediaType.MULTIPART_FORM_DATA) +// @Consumes( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) +// @Produces( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @Path("{activityPath: .*}") public String requestTransition( String postData, @Context HttpHeaders headers, + @FormDataParam ("file") InputStream file, @PathParam("uuid") String uuid, @PathParam("activityPath") String actPath, @QueryParam("transition") String transition, @@ -410,11 +413,12 @@ public String requestTransition( String postData, } else { transition = extractAndCcheckTransitionName(transition, uri); - String execJob = executeJob(item, postData, types, actPath, transition, agent); + String execJob = executeJob(item, postData, types, actPath, transition, agent, file); + if (types.contains(MediaType.APPLICATION_XML) || types.contains(MediaType.TEXT_XML)) { - return execJob; + return execJob; } else { - return XML.toJSONObject(execJob).toString(); + return XML.toJSONObject(execJob).toString(); } } @@ -445,85 +449,6 @@ public String requestTransition( String postData, } - /** - * Method for handling binary uplaod POST methods - * - * @param postData - * @param headers - * @param uuid - * @param actPath - * @param transition - * @param authCookie - * @param uri - * @return - */ - @POST - @Consumes( MediaType.MULTIPART_FORM_DATA ) - @Produces( MediaType.MULTIPART_FORM_DATA) - @Path("{binaryUploadPath: .*}") - public String requestBinaryTransition( String postData, - @FormDataParam ("file") InputStream file, - @Context HttpHeaders headers, - @PathParam("uuid") String uuid, - @PathParam("binaryUploadPath") String actPath, - @QueryParam("transition") String transition, - @CookieParam(COOKIENAME) Cookie authCookie, - @Context UriInfo uri) - { - AgentProxy agent = null; - try { - agent = (AgentProxy)Gateway.getProxyManager().getProxy( checkAuthCookie(authCookie) ); - } - catch (ObjectNotFoundException e1) { - throw ItemUtils.createWebAppException(e1.getMessage(), Response.Status.UNAUTHORIZED); - } - - if (actPath == null) throw ItemUtils.createWebAppException("Must specify activity path", Response.Status.BAD_REQUEST); - - if (file == null) throw ItemUtils.createWebAppException("Must provide a file to upload", Response.Status.BAD_REQUEST); - - // Find agent - ItemProxy item = getProxy(uuid); - - try { - List types = headers.getRequestHeader(HttpHeaders.CONTENT_TYPE); - - Logger.msg(5, "ItemRoot.requestTransition() postData:%s", postData); - - if (actPath.startsWith("workflow/predefined")) { - return executePredefinedStep(item, postData, types, actPath, agent); - } - else { - transition = extractAndCcheckTransitionName(transition, uri); - - return executeUploadJob(item, file, postData, types, actPath, transition, agent); - } - } - catch (OutcomeBuilderException | InvalidDataException | ScriptErrorException | ObjectAlreadyExistsException | InvalidCollectionModification e) { - Logger.error(e); - throw ItemUtils.createWebAppException(e.getMessage(), e, Response.Status.BAD_REQUEST); - } - catch (AccessRightsException e) { // agent doesn't hold the right to execute - throw ItemUtils.createWebAppException(e.getMessage(), e, Response.Status.UNAUTHORIZED); - } - catch (ObjectNotFoundException e) { // workflow, schema, script etc not found. - Logger.error(e); - throw ItemUtils.createWebAppException(e.getMessage(), e, Response.Status.NOT_FOUND); - } - catch (InvalidTransitionException e) { // activity has already changed state - Logger.error(e); - throw ItemUtils.createWebAppException(e.getMessage(), e, Response.Status.CONFLICT); - } - catch (PersistencyException e) { // database failure - Logger.error(e); - throw ItemUtils.createWebAppException(e.getMessage(), e, Response.Status.INTERNAL_SERVER_ERROR); - } - catch (Exception e) { // any other failure - Logger.error(e); - throw ItemUtils.createWebAppException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR); - } - } - /** * * @param item @@ -554,49 +479,6 @@ private String executePredefinedStep(ItemProxy item, String postData, List types, String actPath, String transition, AgentProxy agent) - throws AccessRightsException, ObjectNotFoundException, PersistencyException, InvalidDataException, OutcomeBuilderException, - InvalidTransitionException, ObjectAlreadyExistsException, InvalidCollectionModification, ScriptErrorException, IOException - { - Job thisJob = item.getJobByTransitionName(actPath, transition, agent); - - byte[] binaryData = IOUtils.toByteArray(file); - - if (thisJob == null) - throw ItemUtils.createWebAppException("Job not found for actPath:"+actPath+" transition:"+transition, Response.Status.NOT_FOUND); - - // set outcome if required - if (thisJob.hasOutcome()) { - OutcomeAttachment outcomeAttachment = - new OutcomeAttachment(item.getPath(), thisJob.getSchema().getName(), thisJob.getSchema().getVersion(), -1, MediaType.APPLICATION_OCTET_STREAM, binaryData); - - thisJob.setAttachment(outcomeAttachment); - } - return agent.execute(thisJob); - } /** * @@ -616,15 +498,19 @@ private String executeUploadJob(ItemProxy item, InputStream file, String postDat * @throws ObjectAlreadyExistsException * @throws InvalidCollectionModification * @throws ScriptErrorException + * @throws IOException */ - private String executeJob(ItemProxy item, String postData, List types, String actPath, String transition, AgentProxy agent) + private String executeJob(ItemProxy item, String postData, List types, String actPath, String transition, AgentProxy agent, InputStream file) throws AccessRightsException, ObjectNotFoundException, PersistencyException, InvalidDataException, OutcomeBuilderException, - InvalidTransitionException, ObjectAlreadyExistsException, InvalidCollectionModification, ScriptErrorException + InvalidTransitionException, ObjectAlreadyExistsException, InvalidCollectionModification, ScriptErrorException, IOException { Job thisJob = item.getJobByTransitionName(actPath, transition, agent); if (thisJob == null) throw ItemUtils.createWebAppException("Job not found for actPath:"+actPath+" transition:"+transition, Response.Status.NOT_FOUND); + + byte[] binaryData = null; + if (file != null) binaryData = IOUtils.toByteArray(file); // set outcome if required if (thisJob.hasOutcome()) { @@ -637,6 +523,13 @@ private String executeJob(ItemProxy item, String postData, List types, S //Outcome can be invalid at this point, because Script/Query can be executed later thisJob.setOutcome(builder.getOutcome(false)); } + + if (binaryData != null && binaryData.length > 0) { + OutcomeAttachment outcomeAttachment = + new OutcomeAttachment(item.getPath(), thisJob.getSchema().getName(), thisJob.getSchema().getVersion(), -1, MediaType.APPLICATION_OCTET_STREAM, binaryData); + + thisJob.setAttachment(outcomeAttachment); + } } return agent.execute(thisJob); }