HttpConnection.java

  1package eu.siacs.conversations.http;
  2
  3import java.io.BufferedInputStream;
  4import java.io.IOException;
  5import java.io.OutputStream;
  6import java.net.HttpURLConnection;
  7import java.net.MalformedURLException;
  8import java.net.URL;
  9
 10import javax.net.ssl.HttpsURLConnection;
 11
 12import android.util.Log;
 13
 14import eu.siacs.conversations.Config;
 15import eu.siacs.conversations.entities.Downloadable;
 16import eu.siacs.conversations.entities.DownloadableFile;
 17import eu.siacs.conversations.entities.Message;
 18import eu.siacs.conversations.services.XmppConnectionService;
 19
 20public class HttpConnection implements Downloadable {
 21
 22	private HttpConnectionManager mHttpConnectionManager;
 23	private XmppConnectionService mXmppConnectionService;
 24
 25	private URL mUrl;
 26	private Message message;
 27	private DownloadableFile file;
 28
 29	public HttpConnection(HttpConnectionManager manager) {
 30		this.mHttpConnectionManager = manager;
 31		this.mXmppConnectionService = manager.getXmppConnectionService();
 32	}
 33
 34	@Override
 35	public void start() {
 36		new Thread(new FileDownloader()).start();
 37	}
 38
 39	public void init(Message message) {
 40		this.message = message;
 41		this.message.setDownloadable(this);
 42		try {
 43			mUrl = new URL(message.getBody());
 44			this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
 45			message.setType(Message.TYPE_IMAGE);
 46			mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_OFFER);
 47			checkFileSize();
 48		} catch (MalformedURLException e) {
 49			this.cancel();
 50		}
 51	}
 52	
 53	private void checkFileSize() {
 54		new Thread(new FileSizeChecker()).start();
 55	}
 56
 57	public void cancel() {
 58		mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED);
 59		Log.d(Config.LOGTAG,"canceled download");
 60	}
 61
 62	private class FileSizeChecker implements Runnable {
 63
 64		@Override
 65		public void run() {
 66			try {
 67				long size = retrieveFileSize();
 68				file.setExpectedSize(size);
 69				if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
 70					start();
 71				}
 72				Log.d(Config.LOGTAG,"file size: "+size);
 73			} catch (IOException e) {
 74				cancel();
 75			}
 76		}
 77
 78		private long retrieveFileSize() throws IOException {
 79			HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
 80			connection.setRequestMethod("HEAD");
 81			if (connection instanceof HttpsURLConnection) {
 82				
 83			}
 84			String contentLength = connection.getHeaderField("Content-Length");
 85			if (contentLength == null) {
 86				throw new IOException();
 87			}
 88			try {
 89				return Long.parseLong(contentLength, 10);
 90			} catch (NumberFormatException e) {
 91				throw new IOException();
 92			}
 93		}
 94
 95	}
 96	
 97	private class FileDownloader implements Runnable {
 98
 99		@Override
100		public void run() {
101			try {
102				mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING);
103				download();
104				mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED);
105			} catch (IOException e) {
106				cancel();
107			}
108		}
109		
110		private void download() throws IOException {
111			HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
112			if (connection instanceof HttpsURLConnection) {
113				
114			}
115			BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
116			OutputStream os = file.createOutputStream();
117			int count = -1;
118			byte[] buffer = new byte[1024];
119			while ((count = is.read(buffer)) != -1) {
120				os.write(buffer, 0, count);
121			}
122			os.flush();
123			os.close();
124			is.close();
125			Log.d(Config.LOGTAG,"finished downloading "+file.getAbsolutePath().toString());
126		}
127		
128	}
129}