Skip to content

Commit e4ec9a2

Browse files
authored
Merge pull request #57 from mirisbowring/Fix-#51
Implement Custom User-Agent to prevent firewall banning
2 parents 348e7d6 + 9df3046 commit e4ec9a2

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

app/src/main/java/org/docspell/docspellshare/http/HttpRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class HttpRequest {
3535
ConnectionSpec.CLEARTEXT))
3636
.readTimeout(5, TimeUnit.MINUTES)
3737
.writeTimeout(5, TimeUnit.MINUTES)
38+
.addInterceptor(new UserAgentInterceptor())
3839
.socketFactory(new RestrictedSocketFactory(CHUNK_SIZE))
3940
.followRedirects(true)
4041
.followSslRedirects(true)
@@ -48,15 +49,15 @@ private HttpRequest(String url, List<DataPart> data) {
4849
this.data = data;
4950
}
5051

51-
public int execute(ProgressListener progressListener) throws IOException {
52+
public Response execute(ProgressListener progressListener) throws IOException {
5253
MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM);
5354
for (DataPart dp : data) {
5455
body.addFormDataPart("file", dp.getName(), createPartBody(dp, progressListener));
5556
}
5657

5758
Request req = new Request.Builder().url(url).post(body.build()).build();
5859
Response response = client.newCall(req).execute();
59-
return response.code();
60+
return response;
6061
}
6162

6263
public static Builder newBuilder() {

app/src/main/java/org/docspell/docspellshare/http/UploadManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.concurrent.Executors;
88
import java.util.concurrent.atomic.AtomicReference;
99

10+
import okhttp3.Response;
11+
1012
public final class UploadManager {
1113

1214
private static final UploadManager INSTANCE = new UploadManager();
@@ -46,8 +48,13 @@ static class UploadWorker implements Runnable {
4648
public void run() {
4749
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
4850
try {
49-
int code = request.execute(listener);
50-
listener.onFinish(code);
51+
Response resp = null;
52+
try {
53+
resp = request.execute(listener);
54+
listener.onFinish(resp.code());
55+
} finally {
56+
resp.close();
57+
}
5158
} catch (IOException e) {
5259
Log.e("upload", "Error uploading!", e);
5360
listener.onException(e);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.docspell.docspellshare.http;
2+
3+
import android.os.Build;
4+
5+
import org.docspell.docspellshare.BuildConfig;
6+
7+
import java.io.IOException;
8+
import java.util.Locale;
9+
10+
import okhttp3.Interceptor;
11+
import okhttp3.Request;
12+
import okhttp3.Response;
13+
14+
/**
15+
* Adds a custom {@code User-Agent} header to OkHttp requests.
16+
*/
17+
public class UserAgentInterceptor implements Interceptor {
18+
19+
public final String userAgent;
20+
21+
public UserAgentInterceptor(String userAgent) {
22+
this.userAgent = userAgent;
23+
}
24+
25+
public UserAgentInterceptor() {
26+
this(String.format(Locale.US,
27+
"%s/%s (Android %s; %s; %s %s; %s)",
28+
"Docspellshare",
29+
BuildConfig.VERSION_NAME,
30+
Build.VERSION.RELEASE,
31+
Build.MODEL,
32+
Build.BRAND,
33+
Build.DEVICE,
34+
Locale.getDefault().getLanguage()));
35+
}
36+
37+
@Override
38+
public Response intercept(Chain chain) throws IOException {
39+
Request userAgentRequest = chain.request()
40+
.newBuilder()
41+
.header("User-Agent", userAgent)
42+
.build();
43+
return chain.proceed(userAgentRequest);
44+
}
45+
}

0 commit comments

Comments
 (0)