KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > filters > InitFilter


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.ui.core.filters;
20
21 import java.io.IOException JavaDoc;
22 import javax.servlet.Filter JavaDoc;
23 import javax.servlet.FilterChain JavaDoc;
24 import javax.servlet.FilterConfig JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.config.RollerRuntimeConfig;
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  */

42 public class InitFilter implements Filter JavaDoc {
43     
44     private static Log log = LogFactory.getLog(InitFilter.class);
45     
46     private boolean initialized = false;
47     
48     
49     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain JavaDoc chain)
50             throws IOException JavaDoc, ServletException JavaDoc {
51         
52         if(!initialized) {
53             // first request, lets do our initialization
54
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
55             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) res;
56             
57             // determine absolute and relative url paths to the app
58
String JavaDoc relPath = request.getContextPath();
59             String JavaDoc absPath = this.getAbsoluteUrl(request);
60             
61             // set them in our config
62
RollerRuntimeConfig.setAbsoluteContextURL(absPath);
63             RollerRuntimeConfig.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 JavaDoc getAbsoluteUrl(HttpServletRequest JavaDoc request) {
76         
77         String JavaDoc url = null;
78         
79         String JavaDoc 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     
105     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {}
106     
107     public void destroy() {}
108     
109 }
110
Popular Tags