| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.Date; |
| 5 | |
import java.util.Enumeration; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.Map; |
| 8 | |
import java.security.Principal; |
| 9 | |
import javax.servlet.ServletRequest; |
| 10 | |
import javax.servlet.ServletRequestWrapper; |
| 11 | |
import javax.servlet.http.Cookie; |
| 12 | |
import javax.servlet.http.HttpServletRequest; |
| 13 | |
import javax.servlet.http.HttpServletResponse; |
| 14 | |
import javax.servlet.http.HttpSession; |
| 15 | |
|
| 16 | |
public class FakeHttpServletRequest extends ServletRequestWrapper implements HttpServletRequest { |
| 17 | |
protected final FakeServletContext context; |
| 18 | |
protected final HttpServletResponse response; |
| 19 | |
protected final FakeResource resource; |
| 20 | |
protected final HttpServletRequestConfig config; |
| 21 | |
|
| 22 | |
public FakeHttpServletRequest(FakeServletContext context, ServletRequest request, HttpServletResponse response, FakeResource resource) throws IOException { |
| 23 | 526 | this(context, request, response, resource, new HttpHeadersImpl(), "GET", null); |
| 24 | 526 | } |
| 25 | |
|
| 26 | |
public FakeHttpServletRequest(FakeServletContext context, ServletRequest request, HttpServletResponse response, FakeResource resource, HttpHeaders headers, String method, Principal user) throws IOException { |
| 27 | 526 | this(context, request, response, resource, new HttpServletRequestConfig(method, headers), user); |
| 28 | 526 | } |
| 29 | |
|
| 30 | |
public FakeHttpServletRequest(FakeServletContext context, ServletRequest request, HttpServletResponse response, FakeResource resource, HttpServletRequestConfig config, Principal user) throws IOException { |
| 31 | 526 | super(request); |
| 32 | 526 | this.context = context; |
| 33 | 526 | this.response = response; |
| 34 | 526 | this.resource = resource; |
| 35 | 526 | this.config = config; |
| 36 | 526 | config.setUserPrincipal(user); |
| 37 | 526 | context.initializeRequest(this); |
| 38 | 526 | } |
| 39 | |
|
| 40 | |
public HttpServletRequestConfig getConfig() { |
| 41 | 0 | return config; |
| 42 | |
} |
| 43 | |
|
| 44 | |
public FakeResource getFakeResource() { |
| 45 | 0 | return resource; |
| 46 | |
} |
| 47 | |
|
| 48 | |
public void addCookie(Cookie cookie) { |
| 49 | 0 | config.addCookie(cookie); |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
public void removeCookie(String name) { |
| 53 | 0 | config.removeCookie(name); |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
public void setContextPath(String contextPath) { |
| 57 | 0 | config.setContextPath(contextPath); |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public void setAuthType(String authType) { |
| 61 | 0 | config.setAuthType(authType); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public HttpHeaders getHttpHeaders() { |
| 67 | 230 | return config.getHeaders(); |
| 68 | |
} |
| 69 | |
|
| 70 | |
public String getAuthType() { |
| 71 | 0 | return config.getAuthType(); |
| 72 | |
} |
| 73 | |
|
| 74 | |
public String getContextPath() { |
| 75 | 0 | return config.getContextPath(); |
| 76 | |
} |
| 77 | |
|
| 78 | |
public Cookie[] getCookies() { |
| 79 | 0 | return config.getCookies(); |
| 80 | |
} |
| 81 | |
|
| 82 | |
public long getDateHeader(String name) { |
| 83 | 0 | return getHttpHeaders().getDateHeader(name); |
| 84 | |
} |
| 85 | |
|
| 86 | |
public String getHeader(String name) { |
| 87 | 230 | return getHttpHeaders().getHeader(name); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public Enumeration getHeaderNames() { |
| 91 | 0 | return getHttpHeaders().getHeaderNames(); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public Enumeration getHeaders(String name) { |
| 95 | 0 | return getHttpHeaders().getHeaders(name); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public int getIntHeader(String name) { |
| 99 | 0 | return getHttpHeaders().getIntHeader(name); |
| 100 | |
} |
| 101 | |
|
| 102 | |
public String getMethod() { |
| 103 | 0 | return config.getMethod(); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public String getPathInfo() { |
| 107 | 0 | String pathInfo = getFakeResource().getPathInfo(); |
| 108 | 0 | if (pathInfo.length() == 0) return null; |
| 109 | 0 | return pathInfo; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public String getPathTranslated() { |
| 113 | 0 | throw new UnsupportedOperationException(); |
| 114 | |
} |
| 115 | |
|
| 116 | |
public String getQueryString() { |
| 117 | 0 | return null; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public String getRemoteUser() { |
| 121 | 0 | return null; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public String getRequestedSessionId() { |
| 125 | 0 | return null; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public String getRequestURI() { |
| 129 | 0 | return getContextPath() + getServletPath() + getPathInfo(); |
| 130 | |
} |
| 131 | |
|
| 132 | |
public StringBuffer getRequestURL() { |
| 133 | 0 | throw new UnsupportedOperationException(); |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
public String getServletPath() { |
| 138 | 0 | return getFakeResource().getServletPath(); |
| 139 | |
} |
| 140 | |
|
| 141 | |
public HttpSession getSession() { |
| 142 | 0 | return null; |
| 143 | |
} |
| 144 | |
|
| 145 | |
public HttpSession getSession(boolean create) { |
| 146 | 230 | return null; |
| 147 | |
} |
| 148 | |
|
| 149 | |
public Principal getUserPrincipal() { |
| 150 | 0 | return config.getUserPrincipal(); |
| 151 | |
} |
| 152 | |
|
| 153 | |
public boolean isRequestedSessionIdFromCookie() { |
| 154 | 0 | return false; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
public boolean isRequestedSessionIdFromUrl() { |
| 159 | 0 | return false; |
| 160 | |
} |
| 161 | |
|
| 162 | |
public boolean isRequestedSessionIdFromURL() { |
| 163 | 0 | return false; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public boolean isRequestedSessionIdValid() { |
| 167 | 0 | return false; |
| 168 | |
} |
| 169 | |
|
| 170 | |
public boolean isUserInRole(String role) { |
| 171 | 0 | return context.isUserInRole(getUserPrincipal(), role); |
| 172 | |
} |
| 173 | |
} |