Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.message; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.Locale; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.ProtocolVersion; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.StatusLine; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.ReasonPhraseCatalog; |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Basic implementation of {@link HttpResponse}. |
michael@0 | 40 | * |
michael@0 | 41 | * @since 4.0 |
michael@0 | 42 | */ |
michael@0 | 43 | public class BasicHttpResponse extends AbstractHttpMessage |
michael@0 | 44 | implements HttpResponse { |
michael@0 | 45 | |
michael@0 | 46 | private StatusLine statusline; |
michael@0 | 47 | private HttpEntity entity; |
michael@0 | 48 | private ReasonPhraseCatalog reasonCatalog; |
michael@0 | 49 | private Locale locale; |
michael@0 | 50 | |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Creates a new response. |
michael@0 | 54 | * This is the constructor to which all others map. |
michael@0 | 55 | * |
michael@0 | 56 | * @param statusline the status line |
michael@0 | 57 | * @param catalog the reason phrase catalog, or |
michael@0 | 58 | * <code>null</code> to disable automatic |
michael@0 | 59 | * reason phrase lookup |
michael@0 | 60 | * @param locale the locale for looking up reason phrases, or |
michael@0 | 61 | * <code>null</code> for the system locale |
michael@0 | 62 | */ |
michael@0 | 63 | public BasicHttpResponse(final StatusLine statusline, |
michael@0 | 64 | final ReasonPhraseCatalog catalog, |
michael@0 | 65 | final Locale locale) { |
michael@0 | 66 | super(); |
michael@0 | 67 | if (statusline == null) { |
michael@0 | 68 | throw new IllegalArgumentException("Status line may not be null."); |
michael@0 | 69 | } |
michael@0 | 70 | this.statusline = statusline; |
michael@0 | 71 | this.reasonCatalog = catalog; |
michael@0 | 72 | this.locale = (locale != null) ? locale : Locale.getDefault(); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * Creates a response from a status line. |
michael@0 | 77 | * The response will not have a reason phrase catalog and |
michael@0 | 78 | * use the system default locale. |
michael@0 | 79 | * |
michael@0 | 80 | * @param statusline the status line |
michael@0 | 81 | */ |
michael@0 | 82 | public BasicHttpResponse(final StatusLine statusline) { |
michael@0 | 83 | this(statusline, null, null); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Creates a response from elements of a status line. |
michael@0 | 88 | * The response will not have a reason phrase catalog and |
michael@0 | 89 | * use the system default locale. |
michael@0 | 90 | * |
michael@0 | 91 | * @param ver the protocol version of the response |
michael@0 | 92 | * @param code the status code of the response |
michael@0 | 93 | * @param reason the reason phrase to the status code, or |
michael@0 | 94 | * <code>null</code> |
michael@0 | 95 | */ |
michael@0 | 96 | public BasicHttpResponse(final ProtocolVersion ver, |
michael@0 | 97 | final int code, |
michael@0 | 98 | final String reason) { |
michael@0 | 99 | this(new BasicStatusLine(ver, code, reason), null, null); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | |
michael@0 | 103 | // non-javadoc, see interface HttpMessage |
michael@0 | 104 | public ProtocolVersion getProtocolVersion() { |
michael@0 | 105 | return this.statusline.getProtocolVersion(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | // non-javadoc, see interface HttpResponse |
michael@0 | 109 | public StatusLine getStatusLine() { |
michael@0 | 110 | return this.statusline; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // non-javadoc, see interface HttpResponse |
michael@0 | 114 | public HttpEntity getEntity() { |
michael@0 | 115 | return this.entity; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // non-javadoc, see interface HttpResponse |
michael@0 | 119 | public Locale getLocale() { |
michael@0 | 120 | return this.locale; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // non-javadoc, see interface HttpResponse |
michael@0 | 124 | public void setStatusLine(final StatusLine statusline) { |
michael@0 | 125 | if (statusline == null) { |
michael@0 | 126 | throw new IllegalArgumentException("Status line may not be null"); |
michael@0 | 127 | } |
michael@0 | 128 | this.statusline = statusline; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // non-javadoc, see interface HttpResponse |
michael@0 | 132 | public void setStatusLine(final ProtocolVersion ver, final int code) { |
michael@0 | 133 | // arguments checked in BasicStatusLine constructor |
michael@0 | 134 | this.statusline = new BasicStatusLine(ver, code, getReason(code)); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // non-javadoc, see interface HttpResponse |
michael@0 | 138 | public void setStatusLine(final ProtocolVersion ver, final int code, |
michael@0 | 139 | final String reason) { |
michael@0 | 140 | // arguments checked in BasicStatusLine constructor |
michael@0 | 141 | this.statusline = new BasicStatusLine(ver, code, reason); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | // non-javadoc, see interface HttpResponse |
michael@0 | 145 | public void setStatusCode(int code) { |
michael@0 | 146 | // argument checked in BasicStatusLine constructor |
michael@0 | 147 | ProtocolVersion ver = this.statusline.getProtocolVersion(); |
michael@0 | 148 | this.statusline = new BasicStatusLine(ver, code, getReason(code)); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | // non-javadoc, see interface HttpResponse |
michael@0 | 152 | public void setReasonPhrase(String reason) { |
michael@0 | 153 | |
michael@0 | 154 | if ((reason != null) && ((reason.indexOf('\n') >= 0) || |
michael@0 | 155 | (reason.indexOf('\r') >= 0)) |
michael@0 | 156 | ) { |
michael@0 | 157 | throw new IllegalArgumentException("Line break in reason phrase."); |
michael@0 | 158 | } |
michael@0 | 159 | this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(), |
michael@0 | 160 | this.statusline.getStatusCode(), |
michael@0 | 161 | reason); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // non-javadoc, see interface HttpResponse |
michael@0 | 165 | public void setEntity(final HttpEntity entity) { |
michael@0 | 166 | this.entity = entity; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | // non-javadoc, see interface HttpResponse |
michael@0 | 170 | public void setLocale(Locale loc) { |
michael@0 | 171 | if (loc == null) { |
michael@0 | 172 | throw new IllegalArgumentException("Locale may not be null."); |
michael@0 | 173 | } |
michael@0 | 174 | this.locale = loc; |
michael@0 | 175 | final int code = this.statusline.getStatusCode(); |
michael@0 | 176 | this.statusline = new BasicStatusLine |
michael@0 | 177 | (this.statusline.getProtocolVersion(), code, getReason(code)); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Looks up a reason phrase. |
michael@0 | 182 | * This method evaluates the currently set catalog and locale. |
michael@0 | 183 | * It also handles a missing catalog. |
michael@0 | 184 | * |
michael@0 | 185 | * @param code the status code for which to look up the reason |
michael@0 | 186 | * |
michael@0 | 187 | * @return the reason phrase, or <code>null</code> if there is none |
michael@0 | 188 | */ |
michael@0 | 189 | protected String getReason(int code) { |
michael@0 | 190 | return (this.reasonCatalog == null) ? |
michael@0 | 191 | null : this.reasonCatalog.getReason(code, this.locale); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | public String toString() { |
michael@0 | 195 | return this.statusline + " " + this.headergroup; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | } |