@Override public void serialize(MediaType mediaType, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(mediaType.toString()); } }
@Override public HttpHeaders contentType(MediaType contentType) { requireNonNull(contentType, "contentType"); this.contentType = contentType; return set(HttpHeaderNames.CONTENT_TYPE, contentType.toString()); }
@Nullable @Override public MediaType contentType() { final String contentTypeString = get(HttpHeaderNames.CONTENT_TYPE); if (contentTypeString == null) { return null; } final MediaType contentType = this.contentType; if (contentType != null && Ascii.equalsIgnoreCase(contentType.toString(), contentTypeString.trim())) { return contentType; } try { this.contentType = MediaType.parse(contentTypeString); return this.contentType; } catch (IllegalArgumentException unused) { // Invalid media type return null; } }
private HttpHeaders addCommonHeaders(HttpHeaders headers, HttpFileAttributes attrs, @Nullable String etag) { if (contentType != null) { headers.set(HttpHeaderNames.CONTENT_TYPE, contentType.toString()); } if (dateEnabled) { headers.setTimeMillis(HttpHeaderNames.DATE, clock.millis()); } if (lastModifiedEnabled) { headers.setTimeMillis(HttpHeaderNames.LAST_MODIFIED, attrs.lastModifiedMillis()); } if (etag != null) { headers.set(HttpHeaderNames.ETAG, '\"' + etag + '\"'); } if (!this.headers.isEmpty()) { headers.setAll(this.headers); } return headers; }
@Test public void splitHeaders() throws Exception { when(armeriaCall.tryFinish()).thenReturn(true); final ManualMockCallback callback = new ManualMockCallback(); final BlockingCallSubscriber subscriber = new BlockingCallSubscriber( armeriaCall, callback, new Request.Builder().url("http://bar.com").build()); subscriber.onSubscribe(subscription); subscriber.onNext(HttpHeaders.of(100)); subscriber.onNext(HttpHeaders.of(200)); subscriber.onNext(HttpHeaders.of(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString())); subscriber.onNext(HttpData.ofUtf8("bar")); subscriber.onComplete(); verify(subscription).request(Long.MAX_VALUE); assertThat(callback.callbackCallingCount).isEqualTo(1); assertThat(callback.response.header("content-type")).isEqualToIgnoringCase("text/plain; charset=utf-8"); assertThat(callback.response.body().string()).isEqualTo("bar"); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { response.setStatusCode(HttpStatus.OK); response.getHeaders().add(HttpHeaderNames.CONTENT_TYPE.toString(), MediaType.PLAIN_TEXT_UTF_8.toString()); return response.writeWith(request.getBody()); } }
@Test public void splitHeaders() throws Exception { when(armeriaCall.tryFinish()).thenReturn(true); final ManualMockCallback callback = new ManualMockCallback(); final StreamingCallSubscriber subscriber = new StreamingCallSubscriber( armeriaCall, callback, new Request.Builder().url("http://bar.com").build(), MoreExecutors.directExecutor()); subscriber.onSubscribe(subscription); subscriber.onNext(HttpHeaders.of(100)); subscriber.onNext(HttpHeaders.of(200)); subscriber.onNext(HttpHeaders.of(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString())); subscriber.onNext(HttpData.ofUtf8("bar")); subscriber.onComplete(); verify(subscription, times(4)).request(1L); await().untilAsserted(() -> assertThat(callback.callbackCallingCount).isEqualTo(1)); await().untilAsserted(() -> assertThat(callback.response.header("content-type")) .isEqualToIgnoringCase("text/plain; charset=utf-8")); await().untilAsserted(() -> assertThat(callback.response.body().string()).isEqualTo("bar")); }
private void writeErrorResponse(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status) throws Http2Exception { final byte[] content = status.toString().getBytes(StandardCharsets.UTF_8); writer.writeHeaders( ctx, streamId, new DefaultHttp2Headers(false) .status(status.codeAsText()) .set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) .setInt(HttpHeaderNames.CONTENT_LENGTH, content.length), 0, false, ctx.voidPromise()); writer.writeData(ctx, streamId, Unpooled.wrappedBuffer(content), 0, true, ctx.voidPromise()); final Http2Stream stream = writer.connection().stream(streamId); if (stream != null && writer.flowController().hasFlowControlled(stream)) { // Ensure to flush the error response if it's flow-controlled so that it is sent // before an RST_STREAM frame. writer.flowController().writePendingBytes(); } }
private static void makeUnframedRequest(String name) throws Exception { final HttpClient client = new ClientBuilder(server.uri(SerializationFormat.NONE, "/")) .factory(clientFactory) .addHttpHeader(HttpHeaderNames.CONTENT_TYPE, MediaType.PROTOBUF.toString()) .build(HttpClient.class); final SimpleRequest request = SimpleRequest.newBuilder() .setPayload(Payload.newBuilder() .setBody(ByteString.copyFromUtf8(name))) .build(); try { client.post("/armeria.grpc.testing.TestService/UnaryCall2", request.toByteArray()); } catch (Throwable t) { // Ignore, we will count these up } } }
public static AggregatedHttpMessage login(HttpClient client, String username, String password) { return client.execute( HttpHeaders.of(HttpHeaderNames.METHOD, "POST", HttpHeaderNames.PATH, "/api/v1/login", HttpHeaderNames.CONTENT_TYPE, MediaType.FORM_DATA.toString()), "grant_type=password&username=" + username + "&password=" + password, StandardCharsets.US_ASCII).aggregate().join(); }