List of usage examples for java.net ProtocolException getMessage
public String getMessage()
From source file:org.wso2.carbon.esb.passthru.transport.test.ESBJAVA4891ConsumeAndDiscardTest.java
private static HttpResponse doPost(URL endpoint, String postBody, Map<String, String> headers) throws AutomationFrameworkException, IOException { HttpURLConnection urlConnection = null; try {/*from ww w. j a v a2s .c o m*/ urlConnection = (HttpURLConnection) endpoint.openConnection(); try { urlConnection.setRequestMethod("POST"); } catch (ProtocolException e) { throw new AutomationFrameworkException( "Shouldn't happen: HttpURLConnection doesn't support POST?? " + e.getMessage(), e); } urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setUseCaches(false); urlConnection.setAllowUserInteraction(false); urlConnection.setReadTimeout(10000); for (Map.Entry<String, String> e : headers.entrySet()) { urlConnection.setRequestProperty(e.getKey(), e.getValue()); } OutputStream out = urlConnection.getOutputStream(); try { Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(postBody); writer.close(); } catch (IOException e) { throw new AutomationFrameworkException("IOException while posting data " + e.getMessage(), e); } StringBuilder sb = new StringBuilder(); BufferedReader rd; try { rd = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } catch (FileNotFoundException ignored) { } Iterator<String> itr = urlConnection.getHeaderFields().keySet().iterator(); Map<String, String> responseHeaders = new HashMap<>(); String key; while (itr.hasNext()) { key = itr.next(); if (key != null) { responseHeaders.put(key, urlConnection.getHeaderField(key)); } } return new HttpResponse(sb.toString(), urlConnection.getResponseCode(), responseHeaders); } catch (IOException e) { StringBuilder sb = new StringBuilder(); BufferedReader rd = null; rd = new BufferedReader( new InputStreamReader(urlConnection.getErrorStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } return new HttpResponse(sb.toString(), urlConnection.getResponseCode()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:org.wso2.carbon.esb.passthru.transport.test.PartialInputStreamReadError.java
private static HttpResponse doPost(URL endpoint, String postBody, Map<String, String> headers) throws AutomationFrameworkException, IOException { HttpURLConnection urlConnection = null; try {//w w w . j a va2 s . c om urlConnection = (HttpURLConnection) endpoint.openConnection(); try { urlConnection.setRequestMethod("POST"); } catch (ProtocolException e) { throw new AutomationFrameworkException( "Shouldn't happen: HttpURLConnection doesn't support POST?? " + e.getMessage(), e); } urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setUseCaches(false); urlConnection.setAllowUserInteraction(false); urlConnection.setReadTimeout(10000); for (Map.Entry<String, String> e : headers.entrySet()) { urlConnection.setRequestProperty(e.getKey(), e.getValue()); } OutputStream out = urlConnection.getOutputStream(); try { Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(postBody); writer.close(); } catch (IOException e) { throw new AutomationFrameworkException("IOException while posting data " + e.getMessage(), e); } StringBuilder sb = new StringBuilder(); BufferedReader rd; try { rd = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } catch (FileNotFoundException ignored) { } Iterator<String> itr = urlConnection.getHeaderFields().keySet().iterator(); Object responseHeaders = new HashMap(); String key; while (itr.hasNext()) { key = itr.next(); if (key != null) { ((Map) responseHeaders).put(key, urlConnection.getHeaderField(key)); } } return new HttpResponse(sb.toString(), urlConnection.getResponseCode(), (Map) responseHeaders); } catch (IOException e) { StringBuilder sb = new StringBuilder(); BufferedReader rd = null; rd = new BufferedReader( new InputStreamReader(urlConnection.getErrorStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } return new HttpResponse(sb.toString(), urlConnection.getResponseCode()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:org.wso2.carbon.esb.mediator.test.iterate.IterateJsonPathTest.java
private static HttpResponse doPost(URL endpoint, String postBody, Map<String, String> headers) throws AutomationFrameworkException, IOException { HttpURLConnection urlConnection = null; try {/*from ww w. j av a 2 s . c om*/ urlConnection = (HttpURLConnection) endpoint.openConnection(); try { urlConnection.setRequestMethod("POST"); } catch (ProtocolException e) { throw new AutomationFrameworkException( "Shouldn't happen: HttpURLConnection doesn't support POST?? " + e.getMessage(), e); } urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setUseCaches(false); urlConnection.setAllowUserInteraction(false); urlConnection.setReadTimeout(10000); for (Map.Entry<String, String> e : headers.entrySet()) { urlConnection.setRequestProperty(e.getKey(), e.getValue()); } OutputStream out = urlConnection.getOutputStream(); Writer writer = null; try { writer = new OutputStreamWriter(out, "UTF-8"); writer.write(postBody); } catch (IOException e) { throw new AutomationFrameworkException("IOException while posting data " + e.getMessage(), e); } finally { if (writer != null) { writer.close(); } } StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } finally { if (rd != null) { rd.close(); } } Iterator<String> itr = urlConnection.getHeaderFields().keySet().iterator(); Object responseHeaders = new HashMap(); String key; while (itr.hasNext()) { key = itr.next(); if (key != null) { ((Map) responseHeaders).put(key, urlConnection.getHeaderField(key)); } } return new HttpResponse(sb.toString(), urlConnection.getResponseCode(), (Map) responseHeaders); } catch (IOException e) { StringBuilder sb = new StringBuilder(); BufferedReader rd = null; rd = new BufferedReader( new InputStreamReader(urlConnection.getErrorStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); return new HttpResponse(sb.toString(), urlConnection.getResponseCode()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:com.bia.monitor.service.task.JobCheckImpl.java
/** * Get url connection status/*from w ww .ja v a 2 s . c om*/ * * @return */ private String getStatus(Job job) { String responseCode; try { if (logger.isInfoEnabled()) { logger.info(" pinging " + job.getUrl()); } HttpURLConnection connection = (HttpURLConnection) new URL(job.getUrl()).openConnection(); connection.setRequestMethod("GET"); responseCode = String.valueOf(connection.getResponseCode()); return responseCode; } catch (ProtocolException pe) { responseCode = pe.getMessage(); } catch (IOException io) { responseCode = io.getMessage(); } catch (RuntimeException re) { responseCode = re.getMessage(); } return responseCode; }
From source file:com.esri.geoevent.transport.websocket.WebsocketInboundTransport.java
public synchronized void setup() { try {/* w w w .ja v a 2 s. c o m*/ Property prop = getProperty(URI_PROPERTY_NAME); uri = getURI(prop.getValueAsString()); socket = connect(); setRunningState(RunningState.STARTED); } catch (ProtocolException error) { String errorMessage = LOGGER.translate("CANNOT_CONNECT", getProperty(URI_PROPERTY_NAME)); LOGGER.error(errorMessage); LOGGER.info(error.getMessage(), error); setRunningState(RunningState.ERROR); } catch (Exception ex) { String errorMessage = LOGGER.translate("INIT_ERROR", ex.getMessage()); LOGGER.error(errorMessage); LOGGER.info(ex.getMessage(), ex); setRunningState(RunningState.ERROR); } }
From source file:com.boxupp.utilities.PuppetUtilities.java
public List<SearchModuleBean> searchModule(HttpServletRequest request) { List<SearchModuleBean> moduleList = new ArrayList<SearchModuleBean>(); String module = request.getParameter("query"); String url = "https://forgeapi.puppetlabs.com:443/v3/modules?query=" + module; try {/* w ww . j a va 2s.c o m*/ HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("owner", request.getParameter("owner")); con.setRequestProperty("tag", request.getParameter("tag")); con.setRequestProperty("show+deleted", request.getParameter("show+deleted")); con.setRequestProperty("sort_by", request.getParameter("sort_by")); con.setRequestProperty("operatingsystem", request.getParameter("operatingsystem")); con.setRequestProperty("supported", request.getParameter("supported")); con.setRequestProperty("pe_requirement", request.getParameter("pe_requirement")); con.setRequestProperty("puppet_requirement", request.getParameter("puppet_requirement")); con.setRequestProperty("limit", request.getParameter("limit")); con.setRequestProperty("offset", request.getParameter("offset")); con.setRequestProperty("If-Modified-Since", request.getParameter("If-Modified-Since")); con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject o = new JSONObject(response.toString()); JSONArray jsonArray = o.getJSONArray("results"); for (int i = 0; i < jsonArray.length(); i++) { SearchModuleBean searchModule = new SearchModuleBean(); Gson moduleData = new GsonBuilder().setDateFormat("yyyy'-'MM'-'dd HH':'mm':'ss").create(); searchModule = moduleData.fromJson(jsonArray.get(i).toString(), SearchModuleBean.class); moduleList.add(searchModule); } } catch (ProtocolException e) { logger.error("Error in searching module :" + e.getMessage()); e.printStackTrace(); } catch (IOException e) { logger.error("Error in searching module :" + e.getMessage()); e.printStackTrace(); } catch (JSONException e) { logger.error("Error in searching module :" + e.getMessage()); e.printStackTrace(); } return moduleList; }
From source file:org.wso2.carbon.identity.application.authentication.endpoint.tiqr.QRCode.java
/** * Send REST call/*w ww .ja v a2s.co m*/ */ private String sendRESTCall(String url, String urlParameters, String formParameters, String httpMethod) { String line; StringBuilder responseString = new StringBuilder(); HttpURLConnection connection = null; try { URL tiqrEP = new URL(url + urlParameters); connection = (HttpURLConnection) tiqrEP.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(httpMethod); connection.setRequestProperty(TiqrConstants.HTTP_CONTENT_TYPE, TiqrConstants.HTTP_CONTENT_TYPE_XWFUE); if (httpMethod.toUpperCase().equals(TiqrConstants.HTTP_POST)) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), TiqrConstants.CHARSET); writer.write(formParameters); writer.close(); } if (connection.getResponseCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = br.readLine()) != null) { responseString.append(line); } br.close(); } else { return TiqrConstants.FAILED + TiqrConstants.UNABLE_TO_CONNECT; } } catch (ProtocolException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } catch (MalformedURLException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } finally { connection.disconnect(); } return responseString.toString(); }
From source file:org.wso2.carbon.identity.authenticator.wikid.WiKIDAuthenticator.java
/** * Send REST call// www . j av a 2s . co m */ private String sendRESTCall(String url, String urlParameters, String formParameters, String httpMethod) { String line; StringBuilder responseString = new StringBuilder(); HttpsURLConnection connection = null; try { setHttpsClientCert( "/media/sf_SharedFoldersToVBox/is-connectors/wikid/wikid-authenticator/org.wso2.carbon.identity.authenticator/src/main/resources/localhostWiKID", "shakila"); SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); URL wikidEP = new URL(url + urlParameters); connection = (HttpsURLConnection) wikidEP.openConnection(); connection.setSSLSocketFactory(sslsocketfactory); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(httpMethod); connection.setRequestProperty(WiKIDAuthenticatorConstants.HTTP_CONTENT_TYPE, WiKIDAuthenticatorConstants.HTTP_CONTENT_TYPE_XWFUE); if (httpMethod.toUpperCase().equals(WiKIDAuthenticatorConstants.HTTP_POST)) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), WiKIDAuthenticatorConstants.CHARSET); writer.write(formParameters); writer.close(); } if (connection.getResponseCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = br.readLine()) != null) { responseString.append(line); } br.close(); } else { return WiKIDAuthenticatorConstants.FAILED + WiKIDAuthenticatorConstants.REQUEST_FAILED; } } catch (ProtocolException e) { if (log.isDebugEnabled()) { log.debug(WiKIDAuthenticatorConstants.FAILED + e.getMessage()); } return WiKIDAuthenticatorConstants.FAILED + e.getMessage(); } catch (MalformedURLException e) { if (log.isDebugEnabled()) { log.debug(WiKIDAuthenticatorConstants.FAILED + e.getMessage()); } return WiKIDAuthenticatorConstants.FAILED + e.getMessage(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug(WiKIDAuthenticatorConstants.FAILED + e.getMessage()); } return WiKIDAuthenticatorConstants.FAILED + e.getMessage(); } finally { connection.disconnect(); } return responseString.toString(); }
From source file:org.wso2.carbon.identity.authenticator.tiqr.TiqrAuthenticator.java
/** * Send REST call/*www . jav a 2 s . c o m*/ */ private String sendRESTCall(String url, String urlParameters, String formParameters, String httpMethod) { String line; StringBuilder responseString = new StringBuilder(); HttpURLConnection connection = null; try { URL tiqrEP = new URL(url + urlParameters); connection = (HttpURLConnection) tiqrEP.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(httpMethod); connection.setRequestProperty(TiqrConstants.HTTP_CONTENT_TYPE, TiqrConstants.HTTP_CONTENT_TYPE_XWFUE); if (httpMethod.toUpperCase().equals(TiqrConstants.HTTP_POST)) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), TiqrConstants.CHARSET); writer.write(formParameters); writer.close(); } if (connection.getResponseCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = br.readLine()) != null) { responseString.append(line); } br.close(); } else { return TiqrConstants.FAILED + TiqrConstants.REQUEST_FAILED; } } catch (ProtocolException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } catch (MalformedURLException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug(TiqrConstants.FAILED + e.getMessage()); } return TiqrConstants.FAILED + e.getMessage(); } finally { connection.disconnect(); } return responseString.toString(); }
From source file:org.wso2.carbon.identity.authenticator.emailotp.EmailOTPAuthenticator.java
/** * Send REST call/*from ww w.jav a 2 s. co m*/ */ private String sendRESTCall(String url, String urlParameters, String accessToken, String formParameters, String payload, String httpMethod) { String line; StringBuilder responseString = new StringBuilder(); HttpURLConnection connection = null; try { URL emailOTPEP = new URL(url + urlParameters); connection = (HttpURLConnection) emailOTPEP.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod(httpMethod); if (StringUtils.isNotEmpty(payload)) { if (payload.startsWith("{")) { connection.setRequestProperty(EmailOTPAuthenticatorConstants.HTTP_CONTENT_TYPE, payload.startsWith("{") ? EmailOTPAuthenticatorConstants.HTTP_CONTENT_TYPE_JSON : EmailOTPAuthenticatorConstants.HTTP_CONTENT_TYPE_XML); } } else { connection.setRequestProperty(EmailOTPAuthenticatorConstants.HTTP_CONTENT_TYPE, EmailOTPAuthenticatorConstants.HTTP_CONTENT_TYPE_XWFUE); } if (StringUtils.isNotEmpty(accessToken)) { connection.setRequestProperty(EmailOTPAuthenticatorConstants.HTTP_AUTH, accessToken); } if (httpMethod.toUpperCase().equals(EmailOTPAuthenticatorConstants.HTTP_POST)) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), EmailOTPAuthenticatorConstants.CHARSET); if (StringUtils.isNotEmpty(payload)) { writer.write(payload); } else if (StringUtils.isNotEmpty(formParameters)) { writer.write(formParameters); } writer.close(); } if (connection.getResponseCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = br.readLine()) != null) { responseString.append(line); } br.close(); } else { return EmailOTPAuthenticatorConstants.FAILED + EmailOTPAuthenticatorConstants.REQUEST_FAILED; } } catch (ProtocolException e) { if (log.isDebugEnabled()) { log.debug(EmailOTPAuthenticatorConstants.FAILED + e.getMessage()); } return EmailOTPAuthenticatorConstants.FAILED + e.getMessage(); } catch (MalformedURLException e) { if (log.isDebugEnabled()) { log.debug(EmailOTPAuthenticatorConstants.FAILED + e.getMessage()); } return EmailOTPAuthenticatorConstants.FAILED + e.getMessage(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug(EmailOTPAuthenticatorConstants.FAILED + e.getMessage()); } return EmailOTPAuthenticatorConstants.FAILED + e.getMessage(); } finally { if (connection != null) { connection.disconnect(); } } return responseString.toString(); }