|
3 | 3 | import android.content.ContentResolver; |
4 | 4 | import android.net.Uri; |
5 | 5 |
|
| 6 | +import androidx.annotation.NonNull; |
| 7 | + |
| 8 | +import org.docspell.docspellshare.data.Option; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import okhttp3.ConnectionSpec; |
| 18 | +import okhttp3.MediaType; |
| 19 | +import okhttp3.MultipartBody; |
| 20 | +import okhttp3.OkHttpClient; |
| 21 | +import okhttp3.Request; |
| 22 | +import okhttp3.RequestBody; |
| 23 | +import okio.BufferedSink; |
| 24 | +import okio.Okio; |
| 25 | +import okio.Source; |
| 26 | + |
| 27 | +import static org.docspell.docspellshare.util.Strings.requireNonEmpty; |
| 28 | + |
6 | 29 | public final class HttpRequest { |
7 | | - private HttpRequest() {} |
| 30 | + private static final OkHttpClient client = |
| 31 | + new OkHttpClient.Builder() |
| 32 | + .connectionSpecs( |
| 33 | + Arrays.asList( |
| 34 | + ConnectionSpec.MODERN_TLS, |
| 35 | + ConnectionSpec.COMPATIBLE_TLS, |
| 36 | + ConnectionSpec.CLEARTEXT)) |
| 37 | + .readTimeout(5, TimeUnit.MINUTES) |
| 38 | + .writeTimeout(5, TimeUnit.MINUTES) |
| 39 | + .followRedirects(true) |
| 40 | + .followSslRedirects(true) |
| 41 | + .build(); |
| 42 | + |
| 43 | + private final String url; |
| 44 | + private final List<DataPart> data; |
| 45 | + |
| 46 | + private HttpRequest(String url, List<DataPart> data) { |
| 47 | + this.url = requireNonEmpty(url, "url must be specified"); |
| 48 | + this.data = data; |
| 49 | + } |
| 50 | + |
| 51 | + public void execute() throws IOException { |
| 52 | + MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); |
| 53 | + for (DataPart dp : data) { |
| 54 | + body.addFormDataPart("file", dp.getName(), createPartBody(dp)); |
| 55 | + } |
8 | 56 |
|
9 | | - public void execute() {} |
| 57 | + Request req = new Request.Builder().url(url).post(body.build()).build(); |
| 58 | + client.newCall(req).execute(); |
| 59 | + } |
10 | 60 |
|
11 | 61 | public static Builder newBuilder() { |
12 | 62 | return new Builder(); |
13 | 63 | } |
14 | 64 |
|
15 | 65 | public static class Builder { |
| 66 | + private final List<DataPart> parts = new ArrayList<>(); |
| 67 | + private String url; |
| 68 | + |
| 69 | + public Builder addFile(ContentResolver resolver, Uri data, String type) { |
| 70 | + parts.add( |
| 71 | + new DataPart() { |
| 72 | + @Override |
| 73 | + public InputStream getData() throws IOException { |
| 74 | + return resolver.openInputStream(data); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getName() { |
| 79 | + return data.getLastPathSegment(); |
| 80 | + } |
16 | 81 |
|
17 | | - public Builder addFile(ContentResolver resolver, Uri data) { |
| 82 | + @Override |
| 83 | + public Option<String> getType() { |
| 84 | + return Option.of(type); |
| 85 | + } |
| 86 | + }); |
18 | 87 | return this; |
19 | 88 | } |
20 | 89 |
|
21 | 90 | public Builder setUrl(String url) { |
| 91 | + this.url = url; |
22 | 92 | return this; |
23 | 93 | } |
24 | 94 |
|
25 | 95 | public HttpRequest build() { |
26 | | - return new HttpRequest(); |
| 96 | + return new HttpRequest(url, parts); |
27 | 97 | } |
28 | 98 | } |
| 99 | + |
| 100 | + public interface DataPart { |
| 101 | + InputStream getData() throws IOException; |
| 102 | + |
| 103 | + String getName(); |
| 104 | + |
| 105 | + Option<String> getType(); |
| 106 | + } |
| 107 | + |
| 108 | + private static RequestBody createPartBody(DataPart part) { |
| 109 | + final String octetStream = "application/octet-stream"; |
| 110 | + return new RequestBody() { |
| 111 | + @Override |
| 112 | + public MediaType contentType() { |
| 113 | + final MediaType mt = MediaType.parse(part.getType().orElse(octetStream)); |
| 114 | + return mt != null ? mt : MediaType.get(octetStream); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void writeTo(@NonNull BufferedSink sink) throws IOException { |
| 119 | + try (InputStream in = part.getData(); |
| 120 | + Source source = Okio.source(in)) { |
| 121 | + sink.writeAll(source); |
| 122 | + } |
| 123 | + } |
| 124 | + }; |
| 125 | + } |
29 | 126 | } |
0 commit comments