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.ThreadingBehavior;
38  import org.apache.http.annotation.Contract;
39  import org.apache.http.message.BasicHttpResponse;
40  import org.apache.http.message.BasicStatusLine;
41  import org.apache.http.protocol.HttpContext;
42  import org.apache.http.util.Args;
43  
44  /**
45   * Default factory for creating {@link HttpResponse} objects.
46   *
47   * @since 4.0
48   */
49  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
50  public class DefaultHttpResponseFactory implements HttpResponseFactory {
51  
52      public static final DefaultHttpResponseFactory INSTANCE = new DefaultHttpResponseFactory();
53  
54      /** The catalog for looking up reason phrases. */
55      protected final ReasonPhraseCatalog reasonCatalog;
56  
57  
58      /**
59       * Creates a new response factory with the given catalog.
60       *
61       * @param catalog   the catalog of reason phrases
62       */
63      public DefaultHttpResponseFactory(final ReasonPhraseCatalog catalog) {
64          this.reasonCatalog = Args.notNull(catalog, "Reason phrase catalog");
65      }
66  
67      /**
68       * Creates a new response factory with the default catalog.
69       * The default catalog is {@link EnglishReasonPhraseCatalog}.
70       */
71      public DefaultHttpResponseFactory() {
72          this(EnglishReasonPhraseCatalog.INSTANCE);
73      }
74  
75  
76      // non-javadoc, see interface HttpResponseFactory
77      @Override
78      public HttpResponse newHttpResponse(
79              final ProtocolVersion ver,
80              final int status,
81              final HttpContext context) {
82          Args.notNull(ver, "HTTP version");
83          final Locale loc = determineLocale(context);
84          final String reason   = this.reasonCatalog.getReason(status, loc);
85          final StatusLine statusline = new BasicStatusLine(ver, status, reason);
86          return new BasicHttpResponse(statusline, this.reasonCatalog, loc);
87      }
88  
89  
90      // non-javadoc, see interface HttpResponseFactory
91      @Override
92      public HttpResponse newHttpResponse(
93              final StatusLine statusline,
94              final HttpContext context) {
95          Args.notNull(statusline, "Status line");
96          return new BasicHttpResponse(statusline, this.reasonCatalog, determineLocale(context));
97      }
98  
99      /**
100      * Determines the locale of the response.
101      * The implementation in this class always returns the default locale.
102      *
103      * @param context   the context from which to determine the locale, or
104      *                  {@code null} to use the default locale
105      *
106      * @return  the locale for the response, never {@code null}
107      */
108     protected Locale determineLocale(final HttpContext context) {
109         return Locale.getDefault();
110     }
111 
112 }