001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java $
003: * $Revision: 573864 $
004: * $Date: 2007-09-08 17:53:25 +0200 (Sat, 08 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl;
033:
034: import java.util.Locale;
035:
036: import org.apache.http.HttpResponse;
037: import org.apache.http.HttpResponseFactory;
038: import org.apache.http.ProtocolVersion;
039: import org.apache.http.StatusLine;
040: import org.apache.http.message.BasicHttpResponse;
041: import org.apache.http.message.BasicStatusLine;
042: import org.apache.http.protocol.HttpContext;
043: import org.apache.http.ReasonPhraseCatalog;
044: import org.apache.http.impl.EnglishReasonPhraseCatalog;
045:
046: /**
047: * Default implementation of a factory for creating response objects.
048: *
049: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
050: *
051: * @version $Revision: 573864 $
052: *
053: * @since 4.0
054: */
055: public class DefaultHttpResponseFactory implements HttpResponseFactory {
056:
057: /** The catalog for looking up reason phrases. */
058: protected final ReasonPhraseCatalog reasonCatalog;
059:
060: /**
061: * Creates a new response factory with the given catalog.
062: *
063: * @param catalog the catalog of reason phrases
064: */
065: public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) {
066: if (catalog == null) {
067: throw new IllegalArgumentException(
068: "Reason phrase catalog must not be null.");
069: }
070: this .reasonCatalog = catalog;
071: }
072:
073: /**
074: * Creates a new response factory with the default catalog.
075: * The default catalog is
076: * {@link EnglishReasonPhraseCatalog EnglishReasonPhraseCatalog}.
077: */
078: public DefaultHttpResponseFactory() {
079: this (EnglishReasonPhraseCatalog.INSTANCE);
080: }
081:
082: // non-javadoc, see interface HttpResponseFactory
083: public HttpResponse newHttpResponse(final ProtocolVersion ver,
084: final int status, HttpContext context) {
085: if (ver == null) {
086: throw new IllegalArgumentException(
087: "HTTP version may not be null");
088: }
089: final Locale loc = determineLocale(context);
090: final String reason = reasonCatalog.getReason(status, loc);
091: StatusLine statusline = new BasicStatusLine(ver, status, reason);
092: return new BasicHttpResponse(statusline, reasonCatalog, loc);
093: }
094:
095: // non-javadoc, see interface HttpResponseFactory
096: public HttpResponse newHttpResponse(final StatusLine statusline,
097: HttpContext context) {
098: if (statusline == null) {
099: throw new IllegalArgumentException(
100: "Status line may not be null");
101: }
102: final Locale loc = determineLocale(context);
103: return new BasicHttpResponse(statusline, reasonCatalog, loc);
104: }
105:
106: /**
107: * Determines the locale of the response.
108: * The implementation in this class always returns the default locale.
109: *
110: * @param context the context from which to determine the locale, or
111: * <code>null</code> to use the default locale
112: *
113: * @return the locale for the response, never <code>null</code>
114: */
115: protected Locale determineLocale(HttpContext context) {
116: return Locale.getDefault();
117: }
118: }
|