com.mtvnet.boxspring.httpobjects.HttpServletObjectsHolder Maven / Gradle / Ivy
Show all versions of boxspring-http-objects-holder Show documentation
package com.mtvnet.boxspring.httpobjects;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Holder class to expose the http request and response objects in the form of a
* thread-bound object.
*
*
* Use {@link HttpServletObjectsHolderFilter} to expose the current web request.
*
* @author edelsonj
* @see HttpServletObjectsHolderFilter
*/
public class HttpServletObjectsHolder {
private static final HttpServletRequest currentHttpServletRequest = new CurrentHttpServletRequest();
private static final HttpServletResponse currentHttpServletResponse = new CurrentHttpServletResponse();
private static final ThreadLocal requestHolder = new ThreadLocal();
private static final ThreadLocal responseHolder = new ThreadLocal();
/**
* Return the HttpServletRequest currently bound to the thread.
*
* @return the HttpServletRequest currently bound to the thread, or
* null
if none bound
*/
public static HttpServletRequest getRequest() {
return requestHolder.get();
}
/**
* Return a static facade to the current request. Warning - the object that
* this method returns may throw a NullPointerException is there is no
* current HttpServletRequest.
*
* @return the request facade
*/
public static HttpServletRequest getRequestFacade() {
return currentHttpServletRequest;
}
/**
* Return the HttpServletResponse currently bound to the thread.
*
* @return the HttpServletResponse currently bound to the thread, or
* null
if none bound
*/
public static HttpServletResponse getResponse() {
return responseHolder.get();
}
/**
* Return a static facade to the current response. Warning - the object that
* this method returns may throw a NullPointerException is there is no
* current HttpServletResponse.
*
* @return the response facade
*/
public static HttpServletResponse getResponseFacade() {
return currentHttpServletResponse;
}
/**
* Bind the given HttpServletRequest to the current thread.
*
* @param request the HttpServletRequest to expose
*/
public static void setRequest(HttpServletRequest request) {
requestHolder.set(request);
}
/**
* Bind the given HttpServletResponse to the current thread.
*
* @param response the HttpServletResponse to expose
*/
public static void setResponse(HttpServletResponse response) {
responseHolder.set(response);
}
private static class CurrentHttpServletRequest implements HttpServletRequest {
public Object getAttribute(String name) {
return getRequest().getAttribute(name);
}
public Enumeration getAttributeNames() {
return getRequest().getAttributeNames();
}
public String getAuthType() {
return getRequest().getAuthType();
}
public String getCharacterEncoding() {
return getRequest().getCharacterEncoding();
}
public int getContentLength() {
return getRequest().getContentLength();
}
public String getContentType() {
return getRequest().getContentType();
}
public String getContextPath() {
return getRequest().getContextPath();
}
public Cookie[] getCookies() {
return getRequest().getCookies();
}
public long getDateHeader(String name) {
return getRequest().getDateHeader(name);
}
public String getHeader(String name) {
return getRequest().getHeader(name);
}
public Enumeration getHeaderNames() {
return getRequest().getHeaderNames();
}
public Enumeration getHeaders(String name) {
return getRequest().getHeaders(name);
}
public ServletInputStream getInputStream() throws IOException {
return getRequest().getInputStream();
}
public int getIntHeader(String name) {
return getRequest().getIntHeader(name);
}
public String getLocalAddr() {
return getRequest().getLocalAddr();
}
public Locale getLocale() {
return getRequest().getLocale();
}
public Enumeration getLocales() {
return getRequest().getLocales();
}
public String getLocalName() {
return getRequest().getLocalName();
}
public int getLocalPort() {
return getRequest().getLocalPort();
}
public String getMethod() {
return getRequest().getMethod();
}
public String getParameter(String name) {
return getRequest().getParameter(name);
}
public Map getParameterMap() {
return getRequest().getParameterMap();
}
public Enumeration getParameterNames() {
return getRequest().getParameterNames();
}
public String[] getParameterValues(String name) {
return getRequest().getParameterValues(name);
}
public String getPathInfo() {
return getRequest().getPathInfo();
}
public String getPathTranslated() {
return getRequest().getPathTranslated();
}
public String getProtocol() {
return getRequest().getProtocol();
}
public String getQueryString() {
return getRequest().getQueryString();
}
public BufferedReader getReader() throws IOException {
return getRequest().getReader();
}
public String getRealPath(String path) {
return getRequest().getRealPath(path);
}
public String getRemoteAddr() {
return getRequest().getRemoteAddr();
}
public String getRemoteHost() {
return getRequest().getRemoteHost();
}
public int getRemotePort() {
return getRequest().getRemotePort();
}
public String getRemoteUser() {
return getRequest().getRemoteUser();
}
public RequestDispatcher getRequestDispatcher(String path) {
return getRequest().getRequestDispatcher(path);
}
public String getRequestedSessionId() {
return getRequest().getRequestedSessionId();
}
public String getRequestURI() {
return getRequest().getRequestURI();
}
public StringBuffer getRequestURL() {
return getRequest().getRequestURL();
}
public String getScheme() {
return getRequest().getScheme();
}
public String getServerName() {
return getRequest().getServerName();
}
public int getServerPort() {
return getRequest().getServerPort();
}
public String getServletPath() {
return getRequest().getServletPath();
}
public HttpSession getSession() {
return getRequest().getSession();
}
public HttpSession getSession(boolean create) {
return getRequest().getSession(create);
}
public Principal getUserPrincipal() {
return getRequest().getUserPrincipal();
}
public boolean isRequestedSessionIdFromCookie() {
return getRequest().isRequestedSessionIdFromCookie();
}
public boolean isRequestedSessionIdFromUrl() {
return getRequest().isRequestedSessionIdFromUrl();
}
public boolean isRequestedSessionIdFromURL() {
return getRequest().isRequestedSessionIdFromURL();
}
public boolean isRequestedSessionIdValid() {
return getRequest().isRequestedSessionIdValid();
}
public boolean isSecure() {
return getRequest().isSecure();
}
public boolean isUserInRole(String role) {
return getRequest().isUserInRole(role);
}
public void removeAttribute(String name) {
getRequest().removeAttribute(name);
}
public void setAttribute(String name, Object o) {
getRequest().setAttribute(name, o);
}
public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
getRequest().setCharacterEncoding(env);
}
}
private static class CurrentHttpServletResponse implements HttpServletResponse {
public void addCookie(Cookie cookie) {
getResponse().addCookie(cookie);
}
public void addDateHeader(String name, long date) {
getResponse().addDateHeader(name, date);
}
public void addHeader(String name, String value) {
getResponse().addHeader(name, value);
}
public void addIntHeader(String name, int value) {
getResponse().addIntHeader(name, value);
}
public boolean containsHeader(String name) {
return getResponse().containsHeader(name);
}
public String encodeRedirectUrl(String url) {
return getResponse().encodeRedirectUrl(url);
}
public String encodeRedirectURL(String url) {
return getResponse().encodeRedirectURL(url);
}
public String encodeUrl(String url) {
return getResponse().encodeUrl(url);
}
public String encodeURL(String url) {
return getResponse().encodeURL(url);
}
public void flushBuffer() throws IOException {
getResponse().flushBuffer();
}
public int getBufferSize() {
return getResponse().getBufferSize();
}
public String getCharacterEncoding() {
return getResponse().getCharacterEncoding();
}
public String getContentType() {
return getResponse().getContentType();
}
public Locale getLocale() {
return getResponse().getLocale();
}
public ServletOutputStream getOutputStream() throws IOException {
return getResponse().getOutputStream();
}
public PrintWriter getWriter() throws IOException {
return getResponse().getWriter();
}
public boolean isCommitted() {
return getResponse().isCommitted();
}
public void reset() {
getResponse().reset();
}
public void resetBuffer() {
getResponse().resetBuffer();
}
public void sendError(int sc) throws IOException {
getResponse().sendError(sc);
}
public void sendError(int sc, String msg) throws IOException {
getResponse().sendError(sc, msg);
}
public void sendRedirect(String location) throws IOException {
getResponse().sendRedirect(location);
}
public void setBufferSize(int size) {
getResponse().setBufferSize(size);
}
public void setCharacterEncoding(String charset) {
getResponse().setCharacterEncoding(charset);
}
public void setContentLength(int len) {
getResponse().setContentLength(len);
}
public void setContentType(String type) {
getResponse().setContentType(type);
}
public void setDateHeader(String name, long date) {
getResponse().setDateHeader(name, date);
}
public void setHeader(String name, String value) {
getResponse().setHeader(name, value);
}
public void setIntHeader(String name, int value) {
getResponse().setIntHeader(name, value);
}
public void setLocale(Locale loc) {
getResponse().setLocale(loc);
}
public void setStatus(int sc) {
getResponse().setStatus(sc);
}
public void setStatus(int sc, String sm) {
getResponse().setStatus(sc, sm);
}
}
}