Home | History | Annotate | Download | only in impl
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java $
      3  * $Revision: 618367 $
      4  * $Date: 2008-02-04 10:26:06 -0800 (Mon, 04 Feb 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl;
     33 
     34 import java.util.Locale;
     35 
     36 import org.apache.http.HttpResponse;
     37 import org.apache.http.HttpResponseFactory;
     38 import org.apache.http.ProtocolVersion;
     39 import org.apache.http.StatusLine;
     40 import org.apache.http.message.BasicHttpResponse;
     41 import org.apache.http.message.BasicStatusLine;
     42 import org.apache.http.protocol.HttpContext;
     43 import org.apache.http.ReasonPhraseCatalog;
     44 import org.apache.http.impl.EnglishReasonPhraseCatalog;
     45 
     46 /**
     47  * Default implementation of a factory for creating response objects.
     48  *
     49  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     50  *
     51  * @version $Revision: 618367 $
     52  *
     53  * @since 4.0
     54  */
     55 public class DefaultHttpResponseFactory implements HttpResponseFactory {
     56 
     57     /** The catalog for looking up reason phrases. */
     58     protected final ReasonPhraseCatalog reasonCatalog;
     59 
     60 
     61     /**
     62      * Creates a new response factory with the given catalog.
     63      *
     64      * @param catalog   the catalog of reason phrases
     65      */
     66     public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) {
     67         if (catalog == null) {
     68             throw new IllegalArgumentException
     69                 ("Reason phrase catalog must not be null.");
     70         }
     71         this.reasonCatalog = catalog;
     72     }
     73 
     74     /**
     75      * Creates a new response factory with the default catalog.
     76      * The default catalog is
     77      * {@link EnglishReasonPhraseCatalog EnglishReasonPhraseCatalog}.
     78      */
     79     public DefaultHttpResponseFactory() {
     80         this(EnglishReasonPhraseCatalog.INSTANCE);
     81     }
     82 
     83 
     84     // non-javadoc, see interface HttpResponseFactory
     85     public HttpResponse newHttpResponse(final ProtocolVersion ver,
     86                                         final int status,
     87                                         HttpContext context) {
     88         if (ver == null) {
     89             throw new IllegalArgumentException("HTTP version may not be null");
     90         }
     91         final Locale loc      = determineLocale(context);
     92         final String reason   = reasonCatalog.getReason(status, loc);
     93         StatusLine statusline = new BasicStatusLine(ver, status, reason);
     94         return new BasicHttpResponse(statusline, reasonCatalog, loc);
     95     }
     96 
     97 
     98     // non-javadoc, see interface HttpResponseFactory
     99     public HttpResponse newHttpResponse(final StatusLine statusline,
    100                                         HttpContext context) {
    101         if (statusline == null) {
    102             throw new IllegalArgumentException("Status line may not be null");
    103         }
    104         final Locale loc = determineLocale(context);
    105         return new BasicHttpResponse(statusline, reasonCatalog, loc);
    106     }
    107 
    108 
    109     /**
    110      * Determines the locale of the response.
    111      * The implementation in this class always returns the default locale.
    112      *
    113      * @param context   the context from which to determine the locale, or
    114      *                  <code>null</code> to use the default locale
    115      *
    116      * @return  the locale for the response, never <code>null</code>
    117      */
    118     protected Locale determineLocale(HttpContext context) {
    119         return Locale.getDefault();
    120     }
    121 }
    122