Skip to content
This repository was archived by the owner on Mar 31, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 22 additions & 129 deletions src/main/java/org/cristalise/restapi/ItemRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 } )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the previous types as well

@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,
Expand Down Expand Up @@ -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();
}

}
Expand Down Expand Up @@ -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<String> 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
Expand Down Expand Up @@ -554,49 +479,6 @@ private String executePredefinedStep(ItemProxy item, String postData, List<Strin

return agent.execute(item, actPath, postData);
}


/**
*
* @param item
* @param file
* @param postData
* @param types
* @param actPath
* @param transition
* @param agent
* @return
* @throws AccessRightsException
* @throws ObjectNotFoundException
* @throws PersistencyException
* @throws InvalidDataException
* @throws OutcomeBuilderException
* @throws InvalidTransitionException
* @throws ObjectAlreadyExistsException
* @throws InvalidCollectionModification
* @throws ScriptErrorException
* @throws IOException
*/
private String executeUploadJob(ItemProxy item, InputStream file, String postData, List<String> 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);
}

/**
*
Expand All @@ -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<String> types, String actPath, String transition, AgentProxy agent)
private String executeJob(ItemProxy item, String postData, List<String> 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()) {
Expand All @@ -637,6 +523,13 @@ private String executeJob(ItemProxy item, String postData, List<String> 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);
}
Expand Down