michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.message;
michael@0:
michael@0: import java.util.Locale;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HttpEntity;
michael@0: import ch.boye.httpclientandroidlib.HttpResponse;
michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion;
michael@0: import ch.boye.httpclientandroidlib.StatusLine;
michael@0: import ch.boye.httpclientandroidlib.ReasonPhraseCatalog;
michael@0:
michael@0: /**
michael@0: * Basic implementation of {@link HttpResponse}.
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public class BasicHttpResponse extends AbstractHttpMessage
michael@0: implements HttpResponse {
michael@0:
michael@0: private StatusLine statusline;
michael@0: private HttpEntity entity;
michael@0: private ReasonPhraseCatalog reasonCatalog;
michael@0: private Locale locale;
michael@0:
michael@0:
michael@0: /**
michael@0: * Creates a new response.
michael@0: * This is the constructor to which all others map.
michael@0: *
michael@0: * @param statusline the status line
michael@0: * @param catalog the reason phrase catalog, or
michael@0: * null
to disable automatic
michael@0: * reason phrase lookup
michael@0: * @param locale the locale for looking up reason phrases, or
michael@0: * null
for the system locale
michael@0: */
michael@0: public BasicHttpResponse(final StatusLine statusline,
michael@0: final ReasonPhraseCatalog catalog,
michael@0: final Locale locale) {
michael@0: super();
michael@0: if (statusline == null) {
michael@0: throw new IllegalArgumentException("Status line may not be null.");
michael@0: }
michael@0: this.statusline = statusline;
michael@0: this.reasonCatalog = catalog;
michael@0: this.locale = (locale != null) ? locale : Locale.getDefault();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates a response from a status line.
michael@0: * The response will not have a reason phrase catalog and
michael@0: * use the system default locale.
michael@0: *
michael@0: * @param statusline the status line
michael@0: */
michael@0: public BasicHttpResponse(final StatusLine statusline) {
michael@0: this(statusline, null, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates a response from elements of a status line.
michael@0: * The response will not have a reason phrase catalog and
michael@0: * use the system default locale.
michael@0: *
michael@0: * @param ver the protocol version of the response
michael@0: * @param code the status code of the response
michael@0: * @param reason the reason phrase to the status code, or
michael@0: * null
michael@0: */
michael@0: public BasicHttpResponse(final ProtocolVersion ver,
michael@0: final int code,
michael@0: final String reason) {
michael@0: this(new BasicStatusLine(ver, code, reason), null, null);
michael@0: }
michael@0:
michael@0:
michael@0: // non-javadoc, see interface HttpMessage
michael@0: public ProtocolVersion getProtocolVersion() {
michael@0: return this.statusline.getProtocolVersion();
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public StatusLine getStatusLine() {
michael@0: return this.statusline;
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public HttpEntity getEntity() {
michael@0: return this.entity;
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public Locale getLocale() {
michael@0: return this.locale;
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setStatusLine(final StatusLine statusline) {
michael@0: if (statusline == null) {
michael@0: throw new IllegalArgumentException("Status line may not be null");
michael@0: }
michael@0: this.statusline = statusline;
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setStatusLine(final ProtocolVersion ver, final int code) {
michael@0: // arguments checked in BasicStatusLine constructor
michael@0: this.statusline = new BasicStatusLine(ver, code, getReason(code));
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setStatusLine(final ProtocolVersion ver, final int code,
michael@0: final String reason) {
michael@0: // arguments checked in BasicStatusLine constructor
michael@0: this.statusline = new BasicStatusLine(ver, code, reason);
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setStatusCode(int code) {
michael@0: // argument checked in BasicStatusLine constructor
michael@0: ProtocolVersion ver = this.statusline.getProtocolVersion();
michael@0: this.statusline = new BasicStatusLine(ver, code, getReason(code));
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setReasonPhrase(String reason) {
michael@0:
michael@0: if ((reason != null) && ((reason.indexOf('\n') >= 0) ||
michael@0: (reason.indexOf('\r') >= 0))
michael@0: ) {
michael@0: throw new IllegalArgumentException("Line break in reason phrase.");
michael@0: }
michael@0: this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(),
michael@0: this.statusline.getStatusCode(),
michael@0: reason);
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setEntity(final HttpEntity entity) {
michael@0: this.entity = entity;
michael@0: }
michael@0:
michael@0: // non-javadoc, see interface HttpResponse
michael@0: public void setLocale(Locale loc) {
michael@0: if (loc == null) {
michael@0: throw new IllegalArgumentException("Locale may not be null.");
michael@0: }
michael@0: this.locale = loc;
michael@0: final int code = this.statusline.getStatusCode();
michael@0: this.statusline = new BasicStatusLine
michael@0: (this.statusline.getProtocolVersion(), code, getReason(code));
michael@0: }
michael@0:
michael@0: /**
michael@0: * Looks up a reason phrase.
michael@0: * This method evaluates the currently set catalog and locale.
michael@0: * It also handles a missing catalog.
michael@0: *
michael@0: * @param code the status code for which to look up the reason
michael@0: *
michael@0: * @return the reason phrase, or null
if there is none
michael@0: */
michael@0: protected String getReason(int code) {
michael@0: return (this.reasonCatalog == null) ?
michael@0: null : this.reasonCatalog.getReason(code, this.locale);
michael@0: }
michael@0:
michael@0: public String toString() {
michael@0: return this.statusline + " " + this.headergroup;
michael@0: }
michael@0:
michael@0: }