001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl; 029 030import java.util.Locale; 031 032import org.apache.http.HttpResponse; 033import org.apache.http.HttpResponseFactory; 034import org.apache.http.ProtocolVersion; 035import org.apache.http.ReasonPhraseCatalog; 036import org.apache.http.StatusLine; 037import org.apache.http.annotation.ThreadingBehavior; 038import org.apache.http.annotation.Contract; 039import org.apache.http.message.BasicHttpResponse; 040import org.apache.http.message.BasicStatusLine; 041import org.apache.http.protocol.HttpContext; 042import org.apache.http.util.Args; 043 044/** 045 * Default factory for creating {@link HttpResponse} objects. 046 * 047 * @since 4.0 048 */ 049@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 050public class DefaultHttpResponseFactory implements HttpResponseFactory { 051 052 public static final DefaultHttpResponseFactory INSTANCE = new DefaultHttpResponseFactory(); 053 054 /** The catalog for looking up reason phrases. */ 055 protected final ReasonPhraseCatalog reasonCatalog; 056 057 058 /** 059 * Creates a new response factory with the given catalog. 060 * 061 * @param catalog the catalog of reason phrases 062 */ 063 public DefaultHttpResponseFactory(final ReasonPhraseCatalog catalog) { 064 this.reasonCatalog = Args.notNull(catalog, "Reason phrase catalog"); 065 } 066 067 /** 068 * Creates a new response factory with the default catalog. 069 * The default catalog is {@link EnglishReasonPhraseCatalog}. 070 */ 071 public DefaultHttpResponseFactory() { 072 this(EnglishReasonPhraseCatalog.INSTANCE); 073 } 074 075 076 // non-javadoc, see interface HttpResponseFactory 077 @Override 078 public HttpResponse newHttpResponse( 079 final ProtocolVersion ver, 080 final int status, 081 final HttpContext context) { 082 Args.notNull(ver, "HTTP version"); 083 final Locale loc = determineLocale(context); 084 final String reason = this.reasonCatalog.getReason(status, loc); 085 final StatusLine statusline = new BasicStatusLine(ver, status, reason); 086 return new BasicHttpResponse(statusline, this.reasonCatalog, loc); 087 } 088 089 090 // non-javadoc, see interface HttpResponseFactory 091 @Override 092 public HttpResponse newHttpResponse( 093 final StatusLine statusline, 094 final HttpContext context) { 095 Args.notNull(statusline, "Status line"); 096 return new BasicHttpResponse(statusline, this.reasonCatalog, determineLocale(context)); 097 } 098 099 /** 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}