package de.thedevstack.conversationsplus.services.filetransfer.http.download; import java.io.IOException; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.entities.FileParams; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.http.Http; import de.thedevstack.conversationsplus.http.HttpClient; import de.thedevstack.conversationsplus.http.HttpHeadRetrievedListener; import de.thedevstack.conversationsplus.persistance.DatabaseBackend; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.UiUpdateHelper; import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.Response; /** * */ public class HttpRetrieveHead implements Http, Callback { private static final String LOGTAG = "http-retrieve-head"; private Message message; private String url; private HttpHeadRetrievedListener listener; public HttpRetrieveHead(Message message) { this.message = message; this.url = (null != message && null != message.getFileParams()) ? message.getFileParams().getUrl() : null; if (null == this.url) { /* * If this code is reached and the URL is null something went wrong. * Try again to extract the file parameters from the message. */ MessageUtil.extractFileParamsFromBody(message); this.url = (null != message.getFileParams()) ? message.getFileParams().getUrl() : null; if (null == this.url) { message.setTreatAsDownloadable(Message.Decision.NEVER); // TODO find sth better if (null != message.getFileParams()) { MessageUtil.setAndSaveFileStatus(message, FileStatus.NOT_FOUND); } } } } public void retrieveAndSetContentTypeAndLength() { if (null != this.url) { Logging.d(LOGTAG, "retrieve file size and mime type."); try { MessageUtil.setAndSaveFileStatus(message, FileStatus.CHECKING_FILE_SIZE); HttpClient.retrieveHead(this.url, this); } catch (IOException e) { Logging.e(LOGTAG, "Error while trying to call '" + url + "'.", e); } } } private static long parseContentLength(String contentLength) { long length = -1; if (null != contentLength) { try { length = Long.parseLong(contentLength, 10); } catch (NumberFormatException e) { } } return length; } @Override public void onFailure(Call call, IOException e) { Logging.e(LOGTAG, "Error while trying to call '" + call.request().url() + "'.", e); } @Override public void onResponse(Call call, Response response) throws IOException { Logging.d(LOGTAG, "Response for retrieving file size and mime type received."); FileParams fileParams = message.getFileParams(); if (response.isSuccessful()) { MediaType mediaType = response.body().contentType(); String contentType = null != mediaType ? (mediaType.type() + "/" + mediaType.subtype()) : response.header(MIME_REQUEST_PROPERTY_NAME); String contentLength = response.header(HEADER_NAME_CONTENT_LENGTH); long size = parseContentLength(contentLength); fileParams.setSize(size); fileParams.setMimeType(contentType); if (0 < size) { fileParams.setFileStatus(FileStatus.NEEDS_DOWNLOAD); } DatabaseBackend.getInstance().updateMessage(message); UiUpdateHelper.updateConversationUi(); if (null != this.listener) { this.listener.onFileSizeRetrieved(size, this.message); } } else { if (response.code() == HTTP_NOT_FOUND) { Logging.d(LOGTAG, "remote file '" + response.request().url() + "' not found."); MessageUtil.setAndSaveFileStatus(message, FileStatus.NOT_FOUND); } else { Logging.d(LOGTAG, "remote file '" + response.request().url() + "' not loaded - response code: " + response.code() + "."); } } } public void setListener(HttpHeadRetrievedListener listener) { this.listener = listener; } }