001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.http.servlet;
018
019import javax.servlet.RequestDispatcher;
020import javax.servlet.ServletRequest;
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.wicket.util.lang.Args;
024import org.apache.wicket.util.string.Strings;
025
026/**
027 * Represents additional attributes present in a {@link ServletRequest} when the servlet
028 * container is handling a forward to another path than the initially requested one.
029 * 
030 * See documentation for the following request attributes for the values stored in this object:
031 * <ul>
032 * <li>{@link javax.servlet.RequestDispatcher#FORWARD_CONTEXT_PATH}</li>
033 * <li>{@link javax.servlet.RequestDispatcher#FORWARD_PATH_INFO}</li>
034 * <li>{@link javax.servlet.RequestDispatcher#FORWARD_QUERY_STRING}</li>
035 * <li>{@link javax.servlet.RequestDispatcher#FORWARD_REQUEST_URI}</li>
036 * <li>{@link javax.servlet.RequestDispatcher#FORWARD_SERVLET_PATH}</li>
037 * </ul>
038 * 
039 */
040public class ForwardAttributes
041{
042        // javax.servlet.forward.request_uri
043        private final String requestUri;
044
045        // javax.servlet.forward.servlet_path
046        private final String servletPath;
047
048        // javax.servlet.forward.context_path
049        private final String contextPath;
050
051        // javax.servlet.forward.query_string
052        private final String queryString;
053
054        // javax.servlet.forward.path_info
055        private final String pathInfo;
056
057        /**
058         * Constructor.
059         * 
060         * @param requestUri
061         * @param servletPath
062         * @param contextPath
063         * @param queryString
064         * @param pathInfo
065         */
066        private ForwardAttributes(String requestUri, String servletPath, String contextPath,
067                String queryString, String pathInfo)
068        {
069                this.requestUri = requestUri;
070                this.servletPath = servletPath;
071                this.contextPath = contextPath;
072                this.queryString = queryString;
073                this.pathInfo = pathInfo;
074        }
075
076        /**
077         * Gets requestUri.
078         * 
079         * @return requestUri
080         */
081        public String getRequestUri()
082        {
083                return requestUri;
084        }
085
086        /**
087         * Gets servletPath.
088         * 
089         * @return servletPath
090         */
091        public String getServletPath()
092        {
093                return servletPath;
094        }
095
096        /**
097         * Gets contextPath.
098         * 
099         * @return contextPath
100         */
101        public String getContextPath()
102        {
103                return contextPath;
104        }
105
106        /**
107         * Gets the query string.
108         * 
109         * @return the query string
110         */
111        public String getQueryString()
112        {
113                return queryString;
114        }
115
116        /**
117         * @return the path info of the request before the forward dispatch
118         */
119        public String getPathInfo() {
120                return pathInfo;
121        }
122
123        /**
124         * Factory for creating instances of this class.
125         * 
126         * @param request
127         * @return instance of request contains forward attributes or {@code null} if it does not.
128         */
129        public static ForwardAttributes of(HttpServletRequest request, String filterPrefix)
130        {
131                Args.notNull(request, "request");
132
133                final String requestUri = DispatchedRequestUtils.getRequestUri(request, RequestDispatcher.FORWARD_REQUEST_URI, filterPrefix);
134                final String servletPath = (String)request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH);
135                final String contextPath = (String)request.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH);
136                final String queryString = (String)request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
137                final String pathInfo = (String)request.getAttribute(RequestDispatcher.FORWARD_PATH_INFO);
138
139                if (!Strings.isEmpty(requestUri) || !Strings.isEmpty(servletPath) ||
140                        !Strings.isEmpty(contextPath) || !Strings.isEmpty(queryString) ||
141                        !Strings.isEmpty(pathInfo))
142                {
143                        return new ForwardAttributes(requestUri, servletPath, contextPath, queryString, pathInfo);
144                }
145                return null;
146        }
147
148        @Override
149        public String toString()
150        {
151                return "ForwardAttributes [requestUri=" + requestUri + ", servletPath=" + servletPath +
152                        ", contextPath=" + contextPath + ", queryString=" + queryString +
153                           ", pathInfo=" + pathInfo + "]";
154        }
155}