Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.sling.engine.impl.request;
 
 import java.util.List;
 
 
 import  org.osgi.framework.BundleContext;
 import  org.osgi.framework.Constants;
 import  org.osgi.framework.ServiceRegistration;

Felix OSGi console plugin that displays info about recent requests processed by Sling. Info about all requests can be found in the logs, but this is useful when testing or explaining things.
 
 @SuppressWarnings("serial")
 public class RequestHistoryConsolePlugin {
 
     public static final String LABEL = "requests";
 
     public static final String INDEX = "index";
 
     public static final String CLEAR = "clear";
 
     private static Plugin instance;
 
     private static ServiceRegistration serviceRegistration;
 
     public static final int STORED_REQUESTS_COUNT = 20;
 
     private RequestHistoryConsolePlugin() {
     }
 
     public static void recordRequest(SlingHttpServletRequest r) {
         if ( != null) {
             .addRequest(r);
         }
     }
 
     public static void initPlugin(BundleContext contextint maxRequestsList<PatternstorePatterns) {
         if ( == null) {
             Plugin tmp = new Plugin(maxRequestsstorePatterns);
             final Dictionary<StringObjectprops = new Hashtable<StringObject>();
             props.put(Constants.SERVICE_DESCRIPTION,
                 "Web Console Plugin to display information about recent Sling requests");
             props.put(Constants.SERVICE_VENDOR,
                 "The Apache Software Foundation");
             props.put(Constants.SERVICE_PID, tmp.getClass().getName());
             props.put("felix.webconsole.label");
             props.put("felix.webconsole.title""Recent requests");
 
              = context.registerService(
                 "javax.servlet.Servlet"tmpprops);
              = tmp;
         }
     }
 
     public static void destroyPlugin() {
         if ( != null) {
             try {
                 if ( != null) {
                     .unregister();
                      = null;
                 }
             } finally {
                 = null;
            }
        }
    }
    public static final class Plugin extends HttpServlet {
        private final RequestInfoMap requests;
        
        private final List<PatternstorePatterns;
        Plugin(int maxRequestsList<PatternstorePatterns) {
            this. = (maxRequests > 0)
                    ? new RequestInfoMap(maxRequests)
                    : null;
            this. = storePatterns;
        }
        public void deactivate() {
            if ( != null) {
                .unregister();
                 = null;
            }
            clear();
        }
        private void addRequest(SlingHttpServletRequest r) {
            if ( != null) {
                String requestPath = r.getPathInfo();
                boolean accept = true;
                if ( != null && .size() > 0) {
                    accept = false;
                    for (Pattern pattern : ) {
                        if (pattern.matcher(requestPath).matches()) {
                            accept = true;
                            break;
                        }
                    }
                }
                if (accept) {
                    synchronized () {
                        RequestInfo info = new RequestInfo(r);
                        .put(info.getKey(), info);
                    }
                }
            }
        }
        private void clear() {
            if ( != null) {
                synchronized () {
                    .clear();
                }
            }
        }
        private String getLinksTable(String currentRequestIndex) {
            final List<Stringlinks = new ArrayList<String>();
            if ( != null) {
                synchronized () {
                    for (RequestInfo info : .values()) {
                        final String key = ResponseUtil.escapeXml(info.getKey());
                        final boolean isCurrent = info.getKey().equals(
                            currentRequestIndex);
                        final StringBuilder sb = new StringBuilder();
                        sb.append("<span style='white-space: pre; text-align:right; font-size:80%'>");
                        sb.append(String.format("%1$#8s"key));
                        sb.append("</span> ");
                        sb.append("<a href='" +  + "?index=" + key + "'>");
                        if (isCurrent) {
                            sb.append("<b>");
                        }
                        sb.append(ResponseUtil.escapeXml(info.getLabel()));
                        if (isCurrent) {
                            sb.append("</b>");
                        }
                        sb.append("</a> ");
                        links.add(sb.toString());
                    }
                }
            }
            final int nCols = 5;
            while ((links.size() % nCols) != 0) {
                links.add("&nbsp;");
            }
            final StringBuilder tbl = new StringBuilder();
            tbl.append("<table class='nicetable ui-widget'>\n<tr>\n");
            if (links.isEmpty()) {
                tbl.append("No Requests recorded");
            } else {
                int i = 0;
                for (String str : links) {
                    if ((i++ % nCols) == 0) {
                        tbl.append("</tr>\n<tr>\n");
                    }
                    tbl.append("<td>");
                    tbl.append(str);
                    tbl.append("</td>\n");
                }
            }
            tbl.append("</tr>\n");
            tbl.append("</table>\n");
            return tbl.toString();
        }
        @Override
        protected void doGet(HttpServletRequest reqHttpServletResponse resp)
                throws ServletExceptionIOException {
            // Select request to display
            RequestInfo info = null;
            String key = req.getParameter();
            if (key != null &&  != null) {
                synchronized () {
                    info = .get(key);
                }
            }
            final PrintWriter pw = resp.getWriter();
            if ( != null) {
                pw.println("<p class='statline ui-state-highlight'>Recorded "
                    + .size() + " requests (max: "
                    + .getMaxSize() + ")</p>");
            } else {
                pw.println("<p class='statline ui-state-highlight'>Request Recording disabled</p>");
            }
            pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
            pw.println("<span style='float: left; margin-left: 1em'>Recent Requests</span>");
            pw.println("<form method='POST'><input type='hidden' name='clear' value='clear'><input type='submit' value='Clear' class='ui-state-default ui-corner-all'></form>");
            pw.println("</div>");
            pw.println(getLinksTable(key));
            pw.println("<br/>");
            if (info != null) {
                pw.println("<table class='nicetable ui-widget'>");
                // Links to other requests
                pw.println("<thead>");
                pw.println("<tr>");
                pw.printf(
                    "<th class='ui-widget-header'>Request %s (%s %s) by %s - RequestProgressTracker Info</th>%n",
                    key, ResponseUtil.escapeXml(info.getMethod()), 
                    ResponseUtil.escapeXml(info.getPathInfo()), ResponseUtil.escapeXml(info.getUser()));
                pw.println("</tr>");
                pw.println("</thead>");
                pw.println("<tbody>");
                // Request Progress Tracker Info
                pw.println("<tr><td>");
                final Iterator<Stringit = info.getTracker().getMessages();
                pw.print("<pre>");
                while (it.hasNext()) {
                    pw.print(ResponseUtil.escapeXml(it.next()));
                }
                pw.println("</pre></td></tr>");
                pw.println("</tbody></table>");
            }
        }
        @Override
        protected void doPost(HttpServletRequest reqHttpServletResponse resp)
                throws IOException {
            if (req.getParameter() != null) {
                clear();
                resp.sendRedirect(req.getRequestURI());
            }
        }
    }
    private static class RequestInfo {
        private static AtomicLong requestCounter = new AtomicLong(0);
        private final String key;
        private final String method;
        private final String pathInfo;
        private final String user;
        private final RequestProgressTracker tracker;
        RequestInfo(SlingHttpServletRequest request) {
            this. = String.valueOf(.incrementAndGet());
            this. = request.getMethod();
            this. = request.getPathInfo();
            this. = request.getRemoteUser();
            this. = request.getRequestProgressTracker();
        }
        public String getKey() {
            return ;
        }
        public String getMethod() {
            return ;
        }
        public String getPathInfo() {
            return ;
        }
        public String getUser() {
            return ;
        }
        public String getLabel() {
            final StringBuilder sb = new StringBuilder();
            sb.append(getMethod());
            sb.append(' ');
            final String path = getPathInfo();
            if (path != null && path.length() > 0) {
                sb.append(ResourceUtil.getName(getPathInfo()));
            } else {
                sb.append('/');
            }
            return sb.toString();
        }
        public RequestProgressTracker getTracker() {
            return ;
        }
    }
    private static class RequestInfoMap extends
            LinkedHashMap<StringRequestInfo> {
        private int maxSize;
        RequestInfoMap(int maxSize) {
            this. = maxSize;
        }
        @Override
        protected boolean removeEldestEntry(
                java.util.Map.Entry<StringRequestInfoeldest) {
            return size() > ;
        }
        public int getMaxSize() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X