@Test public void doHttpPostTest2() throws Exception{ PowerMockito.when(httpClient.execute((HttpPost) any())).thenReturn(httpResponse); final Map<String, String> headers = Maps.newHashMap(); headers.put("key1", "value1"); assertNotNull(CommonUtils.doHttpPost("http: }
public static String doHttpPost(final String url, String requestBody) throws Exception { CloseableHttpClient httpclient = null; try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost = new HttpPost(url); httppost.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(requestBody); httppost.setEntity(jsonEntity); HttpResponse httpresponse = httpclient.execute(httppost); int statusCode = httpresponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse.getEntity()); } else {} try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost1 = new HttpPost(url); if(AuthManager.getToken()!=null){ String accessToken = AuthManager.getToken(); if(!Strings.isNullOrEmpty(accessToken)) { httppost1.setHeader(PacmanSdkConstants.AUTH_HEADER, "Bearer " + accessToken); } } httppost1.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity1 = new StringEntity(requestBody); httppost1.setEntity(jsonEntity1); HttpResponse httpresponse1 = httpclient.execute(httppost1); int statusCode1 = httpresponse1.getStatusLine().getStatusCode(); if (statusCode1 == HttpStatus.SC_OK || statusCode1 == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse1.getEntity()); } else { LOGGER.error(requestBody); throw new Exception( "unable to execute post request because " + httpresponse1.getStatusLine().getReasonPhrase()); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } }
CommonUtils { public static String doHttpPost(final String url, String requestBody) throws Exception { CloseableHttpClient httpclient = null; try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost = new HttpPost(url); httppost.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(requestBody); httppost.setEntity(jsonEntity); HttpResponse httpresponse = httpclient.execute(httppost); int statusCode = httpresponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse.getEntity()); } else {} try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost1 = new HttpPost(url); if(AuthManager.getToken()!=null){ String accessToken = AuthManager.getToken(); if(!Strings.isNullOrEmpty(accessToken)) { httppost1.setHeader(PacmanSdkConstants.AUTH_HEADER, "Bearer " + accessToken); } } httppost1.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity1 = new StringEntity(requestBody); httppost1.setEntity(jsonEntity1); HttpResponse httpresponse1 = httpclient.execute(httppost1); int statusCode1 = httpresponse1.getStatusLine().getStatusCode(); if (statusCode1 == HttpStatus.SC_OK || statusCode1 == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse1.getEntity()); } else { LOGGER.error(requestBody); throw new Exception( "unable to execute post request because " + httpresponse1.getStatusLine().getReasonPhrase()); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } }
CommonUtils { public static String doHttpPost(final String url, String requestBody) throws Exception { CloseableHttpClient httpclient = null; try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost = new HttpPost(url); httppost.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(requestBody); httppost.setEntity(jsonEntity); HttpResponse httpresponse = httpclient.execute(httppost); int statusCode = httpresponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse.getEntity()); } else {} try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost1 = new HttpPost(url); if(AuthManager.getToken()!=null){ String accessToken = AuthManager.getToken(); if(!Strings.isNullOrEmpty(accessToken)) { httppost1.setHeader(PacmanSdkConstants.AUTH_HEADER, "Bearer " + accessToken); } } httppost1.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity1 = new StringEntity(requestBody); httppost1.setEntity(jsonEntity1); HttpResponse httpresponse1 = httpclient.execute(httppost1); int statusCode1 = httpresponse1.getStatusLine().getStatusCode(); if (statusCode1 == HttpStatus.SC_OK || statusCode1 == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse1.getEntity()); } else { LOGGER.error(requestBody); throw new Exception( "unable to execute post request because " + httpresponse1.getStatusLine().getReasonPhrase()); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } }  }
CommonUtils { public static String doHttpPost(final String url, String requestBody) throws Exception { CloseableHttpClient httpclient = null; try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost = new HttpPost(url); httppost.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(requestBody); httppost.setEntity(jsonEntity); HttpResponse httpresponse = httpclient.execute(httppost); int statusCode = httpresponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse.getEntity()); } else {} try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost1 = new HttpPost(url); if(AuthManager.getToken()!=null){ String accessToken = AuthManager.getToken(); if(!Strings.isNullOrEmpty(accessToken)) { httppost1.setHeader(PacmanSdkConstants.AUTH_HEADER, "Bearer " + accessToken); } } httppost1.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity1 = new StringEntity(requestBody); httppost1.setEntity(jsonEntity1); HttpResponse httpresponse1 = httpclient.execute(httppost1); int statusCode1 = httpresponse1.getStatusLine().getStatusCode(); if (statusCode1 == HttpStatus.SC_OK || statusCode1 == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse1.getEntity()); } else { LOGGER.error(requestBody); throw new Exception( "unable to execute post request because " + httpresponse1.getStatusLine().getReasonPhrase()); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } }  static Boolean isEnvVariableExists(String envVariableName); static String getEnvVariableValue(String envVariableName); static String doHttpPost(final String url,  String requestBody); static String doHttpPut(final String url, final String requestBody); static boolean isValidResource(String esUrl); static Object buildQueryForExistingIssues(Map<String, Object> filter); static String getIndexNameFromRuleParam(Map<String, String> ruleParam); @SuppressWarnings("unchecked") static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap); static String getUniqueAnnotationId(Annotation annotation); static String getUniqueAnnotationId(String parentId, String ruleId); static String getUniqueIdForString(String inStr); static String hash(String s); static Map<String, String> createParamMap(String ruleParams); static String decrypt(String encryptedText); static Map<String, String> buildMapFromString(String input, String splitOn, String keyValueSeparator); static Long getElapseTimeSince(long startTime); static String getCurrentDateStringWithFormat(String timeZone, String format); static Date getDateFromString(final String dateInString, final String format); static Date dateFormat(final String dateInString, String formatFrom, String formatTo); static int compareDate(final Date firstDate, final Date lastDate); static boolean resourceCreatedBeforeCutoffData(final Date resourceCreationDate); static String doHttpPost(final String url, final String requestBody, final Map<String, String> headers); static String doHttpGet(final String url); static SSLContext createNoSSLContext(); static String getJsonString(final Object annotation); static String encryptB64(String plainText); static String decryptB64(String text); static String encrypt(String plainText, final String key); static String decrypt(String encryptedText, final String key); static String getPropValue(final String keyname); static String getTemplateContent(final String templateName); static String serializeToString(Object object); static Object deSerializeToObject(String jsonString); static Boolean doesAllHaveValue(String...strings ); }
CommonUtils { public static String doHttpPost(final String url, String requestBody) throws Exception { CloseableHttpClient httpclient = null; try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost = new HttpPost(url); httppost.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(requestBody); httppost.setEntity(jsonEntity); HttpResponse httpresponse = httpclient.execute(httppost); int statusCode = httpresponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse.getEntity()); } else {} try { if (url.contains(HTTPS)) { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createNoSSLContext()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } else { httpclient = HttpClients.custom().build(); } HttpPost httppost1 = new HttpPost(url); if(AuthManager.getToken()!=null){ String accessToken = AuthManager.getToken(); if(!Strings.isNullOrEmpty(accessToken)) { httppost1.setHeader(PacmanSdkConstants.AUTH_HEADER, "Bearer " + accessToken); } } httppost1.setHeader(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity1 = new StringEntity(requestBody); httppost1.setEntity(jsonEntity1); HttpResponse httpresponse1 = httpclient.execute(httppost1); int statusCode1 = httpresponse1.getStatusLine().getStatusCode(); if (statusCode1 == HttpStatus.SC_OK || statusCode1 == HttpStatus.SC_CREATED) { return EntityUtils.toString(httpresponse1.getEntity()); } else { LOGGER.error(requestBody); throw new Exception( "unable to execute post request because " + httpresponse1.getStatusLine().getReasonPhrase()); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } } catch (ParseException parseException) { LOGGER.error("error closing issue" + parseException); throw parseException; } catch (Exception exception) { LOGGER.error("error closing issue" + exception.getMessage()); throw exception; } finally { if (null != httpclient) httpclient.close(); } }  static Boolean isEnvVariableExists(String envVariableName); static String getEnvVariableValue(String envVariableName); static String doHttpPost(final String url,  String requestBody); static String doHttpPut(final String url, final String requestBody); static boolean isValidResource(String esUrl); static Object buildQueryForExistingIssues(Map<String, Object> filter); static String getIndexNameFromRuleParam(Map<String, String> ruleParam); @SuppressWarnings("unchecked") static Map<String, String> flatNestedMap(String notation, Map<String, Object> nestedMap); static String getUniqueAnnotationId(Annotation annotation); static String getUniqueAnnotationId(String parentId, String ruleId); static String getUniqueIdForString(String inStr); static String hash(String s); static Map<String, String> createParamMap(String ruleParams); static String decrypt(String encryptedText); static Map<String, String> buildMapFromString(String input, String splitOn, String keyValueSeparator); static Long getElapseTimeSince(long startTime); static String getCurrentDateStringWithFormat(String timeZone, String format); static Date getDateFromString(final String dateInString, final String format); static Date dateFormat(final String dateInString, String formatFrom, String formatTo); static int compareDate(final Date firstDate, final Date lastDate); static boolean resourceCreatedBeforeCutoffData(final Date resourceCreationDate); static String doHttpPost(final String url, final String requestBody, final Map<String, String> headers); static String doHttpGet(final String url); static SSLContext createNoSSLContext(); static String getJsonString(final Object annotation); static String encryptB64(String plainText); static String decryptB64(String text); static String encrypt(String plainText, final String key); static String decrypt(String encryptedText, final String key); static String getPropValue(final String keyname); static String getTemplateContent(final String templateName); static String serializeToString(Object object); static Object deSerializeToObject(String jsonString); static Boolean doesAllHaveValue(String...strings );  }