use of javax.ws.rs.client.ClientRequestContext in project cxf by apache.
the class SpecExamplesTest method interceptorBasicTest.
@Test
public void interceptorBasicTest() {
URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
String method = "POST";
MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "Date"));
CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
interceptor.setMessageSigner(messageSigner);
Map<String, List<String>> headers = createMockHeaders();
MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
headers.forEach((header, value) -> {
requestHeaders.add(header, value.get(0));
requestStringHeaders.add(header, value.get(0));
});
final Message message = new MessageImpl();
doInPhaseInterceptor(message, () -> {
ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
interceptor.filter(requestContext);
String signatureHeader = (String) requestHeaders.get("Signature").get(0);
requestStringHeaders.add("Signature", signatureHeader);
String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "headers=\"(request-target) host date\",signature=\"qdx+H7PHHDZgy4" + "y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn" + "7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBs" + "kLu6kd9Fswtemr3lgdDEmn04swr2Os0=\"";
assertEquals(signatureHeader, expectedHeader);
// Verify that the request signature can be verified by the filter
MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
messageVerifier.setAddDefaultRequiredHeaders(false);
messageVerifier.setSecurityProvider(new MockSecurityProvider());
messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
verifySignatureFilter.setMessageVerifier(messageVerifier);
ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
verifySignatureFilter.filter(containerRequestContext);
});
}
use of javax.ws.rs.client.ClientRequestContext in project openremote by openremote.
the class QueryParameterInjectorFilter method filter.
@SuppressWarnings("unchecked")
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
MultivaluedMap<String, String> queryParameters = (MultivaluedMap<String, String>) requestContext.getProperty(QUERY_PARAMETERS_PROPERTY);
String dynamicValue = requestContext.getProperty(DYNAMIC_VALUE_PROPERTY) != null ? (String) requestContext.getProperty(DYNAMIC_VALUE_PROPERTY) : "";
if (queryParameters == null) {
return;
}
UriBuilder uriBuilder = UriBuilder.fromUri(requestContext.getUri());
queryParameters.forEach((name, values) -> {
Object[] formattedValues = values.stream().map(v -> {
v = v.replaceAll(Protocol.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, dynamicValue);
Matcher matcher = DYNAMIC_TIME_PATTERN.matcher(v);
if (matcher.find()) {
long millisToAdd = 0L;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
if (matcher.groupCount() > 0) {
dateTimeFormatter = DateTimeFormatter.ofPattern(matcher.group(1));
}
if (matcher.groupCount() == 2) {
millisToAdd = Long.parseLong(matcher.group(2));
}
v = v.replaceAll(Protocol.DYNAMIC_TIME_PLACEHOLDER_REGEXP, dateTimeFormatter.format(Instant.now().plusMillis(millisToAdd).atZone(ZoneId.systemDefault())));
}
return v;
}).toArray();
uriBuilder.queryParam(name, formattedValues);
});
requestContext.setUri(uriBuilder.build());
}
use of javax.ws.rs.client.ClientRequestContext in project Payara by payara.
the class AwsAuthFeature method filter.
@Override
public synchronized void filter(ClientRequestContext requestContext) throws IOException {
MultivaluedMap<String, String> headers = requestContext.getStringHeaders();
Context filterContext = new Context();
filterContext.method = requestContext.getMethod();
filterContext.query = requestContext.getUri().getQuery();
filterContext.timestamp = headers.getFirst("X-Amz-Date");
filterContext.date = filterContext.timestamp.split("T")[0];
filterContext.host = requestContext.getUri().getHost();
filterContext.region = filterContext.host.split("\\.")[1];
filterContext.serviceName = filterContext.host.split("\\.")[0];
filterContext.payload = requestContext.getEntity().toString().trim();
filterContext.xAmzContentSha256 = requestContext.getHeaderString("X-Amz-Content-Sha256");
filterContext.xAmzTarget = requestContext.getHeaderString("X-Amz-Target");
filterContext.contentType = requestContext.getHeaderString("Content-Type");
try {
requestContext.getHeaders().putSingle("Authorization", generateAwsAuthSignature(filterContext));
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Failed to create Authorization header", ex);
}
}
use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.
the class JerseyClientTest method testTargetConfigUpdate.
@Test
public void testTargetConfigUpdate() {
final JerseyWebTarget target = client.target("http://jersey.java.net/examples");
target.getConfiguration().register(new ClientRequestFilter() {
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
throw new UnsupportedOperationException("Not supported yet");
}
});
assertEquals(1, target.getConfiguration().getInstances().size());
}
use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.
the class JerseyInvocationTest method testNullResponseType.
@Test
public void testNullResponseType() throws Exception {
final Client client = ClientBuilder.newClient();
client.register(new ClientRequestFilter() {
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.ok().build());
}
});
final WebTarget target = client.target("http://localhost:8080/mypath");
final Class<Response> responseType = null;
final String[] methods = new String[] { "GET", "PUT", "POST", "DELETE", "OPTIONS" };
for (final String method : methods) {
final Invocation.Builder request = target.request();
try {
request.method(method, responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
final Invocation build = "PUT".equals(method) ? request.build(method, Entity.entity("", MediaType.TEXT_PLAIN_TYPE)) : request.build(method);
try {
build.submit(responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
try {
build.invoke(responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
try {
request.async().method(method, responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
}
}
Aggregations