@@ -41,7 +41,9 @@ public class InternalBatchDriverImpl extends BaseArangoDriverImpl {
41
41
42
42
private static String newline = System .getProperty ("line.separator" );
43
43
44
- private String delimiter = "dlmtrMLTPRT" ;
44
+ private static final String BOUNDARY = "dlmtrMLTPRT" ;
45
+
46
+ private static final String DELIMITER = "--" + BOUNDARY ;
45
47
46
48
private BatchResponseListEntity batchResponseListEntity ;
47
49
@@ -51,91 +53,111 @@ public class InternalBatchDriverImpl extends BaseArangoDriverImpl {
51
53
52
54
public DefaultEntity executeBatch (List <BatchPart > callStack , String defaultDataBase ) throws ArangoException {
53
55
54
- String body = "" ;
56
+ StringBuilder sb = new StringBuilder () ;
55
57
56
58
Map <String , InvocationObject > resolver = new HashMap <String , InvocationObject >();
57
59
58
60
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 );
65
62
resolver .put (bp .getId (), bp .getInvocationObject ());
66
63
}
67
- body += "--" + delimiter + "--" ;
64
+
65
+ sb .append (DELIMITER + "--" );
68
66
69
67
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 );
71
69
72
70
HttpResponseEntity res = httpManager .doPostWithHeaders (createEndpointUrl (defaultDataBase , "/_api/batch" ), null ,
73
- null , headers , body );
71
+ null , headers , sb . toString () );
74
72
75
73
String data = res .getText ();
76
74
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 ) {
77
87
String currentId = null ;
78
88
Boolean fetchText = false ;
79
- res .setText ("" );
80
89
List <BatchResponseEntity > batchResponseEntityList = new ArrayList <BatchResponseEntity >();
81
90
BatchResponseEntity batchResponseEntity = new BatchResponseEntity (null );
82
- String t = null ;
91
+ StringBuilder sb = new StringBuilder () ;
83
92
for (String line : data .split (newline )) {
84
93
line .trim ();
85
94
line .replaceAll ("\r " , "" );
86
95
if (line .indexOf ("Content-Id" ) != -1 ) {
87
- if (currentId != null ) {
88
- batchResponseEntityList .add (batchResponseEntity );
89
- }
96
+ addBatchResponseEntity (currentId , batchResponseEntityList , batchResponseEntity );
90
97
currentId = line .split (" " )[1 ].trim ();
91
98
batchResponseEntity = new BatchResponseEntity (resolver .get (currentId ));
92
99
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 )) {
97
101
String ct = line .replaceAll ("Content-Type: " , "" );
98
102
batchResponseEntity .httpResponseEntity .setContentType (ct );
99
- continue ;
100
- }
101
- if (line .indexOf ("Etag" ) != -1 ) {
103
+ } else if (line .indexOf ("Etag" ) != -1 ) {
102
104
String etag = line .split (" " )[1 ].replaceAll ("\" " , "" ).trim ();
103
105
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 ) {
107
107
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 ) {
111
109
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 )) {
116
112
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 );
127
116
}
128
117
}
129
118
if (batchResponseEntity .getHttpResponseEntity () != null ) {
130
119
batchResponseEntityList .add (batchResponseEntity );
131
120
}
132
- BatchResponseListEntity batchResponseListEntityTmp = new BatchResponseListEntity ();
133
- batchResponseListEntityTmp .setBatchResponseEntities (batchResponseEntityList );
134
- this .batchResponseListEntity = batchResponseListEntityTmp ;
135
- return createEntity (res , DefaultEntity .class , null , false );
121
+ return batchResponseEntityList ;
136
122
}
137
123
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 );
140
162
}
141
163
}
0 commit comments