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.graphics.BitmapFactory;
 13
 14import eu.siacs.conversations.entities.Downloadable;
 15import eu.siacs.conversations.entities.DownloadableFile;
 16import eu.siacs.conversations.entities.Message;
 17import eu.siacs.conversations.services.XmppConnectionService;
 18
 19public class HttpConnection implements Downloadable {
 20
 21	private HttpConnectionManager mHttpConnectionManager;
 22	private XmppConnectionService mXmppConnectionService;
 23
 24	private URL mUrl;
 25	private Message message;
 26	private DownloadableFile file;
 27	private long mPreviousFileSize = Long.MIN_VALUE;
 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			message.setStatus(Message.STATUS_RECEIVED_CHECKING);
 47			mXmppConnectionService.updateConversationUi();
 48			checkFileSize();
 49		} catch (MalformedURLException e) {
 50			this.cancel();
 51		}
 52	}
 53	
 54	public void init(Message message, URL url) {
 55		this.message = message;
 56		this.message.setDownloadable(this);
 57		this.mUrl = url;
 58		this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
 59		this.mPreviousFileSize = message.getImageParams().size;
 60		message.setType(Message.TYPE_IMAGE);
 61		message.setStatus(Message.STATUS_RECEIVED_CHECKING);
 62		mXmppConnectionService.updateConversationUi();
 63		checkFileSize();
 64	}
 65	
 66	private void checkFileSize() {
 67		new Thread(new FileSizeChecker()).start();
 68	}
 69
 70	public void cancel() {
 71		mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED);
 72		mHttpConnectionManager.finishConnection(this);
 73	}
 74	
 75	private void finish() {
 76		message.setStatus(Message.STATUS_RECEIVED);
 77		mXmppConnectionService.updateMessage(message);
 78		mHttpConnectionManager.finishConnection(this);
 79	}
 80
 81	private class FileSizeChecker implements Runnable {
 82
 83		@Override
 84		public void run() {
 85			long size;
 86			try {
 87				size = retrieveFileSize();
 88			} catch (IOException e) {
 89				cancel();
 90				return;
 91			}
 92			file.setExpectedSize(size);
 93			message.setBody(mUrl.toString()+","+String.valueOf(size));
 94			if (size <= mHttpConnectionManager.getAutoAcceptFileSize() || size == mPreviousFileSize) {
 95				mXmppConnectionService.updateMessage(message);
 96				start();
 97			} else {
 98				message.setStatus(Message.STATUS_RECEIVED_OFFER);
 99				mXmppConnectionService.updateMessage(message);
100			}
101		}
102
103		private long retrieveFileSize() throws IOException {
104			HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
105			connection.setRequestMethod("HEAD");
106			if (connection instanceof HttpsURLConnection) {
107				
108			}
109			String contentLength = connection.getHeaderField("Content-Length");
110			if (contentLength == null) {
111				throw new IOException();
112			}
113			try {
114				return Long.parseLong(contentLength, 10);
115			} catch (NumberFormatException e) {
116				throw new IOException();
117			}
118		}
119
120	}
121	
122	private class FileDownloader implements Runnable {
123
124		@Override
125		public void run() {
126			try {
127				mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING);
128				download();
129				updateImageBounds();
130				finish();
131			} catch (IOException e) {
132				cancel();
133			}
134		}
135		
136		private void download() throws IOException {
137			HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
138			if (connection instanceof HttpsURLConnection) {
139				
140			}
141			BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
142			OutputStream os = file.createOutputStream();
143			int count = -1;
144			byte[] buffer = new byte[1024];
145			while ((count = is.read(buffer)) != -1) {
146				os.write(buffer, 0, count);
147			}
148			os.flush();
149			os.close();
150			is.close();
151		}
152		
153		private void updateImageBounds() {
154			BitmapFactory.Options options = new BitmapFactory.Options();
155			options.inJustDecodeBounds = true;
156			BitmapFactory.decodeFile(file.getAbsolutePath(), options);
157			int imageHeight = options.outHeight;
158			int imageWidth = options.outWidth;
159			message.setBody(mUrl.toString()+","+file.getSize() + ','
160					+ imageWidth + ',' + imageHeight);
161			
162		}
163		
164	}
165}