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

cat.inspiracio.servlet.SetServletContext 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;

import cat.inspiracio.util.CollectionEnumeration;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/** A simulation of servlet context with settable resources. */
public class SetServletContext extends InitialServletContext {

	private static final String NL = "\n";

	// state -------------------------------------------

	private String contextPath="";

	private int majorVersion;
	private int minorVersion;

	private int effectiveMajorVersion;
	private int effectiveMinorVersion;

	/** Path to resource */
	private final Mapresources= new HashMap<>();

	private final Map mimes = new HashMap<>();

	private String serverInfo;

	private final MapinitParameters=new HashMap<>();

	private final Mapattributes=new HashMap<>();

	private String servletContextName;
	
	// lifecycle ---------------------------------------

	/** Make one. */
	public SetServletContext(){}
	
	// override methods --------------------------------

	/** Adds a resource
	 * @param path under this path
	 * @param resource path to the resource.
	 * @return this */
	public SetServletContext setResource(String path, String resource){
		resources.put(path, resource);
		return this;
	}
	@Override public InputStream getResourceAsStream(String path){
		String r=resources.get(path);
		if(r==null)
			return null;
        byte[] bytes = r.getBytes();// with platform-default encoding
        return new ByteArrayInputStream(bytes);
	}

	/** Sets the context path
	 * @param path to this.
	 * @return this */
	public SetServletContext setContextPath(String path){contextPath=path;return this;}
	@Override public String getContextPath(){return contextPath;}

	/** Sets the major version
	 * @param v to this. */
	public void setMajorVersion(int v){majorVersion=v;}
	@Override public int getMajorVersion(){return majorVersion;}

	/** Sets the minor version
	 * @param v to this. */
	public void setMinorVersion(int v){minorVersion=v;}
	@Override public int getMinorVersion(){return minorVersion;}

	/** Sets effective major version
	 * @param v to this. */
	public void setEffectiveMajorVersion(int v){effectiveMajorVersion=v;}
	@Override public int getEffectiveMajorVersion(){return effectiveMajorVersion;}

	/** Sets effective minor version
	 * @param v to this. */
	public void setEffectiveMinorVersion(int v){effectiveMinorVersion=v;}
	@Override public int getEffectiveMinorVersion(){return effectiveMinorVersion;}

	/** Sets MIME type
	 * @param key for this
	 * @param value to this. */
	public void setMimeType(String key,String value){mimes.put(key,value);}
	@Override public String getMimeType(String key){return mimes.get(key);}

	@Override public void log(String msg){System.out.println(msg);}
	@Override public void log(String message, Throwable throwable){log(message + NL + throwable);}

	/** Sets server info
	 * @param i to this. */
	public void setServerInfo(String i){serverInfo=i;}
	@Override public String getServerInfo(){return serverInfo;}

	@Override public String getInitParameter(String key){return initParameters.get(key);}
	@Override public Enumeration getInitParameterNames(){return new CollectionEnumeration<>(initParameters.keySet());}
	@Override public boolean setInitParameter(String key, String value){
		boolean already=initParameters.containsKey(key);
		if(already) {
			return false;
		}else {
			initParameters.put(key, value);
			return true;
		}
	}

	@Override public Object getAttribute(String key){return attributes.get(key);}
	@Override public Enumeration getAttributeNames(){return new CollectionEnumeration<>(attributes.keySet());}
	@Override public void setAttribute(String key, Object value){attributes.put(key,value);}
	@Override public void removeAttribute(String key){attributes.remove(key);}

	/** Sets servlet context name
	 * @param n to this. */
	public void setServletContextName(String n){servletContextName=n;}
	@Override public String getServletContextName(){return servletContextName;}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy