//# 0 errors, 81 messages
//#
/*
    //#InitFilter.java:1:1: class: org.apache.roller.weblogger.ui.core.filters.InitFilter
 * 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.ui.core.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;


/**
 * A special initialization filter which ensures that we have an opportunity
 * to extract a few pieces of information about the environment we are running
 * in when the first request is sent.
 *
 * @web.filter name="InitFilter"
 */
public class InitFilter implements Filter {
    //#InitFilter.java:42: method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.org.apache.roller.weblogger.ui.core.filters.InitFilter()
    //#input(void org.apache.roller.weblogger.ui.core.filters.InitFilter()): this
    //#output(void org.apache.roller.weblogger.ui.core.filters.InitFilter()): this.initialized
    //#post(void org.apache.roller.weblogger.ui.core.filters.InitFilter()): this.initialized == 0
    
    private static Log log = LogFactory.getLog(InitFilter.class);
    //#InitFilter.java:44: method: org.apache.roller.weblogger.ui.core.filters.InitFilter.org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init
    //#InitFilter.java:44: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/core/filters/InitFilter]
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.destroy()V
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.doFilter(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Ljavax/servlet/FilterChain;)V
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.getAbsoluteUrl(Ljavax/servlet/http/HttpServletRequest;)Ljava/lang/String;
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.init(Ljavax/servlet/FilterConfig;)V
    //#output(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): log
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/core/filters/InitFilter] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.destroy()V == &destroy
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.doFilter(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Ljavax/servlet/FilterChain;)V == &doFilter
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.getAbsoluteUrl(Ljavax/servlet/http/HttpServletRequest;)Ljava/lang/String; == &getAbsoluteUrl
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): __Dispatch_Table.init(Ljavax/servlet/FilterConfig;)V == &init
    //#post(org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init): init'ed(log)
    //#InitFilter.java:44: end of method: org.apache.roller.weblogger.ui.core.filters.InitFilter.org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init
    
    private boolean initialized = false;
    //#InitFilter.java:46: end of method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.org.apache.roller.weblogger.ui.core.filters.InitFilter()
    
    
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        
        if(!initialized) {
    //#InitFilter.java:52: method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.doFilter(ServletRequest, ServletResponse, FilterChain)
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): "absPath = "._tainted
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): "relPath = "._tainted
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): chain
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): log
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): req
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): res
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): this
    //#input(void doFilter(ServletRequest, ServletResponse, FilterChain)): this.initialized
    //#output(void doFilter(ServletRequest, ServletResponse, FilterChain)): this.initialized
    //#pre[1] (void doFilter(ServletRequest, ServletResponse, FilterChain)): chain != null
    //#pre[3] (void doFilter(ServletRequest, ServletResponse, FilterChain)): init'ed(this.initialized)
    //#pre[2] (void doFilter(ServletRequest, ServletResponse, FilterChain)): (soft) log != null
    //#pre[4] (void doFilter(ServletRequest, ServletResponse, FilterChain)): (soft) req != null
    //#post(void doFilter(ServletRequest, ServletResponse, FilterChain)): this.initialized == 1
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getRequestURL
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.StringBuffer:toString
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getRequestURI
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.String:equals
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.String:length
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.String:substring
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:org.apache.commons.logging.Log:info
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getServerName
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.String:indexOf
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:javax.servlet.http.HttpServletRequest:getContextPath
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(void doFilter(ServletRequest, ServletResponse, FilterChain)): Effects-of-calling:java.lang.String:endsWith
    //#test_vector(void doFilter(ServletRequest, ServletResponse, FilterChain)): this.initialized: {1}, {0}
            // first request, lets do our initialization
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
    //#InitFilter.java:55: Warning: unused assignment
    //#    unused assignment into response
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: void doFilter(ServletRequest, ServletResponse, FilterChain)
            
            // determine absolute and relative url paths to the app
            String relPath = request.getContextPath();
            String absPath = this.getAbsoluteUrl(request);
            
            // set them in our config
            WebloggerRuntimeConfig.setAbsoluteContextURL(absPath);
    //#InitFilter.java:62: Warning: method not available
    //#    -- call on void org.apache.roller.weblogger.config.WebloggerRuntimeConfig:setAbsoluteContextURL(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: void doFilter(ServletRequest, ServletResponse, FilterChain)
    //#    unanalyzed callee: void org.apache.roller.weblogger.config.WebloggerRuntimeConfig:setAbsoluteContextURL(String)
            WebloggerRuntimeConfig.setRelativeContextURL(relPath);
    //#InitFilter.java:63: Warning: method not available
    //#    -- call on void org.apache.roller.weblogger.config.WebloggerRuntimeConfig:setRelativeContextURL(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: void doFilter(ServletRequest, ServletResponse, FilterChain)
    //#    unanalyzed callee: void org.apache.roller.weblogger.config.WebloggerRuntimeConfig:setRelativeContextURL(String)
            
            log.debug("relPath = "+relPath);
    //#InitFilter.java:65: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: void doFilter(ServletRequest, ServletResponse, FilterChain)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
            log.debug("absPath = "+absPath);
    //#InitFilter.java:66: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: void doFilter(ServletRequest, ServletResponse, FilterChain)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
            
            this.initialized = true;
        }
        
        chain.doFilter(req, res);
    }
    //#InitFilter.java:72: end of method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.doFilter(ServletRequest, ServletResponse, FilterChain)
    
    
    private String getAbsoluteUrl(HttpServletRequest request) {
        
        String url = null;
    //#InitFilter.java:77: method: String org.apache.roller.weblogger.ui.core.filters.InitFilter.getAbsoluteUrl(HttpServletRequest)
    //#InitFilter.java:77: Warning: unused assignment
    //#    unused assignment into url
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: String getAbsoluteUrl(HttpServletRequest)
    //#    Attribs:  Uncertain
    //#input(String getAbsoluteUrl(HttpServletRequest)): log
    //#input(String getAbsoluteUrl(HttpServletRequest)): request
    //#output(String getAbsoluteUrl(HttpServletRequest)): java.lang.String:substring(...)._tainted
    //#output(String getAbsoluteUrl(HttpServletRequest)): java.lang.StringBuilder:toString(...)._tainted
    //#output(String getAbsoluteUrl(HttpServletRequest)): return_value
    //#new obj(String getAbsoluteUrl(HttpServletRequest)): java.lang.String:substring(...)
    //#new obj(String getAbsoluteUrl(HttpServletRequest)): java.lang.StringBuilder:toString(...)
    //#pre[2] (String getAbsoluteUrl(HttpServletRequest)): request != null
    //#pre[1] (String getAbsoluteUrl(HttpServletRequest)): (soft) log != null
    //#presumption(String getAbsoluteUrl(HttpServletRequest)): javax.servlet.http.HttpServletRequest:getRequestURL(...)@79 != null
    //#post(String getAbsoluteUrl(HttpServletRequest)): java.lang.String:substring(...)._tainted == 0
    //#post(String getAbsoluteUrl(HttpServletRequest)): java.lang.StringBuilder:toString(...)._tainted == 0
    //#post(String getAbsoluteUrl(HttpServletRequest)): return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&java.lang.String:substring(...),&java.lang.String:substring(...)}
    //#test_vector(String getAbsoluteUrl(HttpServletRequest)): java.lang.String:endsWith(...)@97: {0}, {1}
    //#test_vector(String getAbsoluteUrl(HttpServletRequest)): java.lang.String:equals(...)@82: {0}, {1}
        
        String fullUrl = request.getRequestURL().toString();
        
        // if the uri is only "/" then we are basically done
        if("/".equals(request.getRequestURI())) {
            log.info(fullUrl.substring(0, fullUrl.length()-1));
    //#InitFilter.java:83: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:info(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.core.filters.InitFilter
    //#    method: String getAbsoluteUrl(HttpServletRequest)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:info(Object)
            return fullUrl.substring(0, fullUrl.length()-1);
        }
        
        // find first "/" starting after hostname is specified
        int index = fullUrl.indexOf("/", fullUrl.indexOf(request.getServerName()));
        
        // extract just the part leading up to uri
        url = fullUrl.substring(0, index);
        
        // then just add on the context path
        url += request.getContextPath();
        
        // make certain that we don't end with a /
        if(url.endsWith("/")) {
            url = url.substring(0, url.length()-1);
        }
        
        return url;
    //#InitFilter.java:101: end of method: String org.apache.roller.weblogger.ui.core.filters.InitFilter.getAbsoluteUrl(HttpServletRequest)
    }
    
    
    public void init(FilterConfig filterConfig) throws ServletException {}
    //#InitFilter.java:105: method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.init(FilterConfig)
    //#InitFilter.java:105: end of method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.init(FilterConfig)
    
    public void destroy() {}
    //#InitFilter.java:107: method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.destroy()
    //#InitFilter.java:107: end of method: void org.apache.roller.weblogger.ui.core.filters.InitFilter.destroy()
    
}
    //#InitFilter.java:: end of class: org.apache.roller.weblogger.ui.core.filters.InitFilter
