KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > internal > HttpServletRequestAdaptor


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.http.servlet.internal;
13
14 import javax.servlet.RequestDispatcher JavaDoc;
15 import javax.servlet.Servlet JavaDoc;
16 import javax.servlet.http.*;
17 import org.osgi.service.http.HttpContext;
18
19 public class HttpServletRequestAdaptor extends HttpServletRequestWrapper {
20
21     private String JavaDoc alias;
22     private Servlet JavaDoc servlet;
23     private boolean isRequestDispatcherInclude;
24
25     static final String JavaDoc INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri"; //$NON-NLS-1$
26
static final String JavaDoc INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path"; //$NON-NLS-1$
27
static final String JavaDoc INCLUDE_SERVLET_PATH_ATTRIBUTE = "javax.servlet.include.servlet_path"; //$NON-NLS-1$
28
static final String JavaDoc INCLUDE_PATH_INFO_ATTRIBUTE = "javax.servlet.include.path_info"; //$NON-NLS-1$
29

30     public HttpServletRequestAdaptor(HttpServletRequest req, String JavaDoc alias, Servlet JavaDoc servlet) {
31         super(req);
32         this.alias = alias;
33         this.servlet = servlet;
34         isRequestDispatcherInclude = req.getAttribute(HttpServletRequestAdaptor.INCLUDE_REQUEST_URI_ATTRIBUTE) != null;
35     }
36     
37     public String JavaDoc getAuthType() {
38         String JavaDoc authType = (String JavaDoc) super.getAttribute(HttpContext.AUTHENTICATION_TYPE);
39         if (authType != null)
40             return authType;
41         
42         return super.getAuthType();
43     }
44
45     public String JavaDoc getRemoteUser() {
46         String JavaDoc remoteUser = (String JavaDoc) super.getAttribute(HttpContext.REMOTE_USER);
47         if (remoteUser != null)
48             return remoteUser;
49         
50         return super.getRemoteUser();
51     }
52
53     public String JavaDoc getPathInfo() {
54         if (isRequestDispatcherInclude)
55             return super.getPathInfo();
56
57         if (alias.equals("/")) { //$NON-NLS-1$
58
return super.getPathInfo();
59         }
60         String JavaDoc pathInfo = super.getPathInfo().substring(alias.length());
61         if (pathInfo.length() == 0)
62             return null;
63         
64         return pathInfo;
65     }
66
67     public String JavaDoc getServletPath() {
68         if (isRequestDispatcherInclude)
69             return super.getServletPath();
70
71         if (alias.equals("/")) { //$NON-NLS-1$
72
return ""; //$NON-NLS-1$
73
}
74         return alias;
75     }
76
77     public String JavaDoc getContextPath() {
78         if (isRequestDispatcherInclude)
79             return super.getContextPath();
80
81         return super.getContextPath() + super.getServletPath();
82     }
83
84     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
85         if (isRequestDispatcherInclude) {
86             if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_CONTEXT_PATH_ATTRIBUTE)) {
87                 String JavaDoc contextPath = (String JavaDoc) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_CONTEXT_PATH_ATTRIBUTE);
88                 if (contextPath == null || contextPath.equals("/")) //$NON-NLS-1$
89
contextPath = ""; //$NON-NLS-1$
90

91                 String JavaDoc servletPath = (String JavaDoc) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_SERVLET_PATH_ATTRIBUTE);
92                 if (servletPath == null || servletPath.equals("/")) //$NON-NLS-1$
93
servletPath = ""; //$NON-NLS-1$
94

95                 return contextPath + servletPath;
96             } else if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_SERVLET_PATH_ATTRIBUTE)) {
97                 if (alias.equals("/")) { //$NON-NLS-1$
98
return ""; //$NON-NLS-1$
99
}
100                 return alias;
101             } else if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_PATH_INFO_ATTRIBUTE)) {
102                 String JavaDoc pathInfo = (String JavaDoc) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_PATH_INFO_ATTRIBUTE);
103                 if (alias.equals("/")) { //$NON-NLS-1$
104
return pathInfo;
105                 }
106                 
107                 pathInfo = pathInfo.substring(alias.length());
108                 if (pathInfo.length() == 0)
109                     return null;
110                 
111                 return pathInfo;
112             }
113         }
114
115         return super.getAttribute(attributeName);
116     }
117
118     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc arg0) {
119         return new RequestDispatcherAdaptor(super.getRequestDispatcher(super.getServletPath() + arg0));
120     }
121
122     public static String JavaDoc getDispatchPathInfo(HttpServletRequest req) {
123         if (req.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null)
124             return (String JavaDoc) req.getAttribute(INCLUDE_PATH_INFO_ATTRIBUTE);
125
126         return req.getPathInfo();
127     }
128
129     public static String JavaDoc getDispatchServletPath(HttpServletRequest req) {
130         if (req.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null) {
131             String JavaDoc servletPath = (String JavaDoc) req.getAttribute(INCLUDE_SERVLET_PATH_ATTRIBUTE);
132             return (servletPath == null) ? "" : servletPath; //$NON-NLS-1$
133
}
134         return req.getServletPath();
135     }
136
137     public HttpSession getSession() {
138         HttpSession session = super.getSession();
139         if (session != null)
140             return new HttpSessionAdaptor(session, servlet);
141
142         return null;
143     }
144
145     public HttpSession getSession(boolean create) {
146         HttpSession session = super.getSession(create);
147         if (session != null)
148             return new HttpSessionAdaptor(session, servlet);
149
150         return null;
151     }
152
153 }
154
Popular Tags