From 87fc05dae0d587520701aaaa514c4b7cb0143b99 Mon Sep 17 00:00:00 2001 From: Jan-Willem Gmelig Meyling Date: Wed, 20 Apr 2016 10:28:34 +0200 Subject: [PATCH] Fixed NPE --- .../ewi/build/jaxrs/BuildsResource.java | 85 ++++++++++--------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/build-server/src/main/java/nl/tudelft/ewi/build/jaxrs/BuildsResource.java b/build-server/src/main/java/nl/tudelft/ewi/build/jaxrs/BuildsResource.java index 29bb01a..4b16dcb 100644 --- a/build-server/src/main/java/nl/tudelft/ewi/build/jaxrs/BuildsResource.java +++ b/build-server/src/main/java/nl/tudelft/ewi/build/jaxrs/BuildsResource.java @@ -59,71 +59,74 @@ public Response onBuildRequest(@Valid final BuildRequest buildRequest) { Build build = null; try { build = manager.schedule(buildRequest); - Futures.addCallback(build, new FutureCallback() { - @Override - public void onSuccess(BuildResult result) { - if (Strings.isNullOrEmpty(buildRequest.getCallbackUrl())) { - return; - } + if (build != null) { + Futures.addCallback(build, new FutureCallback() { + + @Override + public void onSuccess(BuildResult result) { + if (Strings.isNullOrEmpty(buildRequest.getCallbackUrl())) { + return; + } - log.info("Returning build results to callback URL: {}", + log.info("Returning build results to callback URL: {}", buildRequest.getCallbackUrl()); - for (int i = 0; i <= 4; i++) { - Client client = ClientBuilder.newClient(); - try { - Response response = prepareCallback(client).post( + for (int i = 0; i <= 4; i++) { + Client client = ClientBuilder.newClient(); + try { + Response response = prepareCallback(client).post( Entity.json(result)); - StatusType statusInfo = response.getStatusInfo(); - if (statusInfo.getStatusCode() >= 200 + StatusType statusInfo = response.getStatusInfo(); + if (statusInfo.getStatusCode() >= 200 && statusInfo.getStatusCode() < 300) { - log.info( + log.info( "Build result successfully returned to: {}", buildRequest.getCallbackUrl()); - return; - } - log.warn( + return; + } + log.warn( "Could not return build result to: {}, status was: {} - {}", buildRequest.getCallbackUrl(), response.getStatus(), statusInfo.getReasonPhrase()); - } catch (Throwable e) { - log.warn(e.getMessage(), e); - } finally { - if (client != null) { - client.close(); + } catch (Throwable e) { + log.warn(e.getMessage(), e); + } finally { + if (client != null) { + client.close(); + } } - } - // Exponential backoff. - if (i < 4) { - try { - Thread.sleep(5000L * 2 ^ i); - } catch (InterruptedException e) { + // Exponential backoff. + if (i < 4) { + try { + Thread.sleep(5000L * 2 ^ i); + } catch (InterruptedException e) { + } } } - } - log.error("Could not return build result to: {}", + log.error("Could not return build result to: {}", buildRequest.getCallbackUrl()); - } + } - private Builder prepareCallback(Client client) { - String userPass = config.getClientId() + ":" + private Builder prepareCallback(Client client) { + String userPass = config.getClientId() + ":" + config.getClientSecret(); - String authorization = "Basic " + String authorization = "Basic " + Base64.encodeBytes(userPass.getBytes()); - return client.target(buildRequest.getCallbackUrl()) + return client.target(buildRequest.getCallbackUrl()) .request().header("Authorization", authorization); - } + } - @Override - public void onFailure(Throwable t) { - // TODO Auto-generated method stub + @Override + public void onFailure(Throwable t) { + // TODO Auto-generated method stub - } + } - }, executor); + }, executor); + } } catch (Throwable e) { log.error(e.getMessage(), e); }