Search in sources :

Example 81 with BasicScheme

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);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) StringEntity(org.apache.http.entity.StringEntity) HttpClientManager(org.pentaho.di.core.util.HttpClientManager) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpHost(org.apache.http.HttpHost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) List(java.util.List) ArrayList(java.util.ArrayList) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) UnknownHostException(java.net.UnknownHostException) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) FileInputStream(java.io.FileInputStream) KettleStepException(org.pentaho.di.core.exception.KettleStepException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) UnknownHostException(java.net.UnknownHostException) URIBuilder(org.apache.http.client.utils.URIBuilder) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) File(java.io.File)

Example 82 with BasicScheme

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;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)82 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 CredentialsProvider (org.apache.http.client.CredentialsProvider)48 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)47 AuthCache (org.apache.http.client.AuthCache)46 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)46 HttpHost (org.apache.http.HttpHost)45 AuthScope (org.apache.http.auth.AuthScope)38 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)23 URI (java.net.URI)20 Test (org.junit.Test)18 IOException (java.io.IOException)13 Credentials (org.apache.http.auth.Credentials)13 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)9 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)9 Header (org.apache.http.Header)8