Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Charset encoding specification during response parsing via NSTServiceWrapperProcessor (v1.1.10) #38

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion NST/src/main/java/com/ebay/nst/NSTServiceWrapperProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ebay.runtime.arguments.DisableConsoleLog;
import org.json.JSONObject;
Expand All @@ -23,12 +26,15 @@
import com.ebay.service.protocol.http.NSTHttpResponse;
import com.ebay.utility.service.ServiceUtil;

import javax.validation.constraints.NotNull;

public final class NSTServiceWrapperProcessor {

private boolean disableSchemaValidation = false;
private boolean disableRequestResponseLogging = false;
private boolean confirmSuccess = true;
private NSTHttpClient<NSTHttpRequest, NSTHttpResponse> client;
private Charset responseParsingCharset = StandardCharsets.UTF_8;
yarg0007 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a new service process with the default HttpUrlConnection used for
Expand Down Expand Up @@ -138,6 +144,15 @@ public NSTServiceWrapperProcessor resetConfirmSuccess() {
return this;
}

/**
* Set the response parsing charset to use. Default is UTF_8.
* @param responseParsingCharset Response parsing Charset to use when parsing the response.
*/
public void setResponseParsingCharset(@NotNull Charset responseParsingCharset) {
Objects.requireNonNull(responseParsingCharset, "Response parsing charset MUST NOT be null.");
this.responseParsingCharset = responseParsingCharset;
}

/**
* Send the request and get back the response JSON object.
*
Expand Down Expand Up @@ -292,7 +307,7 @@ protected void logRequestDetailsToConsole(NSTServiceWrapper<? extends NSTSchemaV
protected NSTHttpResponse sendRequest(NSTHttpRequest request)
throws URISyntaxException, IOException, IllegalStateException {

return client.sendRequest(request);
return client.sendRequest(request, responseParsingCharset);
}

protected void logResponseDetailsToConsole(NSTServiceWrapper<? extends NSTSchemaValidator> serviceWrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package com.ebay.service.protocol.http;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public interface NSTHttpClient<Request extends NSTHttpRequest, Response extends NSTHttpResponse> {

/**
* Send the request and return the response. Uses the default Charset specified by the implementation.
* @param request Request to send.
* @return Response model.
*/
public Response sendRequest(Request request);

/**
* Send the request and return the response.
* @param request Request to send.
* @param readResponseCharSet Character set to use when parsing the response payload. Recommend sourcing from java.nio.charset.StandardCharsets.
* @return Response model.
*/
public Response sendRequest(Request request, Charset readResponseCharSet);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
Expand All @@ -19,14 +21,20 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.validation.constraints.NotNull;

import com.ebay.nst.NstRequestType;

public class NSTHttpClientImpl implements NSTHttpClient<NSTHttpRequest, NSTHttpResponse> {

@SuppressWarnings("fallthrough")
@Override
public NSTHttpResponse sendRequest(NSTHttpRequest request) {
return sendRequest(request, StandardCharsets.UTF_8);
}

@SuppressWarnings("fallthrough")
@Override
public NSTHttpResponse sendRequest(NSTHttpRequest request, Charset readResponseCharSet) {

Objects.requireNonNull(request, "Request MUST NOT be null.");

Expand Down Expand Up @@ -78,7 +86,7 @@ public NSTHttpResponse sendRequest(NSTHttpRequest request) {

try {
OutputStream os = connection.getOutputStream();
byte[] input = payload.getBytes("utf-8");
byte[] input = payload.getBytes(readResponseCharSet);
os.write(input, 0, input.length);
} catch (Exception e) {
throw new RuntimeException(String.format("Exception occurred getting %s data from the connection.", requestType.name()), e);
Expand All @@ -97,7 +105,7 @@ public NSTHttpResponse sendRequest(NSTHttpRequest request) {

NSTHttpResponse response;
try {
response = parseResponse(connection);
response = parseResponse(connection, readResponseCharSet);
} catch (IOException e) {
throw new RuntimeException("Exception occurred parsing response data.", e);
}
Expand All @@ -117,12 +125,14 @@ public NSTHttpResponse sendRequest(NSTHttpRequest request) {
* @return Parsed response body.
* @throws IOException IO Error.
*/
protected final NSTHttpResponse parseResponse(HttpURLConnection connection) throws IOException {
protected final NSTHttpResponse parseResponse(HttpURLConnection connection, @NotNull Charset readResponseCharSet) throws IOException {

NSTHttpResponseImpl response = new NSTHttpResponseImpl();

if (connection == null) {
return response;
} if (readResponseCharSet == null) {
return response;
}

response.setResponseCode(connection.getResponseCode());
Expand All @@ -142,7 +152,7 @@ protected final NSTHttpResponse parseResponse(HttpURLConnection connection) thro
}
}

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), readResponseCharSet));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
Expand Down
24 changes: 21 additions & 3 deletions NST/src/test/java/com/ebay/nst/NSTServiceWrapperProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.testng.AssertJUnit.fail;

Expand Down Expand Up @@ -244,7 +247,7 @@ public void testSendRequestAndGetJsonResponse() throws Exception {
when(response.getResponseCode()).thenReturn(200);

NSTHttpClient<NSTHttpRequest, NSTHttpResponse> client = mock(NSTHttpClient.class);
when(client.sendRequest(Mockito.any(NSTHttpRequest.class))).thenReturn(response);
when(client.sendRequest(Mockito.any(NSTHttpRequest.class), Mockito.any(Charset.class))).thenReturn(response);

processor = new NSTServiceWrapperProcessor(client);
JSONObject actualJsonObject = processor.sendRequestAndGetJSONResponse(nstServiceWrapper);
Expand Down Expand Up @@ -632,14 +635,29 @@ public void getServiceWrapperNameWithUniqueWrapperName() {
public void sendRequest() throws Exception {

NSTHttpClient<NSTHttpRequest, NSTHttpResponse> client = mock(NSTHttpClient.class);
when(client.sendRequest(Mockito.any(NSTHttpRequest.class))).thenReturn(new NSTHttpResponseImpl());
when(client.sendRequest(Mockito.any(NSTHttpRequest.class), Mockito.any(Charset.class))).thenReturn(new NSTHttpResponseImpl());

NSTHttpRequest request = mock(NSTHttpRequest.class);

processor = new NSTServiceWrapperProcessor(client);
processor.sendRequest(request);

verify(client, times(1)).sendRequest(Mockito.any(NSTHttpRequest.class));
verify(client, times(1)).sendRequest(Mockito.any(NSTHttpRequest.class), eq(StandardCharsets.UTF_8));
}

@Test
public void sendRequestWithAlternateCharacterSetUsedToParseResponse() throws Exception {

NSTHttpClient<NSTHttpRequest, NSTHttpResponse> client = mock(NSTHttpClient.class);
when(client.sendRequest(Mockito.any(NSTHttpRequest.class), Mockito.any(Charset.class))).thenReturn(new NSTHttpResponseImpl());

NSTHttpRequest request = mock(NSTHttpRequest.class);

processor = new NSTServiceWrapperProcessor(client);
processor.setResponseParsingCharset(StandardCharsets.ISO_8859_1);
processor.sendRequest(request);

verify(client, times(1)).sendRequest(Mockito.any(NSTHttpRequest.class), eq(StandardCharsets.ISO_8859_1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -155,7 +156,16 @@ public void delete() throws Exception {
@Test
public void nullConnection() throws Exception {

NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(null);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(null, StandardCharsets.UTF_8);
assertThat(actual.getPayload(), is(nullValue()));
assertThat(actual.getHeaders(), is(anEmptyMap()));
assertThat(actual.getResponseCode(), is(equalTo(0)));
}

@Test
public void nullCharsetUsedToParseResponse() throws Exception {

NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, null);
assertThat(actual.getPayload(), is(nullValue()));
assertThat(actual.getHeaders(), is(anEmptyMap()));
assertThat(actual.getResponseCode(), is(equalTo(0)));
Expand All @@ -165,7 +175,7 @@ public void nullConnection() throws Exception {
public void nullConnectionHeaderFields() throws Exception {

when(connection.getHeaderFields()).thenReturn(null);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.UTF_8);
assertThat(actual.getHeaders(), is(anEmptyMap()));
}

Expand All @@ -178,9 +188,29 @@ public void fullResponse() throws Exception {

InputStream targetStream = new ByteArrayInputStream(payload.getBytes());
when(connection.getInputStream()).thenReturn(targetStream);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.UTF_8);
assertThat(actual.getPayload(), is(equalTo(payload)));
assertThat(actual.getHeaders(), is(equalTo(expectedHeaders)));
assertThat(actual.getResponseCode(), is(equalTo(200)));
}

@Test
public void parseResponseWithExtendedCharacterSetUsingIsoCharset() throws Exception {
InputStream targetStream = new ByteArrayInputStream("©".getBytes(StandardCharsets.UTF_8));
when(connection.getInputStream()).thenReturn(targetStream);
when(connection.getResponseCode()).thenReturn(200);
when(connection.getHeaderFields()).thenReturn(null);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.ISO_8859_1);
assertThat(actual.getPayload(), is(equalTo("©")));
}

@Test
public void parseResponseWithExtendedCharacterSetUsingUtf8Charset() throws Exception {
InputStream targetStream = new ByteArrayInputStream("©".getBytes(StandardCharsets.UTF_8));
when(connection.getInputStream()).thenReturn(targetStream);
when(connection.getResponseCode()).thenReturn(200);
when(connection.getHeaderFields()).thenReturn(null);
NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.UTF_8);
assertThat(actual.getPayload(), is(equalTo("©")));
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<maven.compiler.plugin.version>3.6.0</maven.compiler.plugin.version>
<maven.surefire.plugin.version>2.9</maven.surefire.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nstest.version>1.1.9</nstest.version>
<nstest.version>1.1.10</nstest.version>
<testng.version>7.5</testng.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
Expand Down