SimpleHttpValues.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.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import net.metanotion.web.HttpStatus;
import net.metanotion.web.HttpValues;
/** This is a basic "container" implementation of the {@link net.metanotion.web.HttpValues} interface that simply
returns the parameters passed to it's constructors and does the simplest implementation reasonbly possible. */
public final class SimpleHttpValues implements HttpValues {
private final Iterable<Map.Entry<String,Object>> headers;
private final Iterable<Map.Entry<String,Object>> cookies;
private final int sc;
private final Object output;
/** Create a new HTTP resource with response of 200 OK.
@param headers The headers of the response.
@param cookies The cookies to send with the response.
@param output The content of the response.
*/
public SimpleHttpValues(final Map.Entry<String,Object>[] headers,
final Map.Entry<String,Object>[] cookies,
final Object output) {
this(headers, cookies, output, HttpStatus.OK.codeNumber());
}
/** Create a new HTTP resource.
@param headers The headers of the response.
@param cookies The cookies to send with the response.
@param output The content of the response.
@param status the HTTP status code of the response.
*/
public SimpleHttpValues(final Map.Entry<String,Object>[] headers,
final Map.Entry<String,Object>[] cookies,
final Object output,
final int status) {
this(Arrays.asList(headers), Arrays.asList(cookies), output, status);
}
/** Create a new HTTP resource with response of 200 OK.
@param headers The headers of the response.
@param cookies The cookies to send with the response.
@param output The content of the response.
*/
public SimpleHttpValues(final Iterable<Map.Entry<String,Object>> headers,
final Iterable<Map.Entry<String,Object>> cookies,
final Object output) {
this(headers, cookies, output, HttpStatus.OK.codeNumber());
}
/** Create a new HTTP resource.
@param headers The headers of the response.
@param cookies The cookies to send with the response.
@param output The content of the response.
@param status the HTTP status code of the response.
*/
public SimpleHttpValues(final Iterable<Map.Entry<String,Object>> headers,
final Iterable<Map.Entry<String,Object>> cookies,
final Object output,
final int status) {
this.headers = headers;
this.cookies = cookies;
this.sc = status;
this.output = output;
}
@Override public Iterator<Map.Entry<String,Object>> listHeaders() { return headers.iterator(); }
@Override public Iterator<Map.Entry<String,Object>> listCookies() { return cookies.iterator(); }
@Override public int getHttpStatus() { return sc; }
@Override public Object unwrap() { return output; }
@Override public HttpValues rewrap(final Object o, final HttpValues hv) {
return (HttpValues) new SimpleHttpValues(headers, cookies, o, sc);
}
}