Java Code Examples for com.linecorp.armeria.common.ResponseHeaders#builder()
The following examples show how to use
com.linecorp.armeria.common.ResponseHeaders#builder() .
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: JettyService.java From armeria with Apache License 2.0 | 6 votes |
private static ResponseHeaders toResponseHeaders(ArmeriaHttpTransport transport) { final MetaData.Response info = transport.info; if (info == null) { throw new IllegalStateException("response metadata unavailable"); } final ResponseHeadersBuilder headers = ResponseHeaders.builder(); headers.status(info.getStatus()); info.getFields().forEach(e -> headers.add(HttpHeaderNames.of(e.getName()), e.getValue())); if (transport.method != HttpMethod.HEAD) { headers.setLong(HttpHeaderNames.CONTENT_LENGTH, transport.contentLength); } return headers.build(); }
Example 2
Source File: TestConverters.java From armeria with Apache License 2.0 | 6 votes |
private static HttpResponse httpResponse(HttpData data) { final HttpResponseWriter res = HttpResponse.streaming(); final long current = System.currentTimeMillis(); final ResponseHeadersBuilder headers = ResponseHeaders.builder(HttpStatus.OK); headers.setInt(HttpHeaderNames.CONTENT_LENGTH, data.length()); headers.setTimeMillis(HttpHeaderNames.DATE, current); final MediaType contentType = ServiceRequestContext.current().negotiatedResponseMediaType(); if (contentType != null) { headers.contentType(contentType); } res.write(headers.build()); res.write(data); res.close(); return res; }
Example 3
Source File: TomcatService.java From armeria with Apache License 2.0 | 5 votes |
private static ResponseHeaders convertResponse(Response coyoteRes) { final ResponseHeadersBuilder headers = ResponseHeaders.builder(); headers.status(coyoteRes.getStatus()); final String contentType = coyoteRes.getContentType(); if (contentType != null && !contentType.isEmpty()) { headers.set(HttpHeaderNames.CONTENT_TYPE, contentType); } final long contentLength = coyoteRes.getBytesWritten(true); // 'true' will trigger flush. final String method = coyoteRes.getRequest().method().toString(); if (!"HEAD".equals(method)) { headers.setLong(HttpHeaderNames.CONTENT_LENGTH, contentLength); } final MimeHeaders cHeaders = coyoteRes.getMimeHeaders(); final int numHeaders = cHeaders.size(); for (int i = 0; i < numHeaders; i++) { final AsciiString name = toHeaderName(cHeaders.getName(i)); if (name == null) { continue; } final String value = toHeaderValue(cHeaders.getValue(i)); if (value == null) { continue; } headers.add(name.toLowerCase(), value); } return headers.build(); }
Example 4
Source File: CorsService.java From armeria with Apache License 2.0 | 5 votes |
/** * Handles CORS preflight by setting the appropriate headers. * * @param req the decoded HTTP request */ private HttpResponse handleCorsPreflight(ServiceRequestContext ctx, HttpRequest req) { final ResponseHeadersBuilder headers = ResponseHeaders.builder(HttpStatus.OK); final CorsPolicy policy = setCorsOrigin(ctx, req, headers); if (policy != null) { policy.setCorsAllowMethods(headers); policy.setCorsAllowHeaders(headers); policy.setCorsAllowCredentials(headers); policy.setCorsMaxAge(headers); policy.setCorsPreflightResponseHeaders(headers); } return HttpResponse.of(headers.build()); }
Example 5
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers. */ public static HttpHeaders toArmeria(Http2Headers headers, boolean request, boolean endOfStream) { final HttpHeadersBuilder builder; if (request) { builder = headers.contains(HttpHeaderNames.METHOD) ? RequestHeaders.builder() : HttpHeaders.builder(); } else { builder = headers.contains(HttpHeaderNames.STATUS) ? ResponseHeaders.builder() : HttpHeaders.builder(); } toArmeria(builder, headers, endOfStream); return builder.build(); }
Example 6
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * Converts the headers of the given Netty HTTP/1.x response into Armeria HTTP/2 headers. */ public static ResponseHeaders toArmeria(HttpResponse in) { final io.netty.handler.codec.http.HttpHeaders inHeaders = in.headers(); final ResponseHeadersBuilder out = ResponseHeaders.builder(); out.sizeHint(inHeaders.size()); out.add(HttpHeaderNames.STATUS, HttpStatus.valueOf(in.status().code()).codeAsText()); // Add the HTTP headers which have not been consumed above toArmeria(inHeaders, out); return out.build(); }
Example 7
Source File: AnnotatedServiceFactory.java From armeria with Apache License 2.0 | 4 votes |
/** * Returns a list of {@link AnnotatedService} instances. A single {@link AnnotatedService} is * created per each {@link Route} associated with the {@code method}. */ @VisibleForTesting static List<AnnotatedServiceElement> create(String pathPrefix, Object object, Method method, List<RequestConverterFunction> baseRequestConverters, List<ResponseConverterFunction> baseResponseConverters, List<ExceptionHandlerFunction> baseExceptionHandlers) { final Set<Annotation> methodAnnotations = httpMethodAnnotations(method); if (methodAnnotations.isEmpty()) { throw new IllegalArgumentException("HTTP Method specification is missing: " + method.getName()); } final Class<?> clazz = object.getClass(); final Map<HttpMethod, List<String>> httpMethodPatternsMap = getHttpMethodPatternsMap(method, methodAnnotations); final String computedPathPrefix = computePathPrefix(clazz, pathPrefix); final Set<MediaType> consumableMediaTypes = consumableMediaTypes(method, clazz); final Set<MediaType> producibleMediaTypes = producibleMediaTypes(method, clazz); final List<Route> routes = httpMethodPatternsMap.entrySet().stream().flatMap( pattern -> { final HttpMethod httpMethod = pattern.getKey(); final List<String> pathMappings = pattern.getValue(); return pathMappings.stream().map( pathMapping -> Route.builder() .path(computedPathPrefix, pathMapping) .methods(httpMethod) .consumes(consumableMediaTypes) .produces(producibleMediaTypes) .matchesParams( predicates(method, clazz, MatchesParam.class, MatchesParam::value)) .matchesHeaders( predicates(method, clazz, MatchesHeader.class, MatchesHeader::value)) .build()); }).collect(toImmutableList()); final List<RequestConverterFunction> req = getAnnotatedInstances(method, clazz, RequestConverter.class, RequestConverterFunction.class) .addAll(baseRequestConverters).build(); final List<ResponseConverterFunction> res = getAnnotatedInstances(method, clazz, ResponseConverter.class, ResponseConverterFunction.class) .addAll(baseResponseConverters).build(); final List<ExceptionHandlerFunction> eh = getAnnotatedInstances(method, clazz, ExceptionHandler.class, ExceptionHandlerFunction.class) .addAll(baseExceptionHandlers).add(defaultExceptionHandler).build(); final ResponseHeadersBuilder defaultHeaders = ResponseHeaders.builder(defaultResponseStatus(method)); final HttpHeadersBuilder defaultTrailers = HttpHeaders.builder(); final String classAlias = clazz.getName(); final String methodAlias = String.format("%s.%s()", classAlias, method.getName()); setAdditionalHeader(defaultHeaders, clazz, "header", classAlias, "class", AdditionalHeader.class, AdditionalHeader::name, AdditionalHeader::value); setAdditionalHeader(defaultHeaders, method, "header", methodAlias, "method", AdditionalHeader.class, AdditionalHeader::name, AdditionalHeader::value); setAdditionalHeader(defaultTrailers, clazz, "trailer", classAlias, "class", AdditionalTrailer.class, AdditionalTrailer::name, AdditionalTrailer::value); setAdditionalHeader(defaultTrailers, method, "trailer", methodAlias, "method", AdditionalTrailer.class, AdditionalTrailer::name, AdditionalTrailer::value); if (defaultHeaders.status().isContentAlwaysEmpty() && !defaultTrailers.isEmpty()) { logger.warn("A response with HTTP status code '{}' cannot have a content. " + "Trailers defined at '{}' might be ignored if HTTP/1.1 is used.", defaultHeaders.status().code(), methodAlias); } final ResponseHeaders responseHeaders = defaultHeaders.build(); final HttpHeaders responseTrailers = defaultTrailers.build(); final boolean useBlockingTaskExecutor = AnnotationUtil.findFirst(method, Blocking.class) != null; return routes.stream().map(route -> { final List<AnnotatedValueResolver> resolvers = getAnnotatedValueResolvers(req, route, method, clazz); return new AnnotatedServiceElement( route, new AnnotatedService(object, method, resolvers, eh, res, route, responseHeaders, responseTrailers, useBlockingTaskExecutor), decorator(method, clazz)); }).collect(toImmutableList()); }