diff --git a/.travis.yml b/.travis.yml index 4e62807..ba18bd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ deploy: provider: releases api_key: secure: yep - file: target/Everest-Alpha-1.0.jar \ No newline at end of file + file: target/Everest-Alpha-1.0.jar diff --git a/src/main/java/com/rohitawate/everest/requestmanager/DataDispatchRequestManager.java b/src/main/java/com/rohitawate/everest/requestmanager/DataDispatchRequestManager.java index ede26dc..1ada390 100644 --- a/src/main/java/com/rohitawate/everest/requestmanager/DataDispatchRequestManager.java +++ b/src/main/java/com/rohitawate/everest/requestmanager/DataDispatchRequestManager.java @@ -72,6 +72,11 @@ public class DataDispatchRequestManager extends RequestManager { * @return invocation object */ private Invocation appendBody() throws Exception { + /* + Checks if a custom mime-type is mentioned in the headers. + If present, it will override the logical Content-Type. + */ + String overriddenContentType = request.getHeaders().get("Content-Type"); Invocation invocation = null; Map.Entry mapEntry; @@ -79,6 +84,7 @@ public class DataDispatchRequestManager extends RequestManager { case MediaType.MULTIPART_FORM_DATA: FormDataMultiPart formData = new FormDataMultiPart(); + // Adding the string tuples to the request HashMap pairs = dataDispatchRequest.getStringTuples(); for (Map.Entry entry : pairs.entrySet()) { mapEntry = (Map.Entry) entry; @@ -88,8 +94,10 @@ public class DataDispatchRequestManager extends RequestManager { String filePath; File file; boolean fileException = false; - StringBuilder fileExceptionMessage = new StringBuilder(); + String fileExceptionMessage = null; pairs = dataDispatchRequest.getFileTuples(); + + // Adding the file tuples to the request for (Map.Entry entry : pairs.entrySet()) { mapEntry = (Map.Entry) entry; filePath = mapEntry.getValue(); @@ -100,14 +108,13 @@ public class DataDispatchRequestManager extends RequestManager { file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); else { fileException = true; - fileExceptionMessage.append(" - "); - fileExceptionMessage.append(filePath); - fileExceptionMessage.append("\n"); + // For pretty-printing FileNotFoundException to the UI + fileExceptionMessage = " - " + filePath + "\n"; } } if (fileException) { - throw new FileNotFoundException(fileExceptionMessage.toString()); + throw new FileNotFoundException(fileExceptionMessage); } formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); @@ -118,6 +125,8 @@ public class DataDispatchRequestManager extends RequestManager { invocation = requestBuilder.buildPut(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE)); break; case MediaType.APPLICATION_OCTET_STREAM: + if (overriddenContentType == null) + overriddenContentType = MediaType.APPLICATION_OCTET_STREAM; filePath = dataDispatchRequest.getBody(); File check = new File(filePath); @@ -129,11 +138,14 @@ public class DataDispatchRequestManager extends RequestManager { FileInputStream stream = new FileInputStream(filePath); if (requestType.equals("POST")) - invocation = requestBuilder.buildPost(Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE)); + invocation = requestBuilder.buildPost(Entity.entity(stream, overriddenContentType)); else - invocation = requestBuilder.buildPut(Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE)); + invocation = requestBuilder.buildPut(Entity.entity(stream, overriddenContentType)); break; case MediaType.APPLICATION_FORM_URLENCODED: + if (overriddenContentType == null) + overriddenContentType = MediaType.APPLICATION_FORM_URLENCODED; + Form form = new Form(); for (Map.Entry entry : dataDispatchRequest.getStringTuples().entrySet()) { @@ -142,24 +154,27 @@ public class DataDispatchRequestManager extends RequestManager { } if (requestType.equals("POST")) - invocation = requestBuilder.buildPost(Entity.form(form)); + invocation = requestBuilder.buildPost(Entity.entity(form, overriddenContentType)); else - invocation = requestBuilder.buildPut(Entity.form(form)); + invocation = requestBuilder.buildPut(Entity.entity(form, overriddenContentType)); break; default: // Handles raw data types (JSON, Plain text, XML, HTML) + String originalContentType = dataDispatchRequest.getContentType(); + if (overriddenContentType == null) + overriddenContentType = originalContentType; switch (requestType) { case "POST": invocation = requestBuilder - .buildPost(Entity.entity(dataDispatchRequest.getBody(), dataDispatchRequest.getContentType())); + .buildPost(Entity.entity(dataDispatchRequest.getBody(), overriddenContentType)); break; case "PUT": invocation = requestBuilder - .buildPut(Entity.entity(dataDispatchRequest.getBody(), dataDispatchRequest.getContentType())); + .buildPut(Entity.entity(dataDispatchRequest.getBody(), overriddenContentType)); break; case "PATCH": invocation = requestBuilder - .build("PATCH", Entity.entity(dataDispatchRequest.getBody(), dataDispatchRequest.getContentType())); + .build("PATCH", Entity.entity(dataDispatchRequest.getBody(), overriddenContentType)); break; } } diff --git a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java index 58a1202..2e9f84a 100644 --- a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java +++ b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java @@ -51,11 +51,14 @@ public abstract class RequestManager extends Service { .register(MultiPartFeature.class) .build(); + // Required for making PATCH requests through Jersey client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true); + if (Settings.connectionTimeOutEnable) client.property(ClientProperties.CONNECT_TIMEOUT, Settings.connectionTimeOut); if (Settings.connectionReadTimeOutEnable) client.property(ClientProperties.READ_TIMEOUT, Settings.connectionReadTimeOut); + return client; }