Skip to content

Commit

Permalink
Merge pull request #21 from JWGmeligMeyling/fix-npe
Browse files Browse the repository at this point in the history
Fixed NPE
  • Loading branch information
jwgmeligmeyling committed Apr 20, 2016
2 parents d8e9a72 + 87fc05d commit c3581e8
Showing 1 changed file with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,71 +59,74 @@ public Response onBuildRequest(@Valid final BuildRequest buildRequest) {
Build build = null;
try {
build = manager.schedule(buildRequest);
Futures.addCallback(build, new FutureCallback<BuildResult>() {

@Override
public void onSuccess(BuildResult result) {
if (Strings.isNullOrEmpty(buildRequest.getCallbackUrl())) {
return;
}
if (build != null) {
Futures.addCallback(build, new FutureCallback<BuildResult>() {

@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);
}
Expand Down

0 comments on commit c3581e8

Please sign in to comment.