1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import javax.servlet.RequestDispatcher ; 15 import javax.servlet.Servlet ; 16 import javax.servlet.http.*; 17 import org.osgi.service.http.HttpContext; 18 19 public class HttpServletRequestAdaptor extends HttpServletRequestWrapper { 20 21 private String alias; 22 private Servlet servlet; 23 private boolean isRequestDispatcherInclude; 24 25 static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri"; static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path"; static final String INCLUDE_SERVLET_PATH_ATTRIBUTE = "javax.servlet.include.servlet_path"; static final String INCLUDE_PATH_INFO_ATTRIBUTE = "javax.servlet.include.path_info"; 30 public HttpServletRequestAdaptor(HttpServletRequest req, String alias, Servlet 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 getAuthType() { 38 String authType = (String ) super.getAttribute(HttpContext.AUTHENTICATION_TYPE); 39 if (authType != null) 40 return authType; 41 42 return super.getAuthType(); 43 } 44 45 public String getRemoteUser() { 46 String remoteUser = (String ) super.getAttribute(HttpContext.REMOTE_USER); 47 if (remoteUser != null) 48 return remoteUser; 49 50 return super.getRemoteUser(); 51 } 52 53 public String getPathInfo() { 54 if (isRequestDispatcherInclude) 55 return super.getPathInfo(); 56 57 if (alias.equals("/")) { return super.getPathInfo(); 59 } 60 String pathInfo = super.getPathInfo().substring(alias.length()); 61 if (pathInfo.length() == 0) 62 return null; 63 64 return pathInfo; 65 } 66 67 public String getServletPath() { 68 if (isRequestDispatcherInclude) 69 return super.getServletPath(); 70 71 if (alias.equals("/")) { return ""; } 74 return alias; 75 } 76 77 public String getContextPath() { 78 if (isRequestDispatcherInclude) 79 return super.getContextPath(); 80 81 return super.getContextPath() + super.getServletPath(); 82 } 83 84 public Object getAttribute(String attributeName) { 85 if (isRequestDispatcherInclude) { 86 if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_CONTEXT_PATH_ATTRIBUTE)) { 87 String contextPath = (String ) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_CONTEXT_PATH_ATTRIBUTE); 88 if (contextPath == null || contextPath.equals("/")) contextPath = ""; 91 String servletPath = (String ) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_SERVLET_PATH_ATTRIBUTE); 92 if (servletPath == null || servletPath.equals("/")) servletPath = ""; 95 return contextPath + servletPath; 96 } else if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_SERVLET_PATH_ATTRIBUTE)) { 97 if (alias.equals("/")) { return ""; } 100 return alias; 101 } else if (attributeName.equals(HttpServletRequestAdaptor.INCLUDE_PATH_INFO_ATTRIBUTE)) { 102 String pathInfo = (String ) super.getAttribute(HttpServletRequestAdaptor.INCLUDE_PATH_INFO_ATTRIBUTE); 103 if (alias.equals("/")) { 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 getRequestDispatcher(String arg0) { 119 return new RequestDispatcherAdaptor(super.getRequestDispatcher(super.getServletPath() + arg0)); 120 } 121 122 public static String getDispatchPathInfo(HttpServletRequest req) { 123 if (req.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null) 124 return (String ) req.getAttribute(INCLUDE_PATH_INFO_ATTRIBUTE); 125 126 return req.getPathInfo(); 127 } 128 129 public static String getDispatchServletPath(HttpServletRequest req) { 130 if (req.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null) { 131 String servletPath = (String ) req.getAttribute(INCLUDE_SERVLET_PATH_ATTRIBUTE); 132 return (servletPath == null) ? "" : servletPath; } 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 |