View Javadoc

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