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