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;
}
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;
}
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());
}
}
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());
}
}
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);
}
}
Aggregations