|
1 | 1 | package com.microsoft.graph.content; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
| 3 | +import java.io.IOException; |
4 | 4 | import java.util.HashMap; |
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Map; |
7 | 7 |
|
8 | | -import org.apache.http.Header; |
9 | | -import org.apache.http.HttpEntity; |
10 | | -import org.apache.http.HttpRequest; |
11 | | -import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
12 | | -import org.apache.http.util.EntityUtils; |
| 8 | +import org.json.simple.JSONArray; |
13 | 9 | import org.json.simple.JSONObject; |
14 | | -import org.json.simple.JSONValue; |
| 10 | +import org.json.simple.parser.JSONParser; |
| 11 | +import org.json.simple.parser.ParseException; |
| 12 | + |
| 13 | +import okhttp3.Headers; |
| 14 | +import okhttp3.Request; |
| 15 | +import okhttp3.RequestBody; |
| 16 | +import okio.Buffer; |
15 | 17 |
|
16 | 18 | public class MSBatchRequestContent { |
17 | | - private List<MSBatchRequestStep> batchRequestStepsArray; |
18 | | - private final int maxNumberOfRequests = 20; |
| 19 | + private Map<String, MSBatchRequestStep> batchRequestStepsHashMap; |
| 20 | + |
| 21 | + // Maximum number of requests that can be sent in a batch |
| 22 | + public static final int MAX_NUMBER_OF_REQUESTS = 20; |
19 | 23 |
|
| 24 | + /* |
| 25 | + * Creates Batch request content using list provided |
| 26 | + * |
| 27 | + * @param batchRequestStepsArray List of batch steps for batching |
| 28 | + */ |
20 | 29 | public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) { |
21 | | - this.batchRequestStepsArray = new ArrayList<>(); |
22 | | - if(batchRequestStepsArray.size() <= maxNumberOfRequests) { |
23 | | - for(MSBatchRequestStep requestStep: batchRequestStepsArray) |
24 | | - addBatchRequestStep(requestStep); |
25 | | - } |
| 30 | + if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS) |
| 31 | + throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
| 32 | + |
| 33 | + this.batchRequestStepsHashMap = new HashMap<>(); |
| 34 | + for(MSBatchRequestStep requestStep: batchRequestStepsArray) |
| 35 | + addBatchRequestStep(requestStep); |
26 | 36 | } |
27 | 37 |
|
| 38 | + /* |
| 39 | + * Creates empty batch request content |
| 40 | + */ |
28 | 41 | public MSBatchRequestContent() { |
29 | | - batchRequestStepsArray = new ArrayList<>(); |
| 42 | + batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>(); |
30 | 43 | } |
31 | 44 |
|
| 45 | + /* |
| 46 | + * @param batchRequestStep Batch request step adding to batch content |
| 47 | + * @return true or false based on addition or no addition of batch request step given |
| 48 | + */ |
32 | 49 | public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) { |
33 | | - if(batchRequestStep.getRequestId().compareTo("") == 0) |
| 50 | + if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId())) |
34 | 51 | return false; |
35 | | - if(batchRequestStepsArray.size() == maxNumberOfRequests) |
36 | | - return false; |
37 | | - for(MSBatchRequestStep requestStep: batchRequestStepsArray) { |
38 | | - if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0) |
39 | | - return false; |
40 | | - } |
41 | | - return batchRequestStepsArray.add(batchRequestStep); |
| 52 | + batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
| 53 | + return true; |
42 | 54 | } |
43 | 55 |
|
44 | | - public boolean removeBatchRequesStepWithId(String requestId) { |
45 | | - boolean ret = false; |
46 | | - for (int i = batchRequestStepsArray.size()-1; i >= 0; i--) |
47 | | - { |
48 | | - MSBatchRequestStep requestStep = batchRequestStepsArray.get(i); |
49 | | - for (int j = requestStep.getArrayOfDependsOnIds().size() - 1; j >= 0; j--) |
50 | | - { |
51 | | - String dependsOnId = requestStep.getArrayOfDependsOnIds().get(j); |
52 | | - if(dependsOnId.compareTo(requestId) == 0) |
53 | | - { |
54 | | - requestStep.getArrayOfDependsOnIds().remove(j); |
55 | | - ret = true; |
56 | | - } |
57 | | - } |
58 | | - if(requestId.compareTo(requestStep.getRequestId()) == 0) { |
59 | | - batchRequestStepsArray.remove(i); |
60 | | - ret = true; |
61 | | - } |
62 | | - } |
63 | | - return ret; |
| 56 | + /* |
| 57 | + * @param requestId Id of Batch request step to be removed |
| 58 | + * @return true or false based on removal or no removal of batch request step with given id |
| 59 | + */ |
| 60 | + public boolean removeBatchRequestStepWithId(String requestId) { |
| 61 | + boolean removed = false; |
| 62 | + if(batchRequestStepsHashMap.containsKey(requestId)) { |
| 63 | + batchRequestStepsHashMap.remove(requestId); |
| 64 | + removed = true; |
| 65 | + for(Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) { |
| 66 | + if(steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) { |
| 67 | + while(steps.getValue().getArrayOfDependsOnIds().remove(requestId)); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return removed; |
64 | 72 | } |
65 | 73 |
|
| 74 | + /* |
| 75 | + * @return Batch request content's json as String |
| 76 | + */ |
66 | 77 | public String getBatchRequestContent() { |
67 | | - Map<String, List<Map<String, String>>> batchRequestContentMap = new HashMap<>(); |
68 | | - List<Map<String, String>> batchContentArray = new ArrayList<>(); |
69 | | - for(MSBatchRequestStep requestStep : batchRequestStepsArray) { |
70 | | - batchContentArray.add(getBatchRequestMapFromRequestStep(requestStep)); |
| 78 | + JSONObject batchRequestContentMap = new JSONObject(); |
| 79 | + JSONArray batchContentArray = new JSONArray(); |
| 80 | + for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) { |
| 81 | + batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue())); |
71 | 82 | } |
72 | 83 | batchRequestContentMap.put("requests", batchContentArray); |
73 | | - return JSONValue.toJSONString(batchRequestContentMap); |
| 84 | + |
| 85 | + String content = batchRequestContentMap.toString(); |
| 86 | + return content; |
74 | 87 | } |
75 | 88 |
|
76 | | - private Map<String, String> getBatchRequestMapFromRequestStep(MSBatchRequestStep batchRequestStep){ |
77 | | - Map<String, String> contentmap = new HashMap<>(); |
| 89 | + private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){ |
| 90 | + JSONObject contentmap = new JSONObject(); |
78 | 91 | contentmap.put("id", batchRequestStep.getRequestId()); |
79 | | - contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri()); |
80 | | - contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod()); |
81 | | - Header[] headers = batchRequestStep.getRequest().getAllHeaders(); |
82 | | - if(headers != null && headers.length != 0) { |
83 | | - JSONObject obj = new JSONObject(); |
84 | | - for(Header header: headers) { |
85 | | - obj.put(header.getName(), header.getValue()); |
86 | | - } |
87 | | - contentmap.put("headers", obj.toJSONString()); |
88 | | - } |
89 | | - HttpEntity entity = null; |
90 | | - HttpRequest request = batchRequestStep.getRequest(); |
91 | | - if(request instanceof HttpEntityEnclosingRequestBase) { |
92 | | - HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request; |
93 | | - entity = httprequest.getEntity(); |
94 | | - } |
95 | | - if(entity != null) { |
96 | | - try { |
97 | | - String body = EntityUtils.toString(entity); |
98 | | - contentmap.put("body", body); |
99 | | - } |
100 | | - catch(Exception e) { |
101 | | - e.printStackTrace(); |
| 92 | + |
| 93 | + String url = batchRequestStep.getRequest().url().toString(); |
| 94 | + url = url.replaceAll("https://graph.microsoft.com/v1.0/", ""); |
| 95 | + url = url.replaceAll("http://graph.microsoft.com/v1.0/", ""); |
| 96 | + url = url.replaceAll("https://graph.microsoft.com/beta/", ""); |
| 97 | + url = url.replaceAll("http://graph.microsoft.com/beta/", ""); |
| 98 | + contentmap.put("url", url); |
| 99 | + |
| 100 | + contentmap.put("method", batchRequestStep.getRequest().method().toString()); |
| 101 | + |
| 102 | + Headers headers = batchRequestStep.getRequest().headers(); |
| 103 | + if(headers != null && headers.size() != 0) { |
| 104 | + JSONObject headerMap = new JSONObject(); |
| 105 | + for(Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) { |
| 106 | + headerMap.put(entry.getKey(), getHeaderValuesAsString(entry.getValue())); |
102 | 107 | } |
| 108 | + contentmap.put("headers", headerMap); |
103 | 109 | } |
| 110 | + |
104 | 111 | List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds(); |
105 | 112 | if(arrayOfDependsOnIds != null) { |
106 | | - contentmap.put("dependsOn", JSONValue.toJSONString(arrayOfDependsOnIds)); |
| 113 | + JSONArray array = new JSONArray(); |
| 114 | + for(String dependsOnId : arrayOfDependsOnIds) array.add(dependsOnId); |
| 115 | + contentmap.put("dependsOn", array); |
107 | 116 | } |
108 | 117 |
|
| 118 | + RequestBody body = batchRequestStep.getRequest().body(); |
| 119 | + if(body != null) { |
| 120 | + try { |
| 121 | + contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest())); |
| 122 | + }catch(IOException | ParseException e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + } |
109 | 126 | return contentmap; |
110 | 127 | } |
111 | | - |
| 128 | + |
| 129 | + private String getHeaderValuesAsString(final List<String> list) { |
| 130 | + if(list == null || list.size() == 0)return ""; |
| 131 | + StringBuilder builder = new StringBuilder(list.get(0)); |
| 132 | + for(int i=1;i<list.size();i++) { |
| 133 | + builder.append(";"); |
| 134 | + builder.append(list.get(i)); |
| 135 | + } |
| 136 | + return builder.toString(); |
| 137 | + } |
| 138 | + |
| 139 | + private JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{ |
| 140 | + if(request == null || request.body() == null)return null; |
| 141 | + Request copy = request.newBuilder().build(); |
| 142 | + Buffer buffer = new Buffer(); |
| 143 | + copy.body().writeTo(buffer); |
| 144 | + String requestBody = buffer.readUtf8(); |
| 145 | + JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody); |
| 146 | + return jsonObject; |
| 147 | + } |
| 148 | + |
112 | 149 | } |
0 commit comments