Java Code Examples for com.linecorp.armeria.common.HttpHeadersBuilder#build()
The following examples show how to use
com.linecorp.armeria.common.HttpHeadersBuilder#build() .
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: ArmeriaServerCall.java From armeria with Apache License 2.0 | 6 votes |
static HttpHeaders statusToTrailers( ServiceRequestContext ctx, Status status, Metadata metadata, boolean headersSent) { final HttpHeadersBuilder trailers = GrpcTrailersUtil.statusToTrailers( status.getCode().value(), status.getDescription(), headersSent); MetadataUtil.fillHeaders(metadata, trailers); if (ctx.config().verboseResponses() && status.getCause() != null) { final ThrowableProto proto = GrpcStatus.serializeThrowable(status.getCause()); trailers.add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN, Base64.getEncoder().encodeToString(proto.toByteArray())); } final HttpHeaders additionalTrailers = ctx.additionalResponseTrailers(); ctx.mutateAdditionalResponseTrailers(HttpHeadersBuilder::clear); trailers.add(additionalTrailers); return trailers.build(); }
Example 2
Source File: HttpHeadersUtil.java From armeria with Apache License 2.0 | 6 votes |
public static HttpHeaders mergeTrailers(HttpHeaders headers, HttpHeaders additionalTrailers) { if (additionalTrailers.isEmpty()) { return headers; } if (headers.isEmpty()) { return additionalTrailers; } final HttpHeadersBuilder builder = headers.toBuilder(); for (AsciiString name : additionalTrailers.names()) { if (!ADDITIONAL_RESPONSE_HEADER_BLACKLIST.contains(name) && !isTrailerBlacklisted(name)) { builder.remove(name); additionalTrailers.forEachValue(name, value -> builder.add(name, value)); } } return builder.build(); }
Example 3
Source File: HeaderSanitizer.java From curiostack with MIT License | 5 votes |
@Override public HttpHeaders apply(HttpHeaders entries) { HttpHeadersBuilder builder = entries.toBuilder(); blacklistedHeaders.forEach(builder::remove); customSanitizers.forEach(c -> c.accept(builder)); return builder.build(); }
Example 4
Source File: ArmeriaClientCall.java From armeria with Apache License 2.0 | 5 votes |
@Nullable private static HttpHeaders parseGrpcWebTrailers(ByteBuf buf) { final HttpHeadersBuilder trailers = HttpHeaders.builder(); while (buf.readableBytes() > 0) { int start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE); if (start == -1) { return null; } int endExclusive; if (buf.getByte(start) == ':') { // We need to skip the pseudoheader colon when searching for the separator. buf.skipBytes(1); endExclusive = buf.forEachByte(FIND_COLON); buf.readerIndex(start); } else { endExclusive = buf.forEachByte(FIND_COLON); } if (endExclusive == -1) { return null; } final CharSequence name = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8); buf.readerIndex(endExclusive + 1); start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE); buf.readerIndex(start); endExclusive = buf.forEachByte(ByteProcessor.FIND_CRLF); final CharSequence value = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8); trailers.add(name, value.toString()); start = buf.forEachByte(ByteProcessor.FIND_NON_CRLF); if (start != -1) { buf.readerIndex(start); } else { // Nothing but CRLF remaining, we're done. buf.skipBytes(buf.readableBytes()); } } return trailers.build(); }
Example 5
Source File: DefaultClientRequestContext.java From armeria with Apache License 2.0 | 5 votes |
@Override public void mutateAdditionalRequestHeaders(Consumer<HttpHeadersBuilder> mutator) { requireNonNull(mutator, "mutator"); for (;;) { final HttpHeaders oldValue = additionalRequestHeaders; final HttpHeadersBuilder builder = oldValue.toBuilder(); mutator.accept(builder); final HttpHeaders newValue = builder.build(); if (additionalRequestHeadersUpdater.compareAndSet(this, oldValue, newValue)) { return; } } }
Example 6
Source File: DefaultServiceRequestContext.java From armeria with Apache License 2.0 | 5 votes |
private void mutateAdditionalResponseHeaders( AtomicReferenceFieldUpdater<DefaultServiceRequestContext, HttpHeaders> atomicUpdater, Consumer<HttpHeadersBuilder> mutator) { for (;;) { final HttpHeaders oldValue = atomicUpdater.get(this); final HttpHeadersBuilder builder = oldValue.toBuilder(); mutator.accept(builder); final HttpHeaders newValue = builder.build(); if (atomicUpdater.compareAndSet(this, oldValue, newValue)) { return; } } }
Example 7
Source File: CorsPolicy.java From armeria with Apache License 2.0 | 5 votes |
/** * Generates immutable HTTP response headers that should be added to a CORS preflight response. * * @return {@link HttpHeaders} the HTTP response headers to be added. */ public HttpHeaders generatePreflightResponseHeaders() { final HttpHeadersBuilder headers = HttpHeaders.builder(); preflightResponseHeaders.forEach((key, value) -> { final Object val = getValue(value); if (val instanceof Iterable) { headers.addObject(key, (Iterable<?>) val); } else { headers.addObject(key, val); } }); return headers.build(); }
Example 8
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 9
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers. */ public static HttpHeaders toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders) { if (inHeaders.isEmpty()) { return HttpHeaders.of(); } final HttpHeadersBuilder out = HttpHeaders.builder(); out.sizeHint(inHeaders.size()); toArmeria(inHeaders, out); return out.build(); }
Example 10
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()); }