cmailleux    2005/05/09 15:37:35 CEST

  Modified files:
    war/src/java/com/codeva/webapps/webclipping/servlet 
                                                        WebClippingServlet.java 
    war/src/java/com/codeva/webapps/webclipping Rewriter.java 
    war/src/webapp       init.jsp 
  Log:
  We can manage the cache delay for the clipping

  and an host exclusion list
  
  Revision  Changes    Path
  1.9       +50 -38    
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.8&r2=1.9&f=h
  1.9       +129 -101  
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.8&r2=1.9&f=h
  1.3       +48 -2     webclipping/war/src/webapp/init.jsp
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclipping/war/src/webapp/init.jsp.diff?r1=1.2&r2=1.3&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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- WebClippingServlet.java   2 May 2005 10:22:00 -0000       1.8
  +++ WebClippingServlet.java   9 May 2005 13:37:34 -0000       1.9
  @@ -14,9 +14,7 @@
   import java.io.IOException;
   import java.net.URL;
   import java.net.URLEncoder;
  -import java.util.Iterator;
  -import java.util.List;
  -import java.util.Map;
  +import java.util.*;
   import java.util.prefs.BackingStoreException;
   import java.util.prefs.Preferences;
   
  @@ -30,7 +28,7 @@
   
   public class WebClippingServlet extends HttpServlet {
       private String url = "http://localhost:8080/";;
  -
  +    private List accessibleHost = null;
       private String httpClientName = 
"org.jahia.webapp.webclipping.servlet.htppClient";
       private static final String administrator_role = "administrator";
       static final String ORIGINAL_METHOD_PARAMS = "original_method";
  @@ -40,6 +38,10 @@
       private static final MultiThreadedHttpConnectionManager 
HTPP_CONNECTION_MANAGER = new MultiThreadedHttpConnectionManager();
   
       public WebClippingServlet() {
  +        System.setProperty("org.apache.commons.logging.Log", 
"org.apache.commons.logging.impl.SimpleLog");
  +        
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
  +        
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", 
"debug");
  +        
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
 "debug");
       }
   
       /**
  @@ -47,7 +49,6 @@
        *
        * @param request
        * @param response
  -     *
        * @throws ServletException
        * @throws IOException
        */
  @@ -57,6 +58,7 @@
           try {
               // Get the operationnal mode of jahia
               String opMode = (String) 
request.getAttribute("org.jahia.operationmode");
  +            String contextId = (String) 
request.getAttribute("org.portletapi.contextid");
               // Check if we are already initialized
               if (checkInit(request)) {
                   // do we come from an already clipped url ?
  @@ -67,6 +69,19 @@
                   } else {
                       path = url;
                   }
  +                if (accessibleHost != null) {
  +                    URL url = new URL(path);
  +                    if (!accessibleHost.contains(url.getHost())) {
  +                        if 
(request.isUserInRole(WebClippingServlet.administrator_role) && 
"edit".equals(opMode)) {
  +                            Preferences preferences = 
Preferences.systemNodeForPackage(Rewriter.class);
  +                            preferences.put("list_accessible_host_" + 
contextId, "");
  +                            preferences.put("reinit_" + contextId, 
String.valueOf(true));
  +                            response.sendRedirect(request.getContextPath() + 
"/init.jsp?reinit=true");
  +                            return;
  +                        } else
  +                            throw new ServletException("This host " + 
url.getHost() + " is not authorized into this webclipping instance");
  +                    }
  +                }
                   // get the content of the url and rewrite it
                   getURLContentWithGetMethod(request, path, response);
               } else if 
(request.isUserInRole(WebClippingServlet.administrator_role) && 
"edit".equals(opMode)) {
  @@ -89,7 +104,6 @@
        * @param httpServletRequest
        * @param path
        * @param httpServletResponse
  -     *
        * @throws IOException
        */
       private void getURLContentWithGetMethod(HttpServletRequest 
httpServletRequest,
  @@ -97,8 +111,11 @@
                                               HttpServletResponse 
httpServletResponse)
               throws IOException {
           // Get the httpClient
  -        HttpClient httpClient = getHttpClient(httpServletRequest);
  -        
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay", 
new Long(0));
  +        String contextId = (String) 
httpServletRequest.getAttribute("org.portletapi.contextid");
  +        HttpClient httpClient = getHttpClient(httpServletRequest, contextId);
  +        Preferences preferences = 
Preferences.systemNodeForPackage(Rewriter.class);
  +        Long aLong = new Long(preferences.get("cache_delay_" + contextId, 
"0"));
  +        
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay", 
aLong);
           // Create a get method for accessing the url.
           StringBuffer params = new StringBuffer(4096);
           Map parameters = httpServletRequest.getParameterMap();
  @@ -109,12 +126,12 @@
               final String paramName = entry.getKey().toString();
               // Is not a jahia params so pass it to the url
               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)) {
  +                    !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 = 
(httpServletRequest.getCharacterEncoding() != null) ? 
httpServletRequest.getCharacterEncoding() : "UTF-8";
                   if (value instanceof String[]) {
  @@ -125,11 +142,11 @@
                           buffer.append((i != 0) ? "," : "").append(string);
                       }
                       params.append((index == 0 ? "?" : "&") + 
entry.getKey().toString() + "=" +
  -                                  URLEncoder.encode(buffer.toString(), 
characterEncoding));
  +                            URLEncoder.encode(buffer.toString(), 
characterEncoding));
                       index++;
                   } else {
                       params.append((index == 0 ? "?" : "&") + 
entry.getKey().toString() + "=" +
  -                                  URLEncoder.encode(value.toString(), 
characterEncoding));
  +                            URLEncoder.encode(value.toString(), 
characterEncoding));
                       index++;
                   }
               }
  @@ -139,20 +156,20 @@
   
           // Set a default retry handler (see httpclient doc).
           httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  -                                            new 
DefaultHttpMethodRetryHandler(3, false));
  +                new DefaultHttpMethodRetryHandler(3, false));
           // Get the response of the url in a string.
           getResponse(httpClient, httpMethod, httpServletResponse, 
httpServletRequest);
       }
   
  -    private HttpClient getHttpClient(HttpServletRequest httpServletRequest) {
  -        HttpClient httpClient = (HttpClient) 
httpServletRequest.getSession(true).getAttribute(httpClientName);
  +    private HttpClient getHttpClient(HttpServletRequest httpServletRequest, 
String contextId) {
  +        HttpClient httpClient = (HttpClient) 
httpServletRequest.getSession(true).getAttribute(httpClientName + "_" + 
contextId);
           if (httpClient == null) {
               httpClient = new HttpClient(HTPP_CONNECTION_MANAGER);
               httpClient.getParams().setParameter("http.useragent", 
httpServletRequest.getHeader("User-Agent"));
               final String characterEncoding = 
httpServletRequest.getCharacterEncoding();
               
httpClient.getParams().setParameter("http.protocol.content-charset",
  -                                                (characterEncoding != null) 
? characterEncoding : "UTF-8");
  -            httpServletRequest.getSession().setAttribute(httpClientName, 
httpClient);
  +                    (characterEncoding != null) ? characterEncoding : 
"UTF-8");
  +            httpServletRequest.getSession().setAttribute(httpClientName + 
"_" + contextId, httpClient);
           }
           return httpClient;
       }
  @@ -161,7 +178,10 @@
                             HttpServletResponse httpServletResponse)
               throws ServletException, IOException {
           try {
  -            
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay", 
new Long(0));
  +            Preferences preferences = 
Preferences.systemNodeForPackage(Rewriter.class);
  +            String contextId = (String) 
httpServletRequest.getAttribute("org.portletapi.contextid");
  +            Long aLong = new Long(preferences.get("cache_delay_" + 
contextId, "0"));
  +            
httpServletRequest.setAttribute("org.jahia.portletapi.CacheExpirationDelay", 
aLong);
               String opMode = (String) 
httpServletRequest.getAttribute("org.jahia.operationmode");
               if (checkInit(httpServletRequest)) {
                   // Reconstruct the url
  @@ -172,6 +192,12 @@
                   } else {
                       path = url;
                   }
  +                if (accessibleHost != null) {
  +                    URL url = new URL(path);
  +                    if (!accessibleHost.contains(url.getHost())) {
  +                        throw new ServletException("This Host is not 
authorized into this webclipping instance");
  +                    }
  +                }
                   final String original_method = 
httpServletRequest.getParameter(ORIGINAL_METHOD_PARAMS);
                   if (original_method != null && 
!"get".equalsIgnoreCase(original_method)) {
                       getURLContentWithPostMethod(httpServletRequest, path, 
httpServletResponse);
  @@ -181,8 +207,7 @@
               } else if 
(httpServletRequest.isUserInRole(WebClippingServlet.administrator_role) && 
"edit".equals(opMode)) {
                   
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + 
"/init.jsp");
               } else {
  -                httpServletResponse.getOutputStream().println(
  -                        "This webapp has not been initialized by an 
administrator");
  +                httpServletResponse.getOutputStream().println("This webapp 
has not been initialized by an administrator");
               }
           } catch (BackingStoreException e) {
               throw new ServletException(e);
  @@ -191,10 +216,11 @@
   
       private void getURLContentWithPostMethod(HttpServletRequest 
httpServletRequest, String path,
                                                HttpServletResponse 
httpServletResponse) throws IOException {
  -        HttpClient httpClient = getHttpClient(httpServletRequest);
  +        String contextId = (String) 
httpServletRequest.getAttribute("org.portletapi.contextid");
  +        HttpClient httpClient = getHttpClient(httpServletRequest, contextId);
           PostMethod postMethod = new PostMethod(path);
           postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  -                                            new 
DefaultHttpMethodRetryHandler(3, false));
  +                new DefaultHttpMethodRetryHandler(3, false));
           Map parameters = httpServletRequest.getParameterMap();
           Iterator iterator = parameters.entrySet().iterator();
           while (iterator.hasNext()) {
  @@ -207,7 +233,7 @@
                       buffer.append((i != 0) ? "," : "").append(string);
                   }
                   postMethod.addParameter(entry.getKey().toString(),
  -                                        buffer.toString());
  +                        buffer.toString());
               }
           }
           getResponse(httpClient, postMethod, httpServletResponse, 
httpServletRequest);
  @@ -220,7 +246,6 @@
        * @param httpClient
        * @param httpMethod
        * @param httpServletResponse
  -     *
        * @throws IOException
        */
       private void getResponse(HttpClient httpClient, HttpMethodBase 
httpMethod,
  @@ -230,89 +255,91 @@
           int statusCode = 0;
           try {
               statusCode = httpClient.executeMethod(httpMethod);
  -        } catch (IOException e) {
  -            StringBuffer buffer = new StringBuffer("<html>\n<body>Error 
("+e.getClass().getName()+") during content retrieving of url ");            
  -            buffer.append("\n</body>\n</html>");
  -            rewriteBody(buffer.toString(), httpServletRequest, 
httpServletResponse);
  -            return;
  -        }
  -        if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY ||
  -            statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
  -            statusCode == HttpStatus.SC_SEE_OTHER ||
  -            statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
  -            String redirectLocation;
  -            Header locationHeader = httpMethod.getResponseHeader("location");
  -            if (locationHeader != null) {
  -                redirectLocation = locationHeader.getValue();
  -                if (!redirectLocation.startsWith("http")) {
  -                    URL redirectURL = new URL(url);
  -                    String tmpURL = redirectURL.getProtocol() + "://" + 
redirectURL.getHost() +
  -                                    ((redirectURL.getPort() > 0) ? ":" + 
redirectURL.getPort() : "") +
  -                                    "/" +
  -                                    redirectLocation;
  -                    httpMethod = new GetMethod(tmpURL);
  -                    // Set a default retry handler (see httpclient doc).
  -                    
httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  -                                                        new 
DefaultHttpMethodRetryHandler(3, false));
  +
  +            if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY ||
  +                    statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
  +                    statusCode == HttpStatus.SC_SEE_OTHER ||
  +                    statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
  +                String redirectLocation;
  +                Header locationHeader = 
httpMethod.getResponseHeader("location");
  +                if (locationHeader != null) {
  +                    redirectLocation = locationHeader.getValue();
  +                    if (!redirectLocation.startsWith("http")) {
  +                        URL redirectURL = new URL(url);
  +                        String tmpURL = redirectURL.getProtocol() + "://" + 
redirectURL.getHost() +
  +                                ((redirectURL.getPort() > 0) ? ":" + 
redirectURL.getPort() : "") +
  +                                "/" +
  +                                redirectLocation;
  +                        httpMethod = new GetMethod(tmpURL);
  +                        // Set a default retry handler (see httpclient doc).
  +                        
httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  +                                new DefaultHttpMethodRetryHandler(3, false));
  +                    }
  +                    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()) : "");
  +                    httpMethod.setFollowRedirects(true);
  +                    getResponse(httpClient, httpMethod, httpServletResponse, 
httpServletRequest);
  +                    return;
                   }
  -                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()):"");
  -                httpMethod.setFollowRedirects(true);
  -                getResponse(httpClient, httpMethod, httpServletResponse, 
httpServletRequest);
  +            }
  +            if (statusCode != HttpStatus.SC_OK) {
  +                // status not ok so let us throw a beautiful exception
  +                StringBuffer buffer = new StringBuffer("<html>\n<body>");
  +                buffer.append('\n' + "Error getting " + url + " failed with 
error code " + statusCode);
  +                buffer.append("\n</body>\n</html>");
  +                rewriteBody(buffer.toString(), httpServletRequest, 
httpServletResponse);
                   return;
               }
  -        }
  -        if (statusCode != HttpStatus.SC_OK) {
  -            // status not ok so let us throw a beautiful exception
  -            StringBuffer buffer = new StringBuffer("<html>\n<body>");
  -            buffer.append('\n' + "Error getting " + url + " failed with 
error code " + statusCode);
  -            buffer.append("\n</body>\n</html>");
  -            rewriteBody(buffer.toString(), httpServletRequest, 
httpServletResponse);
  -            return;
  -        }
  -        // return the body as a string
  -        String[] type = 
httpMethod.getResponseHeader("Content-Type").getValue().split(";");
  -        String contentType = type[0];
  -        String contentCharset = "ISO-8859-1";
  -        if (type.length == 2) {
  -            contentCharset = type[1].split("=")[1];
  -        }
  -        httpServletResponse.setContentType(contentType);
  -        url = httpMethod.getURI().getURIReference();
  -        final byte[] responseBodyAsBytes = httpMethod.getResponseBody();
  -        if (contentType.startsWith("text")) {
  -            if (type.length == 1) {
  -                String responseBody = new String(responseBodyAsBytes, 
"US-ASCII");
  -                Source source = new Source(responseBody);
  -                List list = source.findAllStartTags(Tag.META);
  -                for (int i = 0; i < list.size(); i++) {
  -                    StartTag startTag = (StartTag) list.get(i);
  -                    Attributes attributes = startTag.getAttributes();
  -                    final Attribute attribute = attributes.get("http-equiv");
  -                    if (attribute != null && 
attribute.getValue().equalsIgnoreCase("content-type")) {
  -                        type = 
attributes.get("content").getValue().split(";");
  -                        if (type.length == 2) {
  -                            contentCharset = type[1].split("=")[1];
  +            // return the body as a string
  +            String[] type = 
httpMethod.getResponseHeader("Content-Type").getValue().split(";");
  +            String contentType = type[0];
  +            String contentCharset = "ISO-8859-1";
  +            if (type.length == 2) {
  +                contentCharset = type[1].split("=")[1];
  +            }
  +            httpServletResponse.setContentType(contentType);
  +            url = httpMethod.getURI().getURIReference();
  +            final byte[] responseBodyAsBytes = httpMethod.getResponseBody();
  +            if (contentType.startsWith("text")) {
  +                if (type.length == 1) {
  +                    String responseBody = new String(responseBodyAsBytes, 
"US-ASCII");
  +                    Source source = new Source(responseBody);
  +                    List list = source.findAllStartTags(Tag.META);
  +                    for (int i = 0; i < list.size(); i++) {
  +                        StartTag startTag = (StartTag) list.get(i);
  +                        Attributes attributes = startTag.getAttributes();
  +                        final Attribute attribute = 
attributes.get("http-equiv");
  +                        if (attribute != null && 
attribute.getValue().equalsIgnoreCase("content-type")) {
  +                            type = 
attributes.get("content").getValue().split(";");
  +                            if (type.length == 2) {
  +                                contentCharset = type[1].split("=")[1];
  +                            }
                           }
  -                    }
   
  +                    }
                   }
  +                rewriteBody(new String(responseBodyAsBytes, contentCharset), 
httpServletRequest, httpServletResponse);
  +            } else {
  +                
httpServletResponse.getOutputStream().write(responseBodyAsBytes);
               }
  -            rewriteBody(new String(responseBodyAsBytes, contentCharset), 
httpServletRequest, httpServletResponse);
  -        } else {
  -            httpServletResponse.getOutputStream().write(responseBodyAsBytes);
  +        } catch (IOException e) {
  +            StringBuffer buffer = new StringBuffer("<html>\n<body>Error (" + 
e.getClass().getName() + ") during content retrieving of url ");
  +            buffer.append("\n</body>\n</html>");
  +            rewriteBody(buffer.toString(), httpServletRequest, 
httpServletResponse);
  +            return;
  +        } finally {
  +            httpMethod.releaseConnection();
           }
       }
   
       private void rewriteBody(String responseBody, HttpServletRequest 
httpServletRequest,
                                HttpServletResponse httpServletResponse) throws 
IOException {
           OutputDocument document = new Rewriter(url).rewriteBody(responseBody,
  -                                                                
httpServletRequest,
  -                                                                
httpServletResponse);
  +                httpServletRequest,
  +                httpServletResponse);
           final String characterEncoding = 
httpServletRequest.getCharacterEncoding();
  -        httpServletResponse.getOutputStream().write(
  -                
document.toString().getBytes(httpServletResponse.getCharacterEncoding()));
  +        
httpServletResponse.getOutputStream().write(document.toString().getBytes(httpServletResponse.getCharacterEncoding()));
       }
   
   
  @@ -320,16 +347,13 @@
        * Check if we are initialized.
        *
        * @param request
  -     *
        * @return
  -     *
        * @throws BackingStoreException
        */
       private boolean checkInit(HttpServletRequest request)
               throws BackingStoreException {
           // Do we have to reinit ?
  -        boolean reinit = Boolean.valueOf(request.getParameter("reinit") != 
null ? request.getParameter("reinit") : String.valueOf(
  -                false)).booleanValue();
  +        boolean reinit = Boolean.valueOf(request.getParameter("reinit") != 
null ? request.getParameter("reinit") : String.valueOf(false)).booleanValue();
           // Get the system preferences for this webclip
           Preferences preferences = 
Preferences.systemNodeForPackage(Rewriter.class);
           // Get the contextid of this webclip to avoid conflict
  @@ -339,6 +363,10 @@
               String siteUrl = preferences.get("site_url_" + contextId, "");
               if (!"".equals(siteUrl.trim())) {
                   url = siteUrl;
  +                String accessibleHostsStr = 
preferences.get("list_accessible_host_" + contextId, "");
  +                if (!"".equals(accessibleHostsStr.trim())) {
  +                    accessibleHost = 
Arrays.asList(accessibleHostsStr.split(","));
  +                }
                   return true;
               } else {
                   return false;
  
  
  
  Index: Rewriter.java
  ===================================================================
  RCS file: 
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Rewriter.java     2 May 2005 10:22:00 -0000       1.8
  +++ Rewriter.java     9 May 2005 13:37:35 -0000       1.9
  @@ -35,8 +35,8 @@
           StringBuffer buffer = new StringBuffer(256);
           buffer.append("<a href=\"");
           buffer.append(response.encodeURL(httpServletRequest.getContextPath() 
+
  -                                         httpServletRequest.getServletPath() 
+
  -                                         "?reinit=true"));
  +                httpServletRequest.getServletPath() +
  +                "?reinit=true"));
           buffer.append("\">Reinit</a>\n<br>");
           return buffer;
       }
  @@ -59,18 +59,29 @@
               if (end < body.getBegin()) {
                   String s = outputDocument.toString();
                   outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(
  -                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  -                                                                           
'\n').append(s.substring(begin, end)).toString()));
  +                        new 
StringBuffer(4096).append(s.substring(body.getBegin(), 
body.getEnd())).append('\n').append(s.substring(begin, end)).toString()));
                   s = outputDocument.toString();
  -                outputDocument = new OutputDocument(new 
StringBuffer(100000).append(s.substring(0, begin)).append(
  -                        s.substring(end, s.length())).toString());
  +                outputDocument = new OutputDocument(new 
StringBuffer(100000).append(s.substring(0, begin)).append(s.substring(end, 
s.length())).toString());
                   // Now we have changed the document so we must restart the 
list
                   i++;
                   source = new Source(outputDocument.toString());
                   scripts = source.findAllStartTags(Tag.SCRIPT);
               }
           }
  +        source = new Source(outputDocument.toString());
  +        body = (StartTag) source.findAllStartTags(Tag.BODY).get(0);
  +        Attributes attributes = body.getAttributes();
  +        if (attributes != null && attributes.size() > 0) {
  +            Attribute attribute = attributes.get("onload");
  +            if (attribute != null) {
  +                String value = attribute.getValue();
  +                if (body.findEndTag() != null) {
  +                    int start = body.findEndTag().getBegin();
  +                    String s = outputDocument.toString();
  +                    outputDocument.add(new StringOutputSegment(start, start, 
"<script>" + value + "</script>"));
  +                }
  +            }
  +        }
           return outputDocument;
       }
   
  @@ -91,21 +102,28 @@
                   StartTag body = (StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0);
                   String s = outputDocument.toString();
                   outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(
  -                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  -                                                                           
'\n').append(text.substring(begin, end)).toString()));
  +                        new 
StringBuffer(4096).append(s.substring(body.getBegin(), 
body.getEnd())).append('\n').append(text.substring(begin, end)).toString()));
                   outputDocument = new 
OutputDocument(outputDocument.toString());
               } else if (begin > endPos) {
                   // This script isn't enclose in the trunked tag so we must 
move it to ensure everything working
                   EndTag body = ((StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0)).findEndTag();
                   String s = outputDocument.toString();
                   outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(text.substring(begin, end)).append(
  -                                                                   
'\n').append(
  -                                                                           
s.substring(body.getBegin(), body.getEnd())).toString()));
  +                        new StringBuffer(4096).append(text.substring(begin, 
end)).append('\n').append(s.substring(body.getBegin(), 
body.getEnd())).toString()));
                   outputDocument = new 
OutputDocument(outputDocument.toString());
               }
           }
  +        Source source = new Source(outputDocument.toString());
  +        StartTag body = (StartTag) origin.findAllStartTags(Tag.BODY).get(0);
  +        Attributes attributes = body.getAttributes();
  +        if (attributes != null && attributes.size() > 0) {
  +            Attribute attribute = attributes.get("onload");
  +            if (attribute != null) {
  +                String value = attribute.getValue();
  +                int start = ((StartTag) 
source.findAllStartTags().get(0)).findEndTag().getBegin();
  +                outputDocument.add(new StringOutputSegment(start, start, 
"<script>" + value + "</script>"));
  +            }
  +        }
           return outputDocument;
       }
   
  @@ -128,7 +146,6 @@
        * @param responseBody
        * @param request
        * @param response
  -     *
        * @return
        */
       public OutputDocument rewriteBody(String responseBody,
  @@ -183,16 +200,16 @@
   
       private String getAbsoluteURL(String hrefUrl) throws 
MalformedURLException {
           final String s = urlProperties.getProtocol() + "://" + 
urlProperties.getHost() +
  -                         ((urlProperties.getPort() >=
  -                           0) ? ":" +
  -                                urlProperties.getPort() : "");
  +                ((urlProperties.getPort() >=
  +                0) ? ":" +
  +                urlProperties.getPort() : "");
           int endIndex = urlProperties.getPath().lastIndexOf("/");
           if (endIndex < 0) {
               final int i = urlProperties.getPath().length();
               endIndex = (i > 0) ? i : 0;
           }
           String absoluteUrl = s + urlProperties.getPath().substring(0, 
endIndex) + '/' +
  -                             hrefUrl;
  +                hrefUrl;
           if (hrefUrl.trim().length() > 0) {
               if (hrefUrl.startsWith("//")) {
                   // We have a net_path accordind to RFC_2396 definig URI
  @@ -216,13 +233,12 @@
        * @param request
        * @param response
        * @param sourceUrl
  -     *
        * @return
        */
       private String getRewritedUrl(HttpServletRequest request, 
HttpServletResponse response, String sourceUrl)
               throws MalformedURLException {
           final String rewritedUrl = request.getContextPath() + 
request.getServletPath() + '?' +
  -                                   Rewriter.URL_PATH_PARAM_NAME + '=' + 
getAbsoluteURL(sourceUrl);
  +                Rewriter.URL_PATH_PARAM_NAME + '=' + 
getAbsoluteURL(sourceUrl);
           log.debug("original url = " + sourceUrl + " rewrited to " + 
rewritedUrl);
           return response.encodeURL(rewritedUrl);
       }
  @@ -281,7 +297,7 @@
                       Matcher matcher = pattern.matcher(hrefUrl);
                       if (!matcher.find()) {
                           String rewritedUrl = 
getRewritedUrl(httpServletRequest, response,
  -                                                            
hrefUrl.replaceAll("\\?", "&"));
  +                                hrefUrl.replaceAll("\\?", "&"));
                           final Attribute target = attributes.get("target");
                           if (target != null && 
!target.getValue().equals("_self")) {
                               rewritedUrl = getAbsoluteURL(hrefUrl);
  @@ -300,19 +316,18 @@
                           for (int j = 0; j < atList.size(); j++) {
                               Attribute attribute = (Attribute) atList.get(j);
                               if 
(!"href".equalsIgnoreCase(attribute.getName()) &&
  -                                
!"target".equalsIgnoreCase(attribute.getName())) {
  +                                    
!"target".equalsIgnoreCase(attribute.getName())) {
                                   
stringBuffer.append(attribute.getSourceText()).append(' ');
                               }
                           }
                       }
                       stringBuffer.append('>');
                       document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                            stringBuffer.toString()));
                   } else if (hrefUrl.toLowerCase().startsWith("http")) {
                       URL tmp = new URL(hrefUrl);
                       if 
(tmp.getHost().equalsIgnoreCase(urlProperties.getHost())) {
  -                        stringBuffer.append("<a 
href=\"").append(getRewritedUrl(httpServletRequest, response, hrefUrl)).append(
  -                                "\" ");
  +                        stringBuffer.append("<a 
href=\"").append(getRewritedUrl(httpServletRequest, response, 
hrefUrl)).append("\" ");
                           List atList = attributes.getList();
                           for (int j = 0; j < atList.size(); j++) {
                               Attribute attribute = (Attribute) atList.get(j);
  @@ -322,7 +337,7 @@
                           }
                           stringBuffer.append('>');
                           document.add(new StringOutputSegment(startTag,
  -                                                             
stringBuffer.toString()));
  +                                stringBuffer.toString()));
                       } else {
                           stringBuffer.append("<a target=\"new\" ");
                           List atList = attributes.getList();
  @@ -334,7 +349,7 @@
                           }
                           stringBuffer.append('>');
                           document.add(new StringOutputSegment(startTag,
  -                                                             
stringBuffer.toString()));
  +                                stringBuffer.toString()));
                       }
                   }
               }
  @@ -353,8 +368,7 @@
                       String hrefUrl = href.getValue().trim();
                       stringBuffer.setLength(0);
                       if (!hrefUrl.startsWith("http")) {
  -                        stringBuffer.append("<" + startTag.getName() + " 
background=\"").append(
  -                                getAbsoluteURL(hrefUrl)).append("\" ");
  +                        stringBuffer.append("<" + startTag.getName() + " 
background=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                           List atList = attributes.getList();
                           for (int j = 0; j < atList.size(); j++) {
                               Attribute attribute = (Attribute) atList.get(j);
  @@ -383,7 +397,7 @@
                   String actionUrl = action.getValue();
                   stringBuffer.setLength(0);
                   stringBuffer.append("<form 
action=\"").append(getRewritedUrl(httpServletRequest, response,
  -                                                                             
actionUrl)).append("\" ");
  +                        actionUrl)).append("\" ");
                   List atList = attributes.getList();
                   for (int j = 0; j < atList.size(); j++) {
                       Attribute attribute = (Attribute) atList.get(j);
  @@ -394,8 +408,7 @@
                   }
                   Attribute method = attributes.get("method");
                   stringBuffer.append("method=\"post\">\n");
  -                stringBuffer.append("<input type=\"hidden\" 
name=\"original_method\" value=\"").append(
  -                        (method != null) ? method.getValue() : 
"get").append("\">");
  +                stringBuffer.append("<input type=\"hidden\" 
name=\"original_method\" value=\"").append((method != null) ? method.getValue() 
: "get").append("\">");
                   document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
               }
           }
  @@ -422,7 +435,7 @@
                       }
                       stringBuffer.append('>');
                       document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                            stringBuffer.toString()));
                   }
               }
           }
  @@ -449,7 +462,7 @@
                       }
                       stringBuffer.append('>');
                       document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                            stringBuffer.toString()));
                   }
               }
           }
  @@ -529,7 +542,7 @@
                       }
                       stringBuffer.append('>');
                       document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                            stringBuffer.toString()));
                   }
               }
           }
  @@ -556,7 +569,7 @@
                       }
                       stringBuffer.append('>');
                       document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                            stringBuffer.toString()));
                   }
               }
           }
  @@ -566,7 +579,7 @@
                                    StartTag startTag) {
           StringBuffer buffer = new StringBuffer(document.length());
           buffer.append(document.substring(startTag.getBegin(),
  -                                         startTag.findEndTag().getEnd()));
  +                startTag.findEndTag().getEnd()));
           isTrunked = true;
           return new OutputDocument(buffer.toString());
       }
  @@ -577,7 +590,6 @@
        *
        * @param document
        * @param request
  -     *
        * @return
        */
       private OutputDocument trunkDocument(OutputDocument document, 
HttpServletRequest request) {
  
  
  
  Index: init.jsp
  ===================================================================
  RCS file: /home/cvs/repository/webclipping/war/src/webapp/init.jsp,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- init.jsp  13 Dec 2004 09:23:12 -0000      1.2
  +++ init.jsp  9 May 2005 13:37:35 -0000       1.3
  @@ -18,10 +18,13 @@
           Preferences preferences = 
Preferences.systemNodeForPackage(Rewriter.class);
           String contextId = (String) 
request.getAttribute("org.portletapi.contextid");
           String siteUrl = preferences.get("site_url_" + contextId, "");
  +        String cacheDelay = preferences.get("cache_delay_" + contextId, "0");
           String parameter = request.getParameter("showSpecificTag");
           boolean showSpecificTag = new Boolean(parameter != null ? parameter 
: String.valueOf(false)).booleanValue();
           parameter = request.getParameter("showSpecificAttribute");
           boolean showSpecificAttribute = new Boolean(parameter != null ? 
parameter : String.valueOf(false)).booleanValue();
  +        parameter = request.getParameter("restrictAccessibleHost");
  +        boolean restrictAccessibleHost = new Boolean(parameter != null ? 
parameter : String.valueOf(false)).booleanValue();
           boolean reinit = new Boolean(preferences.get("reinit_" + contextId, 
String.valueOf(true))).booleanValue();
           String attribute = request.getParameter("siteUrl");
           String error = null;
  @@ -46,7 +49,12 @@
                   preferences.remove("specific_attr_" + contextId);
                   preferences.remove("specific_attr_val" + contextId);
               }
  -            preferences.flush();
  +                attribute = request.getParameter("listAccessibleHost");
  +                if(restrictAccessibleHost && attribute!=null && 
!"".equals(attribute.trim())) {
  +                    
preferences.put("list_accessible_host_"+contextId,attribute);
  +                }else {
  +                    preferences.remove("list_accessible_host_"+contextId);
  +                }
               }
               else {
                   preferences.put("site_url_" + contextId,"");
  @@ -54,6 +62,14 @@
                   error = "URL must start with http";
               }
           }
  +        attribute = request.getParameter("cacheDelay");
  +        if(attribute!=null && !"".equals(attribute.trim())) {
  +            preferences.put("cache_delay_" + contextId, attribute);
  +            cacheDelay = attribute;
  +        } else {
  +            preferences.put("cache_delay_" + contextId,"0");
  +        }
  +        preferences.flush();
           String specificTag = preferences.get("specific_tag_" + contextId, 
"");
           if(!"".equals(specificTag))
               showSpecificTag=true;
  @@ -61,8 +77,13 @@
           String specificAttributeValue = preferences.get("specific_attr_val" 
+ contextId, "");
           if(!"".equals(specificAttribute))
               showSpecificAttribute=true;
  +        String listAccessibleHost = 
preferences.get("list_accessible_host_"+contextId,"");
  +        if(!"".equals(listAccessibleHost))
  +        restrictAccessibleHost = true;
           // Have we finish the wizard ?
  -        
if(!"".equals(siteUrl.trim())&&request.getParameter("showSpecificTag")!=null) {
  +        if(!"".equals(siteUrl.trim())) {
  +            if(request.getParameter("showSpecificTag")!=null) {
  +
               if(showSpecificTag) {
                   
if(!"".equals(specificTag)&&request.getParameter("showSpecificAttribute")!=null)
 {
                       if(!showSpecificAttribute)
  @@ -74,6 +95,15 @@
               else {
                   reinit=false;
               }
  +            }
  +            if(request.getParameter("restrictAccessibleHost")!=null) {
  +                if(restrictAccessibleHost) {
  +                    reinit = true;
  +                    if( !"".equals(listAccessibleHost.trim())) {
  +                        reinit = false;
  +                    }
  +                }
  +            }
           }
           preferences.put("reinit_"+contextId,String.valueOf(reinit));
           preferences.flush();
  @@ -90,6 +120,10 @@
               <td align="right" >URL :</td>
               <td colspan="5" width="200" align="left" ><input type="text" 
name="siteUrl" value="<%=siteUrl%>" size="40"></td>
           </tr>
  +        <tr>
  +            <td align="right" >Cache in ms :</td>
  +            <td colspan="5" width="200" align="left" ><input type="text" 
name="cacheDelay" value="<%=cacheDelay%>" size="10"></td>
  +        </tr>
           <% if(error==null && null!=siteUrl&&!"".equals(siteUrl)) {%>
           <tr>
               <td>Specific Tag :</td>
  @@ -120,6 +154,18 @@
                   <tr>
                   <%}%>
               <%}%>
  +        <tr>
  +            <td>Restrict accessible host :</td>
  +            <td valign="middle" align="right" ><input type="radio" 
name="restrictAccessibleHost" value="true" 
<%if(restrictAccessibleHost){%>checked="checked"<%}%>></td><td 
valign="middle">Yes</td>
  +            <td valign="middle" align="right" ><input type="radio" 
name="restrictAccessibleHost" value="false" 
<%if(!restrictAccessibleHost){%>checked="checked"<%}%>></td><td 
valign="middle">No</td>
  +            <td width="90">&nbsp;</td>
  +        </tr>
  +            <% if(restrictAccessibleHost) {%>
  +            <tr>
  +                    <td align="right" >List Hosts : <br>(comma 
separated)</td>
  +                    <td align="left" colspan="5" ><input type="text" 
size="40" name="listAccessibleHost" value="<%=listAccessibleHost%>"></td>
  +                </tr>
  +            <%}%>
           <%}%>
           <tr><td colspan="6" align="right"  ><input type="submit" 
name="submit"></td></tr>
       </table>
  

Reply via email to