use of org.apache.http.message.BasicHeader in project cxf by apache.
the class Client method main.
public static void main(String[] args) throws Exception {
String keyStoreLoc = "src/main/config/clientKeystore.jks";
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStoreLoc), "cspass".toCharArray());
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keyStore, null).loadKeyMaterial(keyStore, "ckpass".toCharArray()).build();
/*
* Send HTTP GET request to query customer info using portable HttpClient
* object from Apache HttpComponents
*/
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslcontext);
System.out.println("Sending HTTPS GET request to query customer info");
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sf).build();
HttpGet httpget = new HttpGet(BASE_SERVICE_URL + "/123");
BasicHeader bh = new BasicHeader("Accept", "text/xml");
httpget.addHeader(bh);
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
entity.writeTo(System.out);
response.close();
httpclient.close();
/*
* Send HTTP PUT request to update customer info, using CXF WebClient method
* Note: if need to use basic authentication, use the WebClient.create(baseAddress,
* username,password,configFile) variant, where configFile can be null if you're
* not using certificates.
*/
System.out.println("\n\nSending HTTPS PUT to update customer name");
WebClient wc = WebClient.create(BASE_SERVICE_URL, CLIENT_CONFIG_FILE);
Customer customer = new Customer();
customer.setId(123);
customer.setName("Mary");
Response resp = wc.put(customer);
/*
* Send HTTP POST request to add customer, using JAXRSClientProxy
* Note: if need to use basic authentication, use the JAXRSClientFactory.create(baseAddress,
* username,password,configFile) variant, where configFile can be null if you're
* not using certificates.
*/
System.out.println("\n\nSending HTTPS POST request to add customer");
CustomerService proxy = JAXRSClientFactory.create(BASE_SERVICE_URL, CustomerService.class, CLIENT_CONFIG_FILE);
customer = new Customer();
customer.setName("Jack");
resp = wc.post(customer);
System.out.println("\n");
System.exit(0);
}
use of org.apache.http.message.BasicHeader in project uPortal by Jasig.
the class AuthorizationHeaderProvider method createHeader.
@Override
public Header createHeader(RenderRequest renderRequest, RenderResponse renderResponse) {
// Username
final String username = getUsername(renderRequest);
// Attributes
final Map<String, List<String>> attributes = new HashMap<>();
final IPersonAttributes person = personAttributeDao.getPerson(username);
if (person != null) {
for (Entry<String, List<Object>> y : person.getAttributes().entrySet()) {
final List<String> values = new ArrayList<>();
for (Object value : y.getValue()) {
if (value instanceof String) {
values.add((String) value);
}
}
attributes.put(y.getKey(), values);
}
}
logger.debug("Found the following user attributes for username='{}': {}", username, attributes);
// Groups
final List<String> groups = new ArrayList<>();
final IGroupMember groupMember = GroupService.getGroupMember(username, IPerson.class);
if (groupMember != null) {
Set<IEntityGroup> ancestors = groupMember.getAncestorGroups();
for (IEntityGroup g : ancestors) {
groups.add(g.getName());
}
}
logger.debug("Found the following group affiliations for username='{}': {}", username, groups);
// Expiration of the Bearer token
final PortletSession portletSession = renderRequest.getPortletSession();
final Date expires = new Date(portletSession.getLastAccessedTime() + ((long) portletSession.getMaxInactiveInterval() * 1000L));
// Authorization header
final Bearer bearer = bearerService.createBearer(username, attributes, groups, expires);
final Header rslt = new BasicHeader(Headers.AUTHORIZATION.getName(), Headers.BEARER_TOKEN_PREFIX + bearer.getEncryptedToken());
logger.debug("Produced the following Authorization header for username='{}': {}", username, rslt);
return rslt;
}
use of org.apache.http.message.BasicHeader in project elasticsearch by elastic.
the class RequestLoggerTests method testResponseWarnings.
public void testResponseWarnings() throws Exception {
HttpHost host = new HttpHost("localhost", 9200);
HttpUriRequest request = randomHttpRequest(new URI("/index/type/_api"));
int numWarnings = randomIntBetween(1, 5);
StringBuilder expected = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host).append("/index/type/_api] returned ").append(numWarnings).append(" warnings: ");
Header[] warnings = new Header[numWarnings];
for (int i = 0; i < numWarnings; i++) {
String warning = "this is warning number " + i;
warnings[i] = new BasicHeader("Warning", warning);
if (i > 0) {
expected.append(",");
}
expected.append("[").append(warning).append("]");
}
assertEquals(expected.toString(), RequestLogger.buildWarningMessage(request, host, warnings));
}
use of org.apache.http.message.BasicHeader in project elasticsearch by elastic.
the class RestClientTestUtil method randomHeaders.
/**
* Create a random number of {@link Header}s.
* Generated header names will either be the {@code baseName} plus its index, or exactly the provided {@code baseName} so that the
* we test also support for multiple headers with same key and different values.
*/
static Header[] randomHeaders(Random random, final String baseName) {
int numHeaders = RandomNumbers.randomIntBetween(random, 0, 5);
final Header[] headers = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = baseName;
//randomly exercise the code path that supports multiple headers with same key
if (random.nextBoolean()) {
headerName = headerName + i;
}
headers[i] = new BasicHeader(headerName, RandomStrings.randomAsciiOfLengthBetween(random, 3, 10));
}
return headers;
}
use of org.apache.http.message.BasicHeader in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesDefaultForNonPersistentConnections.
@Test
public void usesDefaultForNonPersistentConnections() throws Exception {
configuration.setKeepAlive(Duration.seconds(1));
assertThat(builder.using(configuration).createClient(apacheBuilder, connectionManager, "test")).isNotNull();
final Field field = FieldUtils.getField(httpClientBuilderClass, "keepAliveStrategy", true);
final DefaultConnectionKeepAliveStrategy strategy = (DefaultConnectionKeepAliveStrategy) field.get(apacheBuilder);
final HttpContext context = mock(HttpContext.class);
final HttpResponse response = mock(HttpResponse.class);
final HeaderIterator iterator = new BasicListHeaderIterator(ImmutableList.of(new BasicHeader(HttpHeaders.CONNECTION, "timeout=50")), HttpHeaders.CONNECTION);
when(response.headerIterator(HTTP.CONN_KEEP_ALIVE)).thenReturn(iterator);
assertThat(strategy.getKeepAliveDuration(response, context)).isEqualTo(50000);
}
Aggregations