MessageBuilder with ExceptionHandler
This commit is contained in:
parent
5f746e99d8
commit
1c9fd312c8
|
@ -16,12 +16,16 @@ public class AsyncHandler implements Runnable {
|
|||
}
|
||||
|
||||
public void enque(Task t) {
|
||||
tasks.add(t);
|
||||
synchronized (tasks) {
|
||||
tasks.add(t);
|
||||
}
|
||||
|
||||
//make sure its running
|
||||
if(asynchandlerthread == null) {
|
||||
asynchandlerthread = new Thread(this, "AsyncHandler");
|
||||
asynchandlerthread.start();
|
||||
} else if(!asynchandlerthread.isAlive()) {
|
||||
asynchandlerthread = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,8 +36,15 @@ public class AsyncHandler implements Runnable {
|
|||
@Override
|
||||
public void run() {
|
||||
while(!tasks.isEmpty()) {
|
||||
Task current = tasks.remove(0);
|
||||
|
||||
Task current;
|
||||
|
||||
synchronized (tasks) {
|
||||
current = tasks.remove(0);
|
||||
}
|
||||
|
||||
if(current == null)
|
||||
continue;
|
||||
|
||||
//run task
|
||||
try {
|
||||
try {
|
||||
|
|
|
@ -18,6 +18,7 @@ public class MessageBuilder {
|
|||
private TReplyMarkup markup = null;
|
||||
private boolean async = false;
|
||||
private Callback<TMessage, ?> callback = null;
|
||||
private Callback<Throwable, Void> excpt = null;
|
||||
private Attachment attachmenttype = Attachment.none;
|
||||
private String attachment = null;
|
||||
private String caption = null;
|
||||
|
@ -84,6 +85,11 @@ public class MessageBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
public MessageBuilder setExceptionHandler(Callback<Throwable, Void> exchndl) {
|
||||
excpt = exchndl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessageBuilder setAttachment(Attachment type, String cont) {
|
||||
if(attachmenttype != Attachment.none) {
|
||||
throw new IllegalArgumentException("You can only attach one thing!");
|
||||
|
@ -142,7 +148,7 @@ public class MessageBuilder {
|
|||
|
||||
String q = "chat_id=" + reciver_id + text + optionals + "&disable_web_page_preview=" + no_web_view + "&disable_notification=" + silent + attachment;
|
||||
if(async)
|
||||
return new AsyncSendable(cmd, q, callback);
|
||||
return new AsyncSendable(cmd, q, callback, excpt);
|
||||
return new SendableMessage(cmd, q);
|
||||
}
|
||||
|
||||
|
@ -165,10 +171,12 @@ public class MessageBuilder {
|
|||
class AsyncSendable extends SendableMessage {
|
||||
|
||||
Callback<TMessage, ?> callback;
|
||||
Callback<Throwable, Void> excpt = null;
|
||||
|
||||
public AsyncSendable(String cmd, String q, Callback<TMessage, ?> clb) {
|
||||
public AsyncSendable(String cmd, String q, Callback<TMessage, ?> clb, Callback<Throwable, Void> excpt) {
|
||||
super(cmd, q);
|
||||
callback = clb;
|
||||
this.excpt = excpt;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,4 +210,5 @@ public class MessageBuilder {
|
|||
super("The Object " + missing_obj + " is missing or invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package de.mrbesen.telegram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
import java.util.LinkedList;
|
||||
|
@ -107,23 +108,29 @@ public class TelegramAPI implements Runnable {
|
|||
|
||||
log.log( "request: " + request + " content " + parameter + " -> " + con.getResponseCode() + ", " + con.getResponseMessage());
|
||||
|
||||
Scanner s = new Scanner(con.getInputStream());
|
||||
StringBuilder sb_apianswer = new StringBuilder();
|
||||
while(s.hasNextLine()) {
|
||||
sb_apianswer.append(s.nextLine()).append('\n');
|
||||
}
|
||||
s.close();
|
||||
if(con.getResponseCode() == 200) {
|
||||
return new JSONObject(sb_apianswer.toString());
|
||||
return new JSONObject(readfromIS(con.getInputStream()));
|
||||
} else {
|
||||
String errdesc = "";
|
||||
String errdesc = "[No description available]";
|
||||
try {
|
||||
JSONObject returned = new JSONObject(sb_apianswer.toString());
|
||||
//try to read anyway to get error message
|
||||
JSONObject returned = new JSONObject(readfromIS(con.getInputStream()));
|
||||
errdesc = returned.getString("description");
|
||||
} catch(Throwable ignore) { }
|
||||
throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||
throw new APIError(parameter, request, con.getResponseCode(), null, errdesc);
|
||||
//throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||
}
|
||||
}
|
||||
|
||||
private String readfromIS(InputStream is) {
|
||||
Scanner s = new Scanner(is);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while(s.hasNextLine()) {
|
||||
sb.append(s.nextLine()).append('\n');
|
||||
}
|
||||
s.close();
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public TMessage sendMessage(TUser user, String text) {
|
||||
MessageBuilder builder = new MessageBuilder();
|
||||
|
@ -135,10 +142,11 @@ public class TelegramAPI implements Runnable {
|
|||
public TMessage sendMessage(SendableMessage msg) {
|
||||
try {
|
||||
if(msg instanceof AsyncSendable) {
|
||||
AsyncSendable asyncm = (AsyncSendable) msg;
|
||||
Callback<JSONObject, TMessage> adapter = getCallbackAdapter(this);
|
||||
adapter.next = ((AsyncSendable) msg).callback;
|
||||
adapter.next = asyncm.callback;
|
||||
Task t = new Task(msg.getCommand(), msg.getQ(), adapter);
|
||||
t.setExceptionhandl(IOE400supressor);
|
||||
t.setExceptionhandl(asyncm.excpt == null ? IOE400supressor : asyncm.excpt);
|
||||
async.enque(t);
|
||||
} else {
|
||||
JSONObject o = request(msg.getCommand(), msg.getQ());
|
||||
|
@ -387,5 +395,47 @@ public class TelegramAPI implements Runnable {
|
|||
isArray = b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class APIError extends IOException {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
String params;
|
||||
String method;
|
||||
int returncode;
|
||||
public String getParams() {
|
||||
return params;
|
||||
}
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
public int getReturncode() {
|
||||
return returncode;
|
||||
}
|
||||
public void setParams(String params) {
|
||||
this.params = params;
|
||||
}
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
public void setReturncode(int returncode) {
|
||||
this.returncode = returncode;
|
||||
}
|
||||
public APIError(String params, String method, int returncode, Throwable t, String msg) {
|
||||
super(msg, t);
|
||||
this.params = params;
|
||||
this.method = method;
|
||||
this.returncode = returncode;
|
||||
}
|
||||
public APIError(String arg0, Throwable arg1) {
|
||||
super(arg0, arg1);
|
||||
}
|
||||
public APIError(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
public APIError(Throwable arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue