cmailleux    2005/09/22 14:42:46 CEST

  Modified files:
    war/src/java/com/codeva/webapps/webclipping Rewriter.java 
    war/src/java/com/codeva/webapps/webclipping/servlet 
                                                        WebClippingServlet.java 
  Log:
  Add management of area tag
  Add more accurate error handling
  
  Revision  Changes    Path
  1.12      +97 -18    
webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java.diff?r1=1.11&r2=1.12&f=h
  1.16      +7 -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.15&r2=1.16&f=h
  
  
  
  Index: Rewriter.java
  ===================================================================
  RCS file: 
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Rewriter.java     4 Jul 2005 09:56:32 -0000       1.11
  +++ Rewriter.java     22 Sep 2005 12:42:45 -0000      1.12
  @@ -148,7 +148,7 @@
        * @param responseBody
        * @param request
        * @param response
  -     * @return
  +     * @return outpuDocument
        */
       public OutputDocument rewriteBody(String responseBody,
                                         HttpServletRequest request,
  @@ -171,6 +171,7 @@
           rewriteBackgroundAttribute(source, stringBuffer, document);
           rewriteFormTag(source, stringBuffer, request, response, document);
           rewriteATag(source, stringBuffer, request, response, document);
  +        rewriteAreaTag(source, stringBuffer, request, response, document);
           rewriteImgTag(source, stringBuffer, document);
           rewriteInputImageTag(source, stringBuffer, document);
           rewriteLinkTag(source, stringBuffer, document);
  @@ -196,11 +197,11 @@
               } else {
                   body = (StartTag) new 
Source(document.toString()).findAllStartTags(Tag.BODY).get(0);
               }
  -            buffer.append('<' + body.getName() + ' ');
  +            buffer.append('<').append(body.getName()).append(' ');
               Iterator attributes = body.getAttributes().iterator();
               while (attributes.hasNext()) {
                   Attribute attribute = (Attribute) attributes.next();
  -                buffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                
buffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
               }
               buffer.append('>');
               document.add(new StringOutputSegment(body, buffer.toString()));
  @@ -242,7 +243,7 @@
        * @param request
        * @param response
        * @param sourceUrl
  -     * @return
  +     * @return string
        */
       private String getRewritedUrl(HttpServletRequest request, 
HttpServletResponse response, String sourceUrl)
               throws MalformedURLException {
  @@ -319,7 +320,7 @@
                           while (atList.hasNext()) {
                               Attribute attribute = (Attribute) atList.next();
                               if 
(!"href".equalsIgnoreCase(attribute.getName())) {
  -                                stringBuffer.append(attribute.getName() + 
"=" + attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                               }
                           }
                       } else {
  @@ -329,7 +330,7 @@
                               Attribute attribute = (Attribute) atList.next();
                               if (!"href".equalsIgnoreCase(attribute.getKey()) 
&&
                                       
!"target".equalsIgnoreCase(attribute.getKey())) {
  -                                stringBuffer.append(attribute.getName() + 
"=" + attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                               }
                           }
                       }
  @@ -345,7 +346,7 @@
                           while (atList.hasNext()) {
                               Attribute attribute = (Attribute) atList.next();
                               if 
(!"href".equalsIgnoreCase(attribute.getName())) {
  -                                stringBuffer.append(attribute.getName() + 
"=" + attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                               }
                           }
                           stringBuffer.append('>');
  @@ -357,7 +358,85 @@
                           while (atList.hasNext()) {
                               Attribute attribute = (Attribute) atList.next();
                               if 
(!"target".equalsIgnoreCase(attribute.getName())) {
  -                                stringBuffer.append(attribute.getName() + 
"=" + attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
  +                            }
  +                        }
  +                        stringBuffer.append('>');
  +                        document.add(new StringOutputSegment(startTag,
  +                                stringBuffer.toString()));
  +                    }
  +                }
  +            }
  +        }
  +    }
  +
  +    private void rewriteAreaTag(Source source, StringBuffer stringBuffer, 
HttpServletRequest httpServletRequest,
  +                             HttpServletResponse response, OutputDocument 
document) throws MalformedURLException {
  +        List aStartTag = source.findAllStartTags(Tag.AREA);
  +        for (int i = 0; i < aStartTag.size(); i++) {
  +            StartTag startTag = (StartTag) aStartTag.get(i);
  +            Attributes attributes = startTag.getAttributes();
  +            Attribute href = attributes.get("href");
  +            if (href != null && href.getValue().length() > 0) {
  +                String hrefUrl = href.getValue().trim();
  +                log.debug("Now we must rewrite this anchor tag for url : " + 
hrefUrl);
  +                stringBuffer.setLength(0);
  +                if (!hrefUrl.toLowerCase().startsWith("http") && 
!hrefUrl.toLowerCase().startsWith("javascript")
  +                && !hrefUrl.toLowerCase().startsWith("mailto") && 
!hrefUrl.toLowerCase().startsWith("ftp")
  +                && !hrefUrl.toLowerCase().startsWith("news") && 
!hrefUrl.toLowerCase().startsWith("wais")
  +                && !hrefUrl.toLowerCase().startsWith("gopher")) {
  +                    Matcher matcher = pattern.matcher(hrefUrl);
  +                    if (!matcher.find()) {
  +                        String rewritedUrl = 
getRewritedUrl(httpServletRequest, response,
  +                                hrefUrl.replaceAll("\\?", "&"));
  +                        final Attribute target = attributes.get("target");
  +                        if (target != null && 
!target.getValue().equals("_self")) {
  +                            rewritedUrl = getAbsoluteURL(hrefUrl);
  +                        }
  +                        stringBuffer.append("<area 
href=\"").append(rewritedUrl).append("\" ");
  +                        Iterator atList = attributes.iterator();
  +                        while (atList.hasNext()) {
  +                            Attribute attribute = (Attribute) atList.next();
  +                            if 
(!"href".equalsIgnoreCase(attribute.getName())) {
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
  +                            }
  +                        }
  +                    } else {
  +                        stringBuffer.append("<area target=\"new\" 
href=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                        Iterator atList = attributes.iterator();
  +                        while (atList.hasNext()) {
  +                            Attribute attribute = (Attribute) atList.next();
  +                            if (!"href".equalsIgnoreCase(attribute.getKey()) 
&&
  +                                    
!"target".equalsIgnoreCase(attribute.getKey())) {
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
  +                            }
  +                        }
  +                    }
  +                    stringBuffer.append('>');
  +                    document.add(new StringOutputSegment(startTag,
  +                            stringBuffer.toString()));
  +                } else if (hrefUrl.toLowerCase().startsWith("http")) {
  +                    Matcher matcher = pattern.matcher(hrefUrl);
  +                    URL tmp = new URL(hrefUrl);
  +                    if (!matcher.find() && 
tmp.getHost().equalsIgnoreCase(urlProperties.getHost())) {
  +                        stringBuffer.append("<area 
href=\"").append(getRewritedUrl(httpServletRequest, response, 
hrefUrl)).append("\" ");
  +                        Iterator atList = attributes.iterator();
  +                        while (atList.hasNext()) {
  +                            Attribute attribute = (Attribute) atList.next();
  +                            if 
(!"href".equalsIgnoreCase(attribute.getName())) {
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
  +                            }
  +                        }
  +                        stringBuffer.append('>');
  +                        document.add(new StringOutputSegment(startTag,
  +                                stringBuffer.toString()));
  +                    } else {
  +                        stringBuffer.append("<area target=\"new\" ");
  +                        Iterator atList = attributes.iterator();
  +                        while (atList.hasNext()) {
  +                            Attribute attribute = (Attribute) atList.next();
  +                            if 
(!"target".equalsIgnoreCase(attribute.getName())) {
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                               }
                           }
                           stringBuffer.append('>');
  @@ -381,12 +460,12 @@
                       String hrefUrl = href.getValue().trim();
                       stringBuffer.setLength(0);
                       if (!hrefUrl.startsWith("http")) {
  -                        stringBuffer.append("<" + startTag.getName() + " 
background=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                        
stringBuffer.append("<").append(startTag.getName()).append(" 
background=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                           Iterator atList = attributes.iterator();
                           while (atList.hasNext()) {
                               Attribute attribute = (Attribute) atList.next();
                               if 
(!"background".equalsIgnoreCase(attribute.getName())) {
  -                                stringBuffer.append(attribute.getName() + 
"=" + attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                                
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                               }
                           }
                           stringBuffer.append('>');
  @@ -416,7 +495,7 @@
                       Attribute attribute = (Attribute) atList.next();
                       final String name = attribute.getName();
                       if (!"action".equalsIgnoreCase(name) && 
!"method".equalsIgnoreCase(name)) {
  -                        stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                        
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                       }
                   }
                   Attribute method = attributes.get("method");
  @@ -443,7 +522,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"src".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -470,7 +549,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"src".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -497,7 +576,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"src".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -524,7 +603,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"src".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -550,7 +629,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"href".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -577,7 +656,7 @@
                       while (atList.hasNext()) {
                           Attribute attribute = (Attribute) atList.next();
                           if (!"src".equalsIgnoreCase(attribute.getName())) {
  -                            stringBuffer.append(attribute.getName() + "=" + 
attribute.getQuoteChar() + attribute.getValue() + 
attribute.getQuoteChar()).append(' ');
  +                            
stringBuffer.append(attribute.getName()).append("=").append(attribute.getQuoteChar()).append(attribute.getValue()).append(attribute.getQuoteChar()).append('
 ');
                           }
                       }
                       stringBuffer.append('>');
  @@ -603,7 +682,7 @@
        *
        * @param document
        * @param request
  -     * @return
  +     * @return ouputDocument
        */
       private OutputDocument trunkDocument(OutputDocument document, 
HttpServletRequest request) {
           // Get the context id
  
  
  
  Index: WebClippingServlet.java
  ===================================================================
  RCS file: 
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/servlet/WebClippingServlet.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- WebClippingServlet.java   13 Jul 2005 14:56:36 -0000      1.15
  +++ WebClippingServlet.java   22 Sep 2005 12:42:45 -0000      1.16
  @@ -80,7 +80,7 @@
               //
           } catch (Throwable e) {
               perfLog.error("Error doGET", e);
  -            throw new ServletException("error during execution of get 
method");
  +            throw new ServletException("error during execution of get method 
: "+ e.getLocalizedMessage());
           }
       }
   
  @@ -110,7 +110,7 @@
               //
           } catch (Throwable e) {
               perfLog.error("Error doPOST", e);
  -            throw new ServletException("error during execution of get 
method");
  +            throw new ServletException("error during execution of post 
method : "+ e.getLocalizedMessage());
           }
       }
   
  @@ -504,6 +504,9 @@
           // Usually when the user performs an action in the same portlet
           //
           String siteUrl = request.getParameter(Rewriter.URL_PATH_PARAM_NAME);
  +        String headerReferer = request.getHeader("referer");
  +        if(headerReferer==null)
  +            headerReferer="";
           if (siteUrl != null && !"".equals(siteUrl.trim())) {
               perfLog.debug("->Request has URL " + contextId);
               //
  @@ -516,7 +519,7 @@
               if (accessibleHostsStr != null && 
!"".equals(accessibleHostsStr.trim())) {
                   map.put(MAP_ACCESSIBLE_HOSTS, 
Arrays.asList(accessibleHostsStr.split(",")));
               }
  -            map.put(MAP_REFERER,request.getHeader("referer"));
  +            map.put(MAP_REFERER,headerReferer);
               //
               return true;
           }
  @@ -527,7 +530,7 @@
               String referer = null;
               if(matcher.find())
                   mapReferer = matcher.group(2);
  -            matcher = pattern.matcher(request.getHeader("referer"));
  +            matcher = pattern.matcher(headerReferer);
               if(matcher.find())
                   referer = matcher.group(2);
               if(mapReferer==null || !mapReferer.equals(referer)) {
  

Reply via email to