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 * @deprecated Please use {@link java.net.URL#openConnection} instead. 56 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 57 * for further details. 58 */ 59 @Deprecated 60 public class DefaultHttpResponseFactory implements HttpResponseFactory { 61 62 /** The catalog for looking up reason phrases. */ 63 protected final ReasonPhraseCatalog reasonCatalog; 64 65 66 /** 67 * Creates a new response factory with the given catalog. 68 * 69 * @param catalog the catalog of reason phrases 70 */ 71 public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) { 72 if (catalog == null) { 73 throw new IllegalArgumentException 74 ("Reason phrase catalog must not be null."); 75 } 76 this.reasonCatalog = catalog; 77 } 78 79 /** 80 * Creates a new response factory with the default catalog. 81 * The default catalog is 82 * {@link EnglishReasonPhraseCatalog EnglishReasonPhraseCatalog}. 83 */ 84 public DefaultHttpResponseFactory() { 85 this(EnglishReasonPhraseCatalog.INSTANCE); 86 } 87 88 89 // non-javadoc, see interface HttpResponseFactory 90 public HttpResponse newHttpResponse(final ProtocolVersion ver, 91 final int status, 92 HttpContext context) { 93 if (ver == null) { 94 throw new IllegalArgumentException("HTTP version may not be null"); 95 } 96 final Locale loc = determineLocale(context); 97 final String reason = reasonCatalog.getReason(status, loc); 98 StatusLine statusline = new BasicStatusLine(ver, status, reason); 99 return new BasicHttpResponse(statusline, reasonCatalog, loc); 100 } 101 102 103 // non-javadoc, see interface HttpResponseFactory 104 public HttpResponse newHttpResponse(final StatusLine statusline, 105 HttpContext context) { 106 if (statusline == null) { 107 throw new IllegalArgumentException("Status line may not be null"); 108 } 109 final Locale loc = determineLocale(context); 110 return new BasicHttpResponse(statusline, reasonCatalog, loc); 111 } 112 113 114 /** 115 * Determines the locale of the response. 116 * The implementation in this class always returns the default locale. 117 * 118 * @param context the context from which to determine the locale, or 119 * <code>null</code> to use the default locale 120 * 121 * @return the locale for the response, never <code>null</code> 122 */ 123 protected Locale determineLocale(HttpContext context) { 124 return Locale.getDefault(); 125 } 126 } 127