Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Ruben Bermudez 2017-07-20 22:38:06 +02:00
commit 8183b77fce

View File

@ -89,102 +89,45 @@ public abstract class DefaultAbsSender extends AbsSender {
throw new TelegramApiException("Parameter file can not be null"); throw new TelegramApiException("Parameter file can not be null");
} }
String url = File.getFileUrl(getBotToken(), filePath); String url = File.getFileUrl(getBotToken(), filePath);
java.io.File output; String tempFileName = Long.toString(System.currentTimeMillis());
try { return downloadToTemporaryFileWrappingExceptions(url, tempFileName);
output = java.io.File.createTempFile(Long.toString(System.currentTimeMillis()), ".tmp");
FileUtils.copyURLToFile(new URL(url), output);
} catch (MalformedURLException e) {
throw new TelegramApiException("Wrong url for file: " + url);
} catch (IOException e) {
throw new TelegramApiRequestException("Error downloading the file", e);
}
return output;
} }
public final java.io.File downloadFile(File file) throws TelegramApiException { public final java.io.File downloadFile(File file) throws TelegramApiException {
if(file == null){ assertParamNotNull(file, "file");
throw new TelegramApiException("Parameter file can not be null");
}
String url = file.getFileUrl(getBotToken()); String url = file.getFileUrl(getBotToken());
java.io.File output; String tempFileName = file.getFileId();
try { return downloadToTemporaryFileWrappingExceptions(url, tempFileName);
output = java.io.File.createTempFile(file.getFileId(), ".tmp");
FileUtils.copyURLToFile(new URL(url), output);
} catch (MalformedURLException e) {
throw new TelegramApiException("Wrong url for file: " + url);
} catch (IOException e) {
throw new TelegramApiRequestException("Error downloading the file", e);
}
return output;
} }
public final void downloadFileAsync(String filePath, DownloadFileCallback<String> callback) throws TelegramApiException { public final void downloadFileAsync(String filePath, DownloadFileCallback<String> callback) throws TelegramApiException {
if(filePath == null || filePath.isEmpty()){ if(filePath == null || filePath.isEmpty()){
throw new TelegramApiException("Parameter filePath can not be null"); throw new TelegramApiException("Parameter filePath can not be null");
} }
if (callback == null) { assertParamNotNull(callback, "callback");
throw new TelegramApiException("Parameter callback can not be null");
}
exe.submit(new Runnable() {
@Override
public void run() {
String url = File.getFileUrl(getBotToken(), filePath); String url = File.getFileUrl(getBotToken(), filePath);
try { String tempFileName = Long.toString(System.currentTimeMillis());
java.io.File output = java.io.File.createTempFile(Long.toString(System.currentTimeMillis()), ".tmp"); exe.submit(getDownloadFileAsyncJob(filePath, callback, url, tempFileName));
FileUtils.copyURLToFile(new URL(url), output);
callback.onResult(filePath, output);
} catch (MalformedURLException e) {
callback.onException(filePath, new TelegramApiException("Wrong url for file: " + url));
} catch (IOException e) {
callback.onException(filePath, new TelegramApiRequestException("Error downloading the file", e));
}
}
});
} }
public final void downloadFileAsync(File file, DownloadFileCallback<File> callback) throws TelegramApiException { public final void downloadFileAsync(File file, DownloadFileCallback<File> callback) throws TelegramApiException {
if(file == null){ assertParamNotNull(file, "file");
throw new TelegramApiException("Parameter file can not be null"); assertParamNotNull(callback, "callback");
}
if (callback == null) {
throw new TelegramApiException("Parameter callback can not be null");
}
exe.submit(new Runnable() {
@Override
public void run() {
String url = file.getFileUrl(getBotToken()); String url = file.getFileUrl(getBotToken());
try { String tempFileName = file.getFileId();
java.io.File output = java.io.File.createTempFile(file.getFileId(), ".tmp"); exe.submit(getDownloadFileAsyncJob(file, callback, url, tempFileName));
FileUtils.copyURLToFile(new URL(url), output);
callback.onResult(file, output);
} catch (MalformedURLException e) {
callback.onException(file, new TelegramApiException("Wrong url for file: " + url));
} catch (IOException e) {
callback.onException(file, new TelegramApiRequestException("Error downloading the file", e));
}
}
});
} }
// Specific Send Requests // Specific Send Requests
@Override @Override
public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException { public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
if(sendDocument == null){ assertParamNotNull(sendDocument, "sendDocument");
throw new TelegramApiException("Parameter sendDocument can not be null");
}
sendDocument.validate(); sendDocument.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendDocument.PATH; String url = getBaseUrl() + SendDocument.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId(), TEXT_PLAIN_CONTENT_TYPE); builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
@ -214,30 +157,20 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendDocument.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send document", e); throw new TelegramApiException("Unable to send document", e);
} }
return sendDocument.deserializeResponse(responseContent);
} }
@Override @Override
public final Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException { public final Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
if(sendPhoto == null){ assertParamNotNull(sendPhoto, "sendPhoto");
throw new TelegramApiException("Parameter sendPhoto can not be null");
}
sendPhoto.validate(); sendPhoto.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendPhoto.PATH; String url = getBaseUrl() + SendPhoto.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId());
@ -267,30 +200,20 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendPhoto.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send photo", e); throw new TelegramApiException("Unable to send photo", e);
} }
return sendPhoto.deserializeResponse(responseContent);
} }
@Override @Override
public final Message sendVideo(SendVideo sendVideo) throws TelegramApiException { public final Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
if(sendVideo == null){ assertParamNotNull(sendVideo, "sendVideo");
throw new TelegramApiException("Parameter sendVideo can not be null");
}
sendVideo.validate(); sendVideo.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendVideo.PATH; String url = getBaseUrl() + SendVideo.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId());
@ -329,30 +252,20 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendVideo.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send video", e); throw new TelegramApiException("Unable to send video", e);
} }
return sendVideo.deserializeResponse(responseContent);
} }
@Override @Override
public final Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException { public final Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException {
if(sendVideoNote == null){ assertParamNotNull(sendVideoNote, "sendVideoNote");
throw new TelegramApiException("Parameter sendVideoNote can not be null");
}
sendVideoNote.validate(); sendVideoNote.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendVideoNote.PATH; String url = getBaseUrl() + SendVideoNote.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId()); builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId());
@ -386,30 +299,20 @@ public abstract class DefaultAbsSender extends AbsSender {
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendVideoNote.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send video note", e); throw new TelegramApiException("Unable to send video note", e);
} }
return sendVideoNote.deserializeResponse(responseContent);
} }
@Override @Override
public final Message sendSticker(SendSticker sendSticker) throws TelegramApiException { public final Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
if(sendSticker == null){ assertParamNotNull(sendSticker, "sendSticker");
throw new TelegramApiException("Parameter sendSticker can not be null");
}
sendSticker.validate(); sendSticker.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendSticker.PATH; String url = getBaseUrl() + SendSticker.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId());
@ -436,16 +339,10 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendSticker.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e); throw new TelegramApiException("Unable to send sticker", e);
} }
return sendSticker.deserializeResponse(responseContent);
} }
/** /**
@ -456,16 +353,11 @@ public abstract class DefaultAbsSender extends AbsSender {
*/ */
@Override @Override
public final Message sendAudio(SendAudio sendAudio) throws TelegramApiException { public final Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
if(sendAudio == null){ assertParamNotNull(sendAudio, "sendAudio");
throw new TelegramApiException("Parameter sendAudio can not be null");
}
sendAudio.validate(); sendAudio.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendAudio.PATH; String url = getBaseUrl() + SendAudio.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId());
if (sendAudio.isNewAudio()) { if (sendAudio.isNewAudio()) {
@ -504,16 +396,10 @@ public abstract class DefaultAbsSender extends AbsSender {
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendAudio.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e); throw new TelegramApiException("Unable to send sticker", e);
} }
return sendAudio.deserializeResponse(responseContent);
} }
/** /**
@ -525,16 +411,11 @@ public abstract class DefaultAbsSender extends AbsSender {
*/ */
@Override @Override
public final Message sendVoice(SendVoice sendVoice) throws TelegramApiException { public final Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
if(sendVoice == null){ assertParamNotNull(sendVoice, "sendVoice");
throw new TelegramApiException("Parameter sendVoice can not be null");
}
sendVoice.validate(); sendVoice.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SendVoice.PATH; String url = getBaseUrl() + SendVoice.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId());
if (sendVoice.isNewVoice()) { if (sendVoice.isNewVoice()) {
@ -566,30 +447,20 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return sendVoice.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to send voice", e); throw new TelegramApiException("Unable to send voice", e);
} }
return sendVoice.deserializeResponse(responseContent);
} }
@Override @Override
public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException { public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException {
if(setChatPhoto == null){ assertParamNotNull(setChatPhoto, "setChatPhoto");
throw new TelegramApiException("Parameter setChatPhoto can not be null");
}
setChatPhoto.validate(); setChatPhoto.validate();
String responseContent;
try { try {
String url = getBaseUrl() + SetChatPhoto.PATH; String url = getBaseUrl() + SetChatPhoto.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = configuredHttpPost(url);
httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SetChatPhoto.CHATID_FIELD, setChatPhoto.getChatId()); builder.addTextBody(SetChatPhoto.CHATID_FIELD, setChatPhoto.getChatId());
@ -601,16 +472,10 @@ public abstract class DefaultAbsSender extends AbsSender {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost)) { return setChatPhoto.deserializeResponse(sendHttpPostRequest(httppost));
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to set chat photo", e); throw new TelegramApiException("Unable to set chat photo", e);
} }
return setChatPhoto.deserializeResponse(responseContent);
} }
// Simplified methods // Simplified methods
@ -622,22 +487,12 @@ public abstract class DefaultAbsSender extends AbsSender {
@Override @Override
public void run() { public void run() {
try { try {
method.validate(); String responseContent = sendMethodRequest(method);
String url = getBaseUrl() + method.getMethod();
HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig);
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try { try {
callback.onResult(method, method.deserializeResponse(responseContent)); callback.onResult(method, method.deserializeResponse(responseContent));
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
callback.onError(method, e); callback.onError(method, e);
} }
}
} catch (IOException | TelegramApiValidationException e) { } catch (IOException | TelegramApiValidationException e) {
callback.onException(method, e); callback.onException(method, e);
} }
@ -648,27 +503,76 @@ public abstract class DefaultAbsSender extends AbsSender {
@Override @Override
protected final <T extends Serializable, Method extends BotApiMethod<T>> T sendApiMethod(Method method) throws TelegramApiException { protected final <T extends Serializable, Method extends BotApiMethod<T>> T sendApiMethod(Method method) throws TelegramApiException {
method.validate();
String responseContent;
try { try {
String url = getBaseUrl() + method.getMethod(); String responseContent = sendMethodRequest(method);
HttpPost httppost = new HttpPost(url); return method.deserializeResponse(responseContent);
httppost.setConfig(requestConfig);
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Unable to execute " + method.getMethod() + " method", e); throw new TelegramApiException("Unable to execute " + method.getMethod() + " method", e);
} }
}
return method.deserializeResponse(responseContent); private <T> Runnable getDownloadFileAsyncJob(T fileIdentifier, DownloadFileCallback<T> callback, String url, String tempFileName) {
//noinspection Convert2Lambda
return new Runnable() {
@Override
public void run() {
try {
callback.onResult(fileIdentifier, downloadToTemporaryFile(url, tempFileName));
} catch (MalformedURLException e) {
callback.onException(fileIdentifier, new TelegramApiException("Wrong url for file: " + url));
} catch (IOException e) {
callback.onException(fileIdentifier, new TelegramApiRequestException("Error downloading the file", e));
}
}
};
}
private java.io.File downloadToTemporaryFileWrappingExceptions(String url, String tempFileName) throws TelegramApiException {
try {
return downloadToTemporaryFile(url, tempFileName);
} catch (MalformedURLException e) {
throw new TelegramApiException("Wrong url for file: " + url);
} catch (IOException e) {
throw new TelegramApiRequestException("Error downloading the file", e);
}
}
private java.io.File downloadToTemporaryFile(String url, String tempFileName) throws IOException {
java.io.File output = java.io.File.createTempFile(tempFileName, ".tmp");
FileUtils.copyURLToFile(new URL(url), output);
return output;
}
private <T extends Serializable, Method extends BotApiMethod<T>> String sendMethodRequest(Method method) throws TelegramApiValidationException, IOException {
method.validate();
String url = getBaseUrl() + method.getMethod();
HttpPost httppost = configuredHttpPost(url);
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON));
return sendHttpPostRequest(httppost);
}
private String sendHttpPostRequest(HttpPost httppost) throws IOException {
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
return EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
}
private HttpPost configuredHttpPost(String url) {
HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig);
return httppost;
} }
protected String getBaseUrl() { protected String getBaseUrl() {
return options.getBaseUrl() + getBotToken() + "/"; return options.getBaseUrl() + getBotToken() + "/";
} }
private void assertParamNotNull(Object param, String paramName) throws TelegramApiException {
if (param == null) {
throw new TelegramApiException("Parameter " + paramName + " can not be null");
}
}
} }