Skip to content

Commit

Permalink
fix(response): fallback to ISO-8859-1 when no charset in header (#48)
Browse files Browse the repository at this point in the history
* Instead of using the default JVM Charset use the ISO-8859-1 Charset
when no charset is defined in response header.
* Add a system property to fallback to current behavior and use the JVM
Charset as fallback
  • Loading branch information
rbioteau authored Nov 20, 2020
1 parent 35f7565 commit eabd14f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.bonitasoft.connectors</groupId>
<artifactId>bonita-connector-rest</artifactId>
<version>1.0.9-SNAPSHOT</version>
<version>1.0.10-SNAPSHOT</version>

<name>Bonita Rest Connector</name>
<description>Rest Connector for Bonita</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.<String, Object> emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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("/"))
Expand Down

0 comments on commit eabd14f

Please sign in to comment.