|
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 ch.boye.httpclientandroidlib.impl; |
|
29 |
|
30 import java.util.Locale; |
|
31 |
|
32 import ch.boye.httpclientandroidlib.HttpResponse; |
|
33 import ch.boye.httpclientandroidlib.HttpResponseFactory; |
|
34 import ch.boye.httpclientandroidlib.ProtocolVersion; |
|
35 import ch.boye.httpclientandroidlib.StatusLine; |
|
36 import ch.boye.httpclientandroidlib.message.BasicHttpResponse; |
|
37 import ch.boye.httpclientandroidlib.message.BasicStatusLine; |
|
38 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
39 import ch.boye.httpclientandroidlib.ReasonPhraseCatalog; |
|
40 import ch.boye.httpclientandroidlib.impl.EnglishReasonPhraseCatalog; |
|
41 |
|
42 /** |
|
43 * Default factory for creating {@link HttpResponse} objects. |
|
44 * |
|
45 * @since 4.0 |
|
46 */ |
|
47 public class DefaultHttpResponseFactory implements HttpResponseFactory { |
|
48 |
|
49 /** The catalog for looking up reason phrases. */ |
|
50 protected final ReasonPhraseCatalog reasonCatalog; |
|
51 |
|
52 |
|
53 /** |
|
54 * Creates a new response factory with the given catalog. |
|
55 * |
|
56 * @param catalog the catalog of reason phrases |
|
57 */ |
|
58 public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) { |
|
59 if (catalog == null) { |
|
60 throw new IllegalArgumentException |
|
61 ("Reason phrase catalog must not be null."); |
|
62 } |
|
63 this.reasonCatalog = 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(final ProtocolVersion ver, |
|
77 final int status, |
|
78 HttpContext context) { |
|
79 if (ver == null) { |
|
80 throw new IllegalArgumentException("HTTP version may not be null"); |
|
81 } |
|
82 final Locale loc = determineLocale(context); |
|
83 final String reason = reasonCatalog.getReason(status, loc); |
|
84 StatusLine statusline = new BasicStatusLine(ver, status, reason); |
|
85 return new BasicHttpResponse(statusline, reasonCatalog, loc); |
|
86 } |
|
87 |
|
88 |
|
89 // non-javadoc, see interface HttpResponseFactory |
|
90 public HttpResponse newHttpResponse(final StatusLine statusline, |
|
91 HttpContext context) { |
|
92 if (statusline == null) { |
|
93 throw new IllegalArgumentException("Status line may not be null"); |
|
94 } |
|
95 final Locale loc = determineLocale(context); |
|
96 return new BasicHttpResponse(statusline, reasonCatalog, loc); |
|
97 } |
|
98 |
|
99 |
|
100 /** |
|
101 * Determines the locale of the response. |
|
102 * The implementation in this class always returns the default locale. |
|
103 * |
|
104 * @param context the context from which to determine the locale, or |
|
105 * <code>null</code> to use the default locale |
|
106 * |
|
107 * @return the locale for the response, never <code>null</code> |
|
108 */ |
|
109 protected Locale determineLocale(HttpContext context) { |
|
110 return Locale.getDefault(); |
|
111 } |
|
112 } |