Example usage for java.net MalformedURLException printStackTrace

List of usage examples for java.net MalformedURLException printStackTrace

Introduction

In this page you can find the example usage for java.net MalformedURLException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:it.publisys.liferay.hook.shibboleth.ShibbolethPostLogoutAction.java

/**
 * Effettua una {@link HttpURLConnection} inviando anche i cookies
 *
 * @param url     url/*from  w ww.j a v  a  2  s.c  om*/
 * @param cookies cookies
 * @return response code
 */
private int _connect(String url, String cookies) {
    int responseCode = -1;
    try {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] xcs, String string)
                    throws CertificateException {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] xcs, String string)
                    throws CertificateException {
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }

    HttpURLConnection connection = null;
    try {
        URL _url = new URL(url);
        connection = (HttpURLConnection) _url.openConnection(Proxy.NO_PROXY);
        connection.setRequestProperty("Cookie", cookies);
        connection.setReadTimeout(5000);
        connection.setRequestMethod("GET");

        responseCode = connection.getResponseCode();
        _log.info("Logout Shibb response code: " + responseCode);

        if (responseCode == 200 && _log.isDebugEnabled()) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                StringBuilder _buffer = new StringBuilder();
                String line = null;
                while ((line = br.readLine()) != null) {
                    _buffer.append(line);
                }
                _log.debug(_buffer.toString());
            } finally {
                if (br != null) {
                    br.close();
                }
            }

        }

    } catch (MalformedURLException mue) {
        mue.printStackTrace(System.err);
    } catch (IOException ioe) {
        ioe.printStackTrace(System.err);
    } finally {
        try {
            if (connection != null) {
                connection.disconnect();
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    return responseCode;
}