本文整理汇总了Java中io.vertx.core.http.HttpHeaders类的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders类的具体用法?Java HttpHeaders怎么用?Java HttpHeaders使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpHeaders类属于io.vertx.core.http包,在下文中一共展示了HttpHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: genBodyBuffer
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
private void genBodyBuffer() throws Exception {
if (bodyBuffer != null) {
return;
}
if (formMap == null) {
return;
}
request.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
try (BufferOutputStream output = new BufferOutputStream()) {
for (Entry<String, Object> entry : formMap.entrySet()) {
output.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
output.write('=');
if (entry.getValue() != null) {
String value = RestObjectMapper.INSTANCE.convertToString(entry.getValue());
value = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
output.write(value.getBytes(StandardCharsets.UTF_8));
}
output.write('&');
}
bodyBuffer = output.getBuffer();
}
}
示例2: end
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Override
public void end(Buffer chunk) {
checkWritten();
if (!chunked && !contentLengthSet()) {
headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(chunk.length()));
}
body().appendBuffer(chunk);
if (!headWritten) {
prepareHeaders();
}
outputResponse();
closed = true;
written = true;
if (bodyEndHandler != null) {
bodyEndHandler.handle(null);
}
if (endHandler != null) {
endHandler.handle(null);
}
}
示例3: addResponseHeaders
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
default void addResponseHeaders(RouteDefinition definition, HttpServerResponse response) {
if (!response.ended() &&
!response.headers().contains(HttpHeaders.CONTENT_TYPE)) {
if (definition != null &&
definition.getProduces() != null) {
for (MediaType produces : definition.getProduces()) {
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaTypeHelper.toString(produces));
}
}
else {
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD);
}
}
}
示例4: handle
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Override
public void handle(final RoutingContext context) {
if (LOG.isErrorEnabled()) {
LOG.error("Unhandled server exception statusCode={} responseEnded={} uri={}", context.statusCode(), context.response().ended(), context.request().uri(), context.failure());
}
if (!context.response().ended()) {
if (context.failure() instanceof ConnectException) {
context.response().setStatusCode(504)
.setStatusMessage(GATEWAY_TIMEOUT)
.putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
.end(Errors.serverError(GATEWAY_TIMEOUT).toBuffer());
} else if (context.failure() instanceof UnknownHostException) {
context.response().setStatusCode(503)
.setStatusMessage(GATEWAY_ERROR)
.putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
.end(Errors.serverError(GATEWAY_ERROR).toBuffer());
} else {
context.response().setStatusCode(context.statusCode() == -1 ? 500 : context.statusCode())
.setStatusMessage(INTERNAL_SERVER_ERROR)
.putHeader(HttpHeaders.CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
.end(Errors.serverError(INTERNAL_SERVER_ERROR).toBuffer());
}
}
}
示例5: reply
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
public static void reply(
final RoutingContext context,
final Envelop envelop) {
// 1. Get response reference
final HttpServerResponse response
= context.response();
// 2. Set response status
final HttpStatusCode code = envelop.status();
response.setStatusCode(code.code());
response.setStatusMessage(code.message());
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
// 3. Response process
if (!response.ended()) {
response.end(envelop.response());
}
response.close();
}
示例6: callback_path_responds_with_302_when_no_auth_code_param_is_present
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test
public void callback_path_responds_with_302_when_no_auth_code_param_is_present(final TestContext ctx) {
final Async async = ctx.async();
vertx.createHttpClient().get(PORT, HOST, "/oauth/callback", res -> {
assertEquals(302, res.statusCode());
final URL redirectUrl = buildUrl(res.headers().get(HttpHeaders.LOCATION));
assertEquals("accounts.google.com", redirectUrl.getHost());
final Map<String, String> params = parseQuery(redirectUrl);
assertEquals("offline", params.get("access_type"));
assertEquals("force", params.get("approval_prompt"));
assertEquals(System.getenv("CLIENT_ID"), params.get("client_id"));
assertEquals(System.getenv("REDIRECT_URL"), params.get("redirect_uri"));
assertEquals("code", params.get("response_type"));
async.complete();
}).putHeader(HttpHeaders.USER_AGENT, "SomeUserAgent/1.0").end();
}
示例7: ask
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
public static void ask(
HttpServerResponse httpServerResponse, String conversationToken,
String initialPrompt, String[] noInputPrompts) {
httpServerResponse.putHeader(
HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
httpServerResponse.putHeader("Google-Assistant-API-Version", "v1");
JsonObject responseJSON = new JsonObject();
if (conversationToken != null) {
responseJSON.put("conversation_token", conversationToken);
}
responseJSON.put("expect_user_response", true);
responseJSON.put(
"expected_inputs",
JsonUtil.getArray(
_getExpectedInputJSON(initialPrompt, noInputPrompts)));
httpServerResponse.end(responseJSON.encode());
}
示例8: apply
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Override
public JsonObject apply(RoutingContext context) throws Throwable {
if (context.request().method() == HttpMethod.GET) {
throw new RequestConvertersionException("Invalid HttpRequestMethod 'GET'");
}
checkContentType(context.request().headers().get(HttpHeaders.CONTENT_TYPE));
final JsonObject jsonObject = context.getBodyAsJson();
checkJsonObject(jsonObject);
return new JsonObject(
ImmutableMap.<String, Object>builder()
.putAll(jsonObject.getMap())
.putAll(pathParams(context.pathParams(), jsonObject))
.build()
);
}
示例9: handle
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Override
public void handle(RoutingContext rc) {
HttpServerResponse response = rc.response();
response
.setChunked(true)
.putHeader(HttpHeaders.CONTENT_TYPE, "text/event-stream")
.putHeader(HttpHeaders.CACHE_CONTROL, "no-cache")
.putHeader(HttpHeaders.CONNECTION, HttpHeaders.KEEP_ALIVE);
rc.request().connection()
.closeHandler(v -> {
connections.remove(response);
endQuietly(response);
})
.exceptionHandler(t -> {
connections.remove(response);
rc.fail(t);
});
connections.add(response);
}
示例10: createSuccessResponse
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
private KnotContext createSuccessResponse(KnotContext inputContext, String renderedContent) {
ClientResponse clientResponse = inputContext.getClientResponse();
if (StringUtils.isBlank(renderedContent)) {
clientResponse.setStatusCode(HttpResponseStatus.NO_CONTENT.code());
} else {
MultiMap headers = clientResponse.getHeaders();
headers.add(HttpHeaders.CONTENT_LENGTH.toString().toLowerCase(),
Integer.toString(renderedContent.length()));
clientResponse.setBody(Buffer.buffer(renderedContent)).setHeaders(headers);
clientResponse.setStatusCode(HttpResponseStatus.OK.code());
}
return new KnotContext()
.setClientRequest(inputContext.getClientRequest())
.setClientResponse(clientResponse);
}
示例11: testOkAddBodyRequiredWithXML
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test
public void testOkAddBodyRequiredWithXML(TestContext context) {
Async async = context.async();
String bodyReqXml = "<BodyType><id>1</id><name>body 1</name></BodyType>";
HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/body/required");
req.handler(response -> {
context.assertEquals(response.statusCode(), 200);
response.bodyHandler(body -> {
context.assertEquals(bodyReqXml, body.toString());
async.complete();
});
})
.putHeader(HttpHeaders.CONTENT_TYPE, "application/xml")
.end(bodyReqXml);
}
示例12: testOkAddArrayBodyWithXML
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test
public void testOkAddArrayBodyWithXML(TestContext context) {
Async async = context.async();
String bodyReqXml = "<BodyType><id>1</id><name>body 1</name></BodyType><BodyType><id>2</id><name>body 2</name></BodyType>";
HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/body/array");
req.handler(response -> {
context.assertEquals(response.statusCode(), 200);
response.bodyHandler(body -> {
context.assertEquals(bodyReqXml, body.toString());
async.complete();
});
})
.putHeader(HttpHeaders.CONTENT_TYPE, "application/xml")
.end(bodyReqXml);
}
示例13: testOkFormDataSimpleRequired
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test()
public void testOkFormDataSimpleRequired(TestContext context) {
Async async = context.async();
HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/formdata/simple/required");
req.handler(response -> {
response.bodyHandler(body -> {
context.assertEquals(response.statusCode(), 200);
context.assertEquals("toto", body.toString());
async.complete();
});
});
// Construct form
StringBuffer payload = new StringBuffer().append("formDataRequired=").append(esc.escape("toto"));
req.putHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
req.end(payload.toString());
}
示例14: testOkFormDataSimpleRequiredAllowEmpty
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test()
public void testOkFormDataSimpleRequiredAllowEmpty(TestContext context) {
Async async = context.async();
HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/formdata/simple/required/allowempty");
req.handler(response -> {
response.bodyHandler(body -> {
context.assertEquals(response.statusCode(), 200);
context.assertEquals("", body.toString());
async.complete();
});
});
// Construct form
StringBuffer payload = new StringBuffer().append("formDataRequired=");
req.putHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
req.end(payload.toString());
}
示例15: testOkFormDataSimpleNotRequiredWithParam
import io.vertx.core.http.HttpHeaders; //导入依赖的package包/类
@Test()
public void testOkFormDataSimpleNotRequiredWithParam(TestContext context) {
Async async = context.async();
HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/formdata/simple/not/required");
req.handler(response -> {
response.bodyHandler(body -> {
context.assertEquals(response.statusCode(), 200);
context.assertEquals("toto", body.toString());
async.complete();
});
});
// Construct form
StringBuffer payload = new StringBuffer().append("formDataNotRequired=").append(esc.escape("toto"));
req.putHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
req.end(payload.toString());
}