use of org.apache.http.impl.auth.BasicScheme in project pentaho-kettle by pentaho.
the class HTTPPOST method callHTTPPOST.
private Object[] callHTTPPOST(Object[] rowData) throws KettleException {
HttpClientManager.HttpClientBuilderFacade clientBuilder = HttpClientManager.getInstance().createBuilder();
if (data.realConnectionTimeout > -1) {
clientBuilder.setConnectionTimeout(data.realConnectionTimeout);
}
if (data.realSocketTimeout > -1) {
clientBuilder.setSocketTimeout(data.realSocketTimeout);
}
if (StringUtils.isNotBlank(data.realHttpLogin)) {
clientBuilder.setCredentials(data.realHttpLogin, data.realHttpPassword);
}
if (StringUtils.isNotBlank(data.realProxyHost)) {
clientBuilder.setProxy(data.realProxyHost, data.realProxyPort);
}
CloseableHttpClient httpClient = clientBuilder.build();
// get dynamic url ?
if (meta.isUrlInField()) {
data.realUrl = data.inputRowMeta.getString(rowData, data.indexOfUrlField);
}
// Prepare HTTP POST
FileInputStream fis = null;
try {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "HTTPPOST.Log.ConnectingToURL", data.realUrl));
}
URIBuilder uriBuilder = new URIBuilder(data.realUrl);
HttpPost post = new HttpPost(uriBuilder.build());
// ISO-8859-1 is assumed by the POSTMethod
if (!data.contentTypeHeaderOverwrite) {
// can be overwritten now
if (Utils.isEmpty(data.realEncoding)) {
post.setHeader(CONTENT_TYPE, CONTENT_TYPE_TEXT_XML);
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue", CONTENT_TYPE, CONTENT_TYPE_TEXT_XML));
}
} else {
post.setHeader(CONTENT_TYPE, CONTENT_TYPE_TEXT_XML + "; " + data.realEncoding);
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue", CONTENT_TYPE, CONTENT_TYPE_TEXT_XML + "; " + data.realEncoding));
}
}
}
// HEADER PARAMETERS
if (data.useHeaderParameters) {
// set header parameters that we want to send
for (int i = 0; i < data.header_parameters_nrs.length; i++) {
post.addHeader(data.headerParameters[i].getName(), data.inputRowMeta.getString(rowData, data.header_parameters_nrs[i]));
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.HeaderValue", data.headerParameters[i].getName(), data.inputRowMeta.getString(rowData, data.header_parameters_nrs[i])));
}
}
}
// BODY PARAMETERS
if (data.useBodyParameters) {
// set body parameters that we want to send
for (int i = 0; i < data.body_parameters_nrs.length; i++) {
String bodyParameterName = data.bodyParameters[i].getName();
String bodyParameterValue = data.inputRowMeta.getString(rowData, data.body_parameters_nrs[i]);
data.bodyParameters[i] = new BasicNameValuePair(bodyParameterName, bodyParameterValue);
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.BodyValue", bodyParameterName, bodyParameterValue));
}
}
String bodyParams = getRequestBodyParamsAsStr(data.bodyParameters, data.realEncoding);
post.setEntity((new StringEntity(bodyParams, ContentType.TEXT_XML.withCharset("US-ASCII"))));
}
// QUERY PARAMETERS
if (data.useQueryParameters) {
for (int i = 0; i < data.query_parameters_nrs.length; i++) {
String queryParameterName = data.queryParameters[i].getName();
String queryParameterValue = data.inputRowMeta.getString(rowData, data.query_parameters_nrs[i]);
data.queryParameters[i] = new BasicNameValuePair(queryParameterName, queryParameterValue);
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.QueryValue", queryParameterName, queryParameterValue));
}
}
post.setEntity(new UrlEncodedFormEntity(Arrays.asList(data.queryParameters)));
}
// Set request entity?
if (data.indexOfRequestEntity >= 0) {
String tmp = data.inputRowMeta.getString(rowData, data.indexOfRequestEntity);
if (meta.isPostAFile()) {
File input = new File(tmp);
fis = new FileInputStream(input);
post.setEntity(new InputStreamEntity(fis, input.length()));
} else {
byte[] bytes;
if ((data.realEncoding != null) && (data.realEncoding.length() > 0)) {
bytes = tmp.getBytes(data.realEncoding);
} else {
bytes = tmp.getBytes();
}
post.setEntity(new ByteArrayEntity(bytes));
}
}
// Execute request
Object[] newRow = null;
if (rowData != null) {
newRow = rowData.clone();
}
CloseableHttpResponse httpResponse = null;
try {
// used for calculating the responseTime
long startTime = System.currentTimeMillis();
HttpHost target = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
// Execute the POST method
if (StringUtils.isNotBlank(data.realProxyHost)) {
httpResponse = httpClient.execute(target, post, localContext);
} else {
httpResponse = httpClient.execute(post, localContext);
}
int statusCode = requestStatusCode(httpResponse);
// calculate the responseTime
long responseTime = System.currentTimeMillis() - startTime;
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseTime", responseTime, data.realUrl));
}
// Display status code
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseCode", String.valueOf(statusCode)));
}
String body;
String headerString = "";
switch(statusCode) {
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new KettleStepException(BaseMessages.getString(PKG, "HTTPPOST.Exception.Authentication", data.realUrl));
case -1:
throw new KettleStepException(BaseMessages.getString(PKG, "HTTPPOST.Exception.IllegalStatusCode", data.realUrl));
case HttpURLConnection.HTTP_NO_CONTENT:
body = "";
break;
default:
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity);
} else {
body = "";
}
Header[] headers = searchForHeaders(httpResponse);
// Use request encoding if specified in component to avoid strange response encodings
// See PDI-3815
JSONObject json = new JSONObject();
for (Header header : headers) {
Object previousValue = json.get(header.getName());
if (previousValue == null) {
json.put(header.getName(), header.getValue());
} else if (previousValue instanceof List) {
List<String> list = (List<String>) previousValue;
list.add(header.getValue());
} else {
ArrayList<String> list = new ArrayList<String>();
list.add((String) previousValue);
list.add(header.getValue());
json.put(header.getName(), list);
}
}
headerString = json.toJSONString();
}
if (isDebug()) {
logDebug(BaseMessages.getString(PKG, "HTTPPOST.Log.ResponseBody", body));
}
int returnFieldsOffset = data.inputRowMeta.size();
if (!Utils.isEmpty(meta.getFieldName())) {
newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, body);
returnFieldsOffset++;
}
if (!Utils.isEmpty(meta.getResultCodeFieldName())) {
newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, new Long(statusCode));
returnFieldsOffset++;
}
if (!Utils.isEmpty(meta.getResponseTimeFieldName())) {
newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, new Long(responseTime));
returnFieldsOffset++;
}
if (!Utils.isEmpty(meta.getResponseHeaderFieldName())) {
newRow = RowDataUtil.addValueData(newRow, returnFieldsOffset, headerString);
}
} finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
if (httpResponse != null) {
httpResponse.close();
}
}
return newRow;
} catch (UnknownHostException uhe) {
throw new KettleException(BaseMessages.getString(PKG, "HTTPPOST.Error.UnknownHostException", uhe.getMessage()));
} catch (Exception e) {
throw new KettleException(BaseMessages.getString(PKG, "HTTPPOST.Error.CanNotReadURL", data.realUrl), e);
} finally {
if (fis != null) {
BaseStep.closeQuietly(fis);
}
}
}
use of org.apache.http.impl.auth.BasicScheme in project acceptance-test-harness by jenkinsci.
the class HttpUtils method buildHttpClientContext.
public static HttpClientContext buildHttpClientContext(@Nonnull URL url, @CheckForNull Credentials credentials) {
HttpClientContext context = HttpClientContext.create();
if (credentials != null) {
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
// Add AuthCache to the execution context
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
}
return context;
}
Aggregations