All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.servlet.http.SetRequest Maven / Gradle / Ivy

/*
Copyright 2016 Alexander Bunkenburg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cat.inspiracio.servlet.http;

import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cat.inspiracio.url.URL;
import cat.inspiracio.url.URLParameters;
import cat.inspiracio.util.CollectionEnumeration;

/** Simulates request for unit tests. */
public class SetRequest extends InitialHttpServletRequest {

	private static final String CRLF = "\r\n";
	private static final String SP = " ";
	
	// state ----------------------------------

	private String method="GET";
	private URL url=new URL();
	private Mapheaders=new HashMap();
	private Mapattributes=new HashMap();
	private HttpSession session;

	// construction ---------------------------
	
	public SetRequest(){}
	
	public SetRequest(String url, HttpSession session){
		this.url=new URL(url);
		this.session=session;
	}

	// interface HttpServletRequest -----------
	
	public void setMethod(String m){method=m;}
	@Override public String getMethod(){return method;}

	// URL parts
	@Override public String getScheme(){return url.scheme();}
	public void setScheme(String scheme){
		if(scheme!=null && scheme.endsWith(":"))
			scheme=scheme.substring(0, scheme.length()-1);
		url.setScheme(scheme);
	}
	@Override public String getServerName(){return url.server();}
	public void setServerName(String server){url.server(server);}
	@Override public int getServerPort(){return url.port();}
	public void setServerPort(int port){url.port(port);}
	
	@Override public String getRequestURI(){return url.path();}
	public void setRequestURI(String u){url.path(u);}
	
	@Override public Enumeration getParameterNames(){
		URLParameters ps=url.getParameters();
		Collectionkeys=ps.keys();
		return new CollectionEnumeration(keys);
	}
	
	public void setParameter(String key, String value){
		URLParameters ps=url.getParameters();
		ps.set(key, value);
	}
	
	@Override public String getParameter(String name){
		URLParameters ps=url.getParameters();
		if(ps.containsKey(name))
			return ps.get(name);
		return "";//not null
	}

	// attributes
	@Override public Object getAttribute(String name){return attributes.get(name);}
	@Override public Enumeration getAttributeNames(){return new CollectionEnumeration(attributes.keySet());}
	@Override public void setAttribute(String name, Object o){attributes.put(name, o);}
	
	@Override public HttpSession getSession(){return session;}
	public void setSession(HttpSession s){session=s;}

	//headers
	public void setHeader(String key, String value){headers.put(key, value);}
	@Override public String getHeader(String name){return headers.get(name);}
	
	/** debug */
	@Override public String toString(){
		StringBuilder builder=new StringBuilder();
		//request line
		builder.append(method).append(SP).append(url).append(SP).append("HTTP/1.0").append(CRLF);
		//headers
		for(Map.Entry header : headers.entrySet())
			builder.append(header.getKey()).append(":").append(header.getValue()).append(CRLF);
		//CRLF
		//builder.append(CRLF);
		//body
		return builder.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy