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

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

Go to download

Dummy Servlet provides some implementations for the interfaces and classes of the Java Servlet spec that are useful for testing and simulation. Version 5.0.0 is for Servlet 5.0.0.

The newest version!
/*
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 jakarta.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();
	
	/** Really, should be a list of Map.Entry. */
	private final Mapheaders=new HashMap();
	
	private final Mapattributes=new HashMap();
	
	private HttpSession session;

	// construction ---------------------------

	/** Make one. */
	public SetRequest(){}

	/** Make a request from
	 * @param u this URL
	 * @param s with this session. */
	public SetRequest(String u, HttpSession s){
		url=new URL(u);
		session=s;
	}

	// interface HttpServletRequest -----------

	/** Sets method
	 * @param m to this.
	 * @return this */
	public SetRequest setMethod(String m){method=m;return this;}
	@Override public String getMethod(){return method;}

	// URL parts
	@Override public String getScheme(){return url.scheme();}
	/** Sets the scheme
	 * @param scheme to this.
	 * @return this */
	public SetRequest setScheme(String scheme) {
		if(scheme!=null && scheme.endsWith(":"))
			scheme=scheme.substring(0, scheme.length()-1);
		url.setScheme(scheme);
		return this;
	}
	@Override public String getServerName(){return url.server();}
	/** Sets server
	 * @param server to this.
	 * @return this */
	public SetRequest setServerName(String server){url.server(server);return this;}
	@Override public int getServerPort(){return url.port();}
	/** Sets port
	 * @param port to this.
	 * @return this */
	public SetRequest setServerPort(int port){url.port(port);return this;}
	
	@Override public String getRequestURI(){return url.path();}
	/** Sets request URI
	 * @param u to this.
	 * @return this */
	public SetRequest setRequestURI(String u){url.path(u);return this;}
	
	@Override public Enumeration getParameterNames(){
		URLParameters ps=url.getParameters();
		Collectionkeys=ps.keys();
		return new CollectionEnumeration(keys);
	}

	/** Sets a URL parameter
	 * @param key with this key
	 * @param value and this value.
	 * @return this */
	public SetRequest setParameter(String key, String value){
		URLParameters ps=url.getParameters();
		ps.set(key, value);
		return this;
	}
	
	@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;}
	/** Sets session
	 * @param s to this.
	 * @return this */
	public SetRequest setSession(HttpSession s){session=s;return this;}

	//headers
	/** Sets header
	 * @param key with this key
	 * @param value and this value.
	 * @return this */
	public SetRequest setHeader(String key, String value){headers.put(key, value);return this;}
	@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