OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 package org.adblockplus.libadblockplus; |
| 18 |
| 19 import java.util.ArrayList; |
| 20 import java.util.HashMap; |
| 21 import java.util.List; |
| 22 |
| 23 import com.github.rjeschke.neetutils.collections.Tuple; |
| 24 |
| 25 public class ServerResponse |
| 26 { |
| 27 public static enum NsStatus |
| 28 { |
| 29 OK(0), ERROR_FAILURE(0x80004005), ERROR_OUT_OF_MEMORY(0x8007000e), ERROR_MAL
FORMED_URI(0x804b000a), ERROR_CONNECTION_REFUSED(0x804b000d), ERROR_NET_TIMEOUT( |
| 30 0x804b000e), ERROR_NO_CONTENT(0x804b0011), ERROR_UNKNOWN_PROTOCOL(0x804b
0012), ERROR_NET_RESET(0x804b0014), ERROR_UNKNOWN_HOST(0x804b001e), ERROR_REDIRE
CT_LOOP( |
| 31 0x804b001f), ERROR_UNKNOWN_PROXY_HOST(0x804b002a), ERROR_NET_INTERRU
PT(0x804b0047), ERROR_UNKNOWN_PROXY_CONNECTION_REFUSED(0x804b0048), CUSTOM_ERROR
_BASE( |
| 32 0x80850000), ERROR_NOT_INITIALIZED(0xc1f30001); |
| 33 |
| 34 private final long statusCode; |
| 35 private final static HashMap<Long, NsStatus> ENUM_MAP = new HashMap<Long, Se
rverResponse.NsStatus>(); |
| 36 |
| 37 static |
| 38 { |
| 39 for (final NsStatus e : NsStatus.values()) |
| 40 { |
| 41 ENUM_MAP.put(e.statusCode, e); |
| 42 } |
| 43 } |
| 44 |
| 45 private NsStatus(final long value) |
| 46 { |
| 47 statusCode = value; |
| 48 } |
| 49 |
| 50 public long getStatusCode() |
| 51 { |
| 52 return statusCode; |
| 53 } |
| 54 |
| 55 public static NsStatus fromStatusCode(final long code) |
| 56 { |
| 57 final NsStatus status = ENUM_MAP.get(code); |
| 58 return status != null ? status : ERROR_FAILURE; |
| 59 } |
| 60 } |
| 61 |
| 62 private long status = NsStatus.OK.getStatusCode(); |
| 63 private int responseStatus = 400; |
| 64 private String[] headers = null; |
| 65 // TODO: This (and the whole downloading) is a waste of memory, change String |
| 66 // to something more suitable |
| 67 private String response = null; |
| 68 |
| 69 public NsStatus getStatus() |
| 70 { |
| 71 return NsStatus.fromStatusCode(this.status); |
| 72 } |
| 73 |
| 74 public void setStatus(final NsStatus status) |
| 75 { |
| 76 this.status = status.getStatusCode(); |
| 77 } |
| 78 |
| 79 public int getResponseStatus() |
| 80 { |
| 81 return this.responseStatus; |
| 82 } |
| 83 |
| 84 public void setResponseStatus(final int status) |
| 85 { |
| 86 this.responseStatus = status; |
| 87 } |
| 88 |
| 89 public String getResponse() |
| 90 { |
| 91 return this.response; |
| 92 } |
| 93 |
| 94 public void setResponse(final String response) |
| 95 { |
| 96 this.response = response; |
| 97 } |
| 98 |
| 99 public List<Tuple<String, String>> getResponseHeaders() |
| 100 { |
| 101 final ArrayList<Tuple<String, String>> ret = new ArrayList<Tuple<String, Str
ing>>(); |
| 102 |
| 103 if (this.headers != null) |
| 104 { |
| 105 for (int i = 0; i < this.headers.length; i += 2) |
| 106 { |
| 107 ret.add(Tuple.of(this.headers[i], this.headers[i + 1])); |
| 108 } |
| 109 } |
| 110 |
| 111 return ret; |
| 112 } |
| 113 |
| 114 public void setReponseHeaders(final List<Tuple<String, String>> headers) |
| 115 { |
| 116 if (headers.isEmpty()) |
| 117 { |
| 118 this.headers = null; |
| 119 } |
| 120 else |
| 121 { |
| 122 this.headers = new String[headers.size() * 2]; |
| 123 |
| 124 int i = 0; |
| 125 for (final Tuple<String, String> e : headers) |
| 126 { |
| 127 this.headers[i] = e.a; |
| 128 this.headers[i + 1] = e.b; |
| 129 i += 2; |
| 130 } |
| 131 } |
| 132 } |
| 133 } |
OLD | NEW |