File Source: TagStatsServlet.java

     1  /*
     2  * Licensed to the Apache Software Foundation (ASF) under one or more
     3  *  contributor license agreements.  The ASF licenses this file to You
     4  * under the Apache License, Version 2.0 (the "License"); you may not
     5  * use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *     http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.  For additional information regarding
    15  * copyright in this work, please see the NOTICE file in the top level
    16  * directory of this distribution.
    17  */
    18  package org.apache.roller.weblogger.webservices.json;
    19  
    20  import java.io.IOException;
    21  import java.util.Iterator;
    22  import java.util.List;
    23  
    24  import javax.servlet.ServletException;
    25  import javax.servlet.http.HttpServlet;
    26  import javax.servlet.http.HttpServletRequest;
    27  import javax.servlet.http.HttpServletResponse;
    28  
    29  import org.apache.roller.weblogger.WebloggerException;
    30  import org.apache.roller.weblogger.business.Weblogger;
    31  import org.apache.roller.weblogger.business.WebloggerFactory;
    32  import org.apache.roller.weblogger.business.UserManager;
    33  import org.apache.roller.weblogger.business.WeblogManager;
    34  import org.apache.roller.weblogger.config.WebloggerConfig;
    35  import org.apache.roller.weblogger.pojos.TagStat;
    36  import org.apache.roller.weblogger.pojos.Weblog;
    37  
    38  /**
    39   * Return list of tags matching a startsWith strings. <br />
    40   * 
    41   * @web.servlet name="TagStatsServlet" 
    42   * @web.servlet-mapping url-pattern="/roller-services/json/tags/*"
    43   * 
    44   * @author Elias Torres (<a href="mailto:eliast@us.ibm.com">eliast@us.ibm.com</a>)
    45   */
    46  public class TagStatsServlet extends HttpServlet {
    47      
    48      // this allows for -1 for no limits.
    49      private final int MAX_LENGTH = WebloggerConfig.getIntProperty("services.json.tags.max", 100);
    50      
    51      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 /* 
    P/P           *  Method: void doPost(HttpServletRequest, HttpServletResponse)
                  * 
                  *  Preconditions:
                  *    request != null
                  *    response != null
                  */
    52          doGet(request, response);
    53      }
    54      
    55      public void doGet(HttpServletRequest request, HttpServletResponse response)
    56              throws ServletException, IOException {    
    57          
                 /* 
    P/P           *  Method: void doGet(HttpServletRequest, HttpServletResponse)
                  * 
                  *  Preconditions:
                  *    request != null
                  *    response != null
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.TRUE)
                  *    java.lang.String:indexOf(...)@82 <= 232-2
                  *    java.util.Iterator:next(...)@133 != null
                  *    javax.servlet.http.HttpServletResponse:getWriter(...)@124 != null
                  *    javax.servlet.http.HttpServletResponse:getWriter(...)@125 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    this.MAX_LENGTH: {-231..-1}, {0..232-2}
                  *    java.lang.String:indexOf(...)@82: {-231..-1}, {0..232-2}
                  *    java.lang.String:indexOf(...)@91: {-231..-1}, {0..232-1}
                  *    java.lang.String:length(...)@97: {0}, {1..232-1}
                  *    java.lang.String:startsWith(...)@78: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@132: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@140: {0}, {1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@61: Addr_Set{null}, Inverse{null}
                  *    javax.servlet.http.HttpServletRequest:getPathInfo(...)@71: Addr_Set{null}, Inverse{null}
                  */
    58          int limit = MAX_LENGTH;       
    59          try {
    60              // only change limit, if specified.
    61              if(request.getParameter("limit") != null) {
    62                limit = Integer.parseInt(request.getParameter("limit"));
    63              }
    64          } catch (Throwable ignored) {}
    65          
    66          // if we didn't specify no limit and user is abusing, kill it.
    67          if(MAX_LENGTH > -1 && limit > MAX_LENGTH) {
    68            limit = MAX_LENGTH;
    69          }
    70          
    71          String pathInfo = request.getPathInfo();
    72          String handle = null;
    73          String prefix = null;
    74          
    75          if(pathInfo != null) {
    76            
    77            // remove first slash
    78            if(pathInfo.startsWith("/"))
    79              pathInfo = pathInfo.substring(1);
    80            
    81            // find a slash
    82            int slash = pathInfo.indexOf("/");
    83            
    84            // if .../tags/handle/
    85            if(slash > -1) {
    86              handle = pathInfo.substring(0,slash);
    87              pathInfo = pathInfo.substring(slash+1);
    88            }
    89            
    90            // double-slash .../tags// or .../tags/handle/adfa/
    91            if(pathInfo.indexOf("/") > -1) {
    92              response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed URL: unncessary slash.");
    93              return;
    94            }
    95            
    96            // keep prefix null unless we have one.
    97            if(pathInfo.length() > 0)
    98              prefix = pathInfo;
    99          }
   100                                          
   101          Weblogger roller = WebloggerFactory.getWeblogger();
   102          try {
   103              response.setContentType("text/html; charset=utf-8");
   104              
   105              WeblogManager wmgr = roller.getWeblogManager();
   106              Weblog website = null;           
   107              
   108              // website handle is always the first path segment,
   109              // only throw an exception when not found if we have a tag prefix 
   110              if(handle != null) {
   111                  try {
   112                      UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
   113                      website = umgr.getWebsiteByHandle(handle, Boolean.TRUE);
   114                      if (website == null)
   115                          throw new WebloggerException();                
   116                  } catch (WebloggerException ex) {
   117                      response.sendError(HttpServletResponse.SC_NOT_FOUND, "Weblog handle not found.");
   118                      return;
   119                  }    
   120              }
   121                                      
   122              List tags = wmgr.getTags(website, null, prefix, limit);
   123              
   124              response.getWriter().println("{");
   125              response.getWriter().print("  prefix : \"");
   126              response.getWriter().print(prefix == null ? "" : prefix);
   127              response.getWriter().println("\",");
   128              response.getWriter().print("  weblog : \"");
   129              response.getWriter().print(website != null ? website.getHandle() : "");
   130              response.getWriter().println("\",");            
   131              response.getWriter().println("  tagcounts : [");
   132              for(Iterator it = tags.iterator(); it.hasNext();) {
   133                  TagStat stat = (TagStat) it.next();
   134                  response.getWriter().print("    { tag : \"");
   135                  response.getWriter().print(stat.getName());
   136                  response.getWriter().print("\", ");
   137                  response.getWriter().print("count : ");
   138                  response.getWriter().print(stat.getCount());
   139                  response.getWriter().print(" }");
   140                  if(it.hasNext())
   141                     response.getWriter().println(", ");
   142              }
   143              response.getWriter().println("\n  ]\n}");
   144              
   145              response.flushBuffer();
   146          } catch (WebloggerException e) {
   147              throw new ServletException(e.getMessage());
   148          }
   149      }
   150  }








SofCheck Inspector Build Version : 2.18479
TagStatsServlet.java 2009-Jan-02 14:25:02
TagStatsServlet.class 2009-Sep-04 03:12:46