BasicRequestObject.java
/***************************************************************************
Copyright 2009 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.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.metanotion.util.Dictionary;
import net.metanotion.util.MapDictionaryAdapter;
import net.metanotion.web.Cookie;
import net.metanotion.web.HttpMethod;
import net.metanotion.web.RequestObject;
/** A very minimal request object implementation. */
public final class BasicRequestObject implements RequestObject {
private static final Map<String,Cookie> NO_COOKIES = Collections.unmodifiableMap(new HashMap<String,Cookie>());
private static final Map<String,String> NO_HEADERS = Collections.unmodifiableMap(new HashMap<String,String>());
private static final Dictionary<String,Object> EMPTY_DICT =
new MapDictionaryAdapter<>(new HashMap<String,Object>());
private final Dictionary<String,Object> variables;
private final HttpMethod method;
private final String server;
private final String resource;
private final Map<String,String> headers;
private final Map<String,Cookie> cookies;
/** Create an instance with default values, basically "GET /" and no parameters. */
public BasicRequestObject() {
this(HttpMethod.GET, null, "/", EMPTY_DICT, NO_HEADERS, NO_COOKIES);
}
/** Create a GET request for the resource provided with no variables.
@param resource The resource this request object is requesting.
*/
public BasicRequestObject(final String resource) {
this(HttpMethod.GET, null, resource, EMPTY_DICT, NO_HEADERS, NO_COOKIES);
}
/** Create a GET request for the resource provided with no variables.
@param server The host this request object is requesting.
@param resource The resource this request object is requesting.
*/
public BasicRequestObject(final String server, final String resource) {
this(HttpMethod.GET, server, resource, EMPTY_DICT, NO_HEADERS, NO_COOKIES);
}
/** Create a request for the resource provided using the HTTP verb with no variables.
@param method The HTTP verb of the request.
@param resource The resource this request object is requesting.
*/
public BasicRequestObject(final HttpMethod method, final String resource) {
this(method, null, resource, EMPTY_DICT, NO_HEADERS, NO_COOKIES);
}
/** Create a request for the resource provided using the HTTP verb with the provided variables.
@param method The HTTP verb of the request.
@param resource The resource this request object is requesting.
@param vars The variables of the request.
*/
public BasicRequestObject(final HttpMethod method, final String resource, final Map<String,Object> vars) {
this(method, null, resource, new MapDictionaryAdapter<>(vars), NO_HEADERS, NO_COOKIES);
}
/** Create a request for the resource provided using the HTTP verb with the provided variables.
@param method The HTTP verb of the request.
@param resource The resource this request object is requesting.
@param vars The variables of the request.
*/
public BasicRequestObject(final HttpMethod method, final String resource, final Dictionary<String,Object> vars) {
this(method, null, resource, vars, NO_HEADERS, NO_COOKIES);
}
/** Create a request for the resource provided using the HTTP verb with the provided variables, headers, and cookies.
@param method The HTTP verb of the request.
@param server The host this request object is requesting.
@param resource The resource this request object is requesting.
@param vars The variables of the request.
@param headers A map of the headers assocated with this request.
@param cookies A map of the cookies associated with this request.
*/
public BasicRequestObject( final HttpMethod method,
final String server,
final String resource,
final Dictionary<String,Object> vars,
final Map<String,String> headers,
final Map<String, Cookie> cookies) {
this.method = method;
this.server = server;
this.resource = resource;
this.variables = vars;
this.headers = headers;
this.cookies = cookies;
}
@Override public Object get(final String name) { return this.variables.get(name); }
@Override public HttpMethod getMethod() { return this.method; }
@Override public String getServer() { return this.server; }
@Override public String getResource() { return resource; }
@Override public String getRawResource() { return resource; }
@Override public String getQuery() { throw new UnsupportedOperationException(); }
@Override public Reader getRawPost() { throw new UnsupportedOperationException(); }
@Override public InputStream getByteStream() { throw new UnsupportedOperationException(); }
@Override public String getRemoteAddr() { throw new UnsupportedOperationException(); }
@Override public String getRemoteHost() { throw new UnsupportedOperationException(); }
@Override public String getHeader(final String name) { return headers.get(name); }
@Override public Iterable<String> getHeaders() { return headers.keySet(); }
@Override public Date getDateHeader(final String name) { return null; }
@Override public Cookie getCookie(final String name) { return cookies.get(name); }
@Override public Iterable<String> getCookies() { return cookies.keySet(); }
}