1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
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.UNDEFINED);
}
}
}
}
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;
}
}
|