|
| 1 | +package com.microsoft.graph.httpcore; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.json.simple.JSONArray; |
| 9 | +import org.json.simple.JSONObject; |
| 10 | +import org.json.simple.parser.JSONParser; |
| 11 | +import org.json.simple.parser.ParseException; |
| 12 | + |
| 13 | +import okhttp3.MediaType; |
| 14 | +import okhttp3.Protocol; |
| 15 | +import okhttp3.Request; |
| 16 | +import okhttp3.RequestBody; |
| 17 | +import okhttp3.Response; |
| 18 | +import okhttp3.ResponseBody; |
| 19 | +import okio.Buffer; |
| 20 | + |
| 21 | +public class RequestSerializer { |
| 22 | + |
| 23 | + public static Map<String, Request> createBatchRequestsHashMap(Response batchResponse) { |
| 24 | + if(batchResponse == null)return null; |
| 25 | + try { |
| 26 | + Map<String, Request> batchRequestsHashMap = new HashMap<>(); |
| 27 | + JSONObject requestJSONObject = requestBodyToJSONObject(batchResponse.request()); |
| 28 | + JSONArray requestArray = (JSONArray)requestJSONObject.get("requests"); |
| 29 | + for(Object item : requestArray) { |
| 30 | + JSONObject requestObject = (JSONObject)item; |
| 31 | + |
| 32 | + Request.Builder builder = new Request.Builder(); |
| 33 | + |
| 34 | + if(requestObject.get("url") != null) { |
| 35 | + StringBuilder fullUrl = new StringBuilder(batchResponse.request().url().toString().replace("$batch","")); |
| 36 | + fullUrl.append(requestObject.get("url").toString()); |
| 37 | + builder.url(fullUrl.toString()); |
| 38 | + } |
| 39 | + if(requestObject.get("headers") != null) { |
| 40 | + JSONObject jsonheaders = (JSONObject)requestObject.get("headers"); |
| 41 | + for(Object key: jsonheaders.keySet()) { |
| 42 | + String strkey = (String)key; |
| 43 | + String strvalue = (String)jsonheaders.get(strkey); |
| 44 | + for(String value : strvalue.split("; ")) { |
| 45 | + builder.header(strkey, value); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + if(requestObject.get("body") != null) { |
| 50 | + JSONObject jsonObject = (JSONObject)requestObject.get("body"); |
| 51 | + String bodyAsString = jsonObject.toJSONString(); |
| 52 | + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString); |
| 53 | + builder.method(requestObject.get("method").toString(), requestBody); |
| 54 | + } else { |
| 55 | + builder.method(requestObject.get("method").toString(), null); |
| 56 | + } |
| 57 | + batchRequestsHashMap.put(requestObject.get("id").toString(), builder.build()); |
| 58 | + } |
| 59 | + return batchRequestsHashMap; |
| 60 | + |
| 61 | + } catch (IOException | ParseException e) { e.printStackTrace(); } |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + * Convert request body to JSONObject |
| 67 | + */ |
| 68 | + public static JSONObject requestToJSONObject(final Request request) throws IOException, ParseException{ |
| 69 | + Request copy = request.newBuilder().build(); |
| 70 | + Buffer buffer = new Buffer(); |
| 71 | + copy.body().writeTo(buffer); |
| 72 | + String body = buffer.readUtf8(); |
| 73 | + JSONObject json = (JSONObject)new JSONParser().parse(body); |
| 74 | + return json; |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + * Converts String to JSONObject |
| 79 | + */ |
| 80 | + public static JSONObject stringToJSONObject(String input) { |
| 81 | + JSONParser parser = new JSONParser(); |
| 82 | + JSONObject jsonObject = null; |
| 83 | + try { |
| 84 | + if(input != null) { |
| 85 | + jsonObject = (JSONObject) parser.parse(input); |
| 86 | + } |
| 87 | + } |
| 88 | + catch(Exception e) { |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + return jsonObject; |
| 92 | + } |
| 93 | + |
| 94 | + /* |
| 95 | + * @param list List of parameters of a single header |
| 96 | + * @return List of headers to a String seperated by "; " |
| 97 | + */ |
| 98 | + public static String getHeaderValuesAsString(final List<String> list) { |
| 99 | + if(list == null || list.size() == 0)return ""; |
| 100 | + StringBuilder builder = new StringBuilder(list.get(0)); |
| 101 | + for(int i=1;i<list.size();i++) { |
| 102 | + builder.append(";"); |
| 103 | + builder.append(list.get(i)); |
| 104 | + } |
| 105 | + return builder.toString(); |
| 106 | + } |
| 107 | + |
| 108 | + public static JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{ |
| 109 | + if(request == null || request.body() == null)return null; |
| 110 | + Request copy = request.newBuilder().build(); |
| 111 | + Buffer buffer = new Buffer(); |
| 112 | + copy.body().writeTo(buffer); |
| 113 | + String requestBody = buffer.readUtf8(); |
| 114 | + JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody); |
| 115 | + return jsonObject; |
| 116 | + } |
| 117 | +} |
0 commit comments