本文整理了Java中co.cask.common.http.HttpRequest.builder()
方法的一些代码示例,展示了HttpRequest.builder()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.builder()
方法的具体详情如下:
包路径:co.cask.common.http.HttpRequest
类名称:HttpRequest
方法名:builder
暂无
代码示例来源:origin: caskdata/cdap
/**
* Create a {@link HttpRequest.Builder} using the specified http method and resource. This client will
* discover the service address and combine the specified resource in order to set a URL for the builder.
*
* @param method the request method
* @param resource the request resource
* @return a builder to create the http request, with method and URL already set
*/
public HttpRequest.Builder requestBuilder(HttpMethod method, String resource) {
return HttpRequest.builder(method, resolve(resource));
}
代码示例来源:origin: co.cask.cdap/cdap-common
/**
* Create a {@link HttpRequest.Builder} using the specified http method and resource. This client will
* discover the service address and combine the specified resource in order to set a URL for the builder.
*
* @param method the request method
* @param resource the request resource
* @return a builder to create the http request, with method and URL already set
*/
public HttpRequest.Builder requestBuilder(HttpMethod method, String resource) {
return HttpRequest.builder(method, resolve(resource));
}
代码示例来源:origin: co.cask.cdap/cdap-explore-client
private HttpResponse doRequest(String resource, HttpMethod requestMethod,
@Nullable Map<String, String> headers,
@Nullable String body) throws ExploreException {
Map<String, String> newHeaders = addSecurityHeaders(headers);
String resolvedUrl = resolve(resource);
try {
URL url = new URL(resolvedUrl);
HttpRequest.Builder builder = HttpRequest.builder(requestMethod, url).addHeaders(newHeaders);
if (body != null) {
builder.withBody(body);
}
return HttpRequests.execute(builder.build(), createRequestConfig());
} catch (IOException e) {
throw new ExploreException(
String.format("Error connecting to Explore Service at %s while doing %s with headers %s and body %s",
resolvedUrl, requestMethod,
newHeaders == null ? "null" : Joiner.on(",").withKeyValueSeparator("=").join(newHeaders),
body == null ? "null" : body), e);
}
}
代码示例来源:origin: cdapio/cdap
private HttpResponse doRequest(String resource, HttpMethod requestMethod,
@Nullable Map<String, String> headers,
@Nullable String body) throws ExploreException {
Map<String, String> newHeaders = addSecurityHeaders(headers);
String resolvedUrl = resolve(resource);
try {
URL url = new URL(resolvedUrl);
HttpRequest.Builder builder = HttpRequest.builder(requestMethod, url).addHeaders(newHeaders);
if (body != null) {
builder.withBody(body);
}
return HttpRequests.execute(builder.build(), createRequestConfig());
} catch (IOException e) {
throw new ExploreException(
String.format("Error connecting to Explore Service at %s while doing %s with headers %s and body %s",
resolvedUrl, requestMethod,
newHeaders == null ? "null" : Joiner.on(",").withKeyValueSeparator("=").join(newHeaders),
body == null ? "null" : body), e);
}
}
代码示例来源:origin: caskdata/cdap
public HttpResponse execute(HttpMethod httpMethod, URL url, AccessToken accessToken, int... allowedErrorCodes)
throws IOException, UnauthenticatedException, DisconnectedException, UnauthorizedException {
return execute(HttpRequest.builder(httpMethod, url)
.addHeaders(getAuthHeaders(accessToken))
.build(), allowedErrorCodes);
}
代码示例来源:origin: caskdata/cdap
public HttpResponse execute(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes)
throws IOException, UnauthenticatedException, DisconnectedException, UnauthorizedException {
return execute(HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(), allowedErrorCodes);
}
代码示例来源:origin: caskdata/cdap
public HttpResponse execute(HttpMethod httpMethod, URL url, Map<String, String> headers, AccessToken accessToken,
int... allowedErrorCodes)
throws IOException, UnauthenticatedException, DisconnectedException, UnauthorizedException {
return execute(HttpRequest.builder(httpMethod, url)
.addHeaders(headers)
.addHeaders(getAuthHeaders(accessToken))
.build(), allowedErrorCodes);
}
代码示例来源:origin: caskdata/cdap
private HttpResponse makeRequest(String path, HttpMethod httpMethod, @Nullable String body)
throws IOException, UnauthenticatedException, BadRequestException, UnauthorizedException {
URL url = resolve(path);
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url);
if (body != null) {
builder.withBody(body);
}
HttpResponse response = execute(builder.build(),
HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
return response;
}
代码示例来源:origin: caskdata/cdap
public HttpResponse upload(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes)
throws IOException, UnauthenticatedException, DisconnectedException {
HttpResponse response = HttpRequests.execute(
HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(),
clientConfig.getUploadRequestConfig());
int responseCode = response.getResponseCode();
if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new UnauthenticatedException("Unauthorized status code received from the server.");
}
throw new IOException(response.getResponseBodyAsString());
}
return response;
}
代码示例来源:origin: co.cask.cdap/cdap-common
private HttpResponse makeRequest(String path, HttpMethod httpMethod, @Nullable String body)
throws IOException, UnauthenticatedException, BadRequestException, UnauthorizedException {
URL url = resolve(path);
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url);
if (body != null) {
builder.withBody(body);
}
HttpResponse response = execute(builder.build(),
HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
return response;
}
代码示例来源:origin: caskdata/cdap
/**
* Stops a batch of programs in the same call.
*
* @param namespace the namespace of the programs
* @param programs the programs to stop
* @return the result of stopping each program
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public List<BatchProgramResult> stop(NamespaceId namespace, List<BatchProgram> programs)
throws IOException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(namespace, "stop");
HttpRequest request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(programs), Charsets.UTF_8).build();
HttpResponse response = restClient.execute(request, config.getAccessToken());
return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON)
.getResponseObject();
}
代码示例来源:origin: caskdata/cdap
public HttpResponse execute(HttpMethod httpMethod, URL url, String body, Map<String, String> headers,
AccessToken accessToken, int... allowedErrorCodes)
throws IOException, UnauthenticatedException, DisconnectedException, UnauthorizedException {
return execute(HttpRequest.builder(httpMethod, url)
.addHeaders(headers)
.addHeaders(getAuthHeaders(accessToken))
.withBody(body).build(), allowedErrorCodes);
}
代码示例来源:origin: caskdata/cdap
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url).addHeaders(headerMap);
if (httpMethod == HttpMethod.GET && (!bodyFile.isEmpty() || !bodyString.isEmpty())) {
throw new UnsupportedOperationException("Sending body in a GET request is not supported");
代码示例来源:origin: caskdata/cdap
/**
* Starts a batch of programs in the same call.
*
* @param namespace the namespace of the programs
* @param programs the programs to start, including any runtime arguments to start them with
* @return the result of starting each program
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public List<BatchProgramResult> start(NamespaceId namespace, List<BatchProgramStart> programs)
throws IOException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(namespace, "start");
HttpRequest request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(programs), Charsets.UTF_8).build();
HttpResponse response = restClient.execute(request, config.getAccessToken());
return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON)
.getResponseObject();
}
代码示例来源:origin: co.cask.cdap/cdap-cli
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url).addHeaders(headerMap);
if (httpMethod == HttpMethod.GET && (!bodyFile.isEmpty() || !bodyString.isEmpty())) {
throw new UnsupportedOperationException("Sending body in a GET request is not supported");
代码示例来源:origin: cdapio/cdap
@Test
public void testDynamicPluginBodyProducer() throws Exception {
// test that a plugin can be instantiated in the body producer chunk() and onFinish() methods
// the good plugin should be found, so the response should be 'x'
DynamicPluginServiceApp.PluginRequest requestBody =
new DynamicPluginServiceApp.PluginRequest(ConstantFunction.NAME, Collections.singletonMap("value", "x"),
ConstantFunction.NAME, Collections.singletonMap("value", "y"));
URL producerUrl = baseURI.resolve("producer").toURL();
HttpRequest request = HttpRequest.builder(HttpMethod.POST, producerUrl)
.withBody(GSON.toJson(requestBody))
.build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("x", response.getResponseBodyAsString());
URL onFinishUrl = baseURI.resolve("onFinishSuccessful").toURL();
request = HttpRequest.builder(HttpMethod.GET, onFinishUrl).build();
response = HttpRequests.execute(request);
Assert.assertTrue(Boolean.valueOf(response.getResponseBodyAsString()));
}
代码示例来源:origin: cdapio/cdap
@Test
public void testDynamicPluginSimple() throws Exception {
// test a single plugin
Map<String, String> properties = new HashMap<>();
properties.put("value", "x");
URL url = baseURI.resolve(String.format("plugins/%s/apply", ConstantFunction.NAME)).toURL();
HttpRequest request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(properties))
.build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("x", response.getResponseBodyAsString());
// test plugin that uses a plugin
Map<String, String> delegateProperties = new HashMap<>();
delegateProperties.put("value", "y");
properties = new HashMap<>();
properties.put("delegateName", ConstantFunction.NAME);
properties.put("properties", GSON.toJson(delegateProperties));
url = baseURI.resolve(String.format("plugins/%s/apply", DelegatingFunction.NAME)).toURL();
request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(properties))
.build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("y", response.getResponseBodyAsString());
}
代码示例来源:origin: cdapio/cdap
@Test
public void testDynamicPluginContentConsumer() throws Exception {
// test that a plugin can be instantiated in the content consumer finish method
// the good plugin should be found, so the response should be 'x'
DynamicPluginServiceApp.PluginRequest requestBody =
new DynamicPluginServiceApp.PluginRequest(ConstantFunction.NAME, Collections.singletonMap("value", "x"),
ConstantFunction.NAME, Collections.singletonMap("value", "y"));
URL url = baseURI.resolve("consumer").toURL();
HttpRequest request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(requestBody))
.build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("x", response.getResponseBodyAsString());
// test that a plugin can be instantiated in the content consumer error method
// the "good" plugin should not be found, so the response should be 'y'
requestBody =
new DynamicPluginServiceApp.PluginRequest("non-existent", Collections.singletonMap("value", "x"),
ConstantFunction.NAME, Collections.singletonMap("value", "y"));
request = HttpRequest.builder(HttpMethod.POST, url)
.withBody(GSON.toJson(requestBody))
.build();
response = HttpRequests.execute(request);
Assert.assertEquals(400, response.getResponseCode());
Assert.assertEquals("y", response.getResponseBodyAsString());
}
}
代码示例来源:origin: cdapio/cdap
/**
* This test the normal operations of the SOCKS proxy.
*/
@Test
public void testSocksProxy() throws Exception {
InetSocketAddress httpAddr = httpService.getBindAddress();
// Make 10 requests. With connection keep-alive, there should only be one SSH tunnel created
URL url = new URL(String.format("http://%s:%d/ping", httpAddr.getHostName(), httpAddr.getPort()));
for (int i = 0; i < 10; i++) {
HttpResponse response = HttpRequests.execute(co.cask.common.http.HttpRequest.get(url).build());
Assert.assertEquals(200, response.getResponseCode());
}
Assert.assertEquals(1, sshSession.portForwardCreated.get());
// Make one more call with Connection: close. This should close the connection, hence close the tunnel.
HttpResponse response = HttpRequests.execute(
co.cask.common.http.HttpRequest.builder(HttpMethod.GET, url)
.addHeader(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.CLOSE.toString())
.build());
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(1, sshSession.portForwardCreated.get());
Tasks.waitFor(1, () -> sshSession.portForwardClosed.get(), 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
内容来源于网络,如有侵权,请联系作者删除!