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