本文整理汇总了Java中javax.ws.rs.client.ClientRequestFilter类的典型用法代码示例。如果您正苦于以下问题:Java ClientRequestFilter类的具体用法?Java ClientRequestFilter怎么用?Java ClientRequestFilter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ClientRequestFilter类属于javax.ws.rs.client包,在下文中一共展示了ClientRequestFilter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldRegisterAMultiTypedProviderInstanceWithPriorities
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderInstanceWithPriorities() {
MultiTypedProvider provider = new MultiTypedProvider();
Map<Class<?>, Integer> priorities = new HashMap<>();
priorities.put(ClientRequestFilter.class, 500);
priorities.put(ClientResponseFilter.class, 501);
priorities.put(MessageBodyReader.class, 502);
priorities.put(MessageBodyWriter.class, 503);
priorities.put(ReaderInterceptor.class, 504);
priorities.put(WriterInterceptor.class, 505);
priorities.put(ResponseExceptionMapper.class, 506);
priorities.put(ParamConverterProvider.class, 507);
RestClientBuilder builder = RestClientBuilder.newBuilder().register(provider, priorities);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
assertTrue(configuration.isRegistered(provider), MultiTypedProvider.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
assertEquals(contracts.size(), priorities.size(),
"There should be "+priorities.size()+" provider types registered");
for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
Integer contractPriority = contracts.get(priority.getKey());
assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
}
}
示例2: getGreeting
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
private Greeting getGreeting(String endpoint, String name) {
String endpointURL = endpoints.get(endpoint);
Client client = ClientBuilder.newClient();
try {
WebTarget target = client.target(endpointURL);
// Provide the authorization information
target.register((ClientRequestFilter) requestContext -> {
requestContext.getHeaders().add("Authorization", "Bearer "+token);
});
if(cmdArgs.debugLevel > 0)
target.register(new LoggingFilter());
IGreeting greetingClient = ((ResteasyWebTarget)target).proxy(IGreeting.class);
Greeting greeting = greetingClient.greeting(name);
return greeting;
} finally {
client.close();
}
}
示例3: shouldRegisterAMultiTypedProviderClassWithPriorities
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderClassWithPriorities() {
Map<Class<?>, Integer> priorities = new HashMap<>();
priorities.put(ClientRequestFilter.class, 500);
priorities.put(ClientResponseFilter.class, 501);
priorities.put(MessageBodyReader.class, 502);
priorities.put(MessageBodyWriter.class, 503);
priorities.put(ReaderInterceptor.class, 504);
priorities.put(WriterInterceptor.class, 505);
priorities.put(ResponseExceptionMapper.class, 506);
priorities.put(ParamConverterProvider.class, 507);
RestClientBuilder builder = RestClientBuilder.newBuilder().register(MultiTypedProvider.class, priorities);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
assertEquals(contracts.size(), priorities.size(),
"There should be "+priorities.size()+" provider types registered");
for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
Integer contractPriority = contracts.get(priority.getKey());
assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
}
}
示例4: testNewClient_Auth
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void testNewClient_Auth() {
config.property("a", "b");
Map<String, ClientRequestFilter> authFilters = new HashMap<>();
authFilters.put("one", mockAuth1);
authFilters.put("two", mockAuth2);
DefaultHttpClientFactory factory = new DefaultHttpClientFactory(config, null, authFilters);
Client c = factory.newAuthenticatedClient("one");
assertNotNull(c);
assertEquals("b", c.getConfiguration().getProperty("a"));
assertTrue(c.getConfiguration().isRegistered(mockAuth1));
assertFalse(c.getConfiguration().isRegistered(mockAuth2));
}
示例5: createClient
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
/**
* Contains some workarounds for HTTP/JAX-RS/Jersey issues. See:
* https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/ClientProperties.html#SUPPRESS_HTTP_COMPLIANCE_VALIDATION
* https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/HttpUrlConnectorProvider.html#SET_METHOD_WORKAROUND
*/
@Override
public <T> T createClient(final Class<T> apiClass, final HostName hostName, final int port, final String pathPrefix, String scheme) {
final UriBuilder uriBuilder = UriBuilder.fromPath(pathPrefix).host(hostName.s()).port(port).scheme(scheme);
ClientBuilder builder = ClientBuilder.newBuilder()
.property(ClientProperties.CONNECT_TIMEOUT, connectTimeoutMs)
.property(ClientProperties.READ_TIMEOUT, readTimeoutMs)
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true) // Allow empty PUT. TODO: Fix API.
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // Allow e.g. PATCH method.
.property(ClientProperties.FOLLOW_REDIRECTS, true);
if (sslContext != null) {
builder.sslContext(sslContext);
}
if (hostnameVerifier != null) {
builder.hostnameVerifier(hostnameVerifier);
}
if (userAgent != null) {
builder.register((ClientRequestFilter) context ->
context.getHeaders().put(HttpHeaders.USER_AGENT, Collections.singletonList(userAgent)));
}
final WebTarget target = builder.build().target(uriBuilder);
// TODO: Check if this fills up non-heap memory with loaded classes.
return WebResourceFactory.newResource(apiClass, target);
}
示例6: getWebTarget
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
private WebTarget getWebTarget() {
if (client == null) {
client = ClientBuilder.newClient();
client.register(JacksonJsonProvider.class);
// register the basic authentication filter if authentication information is provided
if (authenticationToken != null) {
client.register(new ClientRequestFilter() {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().add("Authorization", "Basic " + authenticationToken);
}
});
}
}
WebTarget target = client.target(targetUrl);
return target;
}
示例7: withCredentials
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
public WordPressClientFactory withCredentials(String username, String password) {
this.clientRequestFilter = new ClientRequestFilter() {
private final String charset = StandardCharsets.UTF_8.name();
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
String token = String.format("%s:%s", username, password);
String basicAuthHeader = null;
try {
basicAuthHeader = "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes(charset));
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException("Cannot encode with " + charset, ex);
}
MultivaluedMap<String, Object> headers = requestContext.getHeaders();
headers.add("Authorization", basicAuthHeader);
logger.trace("Added BasicAuth filter with username {}", username);
}
};
return this;
}
示例8: JAXRSWordPressClient
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
/**
* <p>Package private constructor to encourage the caller to use this Client's associated Factory</p>
* @param targetURI The base URI of the Wordpress REST API. The {@code targetUri} should include the FQDN up to, but
* not including the REST endpoints (e.g. "https://wp.foo.com/wp-json/" not
* "https://wp.foo.com/wp-json/posts")
* @param requestFilter A possibly null filter to pass every request this CLient makes to WordPress through. Useful for
* adding BasicAuth header information to every request, if appropriate.
* @param transformer A concrete class to provide transformation services from the JSON returned by the WP-API and a POJO
* that implements the Post marker interface
*/
JAXRSWordPressClient(URI targetURI,
ClientRequestFilter requestFilter,
Transformer<? extends Post> transformer) {
this.targetURI = targetURI;
this.transformer = transformer;
this.jaxrsClient = ClientBuilder.newClient();
// If our authenticator is provided, register it as a default ClientRequestFilter so that every request this Client
// makes has BasicAuth information added to it
if (requestFilter != null) {
jaxrsClient = jaxrsClient.register(requestFilter);
}
logger.debug("Constructing new JAXRSWordPressClient with targetURI '{}' and transformer '{}' "
+ "and requestFilter '{}'", targetURI, transformer, requestFilter);
}
示例9: getGreeting
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
private static Greeting getGreeting(String url, String token, String from) {
Client client = ClientBuilder.newClient();
try {
WebTarget target = client.target(url);
System.out.println("URL : " + url);
target.register((ClientRequestFilter) requestContext -> {
requestContext.getHeaders().add("Authorization", "Bearer " + token);
});
IGreeting greetingClient = ((ResteasyWebTarget) target).proxy(IGreeting.class);
return greetingClient.greeting(from);
} finally {
client.close();
}
}
示例10: shouldRegisterInstanceWithPriority
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterInstanceWithPriority() {
Integer priority = 1000;
TestClientRequestFilter instance = new TestClientRequestFilter();
RestClientBuilder builder = RestClientBuilder.newBuilder().register(instance, priority);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(TestClientRequestFilter.class), TestClientRequestFilter.class + " should be registered");
assertTrue(configuration.isRegistered(instance), TestClientRequestFilter.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(TestClientRequestFilter.class);
assertEquals(contracts.size(), 1, "There should be a registered contract for "+TestClientRequestFilter.class);
assertEquals(contracts.get(ClientRequestFilter.class), priority, "The priority for "+TestClientRequestFilter.class+" should be 1000");
}
示例11: shouldRegisterAMultiTypedProviderInstance
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderInstance() {
MultiTypedProvider provider = new MultiTypedProvider();
Class[] providerTypes = {ClientRequestFilter.class, ClientResponseFilter.class,
MessageBodyReader.class, MessageBodyWriter.class, ReaderInterceptor.class, WriterInterceptor.class,
ResponseExceptionMapper.class, ParamConverterProvider.class};
RestClientBuilder builder = RestClientBuilder.newBuilder().register(provider, providerTypes);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
assertTrue(configuration.isRegistered(provider), MultiTypedProvider.class + " should be registered");
assertEquals(configuration.getContracts(MultiTypedProvider.class).size(), providerTypes.length,
"There should be "+providerTypes.length+" provider types registered");
}
示例12: shouldRegisterProvidersWithPriority
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterProvidersWithPriority() {
Integer priority = 1000;
RestClientBuilder builder = RestClientBuilder.newBuilder().register(TestClientRequestFilter.class, priority);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(TestClientRequestFilter.class), TestClientRequestFilter.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(TestClientRequestFilter.class);
assertEquals(contracts.size(), 1, "There should be a registered contract for "+TestClientRequestFilter.class);
assertEquals(contracts.get(ClientRequestFilter.class), priority, "The priority for "+TestClientRequestFilter.class+" should be 1000");
}
示例13: shouldRegisterAMultiTypedProviderClass
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderClass() {
Class[] providerTypes = {ClientRequestFilter.class, ClientResponseFilter.class,
MessageBodyReader.class, MessageBodyWriter.class, ReaderInterceptor.class, WriterInterceptor.class,
ResponseExceptionMapper.class, ParamConverterProvider.class};
RestClientBuilder builder = RestClientBuilder.newBuilder().register(MultiTypedProvider.class, providerTypes);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
assertEquals(configuration.getContracts(MultiTypedProvider.class).size(), providerTypes.length,
"There should be "+providerTypes.length+" provider types registered");
}
示例14: createAuthFilters
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
protected Map<String, ClientRequestFilter> createAuthFilters(Injector injector) {
Map<String, ClientRequestFilter> filters = new HashMap<>();
if (auth != null) {
auth.forEach((k, v) -> filters.put(k, v.createAuthFilter(injector)));
}
return filters;
}
示例15: createAuthFilter
import javax.ws.rs.client.ClientRequestFilter; //导入依赖的package包/类
@Override
public ClientRequestFilter createAuthFilter(Injector injector) {
Objects.requireNonNull(username);
Objects.requireNonNull(password);
return new BasicAuthenticator(username, password);
}