1 /* 2 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java $ 3 * $Revision: 744524 $ 4 * $Date: 2009-02-14 17:59:14 +0100 (Sat, 14 Feb 2009) $ 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 factory for creating {@link HttpResponse} objects. 48 * 49 * 50 * @version $Revision: 744524 $ 51 * 52 * @since 4.0 53 */ 54 public class DefaultHttpResponseFactory implements HttpResponseFactory { 55 56 /** The catalog for looking up reason phrases. */ 57 protected final ReasonPhraseCatalog reasonCatalog; 58 59 60 /** 61 * Creates a new response factory with the given catalog. 62 * 63 * @param catalog the catalog of reason phrases 64 */ 65 public DefaultHttpResponseFactory(ReasonPhraseCatalog catalog) { 66 if (catalog == null) { 67 throw new IllegalArgumentException 68 ("Reason phrase catalog must not be null."); 69 } 70 this.reasonCatalog = catalog; 71 } 72 73 /** 74 * Creates a new response factory with the default catalog. 75 * The default catalog is {@link EnglishReasonPhraseCatalog}. 76 */ 77 public DefaultHttpResponseFactory() { 78 this(EnglishReasonPhraseCatalog.INSTANCE); 79 } 80 81 82 // non-javadoc, see interface HttpResponseFactory 83 public HttpResponse newHttpResponse(final ProtocolVersion ver, 84 final int status, 85 HttpContext context) { 86 if (ver == null) { 87 throw new IllegalArgumentException("HTTP version may not be null"); 88 } 89 final Locale loc = determineLocale(context); 90 final String reason = reasonCatalog.getReason(status, loc); 91 StatusLine statusline = new BasicStatusLine(ver, status, reason); 92 return new BasicHttpResponse(statusline, reasonCatalog, loc); 93 } 94 95 96 // non-javadoc, see interface HttpResponseFactory 97 public HttpResponse newHttpResponse(final StatusLine statusline, 98 HttpContext context) { 99 if (statusline == null) { 100 throw new IllegalArgumentException("Status line may not be null"); 101 } 102 final Locale loc = determineLocale(context); 103 return new BasicHttpResponse(statusline, reasonCatalog, loc); 104 } 105 106 107 /** 108 * Determines the locale of the response. 109 * The implementation in this class always returns the default locale. 110 * 111 * @param context the context from which to determine the locale, or 112 * <code>null</code> to use the default locale 113 * 114 * @return the locale for the response, never <code>null</code> 115 */ 116 protected Locale determineLocale(HttpContext context) { 117 return Locale.getDefault(); 118 } 119 }