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