//# 0 errors, 109 messages
//#
/*
    //#TagStatsServlet.java:1:1: class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#TagStatsServlet.java:1:1: method: org.apache.roller.weblogger.webservices.json.TagStatsServlet.org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init
* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  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.  For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/
package org.apache.roller.weblogger.webservices.json;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.business.Weblogger;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.business.WeblogManager;
import org.apache.roller.weblogger.config.WebloggerConfig;
import org.apache.roller.weblogger.pojos.TagStat;
import org.apache.roller.weblogger.pojos.Weblog;

/**
 * Return list of tags matching a startsWith strings. <br />
 * 
 * @web.servlet name="TagStatsServlet" 
 * @web.servlet-mapping url-pattern="/roller-services/json/tags/*"
 * 
 * @author Elias Torres (<a href="mailto:eliast@us.ibm.com">eliast@us.ibm.com</a>)
 */
public class TagStatsServlet extends HttpServlet {
    //#TagStatsServlet.java:46: method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.org.apache.roller.weblogger.webservices.json.TagStatsServlet()
    //#input(void org.apache.roller.weblogger.webservices.json.TagStatsServlet()): this
    //#output(void org.apache.roller.weblogger.webservices.json.TagStatsServlet()): this.MAX_LENGTH
    //#post(void org.apache.roller.weblogger.webservices.json.TagStatsServlet()): init'ed(this.MAX_LENGTH)
    
    // this allows for -1 for no limits.
    private final int MAX_LENGTH = WebloggerConfig.getIntProperty("services.json.tags.max", 100);
    //#TagStatsServlet.java:49: Warning: method not available
    //#    -- call on int org.apache.roller.weblogger.config.WebloggerConfig:getIntProperty(String, int)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet()
    //#    unanalyzed callee: int org.apache.roller.weblogger.config.WebloggerConfig:getIntProperty(String, int)
    //#TagStatsServlet.java:49: end of method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.org.apache.roller.weblogger.webservices.json.TagStatsServlet()
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    //#TagStatsServlet.java:52: method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.doPost(HttpServletRequest, HttpServletResponse)
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): __Descendant_Table[org/apache/roller/weblogger/webservices/json/TagStatsServlet]
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): __Descendant_Table[others]
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): __Dispatch_Table.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): java.lang.Boolean.TRUE
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): request
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): response
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): this
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): this.MAX_LENGTH
    //#input(void doPost(HttpServletRequest, HttpServletResponse)): this.__Tag
    //#pre[1] (void doPost(HttpServletRequest, HttpServletResponse)): request != null
    //#pre[2] (void doPost(HttpServletRequest, HttpServletResponse)): response != null
    //#pre[5] (void doPost(HttpServletRequest, HttpServletResponse)): this.__Tag == org/apache/roller/weblogger/webservices/json/TagStatsServlet
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getParameter
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.Integer:parseInt
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getPathInfo
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:startsWith
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:substring
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:indexOf
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletResponse:sendError
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.lang.String:length
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletResponse:setContentType
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.business.Weblogger:getWeblogManager
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.business.Weblogger:getUserManager
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.business.WeblogManager:getTags
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletResponse:getWriter
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.io.PrintWriter:println
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.io.PrintWriter:print
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.pojos.Weblog:getHandle
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.util.List:iterator
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.util.Iterator:hasNext
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:java.util.Iterator:next
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.pojos.TagStat:getName
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.pojos.TagStat:getCount
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.http.HttpServletResponse:flushBuffer
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:org.apache.roller.weblogger.WebloggerException:getMessage
    //#unanalyzed(void doPost(HttpServletRequest, HttpServletResponse)): Effects-of-calling:javax.servlet.ServletException
    }
    //#TagStatsServlet.java:53: end of method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.doPost(HttpServletRequest, HttpServletResponse)
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {    
        
        int limit = MAX_LENGTH;       
    //#TagStatsServlet.java:58: method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.doGet(HttpServletRequest, HttpServletResponse)
    //#input(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.Boolean.TRUE
    //#input(void doGet(HttpServletRequest, HttpServletResponse)): request
    //#input(void doGet(HttpServletRequest, HttpServletResponse)): response
    //#input(void doGet(HttpServletRequest, HttpServletResponse)): this
    //#input(void doGet(HttpServletRequest, HttpServletResponse)): this.MAX_LENGTH
    //#pre[1] (void doGet(HttpServletRequest, HttpServletResponse)): request != null
    //#pre[2] (void doGet(HttpServletRequest, HttpServletResponse)): response != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): init'ed(java.lang.Boolean.TRUE)
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.String:indexOf(...)@82 <= 4_294_967_294
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): java.util.Iterator:next(...)@133 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@124 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@125 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@126 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@127 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@128 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@129 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@130 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@131 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@134 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@135 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@136 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@137 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@138 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@139 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@141 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletResponse:getWriter(...)@143 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle(...)@113 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.WeblogManager:getTags(...)@122 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@112 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@105 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@101 != null
    //#presumption(void doGet(HttpServletRequest, HttpServletResponse)): org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@112 != null
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): this.MAX_LENGTH: {-2_147_483_648..-1}, {0..4_294_967_294}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.String:indexOf(...)@82: {-2_147_483_648..-1}, {0..4_294_967_294}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.String:indexOf(...)@91: {-2_147_483_648..-1}, {0..4_294_967_295}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.String:length(...)@97: {0}, {1..4_294_967_295}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.lang.String:startsWith(...)@78: {0}, {1}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.util.Iterator:hasNext(...)@132: {0}, {1}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): java.util.Iterator:hasNext(...)@140: {0}, {1}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getParameter(...)@61: Addr_Set{null}, Inverse{null}
    //#test_vector(void doGet(HttpServletRequest, HttpServletResponse)): javax.servlet.http.HttpServletRequest:getPathInfo(...)@71: Addr_Set{null}, Inverse{null}
        try {
            // only change limit, if specified.
            if(request.getParameter("limit") != null) {
              limit = Integer.parseInt(request.getParameter("limit"));
            }
        } catch (Throwable ignored) {}
        
        // if we didn't specify no limit and user is abusing, kill it.
        if(MAX_LENGTH > -1 && limit > MAX_LENGTH) {
          limit = MAX_LENGTH;
        }
        
        String pathInfo = request.getPathInfo();
        String handle = null;
        String prefix = null;
        
        if(pathInfo != null) {
          
          // remove first slash
          if(pathInfo.startsWith("/"))
            pathInfo = pathInfo.substring(1);
          
          // find a slash
          int slash = pathInfo.indexOf("/");
          
          // if .../tags/handle/
          if(slash > -1) {
            handle = pathInfo.substring(0,slash);
            pathInfo = pathInfo.substring(slash+1);
          }
          
          // double-slash .../tags// or .../tags/handle/adfa/
          if(pathInfo.indexOf("/") > -1) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed URL: unncessary slash.");
            return;
          }
          
          // keep prefix null unless we have one.
          if(pathInfo.length() > 0)
            prefix = pathInfo;
        }
                                        
        Weblogger roller = WebloggerFactory.getWeblogger();
    //#TagStatsServlet.java:101: Warning: method not available
    //#    -- call on Weblogger org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: Weblogger org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger()
        try {
            response.setContentType("text/html; charset=utf-8");
            
            WeblogManager wmgr = roller.getWeblogManager();
    //#TagStatsServlet.java:105: Warning: method not available
    //#    -- call on WeblogManager org.apache.roller.weblogger.business.Weblogger:getWeblogManager()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: WeblogManager org.apache.roller.weblogger.business.Weblogger:getWeblogManager()
            Weblog website = null;           
            
            // website handle is always the first path segment,
            // only throw an exception when not found if we have a tag prefix 
            if(handle != null) {
                try {
                    UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
    //#TagStatsServlet.java:112: Warning: method not available
    //#    -- call on Weblogger org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: Weblogger org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger()
    //#TagStatsServlet.java:112: Warning: method not available
    //#    -- call on UserManager org.apache.roller.weblogger.business.Weblogger:getUserManager()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: UserManager org.apache.roller.weblogger.business.Weblogger:getUserManager()
                    website = umgr.getWebsiteByHandle(handle, Boolean.TRUE);
    //#TagStatsServlet.java:113: Warning: method not available
    //#    -- call on Weblog org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle(String, Boolean)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: Weblog org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle(String, Boolean)
                    if (website == null)
                        throw new WebloggerException();                
                } catch (WebloggerException ex) {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND, "Weblog handle not found.");
                    return;
                }    
            }
                                    
            List tags = wmgr.getTags(website, null, prefix, limit);
    //#TagStatsServlet.java:122: Warning: method not available
    //#    -- call on List org.apache.roller.weblogger.business.WeblogManager:getTags(Weblog, String, String, int)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: List org.apache.roller.weblogger.business.WeblogManager:getTags(Weblog, String, String, int)
            
            response.getWriter().println("{");
            response.getWriter().print("  prefix : \"");
            response.getWriter().print(prefix == null ? "" : prefix);
            response.getWriter().println("\",");
            response.getWriter().print("  weblog : \"");
            response.getWriter().print(website != null ? website.getHandle() : "");
    //#TagStatsServlet.java:129: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.Weblog:getHandle()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.Weblog:getHandle()
            response.getWriter().println("\",");            
            response.getWriter().println("  tagcounts : [");
            for(Iterator it = tags.iterator(); it.hasNext();) {
                TagStat stat = (TagStat) it.next();
                response.getWriter().print("    { tag : \"");
                response.getWriter().print(stat.getName());
    //#TagStatsServlet.java:135: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.TagStat:getName()
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.TagStat:getName()
                response.getWriter().print("\", ");
                response.getWriter().print("count : ");
                response.getWriter().print(stat.getCount());
    //#TagStatsServlet.java:138: Warning: method not available
    //#    -- call on int org.apache.roller.weblogger.pojos.TagStat:getCount()
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
    //#    method: void doGet(HttpServletRequest, HttpServletResponse)
    //#    unanalyzed callee: int org.apache.roller.weblogger.pojos.TagStat:getCount()
                response.getWriter().print(" }");
                if(it.hasNext())
                   response.getWriter().println(", ");
            }
            response.getWriter().println("\n  ]\n}");
            
            response.flushBuffer();
        } catch (WebloggerException e) {
            throw new ServletException(e.getMessage());
        }
    }
    //#TagStatsServlet.java:149: end of method: void org.apache.roller.weblogger.webservices.json.TagStatsServlet.doGet(HttpServletRequest, HttpServletResponse)
}
    //#output(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Descendant_Table[org/apache/roller/weblogger/webservices/json/TagStatsServlet]
    //#output(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Dispatch_Table.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V
    //#output(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Dispatch_Table.doPost(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V
    //#post(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Descendant_Table[org/apache/roller/weblogger/webservices/json/TagStatsServlet] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Dispatch_Table.doGet(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V == &doGet
    //#post(org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init): __Dispatch_Table.doPost(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V == &doPost
    //#TagStatsServlet.java:: end of method: org.apache.roller.weblogger.webservices.json.TagStatsServlet.org.apache.roller.weblogger.webservices.json.TagStatsServlet__static_init
    //#TagStatsServlet.java:: end of class: org.apache.roller.weblogger.webservices.json.TagStatsServlet
