SlotRequester.java

  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;
 33
 34import java.net.MalformedURLException;
 35import java.net.URL;
 36import java.util.HashMap;
 37
 38import eu.siacs.conversations.Config;
 39import eu.siacs.conversations.entities.Account;
 40import eu.siacs.conversations.entities.DownloadableFile;
 41import eu.siacs.conversations.parser.IqParser;
 42import eu.siacs.conversations.services.XmppConnectionService;
 43import eu.siacs.conversations.xml.Element;
 44import eu.siacs.conversations.xml.Namespace;
 45import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 46import eu.siacs.conversations.xmpp.Jid;
 47
 48public class SlotRequester {
 49
 50	private XmppConnectionService service;
 51
 52	public SlotRequester(XmppConnectionService service) {
 53		this.service = service;
 54	}
 55
 56	public void request(Method method, Account account, DownloadableFile file, String mime, String md5, OnSlotRequested callback) {
 57		if (method == Method.HTTP_UPLOAD) {
 58			Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD);
 59			requestHttpUpload(account, host, file, mime, callback);
 60		} else if (method == Method.HTTP_UPLOAD_LEGACY) {
 61			Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD_LEGACY);
 62			requestHttpUploadLegacy(account, host, file, mime, callback);
 63		} else {
 64			requestP1S3(account, Jid.of(account.getServer()), file.getName(), md5, callback);
 65		}
 66	}
 67
 68	private void requestHttpUploadLegacy(Account account, Jid host, DownloadableFile file, String mime, OnSlotRequested callback) {
 69		IqPacket request = service.getIqGenerator().requestHttpUploadLegacySlot(host, file, mime);
 70		service.sendIqPacket(account, request, (a, packet) -> {
 71			if (packet.getType() == IqPacket.TYPE.RESULT) {
 72				Element slotElement = packet.findChild("slot", Namespace.HTTP_UPLOAD_LEGACY);
 73				if (slotElement != null) {
 74					try {
 75						final String putUrl = slotElement.findChildContent("put");
 76						final String getUrl = slotElement.findChildContent("get");
 77						if (getUrl != null && putUrl != null) {
 78							Slot slot = new Slot(new URL(putUrl));
 79							slot.getUrl = new URL(getUrl);
 80							slot.headers = new HashMap<>();
 81							slot.headers.put("Content-Type", mime == null ? "application/octet-stream" : mime);
 82							callback.success(slot);
 83							return;
 84						}
 85					} catch (MalformedURLException e) {
 86						//fall through
 87					}
 88				}
 89			}
 90			Log.d(Config.LOGTAG, account.getJid().toString() + ": invalid response to slot request " + packet);
 91			callback.failure(IqParser.extractErrorMessage(packet));
 92		});
 93
 94	}
 95
 96	private void requestHttpUpload(Account account, Jid host, DownloadableFile file, String mime, OnSlotRequested callback) {
 97		IqPacket request = service.getIqGenerator().requestHttpUploadSlot(host, file, mime);
 98		service.sendIqPacket(account, request, (a, packet) -> {
 99			if (packet.getType() == IqPacket.TYPE.RESULT) {
100				Element slotElement = packet.findChild("slot", Namespace.HTTP_UPLOAD);
101				if (slotElement != null) {
102					try {
103						final Element put = slotElement.findChild("put");
104						final Element get = slotElement.findChild("get");
105						final String putUrl = put == null ? null : put.getAttribute("url");
106						final String getUrl = get == null ? null : get.getAttribute("url");
107						if (getUrl != null && putUrl != null) {
108							Slot slot = new Slot(new URL(putUrl));
109							slot.getUrl = new URL(getUrl);
110							slot.headers = new HashMap<>();
111							for (Element child : put.getChildren()) {
112								if ("header".equals(child.getName())) {
113									final String name = child.getAttribute("name");
114									final String value = child.getContent();
115									if (HttpUploadConnection.WHITE_LISTED_HEADERS.contains(name) && value != null && !value.trim().contains("\n")) {
116										slot.headers.put(name, value.trim());
117									}
118								}
119							}
120							slot.headers.put("Content-Type", mime == null ? "application/octet-stream" : mime);
121							callback.success(slot);
122							return;
123						}
124					} catch (MalformedURLException e) {
125						//fall through
126					}
127				}
128			}
129			Log.d(Config.LOGTAG, account.getJid().toString() + ": invalid response to slot request " + packet);
130			callback.failure(IqParser.extractErrorMessage(packet));
131		});
132
133	}
134
135	private void requestP1S3(final Account account, Jid host, String filename, String md5, OnSlotRequested callback) {
136		IqPacket request = service.getIqGenerator().requestP1S3Slot(host, md5);
137		service.sendIqPacket(account, request, (a, packet) -> {
138			if (packet.getType() == IqPacket.TYPE.RESULT) {
139				String putUrl = packet.query(Namespace.P1_S3_FILE_TRANSFER).getAttribute("upload");
140				String id = packet.query().getAttribute("fileid");
141				try {
142					if (putUrl != null && id != null) {
143						Slot slot = new Slot(new URL(putUrl));
144						slot.getUrl = P1S3UrlStreamHandler.of(id, filename);
145						slot.headers = new HashMap<>();
146						slot.headers.put("Content-MD5", md5);
147						slot.headers.put("Content-Type", " "); //required to force it to empty. otherwise library will set something
148						callback.success(slot);
149						return;
150					}
151				} catch (MalformedURLException e) {
152					//fall through;
153				}
154			}
155			callback.failure("unable to request slot");
156		});
157		Log.d(Config.LOGTAG, "requesting slot with p1. md5=" + md5);
158	}
159
160
161	public interface OnSlotRequested {
162
163		void success(Slot slot);
164
165		void failure(String message);
166
167	}
168
169	public static class Slot {
170		private final URL putUrl;
171		private URL getUrl;
172		private HashMap<String, String> headers;
173
174		private Slot(URL putUrl) {
175			this.putUrl = putUrl;
176		}
177
178		public URL getPutUrl() {
179			return putUrl;
180		}
181
182		public URL getGetUrl() {
183			return getUrl;
184		}
185
186		public HashMap<String, String> getHeaders() {
187			return headers;
188		}
189	}
190}