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.impl; michael@0: michael@0: import java.util.Locale; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: import ch.boye.httpclientandroidlib.HttpResponseFactory; michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion; michael@0: import ch.boye.httpclientandroidlib.StatusLine; michael@0: import ch.boye.httpclientandroidlib.message.BasicHttpResponse; michael@0: import ch.boye.httpclientandroidlib.message.BasicStatusLine; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: import ch.boye.httpclientandroidlib.ReasonPhraseCatalog; michael@0: import ch.boye.httpclientandroidlib.impl.EnglishReasonPhraseCatalog; michael@0: michael@0: /** michael@0: * Default factory for creating {@link HttpResponse} objects. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class DefaultHttpResponseFactory implements HttpResponseFactory { michael@0: michael@0: /** The catalog for looking up reason phrases. */ michael@0: protected final ReasonPhraseCatalog reasonCatalog; michael@0: michael@0: michael@0: /** michael@0: * Creates a new response factory with the given catalog. michael@0: * michael@0: * @param catalog the catalog of reason phrases michael@0: */ michael@0: public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) { michael@0: if (catalog == null) { michael@0: throw new IllegalArgumentException michael@0: ("Reason phrase catalog must not be null."); michael@0: } michael@0: this.reasonCatalog = catalog; michael@0: } michael@0: michael@0: /** michael@0: * Creates a new response factory with the default catalog. michael@0: * The default catalog is {@link EnglishReasonPhraseCatalog}. michael@0: */ michael@0: public DefaultHttpResponseFactory() { michael@0: this(EnglishReasonPhraseCatalog.INSTANCE); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HttpResponseFactory michael@0: public HttpResponse newHttpResponse(final ProtocolVersion ver, michael@0: final int status, michael@0: HttpContext context) { michael@0: if (ver == null) { michael@0: throw new IllegalArgumentException("HTTP version may not be null"); michael@0: } michael@0: final Locale loc = determineLocale(context); michael@0: final String reason = reasonCatalog.getReason(status, loc); michael@0: StatusLine statusline = new BasicStatusLine(ver, status, reason); michael@0: return new BasicHttpResponse(statusline, reasonCatalog, loc); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HttpResponseFactory michael@0: public HttpResponse newHttpResponse(final StatusLine statusline, michael@0: HttpContext context) { michael@0: if (statusline == null) { michael@0: throw new IllegalArgumentException("Status line may not be null"); michael@0: } michael@0: final Locale loc = determineLocale(context); michael@0: return new BasicHttpResponse(statusline, reasonCatalog, loc); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Determines the locale of the response. michael@0: * The implementation in this class always returns the default locale. michael@0: * michael@0: * @param context the context from which to determine the locale, or michael@0: * null to use the default locale michael@0: * michael@0: * @return the locale for the response, never null michael@0: */ michael@0: protected Locale determineLocale(HttpContext context) { michael@0: return Locale.getDefault(); michael@0: } michael@0: }