本文整理汇总了Java中org.glassfish.jersey.client.HttpUrlConnectorProvider类的典型用法代码示例。如果您正苦于以下问题:Java HttpUrlConnectorProvider类的具体用法?Java HttpUrlConnectorProvider怎么用?Java HttpUrlConnectorProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpUrlConnectorProvider类属于org.glassfish.jersey.client包,在下文中一共展示了HttpUrlConnectorProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ParaClient
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
/**
* Default constructor.
* @param accessKey app access key
* @param secretKey app secret key
*/
public ParaClient(String accessKey, String secretKey) {
this.accessKey = accessKey;
this.secretKey = secretKey;
if (StringUtils.length(secretKey) < 6) {
logger.warn("Secret key appears to be invalid. Make sure you call 'signIn()' first.");
}
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(GenericExceptionMapper.class);
clientConfig.register(new JacksonJsonProvider(ParaObjectUtils.getJsonMapper()));
clientConfig.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
SSLContext sslContext = SslConfigurator.newInstance().securityProtocol("TLSv1").createSSLContext();
System.setProperty("https.protocols", "TLSv1");
apiClient = ClientBuilder.newBuilder().
sslContext(sslContext).
withConfig(clientConfig).build();
}
示例2: patchTodoItem
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Test
public void patchTodoItem() throws AuthenticationException, UnsupportedEncodingException {
final long todoItemId = 1;
final String todoItem = fixture("fixtures/todoItem_new.json");
final TodoItem expectedTodoItem = new TodoItem(1, "update titles", false);
when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
.thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
.thenReturn(expectedTodoItem);
final Response response = resources.getJerseyTest()
.target("/todos/" + todoItemId)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
.request()
.header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
.build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
.invoke();
assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); // 200 OK
assertThat(response.readEntity(TodoItem.class)).isEqualTo(expectedTodoItem);
}
示例3: patchTodoItemWithoutAuthentication
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Test
public void patchTodoItemWithoutAuthentication() {
final long todoItemId = 1;
final String todoItem = fixture("fixtures/todoItem_new.json");
final Response response = resources.getJerseyTest()
.target("/todos/" + todoItemId)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
.request()
.build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
.invoke();
assertThat(response.getStatus()).isEqualTo(Response.Status.UNAUTHORIZED.getStatusCode()); // 401 Unauthorized
verifyZeroInteractions(oaccBasicAuthenticator);
verifyZeroInteractions(todoItemService);
}
示例4: patchTodoItemWithoutAuthorization
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Test
public void patchTodoItemWithoutAuthorization() throws AuthenticationException, UnsupportedEncodingException {
final long todoItemId = 1;
final String todoItem = fixture("fixtures/todoItem_new.json");
when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
.thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
.thenThrow(new NotAuthorizedException("not authorized"));
final Response response = resources.getJerseyTest()
.target("/todos/" + todoItemId)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
.request()
.header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
.build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
.invoke();
assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode()); // 403 Forbidden
}
示例5: patchTodoItemThatDoesNotExist
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Test
public void patchTodoItemThatDoesNotExist() throws AuthenticationException, UnsupportedEncodingException {
final long todoItemId = 1;
final String todoItem = fixture("fixtures/todoItem_new.json");
when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
.thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
.thenThrow(new IllegalArgumentException("Resource " + todoItemId + " not found!"));
final Response response = resources.getJerseyTest()
.target("/todos/" + todoItemId)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
.request()
.header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
.build("PATCH", Entity.entity(todoItem, MediaType.APPLICATION_JSON))
.invoke();
assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode()); // 404 Not Found
verifyZeroInteractions(oacc);
}
示例6: patchTodoItemThatFailsValidation
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Test
public void patchTodoItemThatFailsValidation() throws AuthenticationException, UnsupportedEncodingException {
final long todoItemId = 1;
final String blankTodoItem = fixture("fixtures/todoItem_blank.json");
when(oaccBasicAuthenticator.authenticate(any(BasicCredentials.class)))
.thenReturn(java.util.Optional.ofNullable(oaccPrincipal));
when(oaccPrincipal.getAccessControlContext()).thenReturn(oacc);
when(todoItemService.updateItem(eq(oacc), any(Long.class), any(TodoItem.class)))
.thenThrow(new IllegalArgumentException("Either title or completed (or both) is required."));
final Response response = resources.getJerseyTest()
.target("/todos/" + todoItemId)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // to support PATCH
.request()
.header(Header.Authorization.name(), getBasicAuthHeader(EMAIL, PASSWORD))
.build("PATCH", Entity.entity(blankTodoItem, MediaType.APPLICATION_JSON))
.invoke();
assertThat(response.getStatus()).isEqualTo(422); // 422 Unprocessable Entity
verifyZeroInteractions(oacc);
}
示例7: createClient
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的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);
}
示例8: BaseRestClient
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
/**
*
* @param serviceUrl
*/
public BaseRestClient(String serviceUrl, final String proxyServer, final Integer proxyPort) {
setBaseUrl(serviceUrl);
if (StringUtils.isNotEmpty(proxyServer)) {
ConnectorProvider connectorprovider = new HttpUrlConnectorProvider() {
private Proxy proxy;
private void initializeProxy() {
int port = proxyPort != null ? proxyPort : 80;
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, port));
}
public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
initializeProxy();
return (HttpURLConnection) url.openConnection(proxy);
}
};
client = ClientBuilder.newBuilder().register(connectorprovider).build();
} else {
client = ClientBuilder.newClient();
}
// client.setConnectTimeout(5000);
// client.setFollowRedirects(true);
LOG.info("client for url " + baseUrl + ": proxy="
+ (proxyServer != null ? proxyServer + ":" + proxyPort : "none"));
}
示例9: getInvocationBuilder
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
protected Builder getInvocationBuilder(String url, Map<String, String> queryParameters) {
ClientConfig clientConfig = new ClientConfig();
if (getProxyAddress() != null) {
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(ClientProperties.PROXY_URI, getProxyAddress());
}
Client client = ClientBuilder.newClient(clientConfig);
client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
WebTarget webTarget = client.target(url);
if (queryParameters != null) {
for (Map.Entry<String, String> queryParameter: queryParameters.entrySet())
// webTarget = webTarget.queryParam(queryParameter.getKey(), queryParameter.getValue().replace("_", "_1").replace("%", "_0"));
webTarget = webTarget.queryParam(queryParameter.getKey(), queryParameter.getValue());
}
return webTarget.request(MediaType.APPLICATION_JSON).accept("application/ld+json").header("Authorization", getCloudTokenValue());
}
示例10: makeHttpRequest
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Override
public HttpResponse makeHttpRequest(Entity entity, long startTime) {
String method = request.getMethod();
if ("PATCH".equals(method)) { // http://danofhisword.com/dev/2015/09/04/Jersey-Client-Http-Patch.html
builder.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
}
Response resp;
if (entity != null) {
resp = builder.method(method, entity);
} else {
resp = builder.method(method);
}
byte[] bytes = resp.readEntity(byte[].class);
long responseTime = getResponseTime(startTime);
HttpResponse response = new HttpResponse(responseTime);
response.setUri(getRequestUri());
response.setBody(bytes);
response.setStatus(resp.getStatus());
for (NewCookie c : resp.getCookies().values()) {
com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
cookie.put(DOMAIN, c.getDomain());
cookie.put(PATH, c.getPath());
if (c.getExpiry() != null) {
cookie.put(EXPIRES, c.getExpiry().getTime() + "");
}
cookie.put(SECURE, c.isSecure() + "");
cookie.put(HTTP_ONLY, c.isHttpOnly() + "");
cookie.put(MAX_AGE, c.getMaxAge() + "");
response.addCookie(cookie);
}
for (Entry<String, List<Object>> entry : resp.getHeaders().entrySet()) {
response.putHeader(entry.getKey(), entry.getValue());
}
return response;
}
示例11: config
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
private static ClientConfig config() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
if (JwtClientProperties.isLoggingEnabled()) {
clientConfig.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
clientConfig.register(JacksonFeature.class);
clientConfig.register(MultiPartFeature.class);
return clientConfig;
}
示例12: config
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
private static ClientConfig config() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
clientConfig.register(HttpAuthenticationFeature.basicBuilder().build());
clientConfig.register(JacksonFeature.class);
if (JwtClientProperties.isLoggingEnabled()) {
clientConfig.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
return clientConfig;
}
示例13: containerStart
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Before
public void containerStart() throws Exception {
String root = getWebappRoot();
tomcat = new Tomcat();
tomcat.setPort(8070);
tomcat.setBaseDir(".");
String contextPath = getWebappContext();;
File rootF = new File(root);
if (!rootF.exists()) {
rootF = new File(".");
}
if (!rootF.exists()) {
System.err.println("Can't find root app: " + root);
System.exit(1);
}
tomcat.addWebapp(contextPath, rootF.getAbsolutePath());
tomcat.start();
// Allow arbitrary HTTP methods so we can use PATCH
c = ClientBuilder.newClient();
c.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
checkLive(200);
}
示例14: setup
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@BeforeClass
public static void setup() {
ClientConfig config = new ClientConfig();
config.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
httpClient = ClientBuilder.newClient(config);
}
示例15: configureClient
import org.glassfish.jersey.client.HttpUrlConnectorProvider; //导入依赖的package包/类
@Override
protected void configureClient(final ClientConfig config) {
config.getConfiguration().property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
}