org.glassfish.jersey.client.RequestEntityProcessing Java Examples
The following examples show how to use
org.glassfish.jersey.client.RequestEntityProcessing.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JerseyClientUtil.java From datacollector with Apache License 2.0 | 6 votes |
/** * Helper method to upgrade both HTTP stages to the JerseyConfigBean * @param configs List of configs to upgrade. */ public static void upgradeToJerseyConfigBean(List<Config> configs) { List<Config> configsToAdd = new ArrayList<>(); List<Config> configsToRemove = new ArrayList<>(); List<String> movedConfigs = ImmutableList.of( "conf.requestTimeoutMillis", "conf.numThreads", "conf.authType", "conf.oauth", "conf.basicAuth", "conf.useProxy", "conf.proxy", "conf.sslConfig" ); for (Config config : configs) { if (hasPrefixIn(movedConfigs, config.getName())) { configsToRemove.add(config); configsToAdd.add(new Config(config.getName().replace("conf.", "conf.client."), config.getValue())); } } configsToAdd.add(new Config("conf.client.transferEncoding", RequestEntityProcessing.CHUNKED)); configs.removeAll(configsToRemove); configs.addAll(configsToAdd); }
Example #2
Source File: DefaultDockerClientUnitTest.java From docker-client with Apache License 2.0 | 6 votes |
@Test public void testChunkedRequestEntityProcessing() throws Exception { builder.useRequestEntityProcessing(RequestEntityProcessing.CHUNKED); final DefaultDockerClient dockerClient = new DefaultDockerClient(builder); final HostConfig hostConfig = HostConfig.builder().build(); final ContainerConfig containerConfig = ContainerConfig.builder() .hostConfig(hostConfig) .build(); server.enqueue(new MockResponse()); dockerClient.createContainer(containerConfig); final RecordedRequest recordedRequest = takeRequestImmediately(); assertThat(recordedRequest.getHeader("Content-Length"), nullValue()); assertThat(recordedRequest.getHeader("Transfer-Encoding"), is("chunked")); }
Example #3
Source File: DefaultDockerClientUnitTest.java From docker-client with Apache License 2.0 | 6 votes |
@Test public void testBufferedRequestEntityProcessing() throws Exception { builder.useRequestEntityProcessing(RequestEntityProcessing.BUFFERED); final DefaultDockerClient dockerClient = new DefaultDockerClient(builder); final HostConfig hostConfig = HostConfig.builder().build(); final ContainerConfig containerConfig = ContainerConfig.builder() .hostConfig(hostConfig) .build(); server.enqueue(new MockResponse()); dockerClient.createContainer(containerConfig); final RecordedRequest recordedRequest = takeRequestImmediately(); assertThat(recordedRequest.getHeader("Content-Length"), notNullValue()); assertThat(recordedRequest.getHeader("Transfer-Encoding"), nullValue()); }
Example #4
Source File: ServerContextTest.java From azure-devops-intellij with MIT License | 5 votes |
@Test public void getClientConfig() { AuthenticationInfo info = new AuthenticationInfo("user1", "pass", "server1", "4display"); final ClientConfig config = RestClientHelper.getClientConfig(ServerContext.Type.TFS, info, false); final Map<String, Object> properties = config.getProperties(); Assert.assertEquals(3, properties.size()); Assert.assertEquals(false, properties.get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION)); Assert.assertEquals(RequestEntityProcessing.BUFFERED, properties.get(ClientProperties.REQUEST_ENTITY_PROCESSING)); final CredentialsProvider cp = (CredentialsProvider) properties.get(ApacheClientProperties.CREDENTIALS_PROVIDER); final Credentials credentials = cp.getCredentials(AuthScope.ANY); Assert.assertEquals(info.getPassword(), credentials.getPassword()); Assert.assertEquals(info.getUserName(), credentials.getUserPrincipal().getName()); // Make sure Fiddler properties get set if property is on final ClientConfig config2 = RestClientHelper.getClientConfig(ServerContext.Type.TFS, info, true); final Map<String, Object> properties2 = config2.getProperties(); //proxy setting doesn't automatically mean we need to setup ssl trust store anymore Assert.assertEquals(4, properties2.size()); Assert.assertNotNull(properties2.get(ClientProperties.PROXY_URI)); info = new AuthenticationInfo("users1", "pass", "https://tfsonprem.test", "4display"); final ClientConfig config3 = RestClientHelper.getClientConfig(ServerContext.Type.TFS, info, false); final Map<String, Object> properties3 = config3.getProperties(); Assert.assertEquals(3, properties3.size()); Assert.assertNull(properties3.get(ClientProperties.PROXY_URI)); }
Example #5
Source File: OAuth2TestUtil.java From datacollector with Apache License 2.0 | 5 votes |
public OAuth2ConfigBean setup(Client client, WebTarget target, Invocation.Builder builder, Response response, OAuth2GrantTypes grantType) { OAuth2ConfigBean configBean = new OAuth2ConfigBean(); MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); RequestEntityProcessing transferEncoding = RequestEntityProcessing.BUFFERED; if (grantType == OAuth2GrantTypes.CLIENT_CREDENTIALS) { params.put(OAuth2ConfigBean.CLIENT_ID_KEY, Collections.singletonList(clientId)); params.put(OAuth2ConfigBean.CLIENT_SECRET_KEY, Collections.singletonList(clientSecret)); params.put(OAuth2ConfigBean.GRANT_TYPE_KEY, Collections.singletonList(OAuth2ConfigBean.CLIENT_CREDENTIALS_GRANT)); configBean.clientId = () -> clientId; configBean.clientSecret = () -> clientSecret; } else { params.put(OAuth2ConfigBean.RESOURCE_OWNER_KEY, Collections.singletonList(clientId)); params.put(OAuth2ConfigBean.PASSWORD_KEY, Collections.singletonList(clientSecret)); params.put(OAuth2ConfigBean.GRANT_TYPE_KEY, Collections.singletonList(OAuth2ConfigBean.RESOURCE_OWNER_GRANT)); configBean.username = () -> clientId; configBean.password = () -> clientSecret; } configBean.credentialsGrantType = grantType; configBean.transferEncoding = RequestEntityProcessing.BUFFERED; configBean.tokenUrl = "https://example.com"; Mockito.when(response.readEntity(String.class)).thenReturn(TOKEN_RESPONSE); Mockito.when(response.getStatus()).thenReturn(200); Mockito.when(builder.post(Mockito.argThat(new FormMatcher(Entity.form(params))))).thenReturn(response); Mockito.when(builder.property(ClientProperties.REQUEST_ENTITY_PROCESSING, transferEncoding)).thenReturn(builder); Mockito.when(builder.header(any(String.class), any(Object.class))).thenReturn(builder); Mockito.when(target.request()).thenReturn(builder); Mockito.when(client.target(configBean.tokenUrl)).thenReturn(target); return configBean; }
Example #6
Source File: HttpClientSourcePaginationIT.java From datacollector with Apache License 2.0 | 5 votes |
private HttpClientConfigBean getHttpClientConfigBean( int start, int limit, PaginationMode mode, boolean keepAllFields ) { HttpClientConfigBean conf = new HttpClientConfigBean(); conf.client.authType = AuthenticationType.NONE; conf.httpMode = HttpClientMode.BATCH; conf.resourceUrl = getBaseUri() + String.format( "paging?pageNum=${startAt}&limit=%d&mode=%s", limit, mode.name() ); conf.client.readTimeoutMillis = 0; conf.client.connectTimeoutMillis = 0; conf.client.transferEncoding = RequestEntityProcessing.BUFFERED; conf.basic.maxBatchSize = 10; conf.basic.maxWaitTime = 10000; conf.pollingInterval = 1000; conf.httpMethod = HttpMethod.GET; conf.dataFormat = DataFormat.JSON; conf.dataFormatConfig.jsonContent = JsonMode.MULTIPLE_OBJECTS; conf.pagination.mode = mode; conf.pagination.startAt = start; conf.pagination.resultFieldPath = "/results"; conf.pagination.rateLimit = 0; conf.pagination.keepAllFields = keepAllFields; conf.pagination.nextPageFieldPath = "/next"; conf.pagination.stopCondition = "${!record:exists('/next')}"; return conf; }
Example #7
Source File: DefaultDockerClient.java From docker-client with Apache License 2.0 | 5 votes |
private ClientConfig updateProxy(ClientConfig config, Builder builder) { if (builder.useProxy()) { final String proxyHost = System.getProperty("http.proxyHost"); if (proxyHost != null) { boolean skipProxy = false; String nonProxyHosts = System.getProperty("http.nonProxyHosts"); if (nonProxyHosts != null) { // Remove quotes, if any. Refer to https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html nonProxyHosts = StringUtils.strip(nonProxyHosts, "\""); String host = getHost(); for (String nonProxyHost : nonProxyHosts.split("\\|")) { if (host.matches(toRegExp(nonProxyHost))) { skipProxy = true; break; } } } if (!skipProxy) { String proxyPort = checkNotNull(System.getProperty("http.proxyPort"), "http.proxyPort"); config.property(ClientProperties.PROXY_URI, (!proxyHost.startsWith("http") ? "http://" : "") + proxyHost + ":" + proxyPort); final String proxyUser = System.getProperty("http.proxyUser"); if (proxyUser != null) { config.property(ClientProperties.PROXY_USERNAME, proxyUser); } final String proxyPassword = System.getProperty("http.proxyPassword"); if (proxyPassword != null) { config.property(ClientProperties.PROXY_PASSWORD, proxyPassword); } //ensure Content-Length is populated before sending request via proxy. config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); } } } return config; }
Example #8
Source File: JerseyExtendedNiFiRegistryClient.java From nifi with Apache License 2.0 | 4 votes |
public JerseyExtendedNiFiRegistryClient(final NiFiRegistryClient delegate, final NiFiRegistryClientConfig registryClientConfig) { this.delegate = delegate; // Copied from JerseyNiFiRegistryClient! if (registryClientConfig == null) { throw new IllegalArgumentException("NiFiRegistryClientConfig cannot be null"); } String baseUrl = registryClientConfig.getBaseUrl(); if (StringUtils.isBlank(baseUrl)) { throw new IllegalArgumentException("Base URL cannot be blank"); } if (baseUrl.endsWith("/")) { baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } if (!baseUrl.endsWith(NIFI_REGISTRY_CONTEXT)) { baseUrl = baseUrl + "/" + NIFI_REGISTRY_CONTEXT; } try { new URI(baseUrl); } catch (final Exception e) { throw new IllegalArgumentException("Invalid base URL: " + e.getMessage(), e); } final SSLContext sslContext = registryClientConfig.getSslContext(); final HostnameVerifier hostnameVerifier = registryClientConfig.getHostnameVerifier(); final ClientBuilder clientBuilder = ClientBuilder.newBuilder(); if (sslContext != null) { clientBuilder.sslContext(sslContext); } if (hostnameVerifier != null) { clientBuilder.hostnameVerifier(hostnameVerifier); } final int connectTimeout = registryClientConfig.getConnectTimeout() == null ? DEFAULT_CONNECT_TIMEOUT : registryClientConfig.getConnectTimeout(); final int readTimeout = registryClientConfig.getReadTimeout() == null ? DEFAULT_READ_TIMEOUT : registryClientConfig.getReadTimeout(); final ClientConfig clientConfig = new ClientConfig(); clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout); clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout); clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED); clientConfig.register(jacksonJaxbJsonProvider()); clientBuilder.withConfig(clientConfig); this.client = clientBuilder .register(MultiPartFeature.class) .build(); this.baseTarget = client.target(baseUrl); this.tenantsClient = new JerseyTenantsClient(baseTarget); this.policiesClient = new JerseyPoliciesClient(baseTarget); }
Example #9
Source File: JerseyNiFiRegistryClient.java From nifi-registry with Apache License 2.0 | 4 votes |
private JerseyNiFiRegistryClient(final NiFiRegistryClient.Builder builder) { final NiFiRegistryClientConfig registryClientConfig = builder.getConfig(); if (registryClientConfig == null) { throw new IllegalArgumentException("NiFiRegistryClientConfig cannot be null"); } String baseUrl = registryClientConfig.getBaseUrl(); if (StringUtils.isBlank(baseUrl)) { throw new IllegalArgumentException("Base URL cannot be blank"); } if (baseUrl.endsWith("/")) { baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } if (!baseUrl.endsWith(NIFI_REGISTRY_CONTEXT)) { baseUrl = baseUrl + "/" + NIFI_REGISTRY_CONTEXT; } try { new URI(baseUrl); } catch (final Exception e) { throw new IllegalArgumentException("Invalid base URL: " + e.getMessage(), e); } final SSLContext sslContext = registryClientConfig.getSslContext(); final HostnameVerifier hostnameVerifier = registryClientConfig.getHostnameVerifier(); final ClientBuilder clientBuilder = ClientBuilder.newBuilder(); if (sslContext != null) { clientBuilder.sslContext(sslContext); } if (hostnameVerifier != null) { clientBuilder.hostnameVerifier(hostnameVerifier); } final int connectTimeout = registryClientConfig.getConnectTimeout() == null ? DEFAULT_CONNECT_TIMEOUT : registryClientConfig.getConnectTimeout(); final int readTimeout = registryClientConfig.getReadTimeout() == null ? DEFAULT_READ_TIMEOUT : registryClientConfig.getReadTimeout(); final ClientConfig clientConfig = new ClientConfig(); clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout); clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout); clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED); clientConfig.register(jacksonJaxbJsonProvider()); clientBuilder.withConfig(clientConfig); this.client = clientBuilder .register(MultiPartFeature.class) .build(); this.baseTarget = client.target(baseUrl); this.bucketClient = new JerseyBucketClient(baseTarget); this.flowClient = new JerseyFlowClient(baseTarget); this.flowSnapshotClient = new JerseyFlowSnapshotClient(baseTarget); this.itemsClient = new JerseyItemsClient(baseTarget); }
Example #10
Source File: JerseyDockerHttpClient.java From docker-java with Apache License 2.0 | 4 votes |
public Builder requestEntityProcessing(RequestEntityProcessing value) { this.requestEntityProcessing = value; return this; }
Example #11
Source File: JerseyDockerCmdExecFactory.java From docker-java with Apache License 2.0 | 4 votes |
public JerseyDockerCmdExecFactory withRequestEntityProcessing(RequestEntityProcessing requestEntityProcessing) { clientBuilder = clientBuilder.requestEntityProcessing(requestEntityProcessing); return this; }
Example #12
Source File: DefaultDockerClient.java From docker-client with Apache License 2.0 | 4 votes |
public RequestEntityProcessing getRequestEntityProcessing() { return this.requestEntityProcessing; }
Example #13
Source File: TestHttpClientSourceUpgrader.java From datacollector with Apache License 2.0 | 4 votes |
@Test public void testV6ToV7() throws Exception { configs.add(new Config("conf.requestTimeoutMillis", 1000)); configs.add(new Config("conf.numThreads", 10)); configs.add(new Config("conf.authType", AuthenticationType.NONE)); configs.add(new Config("conf.oauth", new OAuthConfigBean())); configs.add(new Config("conf.basicAuth", new PasswordAuthConfigBean())); configs.add(new Config("conf.useProxy", false)); configs.add(new Config("conf.proxy", new HttpProxyConfigBean())); configs.add(new Config("conf.sslConfig", new SslConfigBean())); HttpClientSourceUpgrader upgrader = new HttpClientSourceUpgrader(); upgrader.upgrade("a", "b", "c", 6, 7, configs); for (Config config : configs) { switch (config.getName()) { case "conf.client.requestTimeoutMillis": Assert.assertEquals(1000, config.getValue()); break; case "conf.client.authType": Assert.assertEquals(AuthenticationType.NONE, config.getValue()); break; case "conf.client.oauth": Assert.assertTrue(config.getValue() instanceof OAuthConfigBean); break; case "conf.client.basicAuth": Assert.assertTrue(config.getValue() instanceof PasswordAuthConfigBean); break; case "conf.client.useProxy": Assert.assertEquals(false, config.getValue()); break; case "conf.client.numThreads": Assert.assertEquals(10, config.getValue()); break; case "conf.client.proxy": Assert.assertTrue(config.getValue() instanceof HttpProxyConfigBean); break; case "conf.client.sslConfig": Assert.assertTrue(config.getValue() instanceof SslConfigBean); break; case "conf.client.transferEncoding": Assert.assertEquals(RequestEntityProcessing.CHUNKED, config.getValue()); break; default: fail(); } } }
Example #14
Source File: DatabricksJobExecutor.java From datacollector with Apache License 2.0 | 4 votes |
@Override public List<Stage.ConfigIssue> init() { List<Stage.ConfigIssue> issues = new ArrayList<>(); Optional .ofNullable(databricksConfigBean.init(getContext(), PREFIX)) .ifPresent(issues::addAll); baseUrl = databricksConfigBean.baseUrl.endsWith("/") ? databricksConfigBean.baseUrl.substring(0, databricksConfigBean.baseUrl.length() - 1) : databricksConfigBean.baseUrl; HttpProxyConfigBean proxyConf = databricksConfigBean.proxyConfigBean; String proxyUsername = null; String proxyPassword = null; if(databricksConfigBean.useProxy) { proxyUsername = proxyConf.resolveUsername(getContext(), "PROXY", "conf.proxyConfigBean.", issues); proxyPassword = proxyConf.resolvePassword(getContext(), "PROXY", "conf.proxyConfigBean.", issues); } if(issues.isEmpty()) { ClientConfig clientConfig = new ClientConfig() .property(ClientProperties.ASYNC_THREADPOOL_SIZE, 1) .property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); if (databricksConfigBean.useProxy) { clientConfig = clientConfig.connectorProvider(new GrizzlyConnectorProvider(new GrizzlyClientCustomizer( true, proxyUsername, proxyPassword ))); } ClientBuilder builder = getClientBuilder() .withConfig(clientConfig) .register(JacksonJsonProvider.class); HttpAuthenticationFeature auth = null; if (databricksConfigBean.credentialsConfigBean.credentialType == CredentialType.PASSWORD) { String username = databricksConfigBean.credentialsConfigBean.resolveUsername( getContext(), "CREDENTIALS", "conf.credentialsConfigBean.", issues ); String password = databricksConfigBean.credentialsConfigBean.resolvePassword( getContext(), "CREDENTIALS", "conf.credentialsConfigBean.", issues ); auth = HttpAuthenticationFeature.basic(username, password); builder.register(auth); } else { String token = databricksConfigBean.credentialsConfigBean.resolveToken( getContext(), "CREDENTIALS", "conf.credentialsConfigBean.", issues ); builder.register((ClientRequestFilter) requestContext -> requestContext.getHeaders().add("Authorization", "Bearer " + token) ); } JerseyClientUtil.configureSslContext(databricksConfigBean.tlsConfigBean, builder); if(databricksConfigBean.useProxy) { JerseyClientUtil.configureProxy( proxyConf.uri, proxyUsername, proxyPassword, builder ); } client = builder.build(); validateWithDatabricks(getContext(), issues); } return issues; }
Example #15
Source File: RequestEntityProcessingChooserValues.java From datacollector with Apache License 2.0 | 4 votes |
public RequestEntityProcessingChooserValues() { super (RequestEntityProcessing.class); }
Example #16
Source File: RestClientHelper.java From azure-devops-intellij with MIT License | 4 votes |
public static ClientConfig getClientConfig(final ServerContext.Type type, final Credentials credentials, final String serverUri, final boolean includeProxySettings) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); final ConnectorProvider connectorProvider = new ApacheConnectorProvider(); // custom json provider ignores new fields that aren't recognized final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); final ClientConfig clientConfig = new ClientConfig(jacksonJsonProvider).connectorProvider(connectorProvider); clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider); clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); // For TFS OnPrem we only support NTLM authentication right now. Since 2016 servers support Basic as well, // we need to let the server and client negotiate the protocol instead of preemptively assuming Basic. // TODO: This prevents PATs from being used OnPrem. We need to fix this soon to support PATs onPrem. clientConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, type != ServerContext.Type.TFS); //Define a local HTTP proxy if (includeProxySettings) { final HttpProxyService proxyService = PluginServiceProvider.getInstance().getHttpProxyService(); final String proxyUrl = proxyService.getProxyURL(); clientConfig.property(ClientProperties.PROXY_URI, proxyUrl); if (proxyService.isAuthenticationRequired()) { // To work with authenticated proxies and TFS, we provide the proxy credentials if they are registered final AuthScope ntlmAuthScope = new AuthScope(proxyService.getProxyHost(), proxyService.getProxyPort(), AuthScope.ANY_REALM, AuthScope.ANY_SCHEME); credentialsProvider.setCredentials(ntlmAuthScope, new UsernamePasswordCredentials(proxyService.getUserName(), proxyService.getPassword())); } } // register a filter to set the User Agent header clientConfig.register(new ClientRequestFilter() { @Override public void filter(final ClientRequestContext requestContext) throws IOException { // The default user agent is something like "Jersey/2.6" final String userAgent = VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client", HttpClientBuilder.class); // Finally, we can add the header requestContext.getHeaders().add(HttpHeaders.USER_AGENT, userAgent); } }); return clientConfig; }
Example #17
Source File: DefaultDockerClient.java From docker-client with Apache License 2.0 | 2 votes |
/** * Allows setting transfer encoding. CHUNKED does not send the content-length header * while BUFFERED does. * * <p>By default ApacheConnectorProvider uses CHUNKED mode. Some Docker API end-points * seems to fail when no content-length is specified but a body is sent. * * @param requestEntityProcessing is the requested entity processing to use when calling docker * daemon (tcp protocol). * @return Builder */ public Builder useRequestEntityProcessing( final RequestEntityProcessing requestEntityProcessing) { this.requestEntityProcessing = requestEntityProcessing; return this; }