/** * Converts the specified formatted string into a US-ASCII {@link HttpData}. The string is formatted by * {@link String#format(Locale, String, Object...)} with {@linkplain Locale#ENGLISH English locale}. * * @param format {@linkplain Formatter the format string} of the response content * @param args the arguments referenced by the format specifiers in the format string * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if {@code format} is empty. */ static HttpData ofAscii(String format, Object... args) { return of(StandardCharsets.US_ASCII, format, args); }
@Override public HttpData decode(HttpData obj) { if (obj instanceof ByteBufHolder) { decoder.writeInbound(((ByteBufHolder) obj).content()); } else { final ByteBuf compressed = Unpooled.wrappedBuffer(obj.array(), obj.offset(), obj.length()); decoder.writeInbound(compressed); } return HttpData.of(fetchDecoderOutput()); }
/** * Converts the specified {@code text} into a US-ASCII {@link HttpData}. * * @param text the {@link String} to convert * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if the length of {@code text} is 0. */ static HttpData ofAscii(String text) { return of(StandardCharsets.US_ASCII, text); }
/** * Converts the specified {@code text} into a UTF-8 {@link HttpData}. * * @param text the {@link String} to convert * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if the length of {@code text} is 0. */ static HttpData ofUtf8(String text) { return of(StandardCharsets.UTF_8, text); }
/** * Converts the specified formatted string into a UTF-8 {@link HttpData}. The string is formatted by * {@link String#format(Locale, String, Object...)} with {@linkplain Locale#ENGLISH English locale}. * * @param format {@linkplain Formatter the format string} of the response content * @param args the arguments referenced by the format specifiers in the format string * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if {@code format} is empty. */ static HttpData ofUtf8(String format, Object... args) { return of(StandardCharsets.UTF_8, format, args); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, byte[] content) { requireNonNull(content, "content"); return of(method, path, mediaType, HttpData.of(content)); }
private HttpData toJsonHttpData(@Nullable Object value) { try { return HttpData.of(mapper.writeValueAsBytes(value)); } catch (Exception e) { return Exceptions.throwUnsafely(e); } }
private HttpData toJsonSequencesHttpData(@Nullable Object value) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(RECORD_SEPARATOR); mapper.writeValue(out, value); out.write(LINE_FEED); return HttpData.of(out.toByteArray()); } catch (Exception e) { return Exceptions.throwUnsafely(e); } } }
/** * Creates a new HTTP response of the specified {@link HttpStatus} and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response */ static HttpResponse of(HttpStatus status, MediaType mediaType, byte[] content) { return of(status, mediaType, HttpData.of(content)); }
/** * Converts the specified Netty {@link ByteBuf} into an {@link HttpData}. Unlike {@link #of(byte[])}, this * method makes a copy of the {@link ByteBuf}. * * @return a new {@link HttpData}. {@link #EMPTY_DATA} if the readable bytes of {@code buf} is 0. */ static HttpData of(ByteBuf buf) { requireNonNull(buf, "buf"); if (!buf.isReadable()) { return EMPTY_DATA; } return of(ByteBufUtil.getBytes(buf)); }
/** * Creates a new HTTP response of the specified {@link HttpStatus} and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response * @param offset the start offset of {@code content} * @param length the length of {@code content} */ static HttpResponse of(HttpStatus status, MediaType mediaType, byte[] content, int offset, int length) { return of(status, mediaType, HttpData.of(content, offset, length)); }
/** * Creates a new HTTP response of the specified {@link HttpStatus}. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response */ static AggregatedHttpMessage of(HttpStatus status, MediaType mediaType, byte[] content) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(content, "content"); return of(status, mediaType, HttpData.of(content)); }
@Override public HttpData finish() { if (decoder.finish()) { return HttpData.of(fetchDecoderOutput()); } else { return HttpData.EMPTY_DATA; } }
/** * Sends an HTTP request with the specified headers and content. */ default HttpResponse execute(HttpHeaders headers, byte[] content) { return execute(AggregatedHttpMessage.of(headers, HttpData.of(content))); }
/** * Sends an HTTP request with the specified headers and content. */ default HttpResponse execute(HttpHeaders headers, String content, Charset charset) { return execute(AggregatedHttpMessage.of(headers, HttpData.of(charset, content))); }
void setContent(byte[] content, MediaType mediaType) { assert file == HttpFile.nonExistent(); file = HttpFileBuilder.of(HttpData.of(content)) .setHeader(HttpHeaderNames.CONTENT_TYPE, mediaType) .build(); } }
@Test public void shouldGetNotFound() { protocols.forEach(scheme -> { final HttpClient client = HttpClient.of(clientFactory, scheme + "://example.com:" + port); assertThat(client.get("/route2").aggregate().join().status()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(client.execute( HttpHeaders.of(HttpMethod.POST, "/route2") .contentType(com.linecorp.armeria.common.MediaType.PLAIN_TEXT_UTF_8), HttpData.of("text".getBytes())).aggregate().join().status()) .isEqualTo(HttpStatus.NOT_FOUND); }); } }
@Test public void deframe_multipleMessagesAfterRequests() throws Exception { deframer.request(2); deframer.deframe(HttpData.of(GrpcTestUtil.uncompressedFrame(GrpcTestUtil.requestByteBuf())), false); deframer.deframe(HttpData.of(GrpcTestUtil.uncompressedFrame(GrpcTestUtil.requestByteBuf())), false); assertThat(deframer.isStalled()).isTrue(); verifyAndReleaseMessage(new ByteBufOrStream(GrpcTestUtil.requestByteBuf()), 2); verifyNoMoreInteractions(listener); }
private void invokeTwice(THttpService service1, THttpService service2) throws Exception { final HttpData content = HttpData.of(out.getArray(), 0, out.length()); invoke0(service1, content, promise); invoke0(service2, content, promise2); }
@Test public void deframe_hasRequests() throws Exception { deframer.request(1); deframer.deframe(HttpData.of(GrpcTestUtil.uncompressedFrame(GrpcTestUtil.requestByteBuf())), false); verifyAndReleaseMessage(new ByteBufOrStream(GrpcTestUtil.requestByteBuf())); verifyNoMoreInteractions(listener); assertThat(deframer.isStalled()).isTrue(); }