org.springframework.http.client.Netty4ClientHttpRequestFactory Java Examples

The following examples show how to use org.springframework.http.client.Netty4ClientHttpRequestFactory. 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: ScmPropertySourceLocator.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Create rest template.
 * 
 * @param readTimeout
 * @return
 */
public RestTemplate createRestTemplate(long readTimeout) {
	Assert.state(readTimeout > 0, String.format("Invalid value for read timeout for %s", readTimeout));

	Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
	factory.setConnectTimeout(config.getConnectTimeout());
	factory.setReadTimeout((int) readTimeout);
	factory.setMaxResponseSize(config.getMaxResponseSize());
	RestTemplate restTemplate = new RestTemplate(factory);

	Map<String, String> headers = new HashMap<>(config.getHeaders());
	if (headers.containsKey(AUTHORIZATION)) {
		// To avoid redundant addition of header
		headers.remove(AUTHORIZATION);
	}
	if (!headers.isEmpty()) {
		restTemplate.setInterceptors(asList(new GenericRequestHeaderInterceptor(headers)));
	}
	return restTemplate;
}
 
Example #2
Source File: CossPipeDeployer.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
public static void transFile(String uploadUrl, File file, String cossProvider, String bucketName) {
    Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
    //factory.setConnectTimeout(10_000);
    //factory.setReadTimeout(60_000);
    //factory.setMaxResponseSize(1024 * 1024 * 10);
    RestTemplate restTemplate = new RestTemplate(factory);
    FileSystemResource resource = new FileSystemResource(file);
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    param.add("file", resource);
    // need add few coss params
    param.add("cossProvider", cossProvider);
    param.add("bucketName", bucketName);
    param.add("acl", "default");
    RespBase respBase = restTemplate.postForObject(uploadUrl, param, RespBase.class);
    Assert2.isTrue(Objects.nonNull(respBase) && respBase.getCode() == 200, "TransFile Fail, cause: %s", JacksonUtils.toJSONString(respBase));
}
 
Example #3
Source File: UploadUtilTests.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Test
public void uploadTest() throws GitAPIException {
    Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
    //factory.setConnectTimeout(10_000);
    //factory.setReadTimeout(60_000);
    //factory.setMaxResponseSize(1024 * 1024 * 10);
    RestTemplate restTemplate = new RestTemplate(factory);

    FileSystemResource resource = new FileSystemResource(new File("/Users/vjay/Downloads/logo.png"));
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    param.add("deviceId", "123424");
    param.add("file", resource);
    String recv = restTemplate.postForObject(uploadUrl, param, String.class);
    System.out.println(recv);

    /*MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("id",id);
    HttpHeaders header = new HttpHeaders();
    // 需求需要传参为form-data格式
    header.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, header);*/

}
 
Example #4
Source File: RestTemplateIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Parameters
public static Iterable<? extends ClientHttpRequestFactory> data() {
	return Arrays.asList(
			new SimpleClientHttpRequestFactory(),
			new HttpComponentsClientHttpRequestFactory(),
			new Netty4ClientHttpRequestFactory(),
			new OkHttp3ClientHttpRequestFactory()
	);
}
 
Example #5
Source File: RestTemplateConfiguration.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory netty4ClientHttpRequestFactory() {
	Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
	factory.setReadTimeout(5000);
	factory.setConnectTimeout(5000);
	factory.setMaxResponseSize(1024 * 1024 * 10);
	return factory;
}
 
Example #6
Source File: ClientRemoteAutoConfiguration.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ClientHttpRequestFactory netty4ClientHttpRequestFactory(
		RemoteProperties config/* , SslContext sslContext */) {
	Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
	factory.setReadTimeout(config.getReadTimeout());
	factory.setConnectTimeout(config.getConnectTimeout());
	factory.setMaxResponseSize(config.getMaxResponseSize());
	// factory.setSslContext(sslContext);
	return factory;
}
 
Example #7
Source File: AbstractVcsOperator.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
	factory.setConnectTimeout(config.getVcs().getConnectTimeout());
	factory.setReadTimeout(config.getVcs().getReadTimeout());
	factory.setMaxResponseSize(config.getVcs().getMaxResponseSize());
	this.restTemplate = new RestTemplate(factory);
}
 
Example #8
Source File: AbstractPcmOperator.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
	factory.setConnectTimeout(config.getPcm().getConnectTimeout());
	factory.setReadTimeout(config.getPcm().getReadTimeout());
	factory.setMaxResponseSize(config.getPcm().getMaxResponseSize());
	this.restTemplate = new RestTemplate(factory);
}
 
Example #9
Source File: ClientHttpRequestFactoryFactoryTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void nettyClientCreated() throws Exception {
	ClientHttpRequestFactory factory = Netty.usingNetty(new ClientOptions());

	assertThat(factory).isInstanceOf(Netty4ClientHttpRequestFactory.class);

	((DisposableBean) factory).destroy();
}
 
Example #10
Source File: ClientHttpRequestFactoryFactory.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
static ClientHttpRequestFactory usingNetty(ClientOptions options, SslConfiguration sslConfiguration)
		throws GeneralSecurityException, IOException {

	Netty4ClientHttpRequestFactory requestFactory = new Netty4ClientHttpRequestFactory();

	if (hasSslConfiguration(sslConfiguration)) {

		SslContextBuilder sslContextBuilder = SslContextBuilder //
				.forClient();

		if (sslConfiguration.getTrustStoreConfiguration().isPresent()) {
			sslContextBuilder
					.trustManager(createTrustManagerFactory(sslConfiguration.getTrustStoreConfiguration()));
		}

		if (sslConfiguration.getKeyStoreConfiguration().isPresent()) {
			sslContextBuilder.keyManager(createKeyManagerFactory(sslConfiguration.getKeyStoreConfiguration(),
					sslConfiguration.getKeyConfiguration()));
		}

		requestFactory.setSslContext(sslContextBuilder.sslProvider(SslProvider.JDK).build());
	}

	requestFactory.setConnectTimeout(Math.toIntExact(options.getConnectionTimeout().toMillis()));
	requestFactory.setReadTimeout(Math.toIntExact(options.getReadTimeout().toMillis()));

	return requestFactory;
}
 
Example #11
Source File: ClientHttpRequestFactoryFactoryIntegrationTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Test
void nettyClientShouldWork() throws Exception {

	ClientHttpRequestFactory factory = Netty.usingNetty(new ClientOptions(), Settings.createSslConfiguration());
	((InitializingBean) factory).afterPropertiesSet();
	RestTemplate template = new RestTemplate(factory);

	String response = request(template);

	assertThat(factory).isInstanceOf(Netty4ClientHttpRequestFactory.class);
	assertThat(response).isNotNull().contains("initialized");

	((DisposableBean) factory).destroy();
}
 
Example #12
Source File: ThreadAffinityTest.java    From riptide with MIT License 5 votes vote down vote up
@Test
void syncNonBlockingNetty() throws Exception {
    final Netty4ClientHttpRequestFactory requestFactory = new Netty4ClientHttpRequestFactory(
            new NioEventLoopGroup(0, threadFactory("io")));

    try {
        final ConfigurationStage stage = Http.builder()
                .asyncRequestFactory(requestFactory);

        test(stage, "main", "io", "io");
    } finally {
        requestFactory.destroy();
    }
}
 
Example #13
Source File: RestTemplateIT.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void test3() throws Exception {
    RestTemplate restTemplate = new RestTemplate(new Netty4ClientHttpRequestFactory());
    String forObject = restTemplate.getForObject(webServer.getCallHttpUrl(), String.class);

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.awaitTrace(event("ASYNC", "Asynchronous Invocation"), 20, 3000);
    verifier.printCache();

    verifier.verifyTrace(event("REST_TEMPLATE", RestTemplate.class.getConstructor()));
    verifier.verifyTrace(event("REST_TEMPLATE", "org.springframework.http.client.AbstractAsyncClientHttpRequest.executeAsync()"));
    verifier.verifyTrace(event("REST_TEMPLATE", "RestTemplate execAsync Result Invocation"));
    verifier.verifyTrace(event("ASYNC", "Asynchronous Invocation"));
    verifier.verifyTrace(event("REST_TEMPLATE", "org.springframework.util.concurrent.SettableListenableFuture.set(java.lang.Object)", annotation("http.status.code", 200)));
}