org.picketbox.http.authentication.SavedRequest Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.picketbox.http.authentication;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
/**
*
* This class is a representation of the state of a previous {@link HttpServletRequest} instance.
*
*
* @author Pedro Silva
*/
public class SavedRequest {
private List cookies = new ArrayList();
private Map headers = new HashMap();
private Map parameters = new HashMap();
private String method;
private String queryString;
private String requestURI;
private String scheme;
private String contextPath;
/**
*
* Create a new instance copying the state from the request passed as argument.
*
*/
public SavedRequest(HttpServletRequest request) {
copyCookies(request);
copyHeaders(request);
copyParameters(request);
// copy general properties from the original request
this.method = request.getMethod();
this.queryString = request.getQueryString();
this.requestURI = request.getRequestURI();
this.scheme = request.getScheme();
this.contextPath = request.getContextPath();
}
/**
*
* Returns the parameters copied from the original request.
*
*/
public Map getParameters() {
return this.parameters;
}
/**
*
* Returns the headers copied from the original request.
*
*/
public Map getHeaders() {
return this.headers;
}
/**
*
* Returns the cookies copied from the original request.
*
*/
public List getCookies() {
return this.cookies;
}
/**
*
* Returns the original HTTP method used by the original request.
*
*/
public String getMethod() {
return this.method;
}
/**
*
* Returns the querystring used by the original request.
*
*/
public String getQueryString() {
return this.queryString;
}
/**
*
* Returns the requestURI used by the original request.
*
*/
public String getRequestURI() {
return this.requestURI;
}
/**
*
* Returns the original scheme used by the original request.
*
*/
public String getScheme() {
return this.scheme;
}
/**
*
* Returns the original context path used by the original request.
*
*/
public String getContextPath() {
return this.contextPath;
}
/**
*
* Copy the parameters from the original {@link HttpServletRequest}.
*
*/
private void copyParameters(HttpServletRequest request) {
Set> parametersEntries = request.getParameterMap().entrySet();
for (Entry parameter : parametersEntries) {
this.getParameters().put(parameter.getKey(), (String[]) parameter.getValue());
}
}
/**
*
* Copy the headers from the original {@link HttpServletRequest}.
*
*/
private void copyHeaders(HttpServletRequest request) {
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
String headerValue = request.getHeader(headerName);
this.getHeaders().put(headerName, headerValue);
}
}
/**
*
* Copy the cookies from the original {@link HttpServletRequest}.
*
*/
private void copyCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return;
}
for (Cookie cookie : cookies) {
this.getCookies().add(cookie);
}
}
}