List of usage examples for java.io OutputStreamWriter toString
public String toString()
From source file:fr.mby.saml2.sp.opensaml.query.engine.SloRequestQueryProcessor.java
/** * Send the SLO Response via the URL Api. * /*from ww w.j a va 2s.c om*/ * @param binding * the binding to use * @param sloResponseRequest * the SLO Response request */ protected void sendSloResponse(final SamlBindingEnum binding, final IOutgoingSaml sloResponseRequest) { URL sloUrl = null; HttpURLConnection sloConnexion = null; try { switch (binding) { case SAML_20_HTTP_REDIRECT: final String redirectUrl = sloResponseRequest.getHttpRedirectBindingUrl(); sloUrl = new URL(redirectUrl); sloConnexion = (HttpURLConnection) sloUrl.openConnection(); sloConnexion.setReadTimeout(10000); sloConnexion.connect(); break; case SAML_20_HTTP_POST: final String sloEndpointUrl = sloResponseRequest.getEndpointUrl(); final Collection<Entry<String, String>> sloPostParams = sloResponseRequest .getHttpPostBindingParams(); final StringBuffer samlDatas = new StringBuffer(1024); final Iterator<Entry<String, String>> itParams = sloPostParams.iterator(); final Entry<String, String> firstParam = itParams.next(); samlDatas.append(firstParam.getKey()); samlDatas.append("="); samlDatas.append(firstParam.getValue()); while (itParams.hasNext()) { final Entry<String, String> param = itParams.next(); samlDatas.append("&"); samlDatas.append(param.getKey()); samlDatas.append("="); samlDatas.append(param.getValue()); } sloUrl = new URL(sloEndpointUrl); sloConnexion = (HttpURLConnection) sloUrl.openConnection(); sloConnexion.setDoInput(true); final OutputStreamWriter writer = new OutputStreamWriter(sloConnexion.getOutputStream()); writer.write(samlDatas.toString()); writer.flush(); writer.close(); sloConnexion.setReadTimeout(10000); sloConnexion.connect(); break; default: break; } if (sloConnexion != null) { final InputStream responseStream = sloConnexion.getInputStream(); final StringWriter writer = new StringWriter(); IOUtils.copy(responseStream, writer, "UTF-8"); final String response = writer.toString(); this.logger.debug(String.format("HTTP response to SLO Request sent: [%s] ", response)); final int responseCode = sloConnexion.getResponseCode(); final String samlMessage = sloResponseRequest.getSamlMessage(); final String endpointUrl = sloResponseRequest.getEndpointUrl(); if (responseCode < 0) { this.logger.error("Unable to send SAML 2.0 Single Logout Response [{}] to endpoint URL [{}] !", samlMessage, endpointUrl); } else if (responseCode == 200) { this.logger.info("SAML 2.0 Single Logout Request correctly sent to [{}] !", endpointUrl); } else { this.logger.error( "HTTP response code: [{}] ! Error while sending SAML 2.0 Single Logout Request [{}] to endpoint URL [{}] !", new Object[] { responseCode, samlMessage, endpointUrl }); } } } catch (final MalformedURLException e) { this.logger.error(String.format("Malformed SAML SLO request URL: [%s] !", sloUrl.toExternalForm()), e); } catch (final IOException e) { this.logger.error(String.format("Unable to send SAML SLO request URL: [%s] !", sloUrl.toExternalForm()), e); } finally { sloConnexion.disconnect(); } }
From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java
public static String doPost(String serviceUrl, String queryString) { URLConnection connection = null; try {/*from w ww . ja va 2s.co m*/ URL url = new URL(serviceUrl); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); // Open the connection connection = url.openConnection(); connection.setDoInput(true); connection.setUseCaches(false); // Disable caching the document connection.setDoOutput(true); // Triggers POST. connection.setRequestProperty("Content-Type", "text/html"); OutputStreamWriter writer = null; log.info("About to write"); try { if (null != connection.getOutputStream()) { writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(queryString); // Write POST query } else { log.warn("connection Null"); } // string. } catch (ConnectException ex) { log.warn("Exception : " + ex); // ex.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (Exception lg) { log.warn("Exception lg: " + lg.toString()); //lg.printStackTrace(); } } } InputStream in = connection.getInputStream(); // StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "utf-8"); String theString = writer.toString(); return theString; } catch (Exception e) { //e.printStackTrace(); log.warn("Error URL " + e.toString()); return ""; } }