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

org.mattressframework.api.http.Response Maven / Gradle / Ivy

/*
 * Copyright 2007-2008, Josh Devins.
 *
 * 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 org.mattressframework.api.http;

import org.apache.commons.lang.Validate;

/**
 * A simple model encapsulating elements of an HTTP response.
 * 
 * 

* This does not include any entity, since that is handled by the return value * of a resource method. This differs greatly from JSR 311 which specifies that * a resource method can return a Response. Not allowing this is much more * pragmatic since it follows a rule that any entity to be returned in a * response must also be returned from the resource method. No exceptions to the * rule. End of story. *

* *

* To build the other elements of an HTTP response, an instance of * {@link Response} can be injected into a resource method. Instances are * semi-immutable since the Response must be instantiated to be passed by * reference to the resource method. Once instantiated however, the setter * methods can only be called once, or else an {@link IllegalStateException} * will be thrown. Using the constructor will create an immutable instance, * although this is only done by the framework. *

* *

* If setting the header fields manually, take care to only set headers that * pertain to the response, and not to the entity since those will be set by the * framework if needed. If entity header fields are added manually, the * framework may override them.
* TODO: More information about what headers can be overriden by the framework. *

* * * * @author Josh Devins ([email protected]) */ public final class Response { private int status = -1; private Header[] headers = null; /** * Default constructor. Setter methods can be called once after * instantiating this way. */ public Response() { // defaults already set } /** * Creates an immutable instance of this class. Internally calls * {@link #setStatus(int)} and {@link #setHeaders(Header[])}. Sets the * headers to an empty array. */ public Response(final int status) { setStatus(status); setHeaders(new Header[0]); } /** * Creates an immutable instance of this class. Internally calls * {@link #setStatus(int)} and {@link #setHeaders(Header[])}. */ public Response(final int status, final Header... headers) { setStatus(status); setHeaders(headers); } public Header[] getHeaders() { return headers; } public int getStatus() { return status; } /** * Determines if the status has been set or not. */ public boolean isStatusSet() { return status != -1; } /** * Sets the headers to be sent back with the response. This can only be * called once. * * @throws IllegalArgumentException * Thrown if the headers being set contain or are themselves * null. * @throws IllegalStateException * Thrown if this method is called more than once. */ public void setHeaders(final Header... headers) { Validate.noNullElements(headers); if (this.headers != null) { throw new IllegalStateException("Headers have already been set."); } this.headers = headers; } /** * Sets the status to be sent back with the response. This can only be * called once. * * @throws IllegalArgumentException * Thrown if the status being set is not greater than or equal * to 0. * @throws IllegalStateException * Thrown if this method is called more than once. */ public void setStatus(final int status) { Validate.isTrue(status >= 0); if (isStatusSet()) { throw new IllegalStateException("Status has already been set."); } this.status = status; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy