From dc639f61a314ff0bfc98203c3e521ca70f16d16a Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Wed, 5 Jun 2013 18:52:15 +0200 Subject: [PATCH] Encode correctly http parameters If the value of the parameters had URI reserved characters, they must be encoded. URI builder handles that. --- .../org/libreplan/web/print/CutyPrint.java | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java b/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java index 11c65b5dd..effe95270 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java @@ -42,6 +42,7 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.UriBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -224,7 +225,29 @@ public class CutyPrint { }); String pageToSnapshot = CallbackServlet.registerAndCreateURLFor( request, snapshotRequestHandler); - return createCaptureURL(request, printParameters, pageToSnapshot); + return createCaptureURL(pageToSnapshot); + } + + private String createCaptureURL(String capturePath) { + String hostName = resolveLocalHost(); + String uri = String.format("%s://%s:%s", request.getScheme(), + hostName, request.getLocalPort()); + UriBuilder result = UriBuilder.fromUri(uri).path(capturePath); + + for (Entry entry : printParameters.entrySet()) { + result = result.queryParam(entry.getKey(), entry.getValue()); + } + return result.build().toASCIIString(); + } + + private String resolveLocalHost() { + try { + InetAddress host = InetAddress + .getByName(request.getLocalName()); + return host.getHostName(); + } catch (UnknownHostException e) { + throw new RuntimeException(e); + } } private int buildMinWidthParam() { @@ -317,32 +340,6 @@ public class CutyPrint { + extension; } - private static String createCaptureURL(HttpServletRequest request, - Map parameters, String capturePath) { - // Add capture destination callback URL - String hostName = resolveLocalHost(request); - String result = request.getScheme() + "://" + hostName - + ":" + request.getLocalPort() + capturePath; - if (parameters != null) { - result += "?"; - for (String key : parameters.keySet()) { - result += key + "=" + parameters.get(key) + "&"; - } - result = result.substring(0, - (result.length() - 1)); - } - return result; - } - - private static String resolveLocalHost(HttpServletRequest request) { - try { - InetAddress host = InetAddress.getByName(request.getLocalName()); - return host.getHostName(); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } - } - private static int calculatePlannerWidthForPrintingScreen(Planner planner, int minWidthForTaskNameColumn) { if (planner != null && planner.getTimeTracker() != null) {