private static HttpPost prepareRequest(String content, String url) { HttpPost POST = new HttpPost(url); try { if (StringUtils.isNotEmpty(content)) POST.setEntity(new StringEntity(content)); } catch (Exception e) { throw new UnexpectedException(e); // will not happen } if (content != null && content.length() > 0) if (!content.startsWith("<?xml")) LOGGER.debug("[ >> ] - {} -> {}", content, url); else LOGGER.debug("[ >> ] - [xml-content] -> {}", url); else LOGGER.debug("[ >> ] - [empty-body] -> {}", url); return POST; // HTTP request prepared }
private static String fetchResponseBody(HttpResponse response) throws IOException { String body = null; HttpEntity entity = response.getEntity(); StatusLine status = response.getStatusLine(); try { body = EntityUtils.toString(entity); } finally { EntityUtils.consume(entity); } if (body.length() < 2048) LOGGER.debug("[ << ] - {} {}", status, body); else LOGGER.debug("[ << ] - {} [body-omitted]", status); return body; // the response body }
private String issueHttpRequest(String command, String content) { String url = driver.getUrl() + "/i/" + command + ".command"; HttpClient client = this.httpClient; HttpPost request = prepareRequest(content, url); String body = null; try { HttpResponse response = client.execute(request); body = fetchResponseBody(response); } catch (SocketTimeoutException ste) { LOGGER.error("fail to POST driver while execute trigger"); } catch (ConnectTimeoutException cte) { LOGGER.error("fail to POST driver while execute trigger"); } catch (Exception e) { LOGGER.error("fail to POST driver while execute trigger"); } return body; // HTTP response body retrieved }
@Override public Tasklet call() { try { execute(); } catch (Exception e) { LOGGER.error("unexpected exception of trigger", e); } return this; /* okay -- done */ }
@Override public void init(Config config, Logger logger) { super.init(config, logger); timeout = config.getInt(CONN_TIMEOUT_KEY, CONN_TIMEOUT_DEFAULT); parms.put(CONN_TIMEOUT_KEY, timeout); endpoint = config.get(ENDPOINT_KEY, ENDPOINT_DEFAULT); accessKey = config.get(AUTH_USERNAME_KEY, AUTH_USERNAME_DEFAULT); secretKey = config.get(AUTH_PASSWORD_KEY, AUTH_PASSWORD_DEFAULT); boolean pathStyleAccess = config.getBoolean(PATH_STYLE_ACCESS_KEY, PATH_STYLE_ACCESS_DEFAULT); String proxyHost = config.get(PROXY_HOST_KEY, ""); String proxyPort = config.get(PROXY_PORT_KEY, ""); parms.put(ENDPOINT_KEY, endpoint); parms.put(AUTH_USERNAME_KEY, accessKey); parms.put(AUTH_PASSWORD_KEY, secretKey); parms.put(PATH_STYLE_ACCESS_KEY, pathStyleAccess); parms.put(PROXY_HOST_KEY, proxyHost); parms.put(PROXY_PORT_KEY, proxyPort); logger.debug("using storage config: {}", parms); ClientConfiguration clientConf = new ClientConfiguration(); clientConf.setConnectionTimeout(timeout); clientConf.setSocketTimeout(timeout); clientConf.withUseExpectContinue(false); clientConf.withSignerOverride("S3SignerType"); // clientConf.setProtocol(Protocol.HTTP); if ((!proxyHost.equals("")) && (!proxyPort.equals(""))) { clientConf.setProxyHost(proxyHost); clientConf.setProxyPort(Integer.parseInt(proxyPort)); } AWSCredentials myCredentials = new BasicAWSCredentials(accessKey, secretKey); client = new AmazonS3Client(myCredentials, clientConf); client.setEndpoint(endpoint); client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(pathStyleAccess)); logger.debug("S3 client has been initialized"); }
protected void issueCommand(String command, String content) { TriggerResponse response = null; String body = issueHttpRequest(command, content); if (body == null) { LOGGER.error("TriggerResponse body is null"); return; } try { response = this.mapper.readValue(body, clazz); } catch (Exception e) { LOGGER.error("can not parse TriggerResponse body", e); return; } if (!response.isSucc()) { String msg = "driver report error: HTTP {} - {}"; LOGGER.error(msg, response.getCode(), response.getError()); return; } handleResponse(response); }
@Override public void init(Config config, Logger logger) { super.init(config, logger); url = config.get(AUTH_URL_KEY, config.get(AUTH_URL_KEY_BACK, URL_DEFAULT)); username = config.get(AUTH_USERNAME_KEY, AUTH_USERNAME_DEFAULT); password = config.get(AUTH_PASSWORD_KEY, AUTH_PASSWORD_DEFAULT); timeout = config.getInt(CONN_TIMEOUT_KEY, CONN_TIMEOUT_DEFAULT); parms.put(AUTH_URL_KEY, url); parms.put(AUTH_USERNAME_KEY, username); parms.put(AUTH_PASSWORD_KEY, password); parms.put(CONN_TIMEOUT_KEY, timeout); logger.debug("using auth config: {}", parms); HttpClient httpClient = HttpClientUtil.createHttpClient(timeout); client = new SwiftAuthClient(httpClient, url, username, password); logger.debug("swauth client has been initialized"); }