Java Code Examples for com.linecorp.armeria.common.HttpData#wrap()
The following examples show how to use
com.linecorp.armeria.common.HttpData#wrap() .
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: RestfulJsonResponseConverter.java From centraldogma with Apache License 2.0 | 6 votes |
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers, @Nullable Object resObj, HttpHeaders trailingHeaders) throws Exception { try { final HttpRequest request = RequestContext.current().request(); final HttpData httpData = resObj != null && resObj.getClass() == Object.class ? EMPTY_RESULT : HttpData.wrap(Jackson.writeValueAsBytes(resObj)); final ResponseHeadersBuilder builder = headers.toBuilder(); if (HttpMethod.POST == request.method()) { builder.status(HttpStatus.CREATED); } if (builder.contentType() == null) { builder.contentType(MediaType.JSON_UTF_8); } return HttpResponse.of(builder.build(), httpData, trailingHeaders); } catch (JsonProcessingException e) { return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e); } }
Example 2
Source File: HttpApiResponseConverter.java From centraldogma with Apache License 2.0 | 5 votes |
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers, @Nullable Object resObj, HttpHeaders trailingHeaders) throws Exception { try { final HttpRequest request = RequestContext.current().request(); if (resObj == null || HttpMethod.DELETE == request.method() || (resObj instanceof Iterable && Iterables.size((Iterable<?>) resObj) == 0)) { return HttpResponse.of(HttpStatus.NO_CONTENT); } final ResponseHeaders resHeaders; if (headers.contentType() == null) { final ResponseHeadersBuilder builder = headers.toBuilder(); builder.contentType(MediaType.JSON_UTF_8); resHeaders = builder.build(); } else { resHeaders = headers; } final HttpData httpData = HttpData.wrap(Jackson.writeValueAsBytes(resObj)); return HttpResponse.of(resHeaders, httpData, trailingHeaders); } catch (JsonProcessingException e) { logger.debug("Failed to convert a response:", e); return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e); } }
Example 3
Source File: ByteArrayResponseConverterFunction.java From armeria with Apache License 2.0 | 5 votes |
private static HttpData toHttpData(@Nullable Object value) { if (value instanceof HttpData) { return (HttpData) value; } if (value instanceof byte[]) { return HttpData.wrap((byte[]) value); } if (value == null) { return HttpData.empty(); } throw new IllegalStateException("Failed to convert an object to an HttpData: " + value.getClass().getName()); }
Example 4
Source File: StringResponseConverterFunction.java From armeria with Apache License 2.0 | 5 votes |
private static HttpData toHttpData(@Nullable Object value, Charset charset) { if (value == null) { // To prevent to convert null value to 'null' string. return HttpData.empty(); } if (value instanceof Iterable) { final StringBuilder sb = new StringBuilder(); ((Iterable<?>) value).forEach(v -> { // TODO(trustin): Inefficient double conversion. Time to write HttpDataBuilder? if (v instanceof HttpData) { sb.append(((HttpData) v).toString(charset)); } else if (v instanceof byte[]) { sb.append(new String((byte[]) v, charset)); } else { sb.append(v); } }); return HttpData.of(charset, sb); } if (value instanceof HttpData) { return (HttpData) value; } if (value instanceof byte[]) { return HttpData.wrap((byte[]) value); } return HttpData.of(charset, String.valueOf(value)); }
Example 5
Source File: JacksonResponseConverterFunction.java From armeria with Apache License 2.0 | 5 votes |
private HttpData toJsonHttpData(@Nullable Object value) { try { return HttpData.wrap(mapper.writeValueAsBytes(value)); } catch (Exception e) { return Exceptions.throwUnsafely(e); } }
Example 6
Source File: JsonTextSequences.java From armeria with Apache License 2.0 | 5 votes |
private static HttpData toHttpData(ObjectMapper mapper, @Nullable Object value) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(RECORD_SEPARATOR); mapper.writeValue(out, value); out.write(LINE_FEED); return HttpData.wrap(out.toByteArray()); } catch (Exception e) { return Exceptions.throwUnsafely(e); } }
Example 7
Source File: HttpDecodedResponseTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void unpooledPayload_unpooledDrain() { final HttpData payload = HttpData.wrap(PAYLOAD); final HttpResponse delegate = HttpResponse.of(RESPONSE_HEADERS, payload); final HttpResponse decoded = new HttpDecodedResponse(delegate, DECODERS, ByteBufAllocator.DEFAULT); final ByteBuf buf = responseBuf(decoded, false); assertThat(buf).isInstanceOf(UnpooledHeapByteBuf.class); }
Example 8
Source File: HttpDecodedResponseTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void unpooledPayload_pooledDrain() { final HttpData payload = HttpData.wrap(PAYLOAD); final HttpResponse delegate = HttpResponse.of(RESPONSE_HEADERS, payload); final HttpResponse decoded = new HttpDecodedResponse(delegate, DECODERS, ByteBufAllocator.DEFAULT); final ByteBuf buf = responseBuf(decoded, true); assertThat(buf).isNotInstanceOf(UnpooledHeapByteBuf.class); }
Example 9
Source File: ThriftServiceTest.java From armeria with Apache License 2.0 | 4 votes |
private void invokeTwice(THttpService service1, THttpService service2) throws Exception { final HttpData content = HttpData.wrap(out.getArray(), 0, out.length()); invoke0(service1, content, promise); invoke0(service2, content, promise2); }