Skip to content

Commit

Permalink
fix: [SUP-1501] stop setting content-length explicitly to avoid trunc…
Browse files Browse the repository at this point in the history
…ating responses (#209)
  • Loading branch information
bstewart00 authored Jul 7, 2022
1 parent 3aad0a2 commit 1e71318
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ workflows:
version: 2
test:
jobs:
- oraclejdk8
# - oraclejdk8 TODO: Repos have been deprecated, commands no longer work
- openjdk8

jobs:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c

VERSION := 8
WOVN_VERSION := 1.14.0
WOVN_VERSION := 1.14.2
TARGET_DIR = ${PWD}
MAVEN = docker run -i --rm -v ${TARGET_DIR}:/project -v wovnjava-maven_repo:/root/.m2 -w /project maven:3-jdk-$(VERSION) mvn
WEBSITE_CONFIG_FILE = pom.xml
Expand Down
4 changes: 2 additions & 2 deletions docker/java8/hello/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<dependency>
<groupId>com.github.wovnio</groupId>
<artifactId>wovnjava</artifactId>
<version>1.14.0</version>
<version>1.14.2</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/wovnjava-1.14.0-jar-with-dependencies.jar</systemPath>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/wovnjava-1.14.2-jar-with-dependencies.jar</systemPath>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down
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>com.github.wovnio</groupId>
<artifactId>wovnjava</artifactId>
<name>wovnjava</name>
<version>1.14.0</version>
<version>1.14.2</version>
<url>https://github.com/WOVNio/wovnjava</url>

<licenses>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/github/wovnio/wovnjava/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ String translate(String lang, String html, HttpURLConnection con) throws ApiExce
ByteArrayOutputStream compressedBody = gzipStream(apiBodyBytes);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Encoding", "gzip");
con.setRequestProperty("Content-Length", String.valueOf(compressedBody.size()));
out = con.getOutputStream();
compressedBody.writeTo(out);
} else {
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Length", String.valueOf(apiBodyBytes.length));
out = con.getOutputStream();
out.write(apiBodyBytes);
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/github/wovnio/wovnjava/Diagnostics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.wovnio.wovnjava;

import java.nio.charset.Charset;
import java.util.Locale;

public class Diagnostics {
public static String getDiagnosticInfo() {
Locale locale = Locale.getDefault();
Charset charset = Charset.defaultCharset();

StringBuilder sb = new StringBuilder();
sb.append("**** Diagnostic info ****\n");
if (locale != null) {
sb.append("Locale: " + locale.toString() + "\n");
}
if (charset != null) {
sb.append("Default charset: " + charset.displayName() + "\n");
}
sb.append("**** End diagnostic info ****\n");

return sb.toString();
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/github/wovnio/wovnjava/RequestOptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.wovnio.wovnjava;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

Expand All @@ -24,17 +27,27 @@ class RequestOptions {
*/
private boolean debugMode;

private String encodingOverride;

RequestOptions(Settings settings, ServletRequest request) {
this.disableMode = false;
this.cacheDisableMode = false;
this.debugMode = false;
this.encodingOverride = null;

String query = ((HttpServletRequest)request).getQueryString();
if (query != null) {
this.disableMode = query.matches("(.*)wovnDisable(.*)");
if (settings.debugMode) {
this.cacheDisableMode = query.matches("(.*)wovnCacheDisable(.*)");
this.debugMode = query.matches("(.*)wovnDebugMode(.*)");

Pattern pattern = Pattern.compile("(.*)wovnEncodingOverride=([^&]*)");
Matcher m = pattern.matcher(query);
if (m.matches()) {
String encoding = m.group(2);
this.encodingOverride = encoding;
}
}
}
}
Expand All @@ -50,4 +63,8 @@ public boolean getCacheDisableMode() {
public boolean getDebugMode() {
return this.debugMode;
}

public String getEncodingOverride() {
return this.encodingOverride;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/github/wovnio/wovnjava/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Settings {

if (this.enableLogging) {
WovnLogger.enable();
WovnLogger.setDebugMode(this.debugMode);
}

this.widgetUrl = this.devMode ? stringOrDefault(reader.getStringParameter("widgetUrl"), DefaultWidgetUrlDevelopment) : stringOrDefault(reader.getStringParameter("widgetUrl"), DefaultWidgetUrlProduction);
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/com/github/wovnio/wovnjava/WovnLogger.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.github.wovnio.wovnjava;


import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

class WovnLogger {
private static Logger logger = Logger.getLogger("wovnLogger");
private static boolean enabled = false;
private static boolean isDebugMode = false;
private static String uuid = "NO_UUID";
private final static String prefix = "WOVN";

private static ArrayList<String> requestLogs;

public static void enable() {
WovnLogger.enabled = true;
}
Expand All @@ -18,9 +22,28 @@ public static void disable() {
WovnLogger.enabled = false;
}

public static void setDebugMode(boolean isDebugMode) {
WovnLogger.isDebugMode = isDebugMode;
}

public static void setUUID(String uuid) {
WovnLogger.uuid = uuid;
}
public static void clear() {
WovnLogger.requestLogs = new ArrayList<>();
}

public static String getRequestLogsHtmlComment() {
StringBuilder sb = new StringBuilder();
sb.append("<!--");
sb.append("\n");
for (int i = 0; i < WovnLogger.requestLogs.size(); i++) {
sb.append(WovnLogger.requestLogs.get(i));
sb.append("\n");
}
sb.append("-->");
return sb.toString();
}

public static String getUUID() {
return WovnLogger.uuid;
Expand All @@ -30,13 +53,21 @@ public static void log(String message, Exception e) {
if (!WovnLogger.enabled) {
return;
}
WovnLogger.logger.log(Level.INFO, String.format("[%s][%s] %s", WovnLogger.prefix, WovnLogger.uuid, message), e);
String formattedMessage = String.format("[%s][%s] %s", WovnLogger.prefix, WovnLogger.uuid, message);
if (WovnLogger.isDebugMode) {
WovnLogger.requestLogs.add(formattedMessage);
}
WovnLogger.logger.log(Level.INFO, formattedMessage, e);
}

public static void log(String message) {
if (!WovnLogger.enabled) {
return;
}
WovnLogger.logger.log(Level.INFO, String.format("[%s][%s] %s", WovnLogger.prefix, WovnLogger.uuid, message));
String formattedMessage = String.format("[%s][%s] %s", WovnLogger.prefix, WovnLogger.uuid, message);
if (WovnLogger.isDebugMode) {
WovnLogger.requestLogs.add(formattedMessage);
}
WovnLogger.logger.log(Level.INFO, formattedMessage);
}
}
30 changes: 23 additions & 7 deletions src/main/java/com/github/wovnio/wovnjava/WovnServletFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ public void init(FilterConfig config) throws ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
WovnLogger.setUUID(UUID.randomUUID().toString());
boolean isRequestAlreadyProcessed = false;
RequestOptions requestOptions = new RequestOptions(this.settings, request);

if (((HttpServletResponse)response).containsHeader("X-Wovn-Handler")) {
isRequestAlreadyProcessed = true;
WovnLogger.log("Request is already processed by WOVN.");
} else {
WovnLogger.clear();
if (requestOptions.getDebugMode()) {
WovnLogger.log(Diagnostics.getDiagnosticInfo());
}
((HttpServletResponse)response).setHeader("X-Wovn-Handler", "wovnjava_" + Settings.VERSION);
}

RequestOptions requestOptions = new RequestOptions(this.settings, request);
Headers headers = new Headers((HttpServletRequest)request, this.settings, this.urlLanguagePatternHandler);

boolean canTranslateRequest = !requestOptions.getDisableMode() &&
Expand All @@ -67,7 +71,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
if (headers.getIsPathInDefaultLanguage()) {
chain.doFilter(wovnRequest, response);
} else {
wovnRequest.getRequestDispatcher(headers.getCurrentContextUrlInDefaultLanguage().getPath()).forward(wovnRequest, response);
String newPath = headers.getCurrentContextUrlInDefaultLanguage().getPath();
WovnLogger.log("Forwarding to " + newPath);
wovnRequest.getRequestDispatcher(newPath).forward(wovnRequest, response);
}
}
}
Expand All @@ -78,15 +84,20 @@ public void destroy() {

private void tryTranslate(Headers headers, RequestOptions requestOptions, HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
WovnHttpServletRequest wovnRequest = new WovnHttpServletRequest(request, headers);
WovnHttpServletResponse wovnResponse = new WovnHttpServletResponse(response, headers, new Utf8(this.settings.encoding));

String overrideEncoding = requestOptions.getEncodingOverride();
String responseEncoding = overrideEncoding != null ? overrideEncoding : this.settings.encoding;
WovnHttpServletResponse wovnResponse = new WovnHttpServletResponse(response, headers, new Utf8(responseEncoding));

ResponseHeaders responseHeaders = new ResponseHeaders(response);
responseHeaders.setApiStatus("Unused");

if (headers.getIsPathInDefaultLanguage()) {
chain.doFilter(wovnRequest, wovnResponse);
} else {
wovnRequest.getRequestDispatcher(headers.getCurrentContextUrlInDefaultLanguage().getPath()).forward(wovnRequest, wovnResponse);
String newPath = headers.getCurrentContextUrlInDefaultLanguage().getPath();
WovnLogger.log("Forwarding to" + newPath);
wovnRequest.getRequestDispatcher(newPath).forward(wovnRequest, wovnResponse);
}

if (htmlChecker.isTextFileContentType(response.getContentType())) {
Expand All @@ -98,12 +109,17 @@ private void tryTranslate(Headers headers, RequestOptions requestOptions, HttpSe
Api api = new Api(settings, headers, requestOptions, responseHeaders);
Interceptor interceptor = new Interceptor(headers, settings, api, responseHeaders);
body = interceptor.translate(originalBody);


if (requestOptions.getDebugMode()) {
body += WovnLogger.getRequestLogsHtmlComment();
}
} else {
// css, javascript or others
body = originalBody;
}
wovnResponse.setContentLength(body.getBytes().length);
wovnResponse.setCharacterEncoding("utf-8");

wovnResponse.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(body);
out.close();
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/com/github/wovnio/wovnjava/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public static HttpServletResponse mockResponse(String contentType, String encodi

public static HttpServletResponse mockResponse(String contentType, String encoding, boolean isPreviouslyProcessed, int statusCode) throws IOException {
HttpServletResponse mock = EasyMock.createMock(HttpServletResponse.class);
mock.setContentLength(EasyMock.anyInt());
EasyMock.expectLastCall();
mock.setCharacterEncoding("utf-8");
mock.setCharacterEncoding("UTF-8");
EasyMock.expectLastCall();
EasyMock.expect(mock.getWriter()).andReturn(new PrintWriter(new StringWriter()));
EasyMock.expect(mock.getContentType()).andReturn(contentType).atLeastOnce();
Expand Down

0 comments on commit 1e71318

Please sign in to comment.