Skip to content

Commit

Permalink
Retry connection in case of 401 and Bring some more details to the ex…
Browse files Browse the repository at this point in the history
…ception.
  • Loading branch information
chDame committed Mar 30, 2023
1 parent d414c9f commit 718a9d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ You can import it to your maven or gradle project as a dependency
<dependency>
<groupId>io.camunda</groupId>
<artifactId>camunda-operate-client-java</artifactId>
<version>8.1.7.0</version>
<version>8.1.7.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'io.camunda'
version = '8.1.7.0'
version = '8.1.7.1'
sourceCompatibility = '8'

repositories {
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/io/camunda/operate/CamundaOperateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,28 @@ public BpmnModelInstance getProcessDefinitionModel(Long key) throws OperateExcep
throw new OperateException(e);
}
}

public static CloseableHttpResponse execute(CloseableHttpClient httpClient, ClassicHttpRequest request) throws IOException, OperateException {
public CloseableHttpResponse execute(CloseableHttpClient httpClient, ClassicHttpRequest request) throws IOException, OperateException {
return execute(httpClient, request, 0);
}
private CloseableHttpResponse execute(CloseableHttpClient httpClient, ClassicHttpRequest request, int count) throws IOException, OperateException {
CloseableHttpResponse response = httpClient.execute(request);
if (response.getCode()==401 && count<=2) {
authentication.authenticate(this);
return execute(httpClient, request, ++count);
}
if (response.getCode()>399) {
throw new OperateException("Authentication error : "+response.getCode()+" "+response.getReasonPhrase());
throw createDetailedException(response, request);
}
return response;
}

private OperateException createDetailedException(CloseableHttpResponse response, ClassicHttpRequest request) throws IOException {
String details = "";
if (request instanceof HttpPost) {
details+= "; Body : " + new String(((HttpPost) request).getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
}
return new OperateException(request.getPath()+" : "+response.getCode()+" "+response.getReasonPhrase()+details);
}

public ProcessInstance getProcessInstance(Long key) throws OperateException {
return get(key, ProcessInstance.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void authenticate(CamundaOperateClient client) throws OperateException {
httpPost.setEntity(new StringEntity(data));

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = CamundaOperateClient.execute(httpClient, httpPost)) {
try (CloseableHttpResponse response = client.execute(httpClient, httpPost)) {
JsonNode responseBody = JsonUtils.toJsonNode(response.getEntity().getContent());
String token = responseBody.get("access_token").asText();

Expand Down

0 comments on commit 718a9d7

Please sign in to comment.