本文整理了Java中com.linecorp.armeria.common.HttpRequest.method()
方法的一些代码示例,展示了HttpRequest.method()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.method()
方法的具体详情如下:
包路径:com.linecorp.armeria.common.HttpRequest
类名称:HttpRequest
方法名:method
[英]Returns the method of this request. This method is a shortcut of headers().method().
[中]返回此请求的方法。此方法是headers()的快捷方式。方法()。
代码示例来源:origin: line/armeria
@Override
public String getMethodValue() {
return req.method().name();
}
代码示例来源:origin: line/armeria
/**
* Sets the {@link HttpMethod} of the request.
*
* @throws IllegalArgumentException if the specified {@link HttpMethod} is not same with the
* {@link HttpMethod} of the {@link HttpRequest} you specified when
* creating this builder. This exception is not thrown if you
* created a builder with an {@link RpcRequest}.
*/
protected B method(HttpMethod method) {
requireNonNull(method, "method");
if (request instanceof HttpRequest) {
checkArgument(method == ((HttpRequest) request).method(),
"method: %s (expected: same as request.method)", method);
} else {
this.method = method;
}
return self();
}
代码示例来源:origin: line/armeria
@Override
public HttpService asService() {
return (ctx, req) -> {
switch (req.method()) {
case HEAD:
case GET:
return HttpResponse.of(HttpStatus.NOT_FOUND);
default:
return HttpResponse.of(HttpStatus.METHOD_NOT_ALLOWED);
}
};
}
}
代码示例来源:origin: line/armeria
/**
* Sets the 'content-length' header to the response.
*/
private static void setContentLength(HttpRequest req, HttpHeaders headers, int contentLength) {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
// prohibits to send message body for below cases.
// and in those cases, content should be empty.
if (req.method() == HttpMethod.HEAD || ArmeriaHttpUtil.isContentAlwaysEmpty(headers.status())) {
return;
}
headers.setInt(HttpHeaderNames.CONTENT_LENGTH, contentLength);
}
代码示例来源:origin: line/armeria
/**
* Returns {@code true} if the specified {@code request} is a CORS preflight request.
*/
public static boolean isCorsPreflightRequest(com.linecorp.armeria.common.HttpRequest request) {
requireNonNull(request, "request");
return request.method() == HttpMethod.OPTIONS &&
request.headers().contains(HttpHeaderNames.ORIGIN) &&
request.headers().contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}
代码示例来源:origin: line/armeria
private HttpResponse execute(@Nullable EventLoop eventLoop, HttpRequest req) {
final String concatPaths = concatPaths(uri().getRawPath(), req.path());
req.path(concatPaths);
final PathAndQuery pathAndQuery = PathAndQuery.parse(concatPaths);
if (pathAndQuery == null) {
req.abort();
return HttpResponse.ofFailure(new IllegalArgumentException("invalid path: " + concatPaths));
}
return execute(eventLoop, req.method(), pathAndQuery.path(), pathAndQuery.query(), null, req,
(ctx, cause) -> {
if (ctx != null && !ctx.log().isAvailable(RequestLogAvailability.REQUEST_START)) {
// An exception is raised even before sending a request, so abort the request to
// release the elements.
req.abort();
}
return HttpResponse.ofFailure(cause);
});
}
代码示例来源:origin: line/armeria
final ArmeriaHttpTransport transport = new ArmeriaHttpTransport(req.method());
final HttpChannel httpChannel = new HttpChannel(
connector,
代码示例来源:origin: line/armeria
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
try {
switch (req.method()) {
case OPTIONS:
return doOptions(ctx, req);
代码示例来源:origin: com.linecorp.centraldogma/centraldogma-server-shaded
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object resObj) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
final HttpData httpData =
resObj.getClass() == Object.class ? EMPTY_RESULT
: HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(HttpMethod.POST == request.method() ? HttpStatus.CREATED
: HttpStatus.OK,
MediaType.JSON_UTF_8,
httpData);
} catch (JsonProcessingException e) {
return HttpApiUtil.newResponse(HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
代码示例来源:origin: com.linecorp.centraldogma/centraldogma-server-shaded
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object resObj) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
if (HttpMethod.DELETE == request.method() ||
(resObj instanceof Iterable && Iterables.size((Iterable<?>) resObj) == 0)) {
return HttpResponse.of(HttpStatus.NO_CONTENT);
}
final HttpData httpData = HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, httpData);
} catch (JsonProcessingException e) {
logger.debug("Failed to convert a response:", e);
return HttpApiUtil.newResponse(HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
代码示例来源:origin: line/centraldogma
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers,
@Nullable Object resObj,
HttpHeaders trailingHeaders) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
if (resObj == null || HttpMethod.DELETE == request.method() ||
(resObj instanceof Iterable && Iterables.size((Iterable<?>) resObj) == 0)) {
return HttpResponse.of(HttpStatus.NO_CONTENT);
}
final HttpHeaders resHeaders;
if (headers.contentType() == null) {
resHeaders = toMutableHeaders(headers);
resHeaders.contentType(MediaType.JSON_UTF_8);
} else {
resHeaders = headers;
}
final HttpData httpData = HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(resHeaders, httpData, trailingHeaders);
} catch (JsonProcessingException e) {
logger.debug("Failed to convert a response:", e);
return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
代码示例来源:origin: line/centraldogma
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers,
@Nullable Object resObj,
HttpHeaders trailingHeaders) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
final HttpData httpData =
resObj != null &&
resObj.getClass() == Object.class ? EMPTY_RESULT
: HttpData.of(Jackson.writeValueAsBytes(resObj));
final HttpHeaders httpHeaders = toMutableHeaders(headers);
if (HttpMethod.POST == request.method()) {
httpHeaders.status(HttpStatus.CREATED);
}
if (httpHeaders.contentType() == null) {
httpHeaders.contentType(MediaType.JSON_UTF_8);
}
return HttpResponse.of(httpHeaders, httpData, trailingHeaders);
} catch (JsonProcessingException e) {
return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
代码示例来源:origin: com.linecorp.centraldogma/centraldogma-server
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers,
@Nullable Object resObj,
HttpHeaders trailingHeaders) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
final HttpData httpData =
resObj != null &&
resObj.getClass() == Object.class ? EMPTY_RESULT
: HttpData.of(Jackson.writeValueAsBytes(resObj));
final HttpHeaders httpHeaders = toMutableHeaders(headers);
if (HttpMethod.POST == request.method()) {
httpHeaders.status(HttpStatus.CREATED);
}
if (httpHeaders.contentType() == null) {
httpHeaders.contentType(MediaType.JSON_UTF_8);
}
return HttpResponse.of(httpHeaders, httpData, trailingHeaders);
} catch (JsonProcessingException e) {
return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
代码示例来源:origin: com.linecorp.centraldogma/centraldogma-server
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers,
@Nullable Object resObj,
HttpHeaders trailingHeaders) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
if (resObj == null || HttpMethod.DELETE == request.method() ||
(resObj instanceof Iterable && Iterables.size((Iterable<?>) resObj) == 0)) {
return HttpResponse.of(HttpStatus.NO_CONTENT);
}
final HttpHeaders resHeaders;
if (headers.contentType() == null) {
resHeaders = toMutableHeaders(headers);
resHeaders.contentType(MediaType.JSON_UTF_8);
} else {
resHeaders = headers;
}
final HttpData httpData = HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(resHeaders, httpData, trailingHeaders);
} catch (JsonProcessingException e) {
logger.debug("Failed to convert a response:", e);
return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!