- private void callRest() {
- // Finding the Macros in the URL..
- Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9.:]*)");
- Matcher match = pattern.matcher(configURL);
- StringBuffer sb = new StringBuffer();
- JsonReader jsonReader = null;
- while (match.find()) {
- LOGGER.info("Found Macro : " + match.group(1));
- String replaceValue = matchValues.get(match.group(1));
- LOGGER.info("Replacing with :" + replaceValue);
- match.appendReplacement(sb, replaceValue);
- }
- match.appendTail(sb);
- LOGGER.info("URL is : " + sb.toString());
- configURL = sb.toString();
- // Calling the Requested service.
- if (matchValues.get("method").equalsIgnoreCase("GET")) {
- httpClient = new DefaultHttpClient();
- try {
- HttpGet getRequest = new HttpGet(configURL);
- // Adding Headers here
- if (header) {
- for (String key : headers.keySet()) {
- getRequest.addHeader(key, headers.get(key));
- }
- }
- HttpResponse result = httpClient.execute(getRequest);
- status = result.getStatusLine().getStatusCode();
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (result.getEntity().getContent())));
- String output = " ";
- String out;
- while ((out = br.readLine()) != null) {
- output = output + out;
- }
- response = output;
- } catch (Exception e) {
- LOGGER.error(e.getMessage()+e);
- response = e.getMessage();
- } finally {
- httpClient.getConnectionManager().shutdown();
- }
- } else if(matchValues.get("method").equalsIgnoreCase("POST")) {
- httpClient = new DefaultHttpClient();
- try {
- HttpPost postRequest = new HttpPost(configURL);
- // Adding Headers here
- if (header) {
- for (String key : headers.keySet()) {
- postRequest.addHeader(key, headers.get(key));
- }
- }
- // Adding the Body.
- URL configURL = new URL(matchValues.get("body"));
- URLConnection connection = null;
- connection = configURL.openConnection();
- // InputStream in = connection.getInputStrem();
- // LOGGER.info("The Body Content is : " + IOUtils.toString(in));
- jsonReader = Json.createReader(connection.getInputStream());
- StringEntity input = new StringEntity(jsonReader.readObject().toString());
- input.setContentType("application/json");
- postRequest.setEntity(input);
- // Executing the Request.
- HttpResponse result = httpClient.execute(postRequest);
- LOGGER.info("Result Headers are : " + result.getAllHeaders());
- status = result.getStatusLine().getStatusCode();
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (result.getEntity().getContent())));
- String output = " ";
- String out;
- while ((out = br.readLine()) != null) {
- output = output + out;
- }
- response = output;
- }catch (Exception e) {
- LOGGER.error(e.getMessage() +e);
- response = e.getMessage();
- } finally {
- if(jsonReader != null) {
- try {
- jsonReader.close();
- } catch (Exception e) {
- LOGGER.error("Exception Occured while closing the JsonReader"+e);
- }
- }
- httpClient.getConnectionManager().shutdown();
- }
- } else if(matchValues.get("method").equalsIgnoreCase("PUT")) {
- httpClient = new DefaultHttpClient();
- try {
- HttpPut putRequest = new HttpPut(configURL);
- // Adding Headers here
- if (header) {
- for (String key : headers.keySet()) {
- putRequest.addHeader(key, headers.get(key));
- }
- }
- // Adding the Body.
- URL configURL = new URL(matchValues.get("body"));
- URLConnection connection = null;
- connection = configURL.openConnection();
- //InputStream in = connection.getInputStream();
- //LOGGER.info("The Body Content is : " + IOUtils.toString(in));
- jsonReader = Json.createReader(connection.getInputStream());
- StringEntity input = new StringEntity(jsonReader.readObject().toString());
- input.setContentType("application/json");
- putRequest.setEntity(input);
- // Executing the Request.
- HttpResponse result = httpClient.execute(putRequest);
- status = result.getStatusLine().getStatusCode();
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (result.getEntity().getContent())));
- String output = " ";
- String out;
- while ((out = br.readLine()) != null) {
- output = output + out;
- }
- response = output;
- } catch (Exception e) {
- LOGGER.error(e.getMessage() +e);
- response = e.getMessage();
- }finally {
- if(jsonReader != null) {
- try {
- jsonReader.close();
- } catch (Exception e) {
- LOGGER.error("Exception Occured while closing the JsonReader"+e);
- }
- }
- httpClient.getConnectionManager().shutdown();
- }
- }
- }
-}
\ No newline at end of file
+ private void callRest() {
+ // Finding the Macros in the URL..
+ final Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9.:]*)");
+ final Matcher match = pattern.matcher(configUrl);
+ final StringBuffer sb = new StringBuffer();
+ JsonReader jsonReader = null;
+ while (match.find()) {
+ logger.info("Found Macro : " + match.group(1));
+ final String replaceValue = matchValues.get(match.group(1));
+ logger.info("Replacing with :" + replaceValue);
+ match.appendReplacement(sb, replaceValue);
+ }
+ match.appendTail(sb);
+ logger.info("URL is : " + sb.toString());
+ configUrl = sb.toString();
+ // Calling the Requested service.
+ if (matchValues.get("method").equalsIgnoreCase("GET")) {
+ httpClient = new DefaultHttpClient();
+ try {
+ final HttpGet getRequest = new HttpGet(configUrl);
+ // Adding Headers here
+ if (header) {
+ for (final String key : headers.keySet()) {
+ getRequest.addHeader(key, headers.get(key));
+ }
+ }
+ final HttpResponse result = httpClient.execute(getRequest);
+ status = result.getStatusLine().getStatusCode();
+ final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
+ String output = " ";
+ String out;
+ while ((out = br.readLine()) != null) {
+ output = output + out;
+ }
+ response = output;
+ } catch (final Exception e) {
+ logger.error(e.getMessage() + e);
+ response = e.getMessage();
+ } finally {
+ httpClient.getConnectionManager().shutdown();
+ }
+ } else if (matchValues.get("method").equalsIgnoreCase("POST")) {
+ httpClient = new DefaultHttpClient();
+ try {
+ final HttpPost postRequest = new HttpPost(configUrl);
+ // Adding Headers here
+ if (header) {
+ for (final String key : headers.keySet()) {
+ postRequest.addHeader(key, headers.get(key));
+ }
+ }
+ // Adding the Body.
+ final URL configUrl = new URL(matchValues.get("body"));
+ URLConnection connection = null;
+ connection = configUrl.openConnection();
+ // InputStream in = connection.getInputStrem();
+ // LOGGER.info("The Body Content is : " + IOUtils.toString(in));
+ jsonReader = Json.createReader(connection.getInputStream());
+ final StringEntity input = new StringEntity(jsonReader.readObject().toString());
+ input.setContentType("application/json");
+ postRequest.setEntity(input);
+ // Executing the Request.
+ final HttpResponse result = httpClient.execute(postRequest);
+ logger.info("Result Headers are : " + result.getAllHeaders());
+ status = result.getStatusLine().getStatusCode();
+ final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
+ String output = " ";
+ String out;
+ while ((out = br.readLine()) != null) {
+ output = output + out;
+ }
+ response = output;
+ } catch (final Exception e) {
+ logger.error(e.getMessage() + e);
+ response = e.getMessage();
+ } finally {
+ if (jsonReader != null) {
+ try {
+ jsonReader.close();
+ } catch (final Exception e) {
+ logger.error("Exception Occured while closing the JsonReader" + e);
+ }
+ }
+ httpClient.getConnectionManager().shutdown();
+ }
+ } else if (matchValues.get("method").equalsIgnoreCase("PUT")) {
+ httpClient = new DefaultHttpClient();
+ try {
+ final HttpPut putRequest = new HttpPut(configUrl);
+ // Adding Headers here
+ if (header) {
+ for (final String key : headers.keySet()) {
+ putRequest.addHeader(key, headers.get(key));
+ }
+ }
+ // Adding the Body.
+ final URL configUrl = new URL(matchValues.get("body"));
+ URLConnection connection = null;
+ connection = configUrl.openConnection();
+ // InputStream in = connection.getInputStream();
+ // LOGGER.info("The Body Content is : " + IOUtils.toString(in));
+ jsonReader = Json.createReader(connection.getInputStream());
+ final StringEntity input = new StringEntity(jsonReader.readObject().toString());
+ input.setContentType("application/json");
+ putRequest.setEntity(input);
+ // Executing the Request.
+ final HttpResponse result = httpClient.execute(putRequest);
+ status = result.getStatusLine().getStatusCode();
+ final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
+ String output = " ";
+ String out;
+ while ((out = br.readLine()) != null) {
+ output = output + out;
+ }
+ response = output;
+ } catch (final Exception e) {
+ logger.error(e.getMessage() + e);
+ response = e.getMessage();
+ } finally {
+ if (jsonReader != null) {
+ try {
+ jsonReader.close();
+ } catch (final Exception e) {
+ logger.error("Exception Occured while closing the JsonReader" + e);
+ }
+ }
+ httpClient.getConnectionManager().shutdown();
+ }
+ }
+ }
+}