001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.core.filters;
020:
021: import java.io.IOException;
022: import javax.servlet.Filter;
023: import javax.servlet.FilterChain;
024: import javax.servlet.FilterConfig;
025: import javax.servlet.ServletException;
026: import javax.servlet.ServletRequest;
027: import javax.servlet.ServletResponse;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.roller.config.RollerRuntimeConfig;
033:
034: /**
035: * A special initialization filter which ensures that we have an opportunity
036: * to extract a few pieces of information about the environment we are running
037: * in when the first request is sent.
038: *
039: * @web.filter name="InitFilter"
040: */
041: public class InitFilter implements Filter {
042:
043: private static Log log = LogFactory.getLog(InitFilter.class);
044:
045: private boolean initialized = false;
046:
047: public void doFilter(ServletRequest req, ServletResponse res,
048: FilterChain chain) throws IOException, ServletException {
049:
050: if (!initialized) {
051: // first request, lets do our initialization
052: HttpServletRequest request = (HttpServletRequest) req;
053: HttpServletResponse response = (HttpServletResponse) res;
054:
055: // determine absolute and relative url paths to the app
056: String relPath = request.getContextPath();
057: String absPath = this .getAbsoluteUrl(request);
058:
059: // set them in our config
060: RollerRuntimeConfig.setAbsoluteContextURL(absPath);
061: RollerRuntimeConfig.setRelativeContextURL(relPath);
062:
063: log.debug("relPath = " + relPath);
064: log.debug("absPath = " + absPath);
065:
066: this .initialized = true;
067: }
068:
069: chain.doFilter(req, res);
070: }
071:
072: private String getAbsoluteUrl(HttpServletRequest request) {
073:
074: String url = null;
075:
076: String fullUrl = request.getRequestURL().toString();
077:
078: // if the uri is only "/" then we are basically done
079: if ("/".equals(request.getRequestURI())) {
080: log.info(fullUrl.substring(0, fullUrl.length() - 1));
081: return fullUrl.substring(0, fullUrl.length() - 1);
082: }
083:
084: // find first "/" starting after hostname is specified
085: int index = fullUrl.indexOf("/", fullUrl.indexOf(request
086: .getServerName()));
087:
088: // extract just the part leading up to uri
089: url = fullUrl.substring(0, index);
090:
091: // then just add on the context path
092: url += request.getContextPath();
093:
094: // make certain that we don't end with a /
095: if (url.endsWith("/")) {
096: url = url.substring(0, url.length() - 1);
097: }
098:
099: return url;
100: }
101:
102: public void init(FilterConfig filterConfig) throws ServletException {
103: }
104:
105: public void destroy() {
106: }
107:
108: }
|