Skip to content

Commit

Permalink
add a util class for commodities we lost from java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
chDame committed Jun 23, 2022
1 parent 2d0b84f commit 7e0deee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/camunda/operate/CamundaOperateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.camunda.operate.search.ProcessInstanceFilter;
import io.camunda.operate.search.SearchQuery;
import io.camunda.operate.search.VariableFilter;
import io.camunda.operate.util.Java8Utils;
import io.camunda.operate.util.JsonUtils;
import io.camunda.zeebe.model.bpmn.Bpmn;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
Expand Down Expand Up @@ -75,7 +76,7 @@ public BpmnModelInstance getProcessDefinitionModel(Long key) throws OperateExcep
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
InputStream processInputStream = new ByteArrayInputStream(
response.getEntity().getContent().readAllBytes());
Java8Utils.readAllBytes(response.getEntity().getContent()));
return Bpmn.readModelFromStream(processInputStream);
}
} catch (IOException e) {
Expand Down Expand Up @@ -162,7 +163,7 @@ private <T> T get(Long key, Class<T> resultType) throws OperateException {
protected String executeQuery(ClassicHttpRequest httpRequest) throws OperateException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
return new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
return new String(Java8Utils.readAllBytes(response.getEntity().getContent()), StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new OperateException(e);
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/io/camunda/operate/CamundaOperateConstants.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package io.camunda.operate;

import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.camunda.operate.dto.FlownodeInstance;
import io.camunda.operate.dto.Incident;
import io.camunda.operate.dto.ProcessDefinition;
import io.camunda.operate.dto.ProcessInstance;
import io.camunda.operate.dto.Variable;
import io.camunda.operate.util.Java8Utils;

public class CamundaOperateConstants {

public static Map<Class<?>, String> OBJECT_APIS = toMap(ProcessDefinition.class, "/v1/process-definitions",
public static Map<Class<?>, String> OBJECT_APIS = Java8Utils.toMap(ProcessDefinition.class, "/v1/process-definitions",
ProcessInstance.class, "/v1/process-instances",
FlownodeInstance.class, "/v1/flownode-instances",
Incident.class, "/v1/incidents",
Variable.class, "/v1/variables");

private static Map<Class<?>, String> toMap(Object... array) {
AbstractMap.SimpleEntry<Class<?>, String>[] entryArray = new AbstractMap.SimpleEntry[array.length/2];
for(int i=0;i<entryArray.length;i++) {
entryArray[i]= new AbstractMap.SimpleEntry<Class<?>, String>((Class<?>)array[i*2], (String) array[i*2+1]);
}
return Stream.of(entryArray)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
51 changes: 51 additions & 0 deletions src/main/java/io/camunda/operate/util/Java8Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.camunda.operate.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8Utils {

private Java8Utils() {

}

public static byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 4 * 0x400; // 4KB
byte[] buf = new byte[bufLen];
int readLen;
IOException exception = null;

try {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);

return outputStream.toByteArray();
}
} catch (IOException e) {
exception = e;
throw e;
} finally {
if (exception == null) inputStream.close();
else try {
inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}
}

public static Map<Class<?>, String> toMap(Object... array) {
AbstractMap.SimpleEntry<Class<?>, String>[] entryArray = new AbstractMap.SimpleEntry[array.length/2];
for(int i=0;i<entryArray.length;i++) {
entryArray[i]= new AbstractMap.SimpleEntry<Class<?>, String>((Class<?>)array[i*2], (String) array[i*2+1]);
}
return Stream.of(entryArray)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}

0 comments on commit 7e0deee

Please sign in to comment.