File Source: InitFilter.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
19 package org.apache.roller.weblogger.ui.core.filters;
20
21 import java.io.IOException;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
33
34
35 /**
36 * A special initialization filter which ensures that we have an opportunity
37 * to extract a few pieces of information about the environment we are running
38 * in when the first request is sent.
39 *
40 * @web.filter name="InitFilter"
41 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.core.filters.InitFilter()
*
* Postconditions:
* this.initialized == 0
*/
42 public class InitFilter implements Filter {
43
/*
P/P * Method: org.apache.roller.weblogger.ui.core.filters.InitFilter__static_init
*
* Postconditions:
* init'ed(log)
*/
44 private static Log log = LogFactory.getLog(InitFilter.class);
45
46 private boolean initialized = false;
47
48
49 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
50 throws IOException, ServletException {
51
/*
P/P * Method: void doFilter(ServletRequest, ServletResponse, FilterChain)
*
* Preconditions:
* chain != null
* init'ed(this.initialized)
* (soft) log != null
* (soft) req != null
*
* Postconditions:
* this.initialized == 1
*
* Test Vectors:
* this.initialized: {1}, {0}
*/
52 if(!initialized) {
53 // first request, lets do our initialization
54 HttpServletRequest request = (HttpServletRequest) req;
+ 55 HttpServletResponse response = (HttpServletResponse) res;
56
57 // determine absolute and relative url paths to the app
58 String relPath = request.getContextPath();
59 String absPath = this.getAbsoluteUrl(request);
60
61 // set them in our config
62 WebloggerRuntimeConfig.setAbsoluteContextURL(absPath);
63 WebloggerRuntimeConfig.setRelativeContextURL(relPath);
64
65 log.debug("relPath = "+relPath);
66 log.debug("absPath = "+absPath);
67
68 this.initialized = true;
69 }
70
71 chain.doFilter(req, res);
72 }
73
74
75 private String getAbsoluteUrl(HttpServletRequest request) {
76
/*
P/P * Method: String getAbsoluteUrl(HttpServletRequest)
*
* Preconditions:
* request != null
* (soft) log != null
*
* Presumptions:
* javax.servlet.http.HttpServletRequest:getRequestURL(...)@79 != null
*
* Postconditions:
* java.lang.String:substring(...)._tainted == 0
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&java.lang.String:substring(...),&java.lang.String:substring(...)}
*
* Test Vectors:
* java.lang.String:endsWith(...)@97: {0}, {1}
* java.lang.String:equals(...)@82: {0}, {1}
*/
77 String url = null;
78
79 String fullUrl = request.getRequestURL().toString();
80
81 // if the uri is only "/" then we are basically done
82 if("/".equals(request.getRequestURI())) {
83 log.info(fullUrl.substring(0, fullUrl.length()-1));
84 return fullUrl.substring(0, fullUrl.length()-1);
85 }
86
87 // find first "/" starting after hostname is specified
88 int index = fullUrl.indexOf("/", fullUrl.indexOf(request.getServerName()));
89
90 // extract just the part leading up to uri
91 url = fullUrl.substring(0, index);
92
93 // then just add on the context path
94 url += request.getContextPath();
95
96 // make certain that we don't end with a /
97 if(url.endsWith("/")) {
98 url = url.substring(0, url.length()-1);
99 }
100
101 return url;
102 }
103
104
/*
P/P * Method: void init(FilterConfig)
*/
105 public void init(FilterConfig filterConfig) throws ServletException {}
106
/*
P/P * Method: void destroy()
*/
107 public void destroy() {}
108
109 }
SofCheck Inspector Build Version : 2.18479
| InitFilter.java |
2009-Jan-02 14:24:52 |
| InitFilter.class |
2009-Sep-04 03:12:44 |