Skip to content

Commit ee5e8f9

Browse files
author
a-brandt
committed
fixed sonarlint issues
1 parent 377a9fe commit ee5e8f9

File tree

1 file changed

+71
-49
lines changed

1 file changed

+71
-49
lines changed

β€Žsrc/main/java/com/arangodb/impl/InternalBatchDriverImpl.java

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public class InternalBatchDriverImpl extends BaseArangoDriverImpl {
4141

4242
private static String newline = System.getProperty("line.separator");
4343

44-
private String delimiter = "dlmtrMLTPRT";
44+
private static final String BOUNDARY = "dlmtrMLTPRT";
45+
46+
private static final String DELIMITER = "--" + BOUNDARY;
4547

4648
private BatchResponseListEntity batchResponseListEntity;
4749

@@ -51,91 +53,111 @@ public class InternalBatchDriverImpl extends BaseArangoDriverImpl {
5153

5254
public DefaultEntity executeBatch(List<BatchPart> callStack, String defaultDataBase) throws ArangoException {
5355

54-
String body = "";
56+
StringBuilder sb = new StringBuilder();
5557

5658
Map<String, InvocationObject> resolver = new HashMap<String, InvocationObject>();
5759

5860
for (BatchPart bp : callStack) {
59-
body += "--" + delimiter + newline;
60-
body += "Content-Type: application/x-arango-batchpart" + newline;
61-
body += "Content-Id: " + bp.getId() + newline + newline;
62-
body += bp.getMethod() + " " + bp.getUrl() + " " + "HTTP/1.1" + newline;
63-
body += "Host: " + this.configure.getArangoHost().getHost() + newline + newline;
64-
body += bp.getBody() == null ? "" : bp.getBody() + newline + newline;
61+
addBatchPart(sb, bp);
6562
resolver.put(bp.getId(), bp.getInvocationObject());
6663
}
67-
body += "--" + delimiter + "--";
64+
65+
sb.append(DELIMITER + "--");
6866

6967
Map<String, Object> headers = new HashMap<String, Object>();
70-
headers.put("Content-Type", "multipart/form-data; boundary=" + delimiter);
68+
headers.put("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
7169

7270
HttpResponseEntity res = httpManager.doPostWithHeaders(createEndpointUrl(defaultDataBase, "/_api/batch"), null,
73-
null, headers, body);
71+
null, headers, sb.toString());
7472

7573
String data = res.getText();
7674
res.setContentType("application/json");
75+
res.setText("");
76+
List<BatchResponseEntity> batchResponseEntityList = handleResponse(resolver, data);
77+
batchResponseListEntity = new BatchResponseListEntity();
78+
batchResponseListEntity.setBatchResponseEntities(batchResponseEntityList);
79+
return createEntity(res, DefaultEntity.class, null, false);
80+
}
81+
82+
public BatchResponseListEntity getBatchResponseListEntity() {
83+
return batchResponseListEntity;
84+
}
85+
86+
private List<BatchResponseEntity> handleResponse(Map<String, InvocationObject> resolver, String data) {
7787
String currentId = null;
7888
Boolean fetchText = false;
79-
res.setText("");
8089
List<BatchResponseEntity> batchResponseEntityList = new ArrayList<BatchResponseEntity>();
8190
BatchResponseEntity batchResponseEntity = new BatchResponseEntity(null);
82-
String t = null;
91+
StringBuilder sb = new StringBuilder();
8392
for (String line : data.split(newline)) {
8493
line.trim();
8594
line.replaceAll("\r", "");
8695
if (line.indexOf("Content-Id") != -1) {
87-
if (currentId != null) {
88-
batchResponseEntityList.add(batchResponseEntity);
89-
}
96+
addBatchResponseEntity(currentId, batchResponseEntityList, batchResponseEntity);
9097
currentId = line.split(" ")[1].trim();
9198
batchResponseEntity = new BatchResponseEntity(resolver.get(currentId));
9299
batchResponseEntity.setRequestId(currentId);
93-
continue;
94-
}
95-
if (line.indexOf("Content-Type:") != -1
96-
&& line.indexOf("Content-Type: application/x-arango-batchpart") == -1) {
100+
} else if (isContentTypeLine(line)) {
97101
String ct = line.replaceAll("Content-Type: ", "");
98102
batchResponseEntity.httpResponseEntity.setContentType(ct);
99-
continue;
100-
}
101-
if (line.indexOf("Etag") != -1) {
103+
} else if (line.indexOf("Etag") != -1) {
102104
String etag = line.split(" ")[1].replaceAll("\"", "").trim();
103105
batchResponseEntity.httpResponseEntity.setEtag(Long.parseLong(etag));
104-
continue;
105-
}
106-
if (line.indexOf("HTTP/1.1") != -1) {
106+
} else if (line.indexOf("HTTP/1.1") != -1) {
107107
batchResponseEntity.httpResponseEntity.setStatusCode(Integer.valueOf(line.split(" ")[1]));
108-
continue;
109-
}
110-
if (line.indexOf("Content-Length") != -1) {
108+
} else if (line.indexOf("Content-Length") != -1) {
111109
fetchText = true;
112-
t = "";
113-
continue;
114-
}
115-
if (line.indexOf("--" + delimiter) != -1 && resolver.get(currentId) != null) {
110+
sb.setLength(0);
111+
} else if (isDelimiterLine(resolver, currentId, line)) {
116112
fetchText = false;
117-
if (!batchResponseEntity.httpResponseEntity.isDumpResponse()) {
118-
batchResponseEntity.httpResponseEntity.setText(t);
119-
} else {
120-
InputStream is = new ByteArrayInputStream(t.getBytes());
121-
batchResponseEntity.httpResponseEntity.setStream(is);
122-
}
123-
continue;
124-
}
125-
if (fetchText && !line.equals(newline)) {
126-
t += line;
113+
copyResponseToEntity(batchResponseEntity, sb);
114+
} else if (canFetchLine(fetchText, line)) {
115+
sb.append(line);
127116
}
128117
}
129118
if (batchResponseEntity.getHttpResponseEntity() != null) {
130119
batchResponseEntityList.add(batchResponseEntity);
131120
}
132-
BatchResponseListEntity batchResponseListEntityTmp = new BatchResponseListEntity();
133-
batchResponseListEntityTmp.setBatchResponseEntities(batchResponseEntityList);
134-
this.batchResponseListEntity = batchResponseListEntityTmp;
135-
return createEntity(res, DefaultEntity.class, null, false);
121+
return batchResponseEntityList;
136122
}
137123

138-
public BatchResponseListEntity getBatchResponseListEntity() {
139-
return batchResponseListEntity;
124+
private void copyResponseToEntity(BatchResponseEntity batchResponseEntity, StringBuilder sb) {
125+
if (!batchResponseEntity.httpResponseEntity.isDumpResponse()) {
126+
batchResponseEntity.httpResponseEntity.setText(sb.toString());
127+
} else {
128+
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
129+
batchResponseEntity.httpResponseEntity.setStream(is);
130+
}
131+
}
132+
133+
private boolean isDelimiterLine(Map<String, InvocationObject> resolver, String currentId, String line) {
134+
return line.indexOf(DELIMITER) != -1 && resolver.get(currentId) != null;
135+
}
136+
137+
private boolean canFetchLine(Boolean fetchText, String line) {
138+
return fetchText && !line.equals(newline);
139+
}
140+
141+
private boolean isContentTypeLine(String line) {
142+
return line.indexOf("Content-Type:") != -1
143+
&& line.indexOf("Content-Type: application/x-arango-batchpart") == -1;
144+
}
145+
146+
private void addBatchResponseEntity(
147+
String currentId,
148+
List<BatchResponseEntity> batchResponseEntityList,
149+
BatchResponseEntity batchResponseEntity) {
150+
if (currentId != null) {
151+
batchResponseEntityList.add(batchResponseEntity);
152+
}
153+
}
154+
155+
private void addBatchPart(StringBuilder sb, BatchPart bp) {
156+
sb.append(DELIMITER + newline);
157+
sb.append("Content-Type: application/x-arango-batchpart" + newline);
158+
sb.append("Content-Id: " + bp.getId() + newline + newline);
159+
sb.append(bp.getMethod() + " " + bp.getUrl() + " " + "HTTP/1.1" + newline);
160+
sb.append("Host: " + this.configure.getArangoHost().getHost() + newline + newline);
161+
sb.append(bp.getBody() == null ? "" : bp.getBody() + newline + newline);
140162
}
141163
}

0 commit comments

Comments
 (0)