Skip to content

Commit

Permalink
Fix character encoding issue when parsing custom HTTP responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Aug 22, 2024
1 parent c866fd4 commit c770f44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.smartregister.fhir.gateway.plugins.endpoint;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smartregister.fhir.gateway.plugins.RestUtils;

import com.google.fhir.gateway.TokenVerifier;
Expand All @@ -14,6 +20,8 @@
import ca.uhn.fhir.parser.IParser;

public abstract class BaseEndpoint extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(BaseEndpoint.class);

protected final TokenVerifier tokenVerifier = TokenVerifier.createFromEnvVars();
protected final FhirContext fhirR4Context = FhirContext.forR4();
protected final IParser fhirR4JsonParser = fhirR4Context.newJsonParser().setPrettyPrint(true);
Expand All @@ -24,4 +32,15 @@ protected void doOptions(HttpServletRequest request, HttpServletResponse respons
}

protected BaseEndpoint() throws IOException {}

protected void writeUTF8StringToStream(OutputStream fileOutputStream, String content) {
try (OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter)) {
printWriter.println(content);
printWriter.flush();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
resultContent = fhirR4JsonParser.encodeResourceToString(resultBundle);
}
response.setContentType("application/json");
response.getOutputStream().print(resultContent);
writeUTF8StringToStream(response.getOutputStream(), resultContent);
response.setStatus(HttpStatus.SC_OK);
} catch (AuthenticationException authenticationException) {
response.setContentType("application/json");
response.getOutputStream().print(authenticationException.getMessage());
writeUTF8StringToStream(
response.getOutputStream(), authenticationException.getMessage());
response.setStatus(authenticationException.getStatusCode());
} catch (Exception exception) {
response.setContentType("application/json");
response.getOutputStream().print(exception.getMessage());
writeUTF8StringToStream(response.getOutputStream(), exception.getMessage());
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
}

response.setContentType("application/json");
response.getOutputStream().print(resultContent);
writeUTF8StringToStream(response.getOutputStream(), resultContent);
response.setStatus(HttpStatus.SC_OK);
} catch (AuthenticationException authenticationException) {
response.setContentType("application/json");
response.getOutputStream().print(authenticationException.getMessage());
writeUTF8StringToStream(
response.getOutputStream(), authenticationException.getMessage());
response.setStatus(authenticationException.getStatusCode());
} catch (Exception exception) {
response.setContentType("application/json");
response.getOutputStream().print(exception.getMessage());
writeUTF8StringToStream(response.getOutputStream(), exception.getMessage());
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
Expand Down

0 comments on commit c770f44

Please sign in to comment.