diff --git a/pom.xml b/pom.xml index dfc1cdc..b1646e0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.bonitasoft.connectors bonita-connector-rest - 1.0.9-SNAPSHOT + 1.0.10-SNAPSHOT Bonita Rest Connector Rest Connector for Bonita diff --git a/src/main/java/org/bonitasoft/connectors/rest/RESTConnector.java b/src/main/java/org/bonitasoft/connectors/rest/RESTConnector.java index 9836453..2f2d057 100644 --- a/src/main/java/org/bonitasoft/connectors/rest/RESTConnector.java +++ b/src/main/java/org/bonitasoft/connectors/rest/RESTConnector.java @@ -100,6 +100,11 @@ public class RESTConnector extends AbstractRESTConnectorImpl { * The class logger */ private static final Logger LOGGER = Logger.getLogger(RESTConnector.class.getName()); + /** + * Forces the character encoding of the HTTP response body to the default JVM Charset if no Charset is defined in the response header. + * If this property is not set, the ISO-8859-1 Charset is used as fallback, as specified in the specification. + */ + static final String DEFAULT_JVM_CHARSET_FALLBACK_PROPERTY = "org.bonitasoft.connectors.rest.response.fallbackToJVMCharset"; @Override public void validateInputParameters() throws ConnectorValidationException { @@ -415,7 +420,8 @@ private void setOutputs(final HttpResponse response, Request request) throws IOE } private void parseResponse(final HttpEntity entity) throws IOException { - String stringContent = EntityUtils.toString(entity, Charset.defaultCharset()); + boolean fallbackToDefaultCharset = Boolean.parseBoolean(System.getProperty(DEFAULT_JVM_CHARSET_FALLBACK_PROPERTY)); + String stringContent = EntityUtils.toString(entity,fallbackToDefaultCharset ? Charset.defaultCharset() : null); final String bodyResponse = stringContent != null ? stringContent.trim() : ""; setBody(bodyResponse); setBody(Collections. emptyMap()); diff --git a/src/test/java/org/bonitasoft/connectors/rest/RESTConnectorTest.java b/src/test/java/org/bonitasoft/connectors/rest/RESTConnectorTest.java index 614c107..52c6a14 100644 --- a/src/test/java/org/bonitasoft/connectors/rest/RESTConnectorTest.java +++ b/src/test/java/org/bonitasoft/connectors/rest/RESTConnectorTest.java @@ -24,12 +24,10 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.google.common.collect.Lists.newArrayList; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; @@ -42,6 +40,7 @@ import org.bonitasoft.connectors.rest.model.AuthorizationType; import org.bonitasoft.engine.connector.ConnectorException; import org.bonitasoft.engine.exception.BonitaException; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; @@ -56,6 +55,7 @@ public class RESTConnectorTest extends AcceptanceTestBase { private static final int NB_OUTPUTS = 5; + //WireMock /** * All HTTP static strings used by WireMock to do tests @@ -193,6 +193,11 @@ public static final void initValues() { HEADERS_ERROR.add(header3); HEADERS_ERROR.add(header4); } + + @After + public void resetSystemProperies() throws Exception { + System.setProperty(RESTConnector.DEFAULT_JVM_CHARSET_FALLBACK_PROPERTY, ""); + } /** * Build a request parameters set based on the given arguments @@ -709,6 +714,31 @@ public void iso88591CharsetResponse() throws BonitaException, UnsupportedEncodin assertEquals("le text reçu a été encodé en ISO-8859-1", output.get(RESTConnector.BODY_AS_STRING_OUTPUT_PARAMETER)); } + @Test + public void useISO88591CharsetWhenNoContentType() throws BonitaException, UnsupportedEncodingException { + stubFor(post(urlEqualTo("/")) + .withHeader(WM_CONTENT_TYPE, equalTo(PLAIN_TEXT + "; " + WM_CHARSET + "=" + ISO_8859_1)) + .willReturn(aResponse() + .withBody(new String("le text reçu n'a pas de header").getBytes(ISO_8859_1)) + .withStatus(HttpStatus.SC_OK))); + + Map output = executeConnector(buildCharsetParametersSet(ISO_8859_1)); + assertEquals("le text reçu n'a pas de header", output.get(RESTConnector.BODY_AS_STRING_OUTPUT_PARAMETER)); + } + + @Test + public void useDefaultCharsetWhenNoContentTypeAndFallbackPropertyIsSet() throws BonitaException, UnsupportedEncodingException { + System.setProperty(RESTConnector.DEFAULT_JVM_CHARSET_FALLBACK_PROPERTY, "true"); + stubFor(post(urlEqualTo("/")) + .withHeader(WM_CONTENT_TYPE, equalTo(PLAIN_TEXT + "; " + WM_CHARSET + "=" + ISO_8859_1)) + .willReturn(aResponse() + .withBody(new String("le text reçu a été encodé avec le charset par default").getBytes(Charset.defaultCharset())) + .withStatus(HttpStatus.SC_OK))); + + Map output = executeConnector(buildCharsetParametersSet(ISO_8859_1)); + assertEquals("le text reçu a été encodé avec le charset par default", output.get(RESTConnector.BODY_AS_STRING_OUTPUT_PARAMETER)); + } + @Test public void utf8CharsetResponse() throws BonitaException, UnsupportedEncodingException { stubFor(post(urlEqualTo("/"))