cmailleux    2005/07/13 11:51:29 CEST

  Modified files:
    war/src/java/com/codeva/webapps/webclipping/servlet 
                                                        WebClippingServlet.java 
  Log:
  Correct encoding problem to ensure that we send data in the encoding expected 
by the clipped url
  
  Revision  Changes    Path
  1.14      +31 -4     
webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java.diff?r1=1.13&r2=1.14&f=h
  
  
  
  Index: WebClippingServlet.java
  ===================================================================
  RCS file: 
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- WebClippingServlet.java   20 May 2005 09:54:45 -0000      1.13
  +++ WebClippingServlet.java   13 Jul 2005 09:51:29 -0000      1.14
  @@ -30,6 +30,7 @@
       static private final String ORIGINAL_METHOD_PARAMS = "original_method";
       static private final String administrator_role = "administrator";
       static private final String httpClientName = 
"org.jahia.webapp.webclipping.servlet.htppClient";
  +    static private final String originalCharset = 
"org.jahia.webapp.webclipping.servlet.charset";
       static private final MultiThreadedHttpConnectionManager 
HTPP_CONNECTION_MANAGER = new MultiThreadedHttpConnectionManager();
   
       static private final String APPID_PARAMS = "appid";
  @@ -37,6 +38,7 @@
       static private final String RESET_PARAMS = "resetAppSession";
   
       static private final Logger perfLog = 
Logger.getLogger("com.codeva.perf");
  +    static private final Logger log = 
Logger.getLogger(WebClippingServlet.class);
       static private final int BUFFER_SIZE = 100 * 1024;
   
       static private final String MAP_SITE_URL = "siteURL";
  @@ -226,7 +228,10 @@
                   if 
(!Rewriter.URL_PATH_PARAM_NAME.equalsIgnoreCase(paramName) && 
!ORIGINAL_METHOD_PARAMS.equalsIgnoreCase(paramName) && 
!"ie".equalsIgnoreCase(paramName) && !APPID_PARAMS.equalsIgnoreCase(paramName) 
&& !APPPARAMS_PARAMS.equalsIgnoreCase(paramName)
                           && !RESET_PARAMS.equalsIgnoreCase(paramName) && 
!"matrix".equalsIgnoreCase(paramName)) {
                       final Object value = entry.getValue();
  -                    final String characterEncoding = 
(request.getCharacterEncoding() != null) ? request.getCharacterEncoding() : 
"UTF-8";
  +                    String characterEncoding = 
httpClient.getParams().getContentCharset();
  +                    String newContentCharset = (String) 
request.getSession().getAttribute(originalCharset+"_"+contextId);
  +                    if(newContentCharset!=null && 
!"".equals(newContentCharset))
  +                        characterEncoding = newContentCharset;
                       if (value instanceof String[]) {
                           String[] strings = (String[]) value;
                           StringBuffer buffer = new StringBuffer(4096);
  @@ -255,8 +260,13 @@
           HttpMethodBase httpMethod = new GetMethod(path);
           // Set a default retry handler (see httpclient doc).
           httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
new DefaultHttpMethodRetryHandler(3, false));
  +        String contentCharset = httpClient.getParams().getContentCharset();
  +        String newContentCharset = (String) 
request.getSession().getAttribute(originalCharset+"_"+contextId);
  +        if(newContentCharset!=null && !"".equals(newContentCharset))
  +        httpClient.getParams().setContentCharset(newContentCharset);
           // Get the response of the url in a string.
           getResponse(httpClient, httpMethod, response, request, map);
  +        httpClient.getParams().setContentCharset(contentCharset);
       }
   
       private void getURLContentWithPostMethod(HttpServletRequest request, 
HttpServletResponse response, Map map) throws IOException {
  @@ -282,13 +292,14 @@
           // Add parameters
           if (parameters != null) {
               Iterator iterator = parameters.entrySet().iterator();
  +            StringBuffer buffer = new StringBuffer(4096);
               while (iterator.hasNext()) {
                   Map.Entry entry = (Map.Entry) iterator.next();
                   if 
(!Rewriter.URL_PATH_PARAM_NAME.equalsIgnoreCase(entry.getKey().toString())) {
                       final Object value = entry.getValue();
                       if (value instanceof String[]) {
  +                        buffer.setLength(0);
                           String[] strings = (String[]) entry.getValue();
  -                        StringBuffer buffer = new StringBuffer(4096);
                           for (int i = 0; i < strings.length; i++) {
                               String string = strings[i];
                               buffer.append((i != 0) ? "," : 
"").append(string);
  @@ -301,7 +312,11 @@
               }
           }
           //
  +        String contentCharset = httpClient.getParams().getContentCharset();
  +        String newContentCharset = (String) 
request.getSession().getAttribute(originalCharset+"_"+contextId);
  +        httpClient.getParams().setContentCharset(newContentCharset);
           getResponse(httpClient, postMethod, response, request, map);
  +        httpClient.getParams().setContentCharset(contentCharset);
       }
   
       private HttpClient getHttpClient(HttpServletRequest request, String 
contextId) {
  @@ -310,7 +325,7 @@
               httpClient = new HttpClient(HTPP_CONNECTION_MANAGER);
               httpClient.getParams().setParameter("http.useragent", 
request.getHeader("User-Agent"));
               final String characterEncoding = request.getCharacterEncoding();
  -            
httpClient.getParams().setParameter("http.protocol.content-charset", 
(characterEncoding != null) ? characterEncoding : "UTF-8");
  +            httpClient.getParams().setContentCharset ((characterEncoding != 
null)? characterEncoding : "UTF-8");
               request.getSession().setAttribute(httpClientName + "_" + 
contextId, httpClient);
           }
           return httpClient;
  @@ -339,8 +354,13 @@
               httpMethod.getParams().setParameter("http.socket.timeout", 
aInteger);
               
httpMethod.getParams().setParameter("http.protocol.expect-continue", aBoolean);
               statusCode = httpClient.executeMethod(httpMethod);
  -
  +            if(log.isDebugEnabled()) {
  +                log.debug("Status code of request : "+statusCode+" for 
request "+map.get(MAP_SITE_URL));
  +            }
               if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode 
== HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_SEE_OTHER || 
statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
  +                if(log.isDebugEnabled()) {
  +                    log.debug("We follow a redirection ");
  +                }
                   String redirectLocation;
                   Header locationHeader = 
httpMethod.getResponseHeader("location");
                   if (locationHeader != null) {
  @@ -352,6 +372,9 @@
                           // Set a default retry handler (see httpclient doc).
                           
httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new 
DefaultHttpMethodRetryHandler(3, false));
                       }
  +                    if(log.isDebugEnabled()) {
  +                        log.debug("redirected to : "+redirectLocation);
  +                    }
                       int i = redirectLocation.indexOf("?");
                       httpMethod.setPath((redirectLocation.startsWith("/") ? 
"" : "/") + redirectLocation.substring(0, i > 0 ? i : 
redirectLocation.length()));
                       httpMethod.setQueryString(i > 0 ? 
redirectLocation.substring(i + 1, redirectLocation.length()) : "");
  @@ -363,6 +386,9 @@
   
               if (statusCode != HttpStatus.SC_OK) {
                   // status not ok so let us throw a beautiful exception
  +                if(log.isDebugEnabled()) {
  +                    log.debug("Status code not okay : "+statusCode+ " for 
request "+map.get(MAP_SITE_URL));
  +                }
                   StringBuffer buffer = new StringBuffer("<html>\n<body>");
                   buffer.append('\n' + "Error getting " + (String) 
map.get(MAP_SITE_URL) + " failed with error code " + statusCode);
                   buffer.append("\n</body>\n</html>");
  @@ -410,6 +436,7 @@
                       if (perfLog.isDebugEnabled()) {
                           perfLog.debug("###### Webclipping execution time 
before rewrting " + ((System.currentTimeMillis() - start) / 1000d));
                       }
  +                    
request.getSession().setAttribute(originalCharset+"_"+contextId,contentCharset.toUpperCase());
                       rewriteBody(new String(responseBodyAsBytes, 
contentCharset), request, response, map);
                   } else {
                       if (perfLog.isDebugEnabled()) {
  

Reply via email to