本文整理了Java中io.micronaut.http.HttpRequest
类的一些代码示例,展示了HttpRequest
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest
类的具体详情如下:
包路径:io.micronaut.http.HttpRequest
类名称:HttpRequest
[英]Common interface for HTTP request implementations.
[中]HTTP请求实现的公共接口。
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @return Obtain the server address
*/
default @Nonnull InetSocketAddress getServerAddress() {
String host = getUri().getHost();
int port = getUri().getPort();
return new InetSocketAddress(host != null ? host : "localhost", port > -1 ? port : 80);
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @param request The {@link HttpRequest} object
* @return Return whether this request is a pre-flight request
*/
static boolean isPreflightRequest(HttpRequest request) {
HttpHeaders headers = request.getHeaders();
Optional<String> origin = headers.getOrigin();
return origin.isPresent() && headers.contains(ACCESS_CONTROL_REQUEST_METHOD) && HttpMethod.OPTIONS == request.getMethod();
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Return a {@link MutableHttpRequest} for a {@link HttpMethod#GET} request for the given URI.
*
* @param uri The URI
* @param <T> The Http request type
* @return The {@link MutableHttpRequest} instance
* @see HttpRequestFactory
*/
static <T> MutableHttpRequest<T> GET(URI uri) {
return GET(uri.toString());
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Obtains the web socket URL.
*
*
* @param ctx The context
* @param req The request
* @return The socket URL
*/
protected String getWebSocketURL(ChannelHandlerContext ctx, HttpRequest req) {
boolean isSecure = ctx.pipeline().get(SslHandler.class) != null;
return (isSecure ? SCHEME_SECURE_WEBSOCKET : SCHEME_WEBSOCKET) + req.getHeaders().get(HttpHeaderNames.HOST) + req.getUri() ;
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @param matcher A {@link OutgointRequestProcessorMatcher} implementation. Entity defining matching rules.
* @param request The request
* @return true if the request should be processed
*/
public boolean shouldProcessRequest(OutgointRequestProcessorMatcher matcher, HttpRequest<?> request) {
Optional<String> serviceId = request.getAttribute(HttpAttributes.SERVICE_ID.toString(), String.class);
String uri = request.getUri().toString();
return shouldProcessRequest(matcher, serviceId.orElse(null), uri);
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
ConvertibleMultiValues<String> parameters = source.getParameters();
Argument<T> argument = context.getArgument();
HttpMethod httpMethod = source.getMethod();
boolean permitsRequestBody = HttpMethod.permitsRequestBody(httpMethod);
boolean bindAll = source.getAttribute(HttpAttributes.ROUTE_MATCH, UriMatchInfo.class)
.flatMap(umi -> umi.getVariables()
.stream()
if (!val.isPresent() && !hasAnnotation) {
result = doBind(context, source.getAttributes(), parameterName);
Optional<ConvertibleValues> body = source.getBody(ConvertibleValues.class);
if (body.isPresent()) {
result = doBind(context, body.get(), parameterName);
} else {
return () -> source.getBody(argumentType);
if (source.getBody().isPresent()) {
Optional<String> text = source.getBody(String.class);
if (text.isPresent()) {
return doConvert(text.get(), context);
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
protected void channelRead0(ChannelHandlerContext ctx, io.micronaut.http.HttpRequest<?> request) {
ctx.channel().config().setAutoRead(false);
io.micronaut.http.HttpMethod httpMethod = request.getMethod();
String requestPath = request.getPath();
} else if (uriRoutes.size() == 1) {
UriRouteMatch<Object, Object> establishedRoute = uriRoutes.get(0);
request.setAttribute(HttpAttributes.ROUTE, establishedRoute.getRoute());
request.setAttribute(HttpAttributes.ROUTE_MATCH, establishedRoute);
request.setAttribute(HttpAttributes.URI_TEMPLATE, establishedRoute.getRoute().getUriMatchTemplate().toString());
routeMatch = Optional.of(establishedRoute);
LOG.debug("No matching route found for URI {} and method {}", request.getUri(), httpMethod);
MediaType contentType = request.getContentType().orElse(null);
if (contentType != null) {
if (!existingRouteConsumes.contains(contentType)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Content type not allowed for URI {}, method {}, and content type {}", request.getUri(), httpMethod, contentType);
.findAny(request.getUri().toString())
.map(UriRouteMatch::getHttpMethod)
.collect(Collectors.toSet());
LOG.debug("Method not allowed for URI {} and method {}", request.getUri(), httpMethod);
代码示例来源:origin: micronaut-projects/micronaut-core
ChannelPool channelPool) throws HttpPostRequestEncoder.ErrorDataEncoderException {
io.micronaut.http.HttpRequest<I> finalRequest = requestWrapper.get();
URI requestURI = finalRequest.getUri();
MediaType requestContentType = finalRequest
.getContentType()
.orElse(MediaType.APPLICATION_JSON_TYPE);
boolean permitsBody = io.micronaut.http.HttpMethod.permitsRequestBody(finalRequest.getMethod());
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
default Optional<Locale> getLocale() {
return getHeaders().findFirst(HttpHeaders.ACCEPT_LANGUAGE)
.map((text) -> {
int len = text.length();
if (len == 0 || (len == 1 && text.charAt(0) == '*')) {
return Locale.getDefault().toLanguageTag();
}
if (text.indexOf(';') > -1) {
text = text.split(";")[0];
}
if (text.indexOf(',') > -1) {
text = text.split(",")[0];
}
return text;
})
.map(Locale::forLanguageTag);
}
代码示例来源:origin: micronaut-projects/micronaut-core
private <I> NettyRequestWriter prepareRequest(io.micronaut.http.HttpRequest<I> request, URI requestURI) throws HttpPostRequestEncoder.ErrorDataEncoderException {
MediaType requestContentType = request
.getContentType()
.orElse(MediaType.APPLICATION_JSON_TYPE);
boolean permitsBody = io.micronaut.http.HttpMethod.permitsRequestBody(request.getMethod());
NettyClientHttpRequest clientHttpRequest = (NettyClientHttpRequest) request;
NettyRequestWriter requestWriter = buildNettyRequest(clientHttpRequest, requestURI, requestContentType, permitsBody);
io.netty.handler.codec.http.HttpRequest nettyRequest = requestWriter.getNettyRequest();
prepareHttpHeaders(requestURI, request, nettyRequest, permitsBody, true);
return requestWriter;
}
代码示例来源:origin: micronaut-projects/micronaut-core
private void traceRequest(io.micronaut.http.HttpRequest<?> request, io.netty.handler.codec.http.HttpRequest nettyRequest) {
HttpHeaders headers = nettyRequest.headers();
traceHeaders(headers);
if (io.micronaut.http.HttpMethod.permitsRequestBody(request.getMethod()) && request.getBody().isPresent() && nettyRequest instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
ByteBuf content = fullHttpRequest.content();
if (log.isTraceEnabled()) {
traceBody("Request", content);
}
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
ConvertibleMultiValues<String> parameters = source.getParameters();
Argument<T> argument = context.getArgument();
HttpMethod httpMethod = source.getMethod();
final Optional<UriMatchInfo> matchInfo = source.getAttribute(HttpAttributes.ROUTE_MATCH, UriMatchInfo.class);
boolean bindAll = matchInfo
.flatMap(umi -> umi.getVariables()
代码示例来源:origin: micronaut-projects/micronaut-core
private <I> void prepareHttpHeaders(URI requestURI, io.micronaut.http.HttpRequest<I> request, io.netty.handler.codec.http.HttpRequest nettyRequest, boolean permitsBody, boolean closeConnection) {
HttpHeaders headers = nettyRequest.headers();
headers.set(HttpHeaderNames.HOST, getHostHeader(requestURI));
if (closeConnection) {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
} else {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
if (permitsBody) {
Optional<I> body = request.getBody();
if (body.isPresent()) {
if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) {
MediaType mediaType = request.getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
headers.set(HttpHeaderNames.CONTENT_TYPE, mediaType);
}
if (nettyRequest instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
headers.set(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
} else {
headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
}
} else {
headers.set(HttpHeaderNames.CONTENT_LENGTH, 0);
}
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
registerDefaultAnnotationBinders(byAnnotation);
byType.put(Argument.of(HttpHeaders.class).typeHashCode(), (RequestArgumentBinder<HttpHeaders>) (argument, source) -> () -> Optional.of(source.getHeaders()));
byType.put(Argument.of(HttpRequest.class).typeHashCode(), (RequestArgumentBinder<HttpRequest>) (argument, source) -> () -> Optional.of(source));
byType.put(Argument.of(HttpParameters.class).typeHashCode(), (RequestArgumentBinder<HttpParameters>) (argument, source) -> () -> Optional.of(source.getParameters()));
byType.put(Argument.of(Cookies.class).typeHashCode(), (RequestArgumentBinder<Cookies>) (argument, source) -> () -> Optional.of(source.getCookies()));
byType.put(Argument.of(Cookie.class).typeHashCode(), (RequestArgumentBinder<Cookie>) (context, source) -> {
Cookies cookies = source.getCookies();
String name = context.getArgument().getName();
Cookie cookie = cookies.get(name);
代码示例来源:origin: micronaut-projects/micronaut-core
ArgumentConversionContext conversionContext = ConversionContext.of(
argument,
request.getLocale().orElse(null),
request.getCharacterEncoding()
);
} else if (HttpMethod.requiresRequestBody(request.getMethod()) || argument.isAnnotationPresent(Nullable.class)) {
value = (UnresolvedArgument) () -> {
ArgumentBinder.BindingResult result = argumentBinder.bind(conversionContext, request);
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public Optional<MediaType> getContentType() {
MediaType contentType = this.mediaType;
if (contentType == null) {
synchronized (this) { // double check
contentType = this.mediaType;
if (contentType == null) {
contentType = HttpRequest.super.getContentType().orElse(null);
this.mediaType = contentType;
}
}
}
return Optional.ofNullable(contentType);
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Return a {@link MutableHttpRequest} that executes an {@link HttpMethod#POST} request for the given URI.
*
* @param uri The URI
* @param body The body of the request (content type defaults to {@link MediaType#APPLICATION_JSON}
* @param <T> The body type
* @return The {@link MutableHttpRequest} instance
* @see HttpRequestFactory
*/
static <T> MutableHttpRequest<T> POST(URI uri, T body) {
return POST(uri.toString(), body);
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public HttpMethod getMethod() {
return getDelegate().getMethod();
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public Optional<Principal> getUserPrincipal() {
return originatingRequest.getAttribute("micronaut.AUTHENTICATION", Principal.class);
}
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public O invoke(FunctionDefinition definition, I input, Argument<O> outputType) {
Optional<URI> opt = definition.getURI();
if (!opt.isPresent()) {
throw new FunctionNotFoundException(definition.getName());
} else {
URI uri = opt.get();
HttpRequest request;
if (input == null) {
request = HttpRequest.GET(uri.toString());
} else {
request = HttpRequest.POST(uri.toString(), input);
}
if (input != null && ClassUtils.isJavaLangType(input.getClass())) {
((MutableHttpRequest) request).contentType(MediaType.TEXT_PLAIN_TYPE);
}
Class<O> outputJavaType = outputType.getType();
if (ClassUtils.isJavaLangType(outputJavaType)) {
((MutableHttpRequest) request).accept(MediaType.TEXT_PLAIN_TYPE);
}
if (Publishers.isConvertibleToPublisher(outputJavaType)) {
Publisher publisher = httpClient.retrieve(request, outputType.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT));
return ConversionService.SHARED.convert(publisher, outputType).orElseThrow(() ->
new FunctionExecutionException("Unsupported Reactive type: " + outputJavaType)
);
} else {
return (O) httpClient.toBlocking().retrieve(request, outputType);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!