|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package DataSci.main; |
| 7 | +import java.util.*; |
| 8 | +import java.io.*; |
| 9 | +import org.apache.http.HttpResponse; |
| 10 | +import org.apache.http.client.HttpClient; |
| 11 | +import org.apache.http.client.methods.HttpPost; |
| 12 | +import org.apache.http.entity.StringEntity; |
| 13 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 14 | +import org.apache.http.protocol.HTTP; |
| 15 | +import org.apache.http.util.*; |
| 16 | + |
| 17 | + |
| 18 | +public class JsonScript { |
| 19 | + // public static JSONObject inpParms; |
| 20 | + public static String apikey; |
| 21 | + public static String apiurl; |
| 22 | + public static String jsonBody; |
| 23 | + |
| 24 | + /** |
| 25 | + * Read the JSON schema from the file rrsJson.json |
| 26 | + * |
| 27 | + * @param filename It expects a fully qualified file name that contains input JSON file |
| 28 | + */ |
| 29 | + public static void readJson(String filename) { |
| 30 | + try { |
| 31 | + File apiFile = new File(filename); |
| 32 | + Scanner sc = new Scanner(apiFile); |
| 33 | + jsonBody = ""; |
| 34 | + while (sc.hasNext()) { |
| 35 | + jsonBody += sc.nextLine()+"\n"; |
| 36 | + } |
| 37 | + sc.close(); |
| 38 | + } |
| 39 | + catch (Exception e){ |
| 40 | + System.out.println(e.toString()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Read the API key and API URL of Azure ML request response REST API |
| 46 | + * |
| 47 | + * @param filename fully qualified file name that contains API key and API URL |
| 48 | + */ |
| 49 | + public static void readApiInfo(String filename) { |
| 50 | + |
| 51 | + try { |
| 52 | + File apiFile = new File(filename); |
| 53 | + Scanner sc = new Scanner(apiFile); |
| 54 | + |
| 55 | + apiurl = sc.nextLine(); |
| 56 | + apikey = sc.nextLine(); |
| 57 | + |
| 58 | + sc.close(); |
| 59 | + } |
| 60 | + catch (Exception e){ |
| 61 | + System.out.println(e.toString()); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Call REST API for retrieving prediction from Azure ML |
| 68 | + * @return response from the REST API |
| 69 | + */ |
| 70 | + public static String rrsHttpPost() { |
| 71 | + |
| 72 | + HttpPost post; |
| 73 | + HttpClient client; |
| 74 | + StringEntity entity; |
| 75 | + |
| 76 | + try { |
| 77 | + // create HttpPost and HttpClient object |
| 78 | + post = new HttpPost(apiurl); |
| 79 | + client = HttpClientBuilder.create().build(); |
| 80 | + |
| 81 | + // setup output message by copying JSON body into |
| 82 | + // apache StringEntity object along with content type |
| 83 | + entity = new StringEntity(jsonBody/*, HTTP.UTF_8*/); |
| 84 | + // entity.setContentEncoding(HTTP.UTF_8); |
| 85 | + entity.setContentType("text/json"); |
| 86 | + |
| 87 | + // add HTTP headers |
| 88 | + post.setHeader("Accept", "text/json"); |
| 89 | + post.setHeader("Accept-Charset", "UTF-8"); |
| 90 | + |
| 91 | + // set Authorization header based on the API key |
| 92 | + post.setHeader("Authorization", ("Bearer "+apikey)); |
| 93 | + post.setEntity(entity); |
| 94 | + |
| 95 | + // Call REST API and retrieve response content |
| 96 | + HttpResponse authResponse = client.execute(post); |
| 97 | + |
| 98 | + return EntityUtils.toString(authResponse.getEntity()); |
| 99 | + |
| 100 | + } |
| 101 | + catch (Exception e) { |
| 102 | + |
| 103 | + return e.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param args the command line arguments specifying JSON and API info file names |
| 110 | + */ |
| 111 | + public static void main(String[] args) { |
| 112 | + // check for mandatory arguments. This program expects 2 arguments |
| 113 | + // first argument is full path with file name of JSON file and |
| 114 | + // second argument is full path with file name of API file that contains API URL and API Key of request response REST API |
| 115 | + if (args.length < 2) { |
| 116 | + System.out.println("Incorrect arguments"); |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + |
| 121 | + // read JSON file name |
| 122 | + String jsonFile = args[0]; |
| 123 | + // read API file name |
| 124 | + String apiFile = args[1]; |
| 125 | + |
| 126 | + // call method to read API URL and key from API file |
| 127 | + readApiInfo(apiFile); |
| 128 | + |
| 129 | + // call method to read JSON input from the JSON file |
| 130 | + readJson(jsonFile); |
| 131 | + |
| 132 | + // print the response from REST API |
| 133 | + System.out.println(rrsHttpPost()); |
| 134 | + } |
| 135 | + catch (Exception e) { |
| 136 | + System.out.println(e.toString()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments