DelegatingRequestObject.java

/***************************************************************************
   Copyright 2008 Emily Estes

   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 net.metanotion.web.concrete;


import java.io.InputStream;
import java.io.Reader;
import java.util.Date;
import java.util.Map;

import net.metanotion.web.Cookie;
import net.metanotion.web.HttpMethod;
import net.metanotion.web.RequestObject;

/** A RequestObject implementation that wraps another request object instance and overrides the resource, HTTP method,
	and variables, but otherwise returns the same values as the object it wraps. */
public final class DelegatingRequestObject implements RequestObject {
	private final RequestObject parent;
	private final Map<String,Object> variables;
	private final String resource;

	/** Create a new RequestObject that delegates to another RequestObject instance.
		@param parent The request object instance to default/delegate to.
		@param vars The values to replace the parent values in calls to {@link #get}.
	*/
	public DelegatingRequestObject(final RequestObject parent, final Map<String,Object> vars) {
		this(parent, null, vars);
	}

	/** Create a new RequestObject that delegates to another RequestObject instance.
		@param parent The request object instance to default/delegate to.
		@param resource The new URI to return in calls to {@link #getResource}.
		@param vars The values to replace the parent values in calls to {@link #get}.
	*/
	public DelegatingRequestObject(final RequestObject parent, final String resource, final Map<String,Object> vars) {
		this.parent = parent;
		this.resource = resource;
		this.variables = vars;
	}

	@Override public Object get(final String name) {
		return (this.variables.containsKey(name)) ? this.variables.get(name) : parent.get(name);
	}

	@Override public String getResource() {
		return (resource != null) ? resource : this.parent.getResource();
	}

	@Override public String getRawResource() { return this.parent.getRawResource(); }
	@Override public HttpMethod getMethod() { return this.parent.getMethod(); }
	@Override public String getServer() { return this.parent.getServer(); }
	@Override public String getQuery() { return this.parent.getQuery(); }
	@Override public Reader getRawPost() { return this.parent.getRawPost(); }
	@Override public InputStream getByteStream() { return this.parent.getByteStream(); }
	@Override public String getRemoteAddr() { return this.parent.getRemoteAddr(); }
	@Override public String getRemoteHost() { return this.parent.getRemoteHost(); }
	@Override public String getHeader(final String name) { return this.parent.getHeader(name); }
	@Override public Iterable<String> getHeaders() { return this.parent.getHeaders(); }
	@Override public Date getDateHeader(final String name) { return this.parent.getDateHeader(name); }
	@Override public Cookie getCookie(final String name) { return this.parent.getCookie(name); }
	@Override public Iterable<String> getCookies() { return this.parent.getCookies(); }
}