Search in sources :

Example 61 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project geode by apache.

the class HttpClientRule method buildHttpPost.

private HttpPost buildHttpPost(String uri, String... params) throws Exception {
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> nvps = new ArrayList<>();
    for (int i = 0; i < params.length; i += 2) {
        nvps.add(new BasicNameValuePair(params[i], params[i + 1]));
    }
    post.setEntity(new UrlEncodedFormEntity(nvps));
    return post;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 62 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project indy by Commonjava.

the class BasicAuthenticationOAuthTranslator method lookupToken.

private AccessTokenResponse lookupToken(final UserPass userPass) {
    final URI uri = KeycloakUriBuilder.fromUri(config.getUrl()).path(ServiceUrlConstants.TOKEN_PATH).build(config.getRealm());
    logger.debug("Looking up token at: {}", uri);
    final HttpPost request = new HttpPost(uri);
    final List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(USERNAME, userPass.getUser()));
    params.add(new BasicNameValuePair(PASSWORD, userPass.getPassword()));
    params.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD));
    final String authorization = BasicAuthHelper.createHeader(config.getServerResource(), config.getServerCredentialSecret());
    request.setHeader(AUTHORIZATION_HEADER, authorization);
    CloseableHttpClient client = null;
    AccessTokenResponse tokenResponse = null;
    try {
        client = http.createClient(uri.getHost());
        final UrlEncodedFormEntity form = new UrlEncodedFormEntity(params, "UTF-8");
        request.setEntity(form);
        CloseableHttpResponse response = client.execute(request);
        logger.debug("Got response status: {}", response.getStatusLine());
        if (response.getStatusLine().getStatusCode() == 200) {
            try (InputStream in = response.getEntity().getContent()) {
                final String json = IOUtils.toString(in);
                logger.debug("Token response:\n\n{}\n\n", json);
                tokenResponse = JsonSerialization.readValue(json, AccessTokenResponse.class);
            }
        }
    } catch (IOException | IndyHttpException e) {
        logger.error(String.format("Keycloak token request failed: %s", e.getMessage()), e);
    } finally {
        IOUtils.closeQuietly(client);
    }
    return tokenResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IndyHttpException(org.commonjava.indy.subsys.http.IndyHttpException) HttpString(io.undertow.util.HttpString) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) URI(java.net.URI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse)

Example 63 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project sling by apache.

the class RemoteTestHttpClient method runTests.

public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) throws ClientProtocolException, IOException {
    final RequestBuilder builder = new RequestBuilder(junitServletUrl);
    // Optionally let the client to consume the response entity
    final RequestExecutor executor = new RequestExecutor(new DefaultHttpClient()) {

        @Override
        protected void consumeEntity() throws ParseException, IOException {
            if (consumeContent) {
                super.consumeEntity();
            }
        }
    };
    // Build path for POST request to execute the tests
    // Test classes selector
    subpath = new StringBuilder();
    if (!junitServletUrl.endsWith(SLASH)) {
        subpath.append(SLASH);
    }
    subpath.append(testClassesSelector);
    // Test method selector
    if (testMethodSelector != null && testMethodSelector.length() > 0) {
        subpath.append("/");
        subpath.append(testMethodSelector);
    }
    // Extension
    if (!extension.startsWith(DOT)) {
        subpath.append(DOT);
    }
    subpath.append(extension);
    // Request options if any
    final List<NameValuePair> opt = new ArrayList<NameValuePair>();
    if (requestOptions != null) {
        for (Map.Entry<String, String> e : requestOptions.entrySet()) {
            opt.add(new BasicNameValuePair(e.getKey(), e.getValue()));
        }
    }
    log.info("Executing test remotely, path={} JUnit servlet URL={}", subpath, junitServletUrl);
    final Request r = builder.buildPostRequest(subpath.toString()).withCredentials(username, password).withCustomizer(requestCustomizer).withEntity(new UrlEncodedFormEntity(opt));
    executor.execute(r).assertStatus(200);
    return executor;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) ArrayList(java.util.ArrayList) Request(org.apache.sling.testing.tools.http.Request) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 64 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project opennms by OpenNMS.

the class HttpCollector method buildPostMethod.

private static HttpPost buildPostMethod(final URI uri, final HttpCollectorAgent collectorAgent) {
    HttpPost method = new HttpPost(uri);
    List<NameValuePair> postParams = buildRequestParameters(collectorAgent);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, StandardCharsets.UTF_8);
    method.setEntity(entity);
    return method;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 65 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project opennms by OpenNMS.

the class RequestTracker method getClientWrapper.

public synchronized HttpClientWrapper getClientWrapper() {
    if (m_clientWrapper == null) {
        m_clientWrapper = HttpClientWrapper.create().setSocketTimeout(m_timeout).setConnectionTimeout(m_timeout).setRetries(m_retries).useBrowserCompatibleCookies().dontReuseConnections();
        final HttpPost post = new HttpPost(m_baseURL + "/REST/1.0/user/" + m_user);
        final List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("user", m_user));
        params.add(new BasicNameValuePair("pass", m_password));
        CloseableHttpResponse response = null;
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
            post.setEntity(entity);
            response = m_clientWrapper.execute(post);
            int responseCode = response.getStatusLine().getStatusCode();
            if (responseCode != HttpStatus.SC_OK) {
                throw new RequestTrackerException("Received a non-200 response code from the server: " + responseCode);
            } else {
                if (response.getEntity() != null) {
                    EntityUtils.consume(response.getEntity());
                }
                LOG.warn("got user session for username: {}", m_user);
            }
        } catch (final Exception e) {
            LOG.warn("Unable to get session (by requesting user details)", e);
        } finally {
            m_clientWrapper.close(response);
        }
    }
    return m_clientWrapper;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13