Search in sources :

Example 91 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project janrufmonitor by tbrandt77.

the class TR064FritzBoxFirmware method doHttpCall.

private StringBuffer doHttpCall(String u, String method, String body, String[][] headers, String user, String pw, boolean isBase64Encoded) throws IOException {
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP call: " + u + ", method " + method);
    URL url = new URL(u);
    URLConnection connection = null;
    if (url.getProtocol().equalsIgnoreCase("https")) {
        connection = (HttpsURLConnection) url.openConnection();
        ((HttpsURLConnection) connection).setRequestMethod(method);
    } else {
        connection = (HttpURLConnection) url.openConnection();
        ((HttpURLConnection) connection).setRequestMethod(method);
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP headers...");
    for (int i = 0; i < headers.length; i++) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info(headers[i][0] + ": " + headers[i][1]);
        connection.setRequestProperty(headers[i][0], headers[i][1]);
    }
    if (user != null && pw != null) {
        connection.setRequestProperty("Authorization", "Basic " + Base64Encoder.encode(user + ":" + pw));
    }
    if (body != null) {
        if (this.m_logger.isLoggable(Level.INFO)) {
            this.m_logger.info("HTTP body...");
            this.m_logger.info(body);
        }
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(body);
        writer.flush();
        writer.close();
    }
    StringBuffer response = new StringBuffer();
    if (!isBase64Encoded) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("HTTP response is plain/text.");
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        for (String line; (line = reader.readLine()) != null; ) {
            response.append(line);
            response.append(IJAMConst.CRLF);
        }
        reader.close();
    } else {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("HTTP response is base64 encoded.");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Base64Encoder b64 = new Base64Encoder(bos);
        Stream.copy(new BufferedInputStream(connection.getInputStream()), b64);
        b64.flush();
        b64.close();
        response.append(new String(bos.toByteArray()));
    }
    if (this.m_logger.isLoggable(Level.INFO)) {
        this.m_logger.info("HTTP response...");
        this.m_logger.info(response.toString());
    }
    return response;
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 92 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project janrufmonitor by tbrandt77.

the class FritzBoxTR064Manager method doHttpCall.

private StringBuffer doHttpCall(String u, String method, String body, String encoding, String[][] headers) throws IOException {
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP call: " + u + ", method " + method);
    URL url = new URL(u);
    URLConnection connection = null;
    if (url.getProtocol().equalsIgnoreCase("https")) {
        connection = (HttpsURLConnection) url.openConnection();
        ((HttpsURLConnection) connection).setRequestMethod(method);
    } else {
        connection = (HttpURLConnection) url.openConnection();
        ((HttpURLConnection) connection).setRequestMethod(method);
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP headers...");
    for (int i = 0; i < headers.length; i++) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info(headers[i][0] + ": " + headers[i][1]);
        connection.setRequestProperty(headers[i][0], headers[i][1]);
    }
    if (body != null) {
        if (this.m_logger.isLoggable(Level.INFO)) {
            this.m_logger.info("HTTP body...");
            this.m_logger.info(body);
        }
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(body);
        writer.flush();
        writer.close();
    }
    StringBuffer response = new StringBuffer();
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("HTTP response set as text or XML data");
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
    for (String line; (line = reader.readLine()) != null; ) {
        response.append(line);
        response.append(IJAMConst.CRLF);
    }
    reader.close();
    if (this.m_logger.isLoggable(Level.INFO)) {
        this.m_logger.info("HTTP response...");
        this.m_logger.info(response.toString());
    }
    return response;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 93 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project components by Talend.

the class MarketoBaseRESTClient method httpFakeGet.

public InputStreamReader httpFakeGet(String content, boolean isForLead) throws MarketoException {
    try {
        current_uri.append(fmtParams(QUERY_METHOD, QUERY_METHOD_GET));
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod(QUERY_METHOD_POST);
        if (isForLead) {
            urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_X_WWW_FORM_URLENCODED);
        } else {
            urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_JSON);
        }
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        urlConn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(content);
        wr.flush();
        wr.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            InputStreamReader reader = new InputStreamReader(inStream);
            return reader;
        } else {
            LOG.error("POST request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("POST request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 94 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project components by Talend.

the class MarketoBaseRESTClient method executeGetRequest.

public MarketoRecordResult executeGetRequest(Schema schema) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("GET");
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            Reader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            MarketoRecordResult mkr = new MarketoRecordResult();
            LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
            mkr.setRequestId(REST + "::" + ltm.get("requestId"));
            mkr.setSuccess(Boolean.parseBoolean(ltm.get("success").toString()));
            mkr.setStreamPosition((String) ltm.get(FIELD_NEXT_PAGE_TOKEN));
            if (!mkr.isSuccess() && ltm.get(FIELD_ERRORS) != null) {
                List<LinkedTreeMap> errors = (List<LinkedTreeMap>) ltm.get(FIELD_ERRORS);
                for (LinkedTreeMap err : errors) {
                    MarketoError error = new MarketoError(REST, (String) err.get("code"), (String) err.get("message"));
                    mkr.setErrors(Arrays.asList(error));
                }
            }
            if (mkr.isSuccess()) {
                List<LinkedTreeMap> tmp = (List<LinkedTreeMap>) ltm.get("result");
                if (tmp != null) {
                    mkr.setRecordCount(tmp.size());
                    mkr.setRecords(parseRecords(tmp, schema));
                }
                if (mkr.getStreamPosition() != null) {
                    mkr.setRemainCount(mkr.getRecordCount());
                }
            }
            return mkr;
        } else {
            LOG.error("GET request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Example 95 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project components by Talend.

the class MarketoBulkExecClient method executeDownloadFileRequest.

public void executeDownloadFileRequest(File filename) throws MarketoException {
    String err;
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("GET");
        urlConn.setRequestProperty("accept", "text/json");
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            FileUtils.copyInputStreamToFile(inStream, filename);
        } else {
            err = String.format("Download failed for %s. Status: %d", filename, responseCode);
            throw new MarketoException(REST, err);
        }
    } catch (IOException e) {
        err = String.format("Download failed for %s. Cause: %s", filename, e.getMessage());
        LOG.error(err);
        throw new MarketoException(REST, err);
    }
}
Also used : InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)522 URL (java.net.URL)310 IOException (java.io.IOException)177 HttpURLConnection (java.net.HttpURLConnection)128 InputStreamReader (java.io.InputStreamReader)93 InputStream (java.io.InputStream)89 Test (org.junit.Test)83 BufferedReader (java.io.BufferedReader)78 SSLContext (javax.net.ssl.SSLContext)70 OutputStream (java.io.OutputStream)54 HostnameVerifier (javax.net.ssl.HostnameVerifier)50 MalformedURLException (java.net.MalformedURLException)48 URLConnection (java.net.URLConnection)47 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)32 JSONObject (org.json.JSONObject)29