Skip to content

Commit

Permalink
JBPM-7791 - add content type charset input param to rest workitem han…
Browse files Browse the repository at this point in the history
…dler (#1337)
  • Loading branch information
Tihomir Surdilovic authored and mswiderski committed Sep 28, 2018
1 parent 1512754 commit 6ef3c43
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
Expand Up @@ -117,6 +117,7 @@ public class RESTWorkItemHandler extends AbstractLogOrThrowWorkItemHandler imple
public static final String PARAM_CONNECT_TIMEOUT = "ConnectTimeout";
public static final String PARAM_READ_TIMEOUT = "ReadTimeout";
public static final String PARAM_CONTENT_TYPE = "ContentType";
public static final String PARAM_CONTENT_TYPE_CHARSET = "ContentTypeCharset";
public static final String PARAM_HEADERS = "Headers";
public static final String PARAM_CONTENT = "Content";
public static final String PARAM_CONTENT_DATA = "ContentData";
Expand Down Expand Up @@ -659,10 +660,10 @@ protected void setBody(RequestBuilder builder,
Object content = params.get(PARAM_CONTENT_DATA) != null ? params.get(PARAM_CONTENT_DATA) : params.get(PARAM_CONTENT);
if (!(content instanceof String)) {
content = transformRequest(content,
(String) params.get(PARAM_CONTENT_TYPE));
getContentTypeAndCharset(params));
}
StringEntity entity = new StringEntity((String) content,
ContentType.parse((String) params.get(PARAM_CONTENT_TYPE)));
ContentType.parse(getContentTypeAndCharset(params)));
builder.setEntity(entity);
} catch (UnsupportedCharsetException e) {
throw new RuntimeException("Cannot set body for REST request [" + builder.getMethod() + "] " + builder.getUri(),
Expand All @@ -678,10 +679,10 @@ protected void setBody(HttpRequestBase theMethod,
Object content = params.get(PARAM_CONTENT_DATA) != null ? params.get(PARAM_CONTENT_DATA) : params.get(PARAM_CONTENT);
if (!(content instanceof String)) {
content = transformRequest(content,
(String) params.get(PARAM_CONTENT_TYPE));
getContentTypeAndCharset(params));
}
((HttpEntityEnclosingRequestBase) theMethod).setEntity(new StringEntity((String) content,
ContentType.parse((String) params.get(PARAM_CONTENT_TYPE))));
ContentType.parse(getContentTypeAndCharset(params))));
}
}

Expand Down Expand Up @@ -1160,6 +1161,21 @@ protected Object configureRequest(String method,
}
}

protected String getContentTypeAndCharset(Map<String, Object> params) {
String contentType = (String) params.get(PARAM_CONTENT_TYPE);
String contentTypeCharset = (String) params.get(PARAM_CONTENT_TYPE_CHARSET);

if(contentType == null || contentTypeCharset == null) {
return contentType;
}

if(!contentType.contains("charset=")) {
return contentType + ";charset=" + contentTypeCharset;
} else {
return contentType;
}
}

@Override
public void close() {
try {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

import javax.ws.rs.ext.RuntimeDelegate;

Expand All @@ -42,6 +43,7 @@
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_CONTENT;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_CONTENT_DATA;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_CONTENT_TYPE;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_CONTENT_TYPE_CHARSET;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_READ_TIMEOUT;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_RESULT;
import static org.jbpm.process.workitem.rest.RESTWorkItemHandler.PARAM_STATUS;
Expand Down Expand Up @@ -323,6 +325,8 @@ public void testPOSTOperation() {
"POST");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");

Expand Down Expand Up @@ -359,6 +363,8 @@ public void testPOSTOperationWthJSONHeader() {
"POST");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");

Expand Down Expand Up @@ -393,6 +399,8 @@ public void testPOSTOperationWithXMLHeader() {
"POST");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");
workItem.setParameter("AcceptHeader",
Expand Down Expand Up @@ -461,6 +469,8 @@ public void testPUTOperation() {
"PUT");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");

Expand Down Expand Up @@ -497,6 +507,8 @@ public void testPUTOperationWithXMLHeader() {
"PUT");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");
workItem.setParameter("AcceptHeader",
Expand Down Expand Up @@ -535,6 +547,8 @@ public void testPUTOperationWithJSONHeader() {
"PUT");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");
workItem.setParameter("AcceptHeader",
Expand Down Expand Up @@ -705,7 +719,9 @@ public void testPOSTOperationWithXmlTransformation() {
workItem.setParameter("Method",
"POST");
workItem.setParameter(PARAM_CONTENT_TYPE,
"Application/XML;charset=utf-8");
"Application/XML");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");
workItem.setParameter("ResultClass",
Expand Down Expand Up @@ -743,7 +759,9 @@ public void testPUTOperationWithXmlTransformation() {
workItem.setParameter("Method",
"PUT");
workItem.setParameter(PARAM_CONTENT_TYPE,
"Application/Xml;charset=utf-8");
"Application/Xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>john</name><age>25</age></person>");
workItem.setParameter("ResultClass",
Expand Down Expand Up @@ -786,6 +804,8 @@ public void testPOSTOperationWithCompleteXmlTransformation() {
"POST");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
request);
workItem.setParameter("ResultClass",
Expand Down Expand Up @@ -915,7 +935,45 @@ public void testPUTOperationWithDefaultCharset() {
}

@Test
public void testPUTOperationWithCharsetSpecified() {
public void testPUTOperationWithSetCharset() {
RESTWorkItemHandler handler = new RESTWorkItemHandler();
String nonAsciiData = "\u0418\u0432\u0430\u043d";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person><age>25</age><name>Put Иван</name></person>";

WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter("Url",
serverURL + "/xml-charset");
workItem.setParameter("Method",
"PUT");
workItem.setParameter(PARAM_CONTENT_TYPE,
"application/xml");
workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET,
"UTF-8");
workItem.setParameter(contentParamName,
"<person><name>" + nonAsciiData + "</name><age>25</age></person>");

WorkItemManager manager = new TestWorkItemManager();
handler.executeWorkItem(workItem,
manager);

Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId());
String result = (String) results.get(PARAM_RESULT);
assertNotNull("result cannot be null",
result);
assertEquals(expected,
result);
int responseCode = (Integer) results.get(PARAM_STATUS);
assertNotNull(responseCode);
assertEquals(200,
responseCode);
String responseMsg = (String) results.get(PARAM_STATUS_MSG);
assertNotNull(responseMsg);
assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK",
responseMsg);
}

@Test
public void testPUTOperationWithCharsetUnspecifiedViaParameter() {
RESTWorkItemHandler handler = new RESTWorkItemHandler();
String nonAsciiData = "\u0418\u0432\u0430\u043d";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
Expand Down Expand Up @@ -1085,4 +1143,25 @@ public void testGETCharsetOperation() throws UnsupportedEncodingException {
assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK",
responseMsg);
}

@Test
public void testGetContentTypeAndCharset() {
RESTWorkItemHandler handler = new RESTWorkItemHandler();

Map<String, Object> params = new java.util.HashMap<>();
params.put(PARAM_CONTENT_TYPE, "application/xml");
params.put(PARAM_CONTENT_TYPE_CHARSET, "UTF-8");
assertEquals("application/xml;charset=UTF-8", handler.getContentTypeAndCharset(params) );

params.put(PARAM_CONTENT_TYPE, "application/xml;charset=UTF-8");
params.put(PARAM_CONTENT_TYPE_CHARSET, "SOME_WROMG_CHARSET");
assertEquals("application/xml;charset=UTF-8", handler.getContentTypeAndCharset(params) );

params.remove(PARAM_CONTENT_TYPE_CHARSET);
assertEquals("application/xml;charset=UTF-8", handler.getContentTypeAndCharset(params) );

params.remove(PARAM_CONTENT_TYPE);
assertNull(handler.getContentTypeAndCharset(params));

}
}

0 comments on commit 6ef3c43

Please sign in to comment.