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