1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.http;
31
32import android.util.Log;
33import com.google.common.base.Strings;
34import com.google.common.collect.ImmutableMap;
35import com.google.common.util.concurrent.Futures;
36import com.google.common.util.concurrent.ListenableFuture;
37import com.google.common.util.concurrent.MoreExecutors;
38import eu.siacs.conversations.Config;
39import eu.siacs.conversations.entities.Account;
40import eu.siacs.conversations.entities.DownloadableFile;
41import eu.siacs.conversations.services.XmppConnectionService;
42import eu.siacs.conversations.xml.Namespace;
43import eu.siacs.conversations.xmpp.Jid;
44import im.conversations.android.xmpp.model.stanza.Iq;
45import im.conversations.android.xmpp.model.upload.Header;
46import im.conversations.android.xmpp.model.upload.Slot;
47import java.util.Map;
48import okhttp3.Headers;
49import okhttp3.HttpUrl;
50
51public class SlotRequester {
52
53 private final XmppConnectionService service;
54
55 public SlotRequester(XmppConnectionService service) {
56 this.service = service;
57 }
58
59 public ListenableFuture<Slot> request(
60 final Account account, final DownloadableFile file, final String mime) {
61 final var result =
62 account.getXmppConnection()
63 .getServiceDiscoveryResultByFeature(Namespace.HTTP_UPLOAD);
64 if (result == null) {
65 return Futures.immediateFailedFuture(
66 new IllegalStateException("No HTTP upload host found"));
67 }
68 return requestHttpUpload(account, result.getKey(), file, mime);
69 }
70
71 private ListenableFuture<Slot> requestHttpUpload(
72 final Account account, final Jid host, final DownloadableFile file, final String mime) {
73 final Iq request = service.getIqGenerator().requestHttpUploadSlot(host, file, mime);
74 final var iqFuture = service.sendIqPacket(account, request);
75 return Futures.transform(
76 iqFuture,
77 response -> {
78 final var slot =
79 response.getExtension(
80 im.conversations.android.xmpp.model.upload.Slot.class);
81 if (slot == null) {
82 Log.d(Config.LOGTAG, "-->" + response);
83 throw new IllegalStateException("Slot not found in IQ response");
84 }
85 final var getUrl = slot.getGetUrl();
86 final var put = slot.getPut();
87 if (getUrl == null || put == null) {
88 throw new IllegalStateException("Missing get or put in slot response");
89 }
90 final var putUrl = put.getUrl();
91 if (putUrl == null) {
92 throw new IllegalStateException("Missing put url");
93 }
94 final var headers = new ImmutableMap.Builder<String, String>();
95 for (final Header header : put.getHeaders()) {
96 final String name = header.getHeaderName();
97 final String value = header.getContent();
98 if (Strings.isNullOrEmpty(value) || value.contains("\n")) {
99 continue;
100 }
101 headers.put(name, value.trim());
102 }
103 headers.put("Content-Type", mime == null ? "application/octet-stream" : mime);
104 return new Slot(putUrl, getUrl, headers.buildKeepingLast());
105 },
106 MoreExecutors.directExecutor());
107 }
108
109 public static class Slot {
110 public final HttpUrl put;
111 public final HttpUrl get;
112 public final Headers headers;
113
114 private Slot(HttpUrl put, HttpUrl get, Headers headers) {
115 this.put = put;
116 this.get = get;
117 this.headers = headers;
118 }
119
120 private Slot(HttpUrl put, HttpUrl getUrl, Map<String, String> headers) {
121 this.put = put;
122 this.get = getUrl;
123 this.headers = Headers.of(headers);
124 }
125 }
126}