| 16 | import java.util.concurrent.ExecutorService; |
| 17 | |
| 18 | public class AsyncCompletion<Req extends HttpRequest, Res extends HttpResponse> extends AbstractWatchableCallback<Req, Res> |
| 19 | implements FutureCallback<Res> { |
| 20 | private Req request; |
| 21 | private ExecutorService executor; |
| 22 | |
| 23 | public AsyncCompletion(Req request, ExecutorService executor){ |
| 24 | this.request = request; |
| 25 | this.executor = executor; |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public void completed(Res res) { |
| 30 | this.onCompleted(request, res); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public void failed(Exception ex) { |
| 35 | ex.printStackTrace(); |
| 36 | final Object e; |
| 37 | if (ex instanceof ClientException) { |
| 38 | e = ex; |
| 39 | } else if (ex instanceof ServerException) { |
| 40 | e = ex; |
| 41 | } else if(ex instanceof SocketTimeoutException) { |
| 42 | e = new ClientException("SDK.ServerUnreachable", |
| 43 | "SocketTimeoutException has occurred on a socket read or accept." + ex.toString()); |
| 44 | } else if (ex instanceof InvalidKeyException) { |
| 45 | e = new ClientException("SDK.InvalidAccessSecret", |
| 46 | "Speicified access secret is not valid."); |
| 47 | } else if (ex instanceof IOException) { |
| 48 | e = new ClientException("SDK.ServerUnreachable", |
| 49 | "Server unreachable: " + ex.toString()); |
| 50 | } else if (ex instanceof NoSuchAlgorithmException) { |
| 51 | e = new ClientException("SDK.InvalidMD5Algorithm", |
| 52 | "MD5 hash is not supported by client side."); |
| 53 | } else if (ex instanceof URISyntaxException) { |
| 54 | throw new ClientException("SDK.InvalidURL", "url is not valid: " + ex.getMessage()); |
| 55 | } else { |
| 56 | e = new ClientException("SDK.UnknownError", ex.getMessage(), ex.getCause()); |
| 57 | } |
| 58 | this.onFailed(request, (Exception) e); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void cancelled() { |
| 63 | |
| 64 | } |
| 65 | |
| 66 | public void onCompleted(final Req req, final Res res) { |
| 67 | Iterator it = callbacks.iterator(); |
| 68 | |
| 69 | while(it.hasNext()) { |
| 70 | final FcCallback<Req, Res> cb = (FcCallback)it.next(); |
| 71 | executor.submit(new Runnable() { |
| 72 | public void run() { |
| 73 | cb.onCompleted(req, res); |
| 74 | } |
| 75 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected