View Javadoc
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.hc.core5.http.message;
29  
30  import java.util.Locale;
31  
32  import org.apache.hc.core5.http.HttpResponse;
33  import org.apache.hc.core5.http.ProtocolVersion;
34  import org.apache.hc.core5.http.ReasonPhraseCatalog;
35  import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
36  import org.apache.hc.core5.util.Args;
37  import org.apache.hc.core5.util.TextUtils;
38  
39  /**
40   * Basic implementation of {@link HttpResponse}.
41   *
42   * @since 4.0
43   */
44  public class BasicHttpResponse extends HeaderGroup implements HttpResponse {
45  
46      private static final long serialVersionUID = 1L;
47  
48      private final ReasonPhraseCatalog reasonCatalog;
49  
50      private ProtocolVersion version;
51      private Locale locale;
52      private int code;
53      private String reasonPhrase;
54  
55      /**
56       * Creates a new response.
57       *
58       * @param code              the status code
59       * @param catalog           the reason phrase catalog, or
60       *                          {@code null} to disable automatic
61       *                          reason phrase lookup
62       * @param locale            the locale for looking up reason phrases, or
63       *                          {@code null} for the system locale
64       */
65      public BasicHttpResponse(
66              final int code,
67              final ReasonPhraseCatalog catalog,
68              final Locale locale) {
69          super();
70          this.code = Args.positive(code, "Status code");
71          this.reasonCatalog = catalog != null ? catalog : EnglishReasonPhraseCatalog.INSTANCE;
72          this.locale = locale;
73      }
74  
75      /**
76       * Creates a new response.
77       *
78       * @param code          the status code of the response
79       * @param reasonPhrase  the reason phrase to the status code, or {@code null}
80       */
81      public BasicHttpResponse(final int code, final String reasonPhrase) {
82          this.code = Args.positive(code, "Status code");
83          this.reasonPhrase = reasonPhrase;
84          this.reasonCatalog = EnglishReasonPhraseCatalog.INSTANCE;
85      }
86  
87      /**
88       * Creates a new response.
89       *
90       * @param code          the status code of the response
91       */
92      public BasicHttpResponse(final int code) {
93          this.code = Args.positive(code, "Status code");
94          this.reasonPhrase = null;
95          this.reasonCatalog = EnglishReasonPhraseCatalog.INSTANCE;
96      }
97  
98      @Override
99      public void addHeader(final String name, final Object value) {
100         Args.notNull(name, "Header name");
101         addHeader(new BasicHeader(name, value));
102     }
103 
104     @Override
105     public void setHeader(final String name, final Object value) {
106         Args.notNull(name, "Header name");
107         setHeader(new BasicHeader(name, value));
108     }
109 
110     @Override
111     public void setVersion(final ProtocolVersion version) {
112         this.version = version;
113     }
114 
115     @Override
116     public ProtocolVersion getVersion() {
117         return this.version;
118     }
119 
120     @Override
121     public int getCode() {
122         return this.code;
123     }
124 
125     @Override
126     public Locale getLocale() {
127         return this.locale;
128     }
129 
130     @Override
131     public void setCode(final int code) {
132         Args.positive(code, "Status code");
133         this.code = code;
134         this.reasonPhrase = null;
135     }
136 
137     @Override
138     public String getReasonPhrase() {
139         return this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code);
140     }
141 
142     @Override
143     public void setReasonPhrase(final String reason) {
144         this.reasonPhrase = TextUtils.isBlank(reason) ? null : reason;
145     }
146 
147     @Override
148     public void setLocale(final Locale locale) {
149         this.locale = Args.notNull(locale, "Locale");
150     }
151 
152     /**
153      * Looks up a reason phrase.
154      * This method evaluates the currently set catalog and locale.
155      * It also handles a missing catalog.
156      *
157      * @param code      the status code for which to look up the reason
158      *
159      * @return  the reason phrase, or {@code null} if there is none
160      */
161     protected String getReason(final int code) {
162         return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
163                 this.locale != null ? this.locale : Locale.getDefault()) : null;
164     }
165 
166     @Override
167     public String toString() {
168         final StringBuilder sb = new StringBuilder();
169         sb.append(this.code).append(' ').append(this.reasonPhrase).append(' ').append(this.version);
170         return sb.toString();
171     }
172 
173 }