Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/android/AndroidWebRequest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Patch Set: Created May 27, 2016, 1:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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.android;
19
20 import java.io.BufferedReader;
21 import java.io.InputStreamReader;
22 import java.net.HttpURLConnection;
23 import java.net.URL;
24 import java.util.HashSet;
25 import java.util.List;
26
27 import org.adblockplus.libadblockplus.AdblockPlusException;
28 import org.adblockplus.libadblockplus.FilterEngine;
29 import org.adblockplus.libadblockplus.HeaderEntry;
30 import org.adblockplus.libadblockplus.ServerResponse;
31 import org.adblockplus.libadblockplus.ServerResponse.NsStatus;
32 import org.adblockplus.libadblockplus.WebRequest;
33
34 import android.util.Log;
35
36 public class AndroidWebRequest extends WebRequest
37 {
38 public final static String TAG = Utils.getTag(WebRequest.class);
39
40 private final HashSet<String> subscriptionURLs = new HashSet<String>();
41 private final boolean elemhideEnabled;
42
43 public AndroidWebRequest(boolean enableElemhide)
44 {
45 this.elemhideEnabled = enableElemhide;
46 }
47
48 public AndroidWebRequest()
49 {
50 this(false);
51 }
52
53 private boolean isListedSubscriptionUrl(final URL url)
54 {
55 String toCheck = url.toString();
56
57 final int idx = toCheck.indexOf('?');
58 if (idx != -1)
59 {
60 toCheck = toCheck.substring(0, idx);
61 }
62
63 return this.subscriptionURLs.contains(toCheck);
64 }
65
66 protected void updateSubscriptionURLs(final FilterEngine engine)
67 {
68 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail ableSubscriptions())
69 {
70 this.subscriptionURLs.add(s.getProperty("url").toString());
71 }
72 this.subscriptionURLs.add(engine.getPref("subscriptions_exceptionsurl").toSt ring());
73 }
74
75 @Override
76 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders)
77 {
78 try
79 {
80 final URL url = new URL(urlStr);
81 Log.d(TAG, "Downloading from: " + url);
82
83 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n();
84 connection.setRequestMethod("GET");
85 connection.connect();
86
87 final ServerResponse response = new ServerResponse();
88 response.setResponseStatus(connection.getResponseCode());
89
90 if (response.getResponseStatus() == 200)
91 {
92 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8"));
93 final StringBuilder sb = new StringBuilder();
94
95 if (!this.elemhideEnabled && isListedSubscriptionUrl(url))
96 {
97 Log.d(TAG, "Removing element hiding rules from: '" + url + "'");
98
99 String line;
100 while ((line = reader.readLine()) != null)
101 {
102 // We're only appending non-element-hiding filters here.
103 //
104 // See:
105 // https://issues.adblockplus.org/ticket/303
106 //
107 // Follow-up issue for removing this hack:
108 // https://issues.adblockplus.org/ticket/1541
109 //
110 if (line.indexOf('#') == -1)
111 {
112 sb.append(line);
113 sb.append('\n');
114 }
115 }
116 }
117 else
118 {
119 int character;
120
121 while ((character = reader.read()) != -1)
122 {
123 sb.append((char) character);
124 }
125 }
126
127 connection.disconnect();
128
129 response.setStatus(NsStatus.OK);
130 response.setResponse(sb.toString());
131 }
132 else
133 {
134 response.setStatus(NsStatus.ERROR_FAILURE);
135 }
136 return response;
137 }
138 catch (final Throwable t)
139 {
140 throw new AdblockPlusException("WebRequest failed", t);
141 }
142 }
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld