OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 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 | |
18 package org.adblockplus.libadblockplus; | |
19 | |
20 import java.util.ArrayList; | |
21 import java.util.HashMap; | |
22 import java.util.List; | |
23 | |
24 public final class ServerResponse | |
25 { | |
26 public static enum NsStatus | |
27 { | |
28 OK(0), ERROR_FAILURE(0x80004005), ERROR_OUT_OF_MEMORY(0x8007000e), ERROR_MAL
FORMED_URI(0x804b000a), ERROR_CONNECTION_REFUSED(0x804b000d), ERROR_NET_TIMEOUT( | |
29 0x804b000e), ERROR_NO_CONTENT(0x804b0011), ERROR_UNKNOWN_PROTOCOL(0x804b
0012), ERROR_NET_RESET(0x804b0014), ERROR_UNKNOWN_HOST(0x804b001e), ERROR_REDIRE
CT_LOOP( | |
30 0x804b001f), ERROR_UNKNOWN_PROXY_HOST(0x804b002a), ERROR_NET_INTERRUPT(0
x804b0047), ERROR_UNKNOWN_PROXY_CONNECTION_REFUSED(0x804b0048), CUSTOM_ERROR_BAS
E( | |
31 0x80850000), ERROR_NOT_INITIALIZED(0xc1f30001); | |
32 | |
33 private final long statusCode; | |
34 private final static HashMap<Long, NsStatus> ENUM_MAP = new HashMap<Long, Se
rverResponse.NsStatus>(); | |
35 | |
36 static | |
37 { | |
38 for (final NsStatus e : NsStatus.values()) | |
39 { | |
40 ENUM_MAP.put(e.statusCode, e); | |
41 } | |
42 } | |
43 | |
44 private NsStatus(final long value) | |
45 { | |
46 this.statusCode = value; | |
47 } | |
48 | |
49 public long getStatusCode() | |
50 { | |
51 return this.statusCode; | |
52 } | |
53 | |
54 public static NsStatus fromStatusCode(final long code) | |
55 { | |
56 final NsStatus status = ENUM_MAP.get(code); | |
57 return status != null ? status : ERROR_FAILURE; | |
58 } | |
59 } | |
60 | |
61 private long status = NsStatus.OK.getStatusCode(); | |
62 private int responseStatus = 400; | |
63 private String[] headers = null; | |
64 // TODO: This (and the whole downloading) is a waste of memory, change String | |
65 // to something more suitable | |
66 private String response = null; | |
67 | |
68 public NsStatus getStatus() | |
69 { | |
70 return NsStatus.fromStatusCode(this.status); | |
71 } | |
72 | |
73 public void setStatus(final NsStatus status) | |
74 { | |
75 this.status = status.getStatusCode(); | |
76 } | |
77 | |
78 public int getResponseStatus() | |
79 { | |
80 return this.responseStatus; | |
81 } | |
82 | |
83 public void setResponseStatus(final int status) | |
84 { | |
85 this.responseStatus = status; | |
86 } | |
87 | |
88 public String getResponse() | |
89 { | |
90 return this.response; | |
91 } | |
92 | |
93 public void setResponse(final String response) | |
94 { | |
95 this.response = response; | |
96 } | |
97 | |
98 public List<HeaderEntry> getResponseHeaders() | |
99 { | |
100 final ArrayList<HeaderEntry> ret = new ArrayList<HeaderEntry>(); | |
101 | |
102 if (this.headers != null) | |
103 { | |
104 for (int i = 0; i < this.headers.length; i += 2) | |
105 { | |
106 ret.add(HeaderEntry.of(this.headers[i], this.headers[i + 1])); | |
107 } | |
108 } | |
109 | |
110 return ret; | |
111 } | |
112 | |
113 public void setReponseHeaders(final List<HeaderEntry> headers) | |
114 { | |
115 if (headers.isEmpty()) | |
116 { | |
117 this.headers = null; | |
118 } | |
119 else | |
120 { | |
121 this.headers = new String[headers.size() * 2]; | |
122 | |
123 int i = 0; | |
124 for (final HeaderEntry e : headers) | |
125 { | |
126 this.headers[i] = e.getKey(); | |
127 this.headers[i + 1] = e.getValue(); | |
128 i += 2; | |
129 } | |
130 } | |
131 } | |
132 } | |
OLD | NEW |