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