Java Code Examples for com.linecorp.armeria.common.HttpData#ofUtf8()

The following examples show how to use com.linecorp.armeria.common.HttpData#ofUtf8() . 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: CentralDogma.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
static HttpFile webAppTitleFile(@Nullable String webAppTitle, String hostname) {
    requireNonNull(hostname, "hostname");
    final Map<String, String> titleAndHostname = ImmutableMap.of(
            "title", firstNonNull(webAppTitle, "Central Dogma at {{hostname}}"),
            "hostname", hostname);

    try {
        final HttpData data = HttpData.ofUtf8(Jackson.writeValueAsString(titleAndHostname));
        return HttpFile.builder(data)
                       .contentType(MediaType.JSON_UTF_8)
                       .cacheControl(ServerCacheControl.REVALIDATED)
                       .build();
    } catch (JsonProcessingException e) {
        throw new Error("Failed to encode the title and hostname:", e);
    }
}
 
Example 2
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingNettyDataBufferFactory_HttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT));

    final HttpData httpData1 = HttpData.ofUtf8("abc");

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
    assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne();

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).content())
            .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer());
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
}
 
Example 3
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingDefaultDataBufferFactory_HttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new DefaultDataBufferFactory());

    final HttpData httpData1 = HttpData.ofUtf8("abc");

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
    assertThat(buffer.asByteBuffer()).isEqualTo(ByteBuffer.wrap("abc".getBytes()));

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
    assertThat(ByteBufUtil.getBytes(((PooledHttpData) httpData2).content())).isEqualTo("abc".getBytes());
}
 
Example 4
Source File: HttpPostBindingUtil.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link HttpData} which holds a SSO form.
 */
static HttpData getSsoForm(String remoteEndpointUrl,
                           String paramName, String paramValue,
                           @Nullable String relayState) {
    requireNonNull(remoteEndpointUrl, "remoteEndpointUrl");
    requireNonNull(paramName, "paramName");
    requireNonNull(paramValue, "paramValue");

    final StringBuilder html = new StringBuilder();
    html.append(XHTML.get(0))
        .append(XHTML.get(1)).append(HTML_ESCAPER.escape(remoteEndpointUrl)).append(XHTML.get(2))
        .append(XHTML.get(3)).append(HTML_ESCAPER.escape(paramName))
        .append(XHTML.get(4)).append(HTML_ESCAPER.escape(paramValue)).append(XHTML.get(5));

    if (relayState != null) {
        html.append(XHTML.get(3)).append(RELAY_STATE)
            .append(XHTML.get(4)).append(HTML_ESCAPER.escape(relayState)).append(XHTML.get(5));
    }
    html.append(XHTML.get(6));

    return HttpData.ofUtf8(html.toString());
}
 
Example 5
Source File: MessageConverterService.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse convertResponse(
        ServiceRequestContext ctx, ResponseHeaders headers,
        @Nullable Object result, HttpHeaders trailers) throws Exception {

    if (result instanceof Response) {
        final Response response = (Response) result;
        final HttpData body = HttpData.ofUtf8(response.result() + ':' + response.from());
        return HttpResponse.of(headers, body, trailers);
    }
    return ResponseConverterFunction.fallthrough();
}
 
Example 6
Source File: Http1RequestDecoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
private void fail(int id, HttpResponseStatus status, @Nullable HttpData content) {
    discarding = true;
    req = null;

    final HttpData data = content != null ? content : HttpData.ofUtf8(status.toString());
    final ResponseHeaders headers =
            ResponseHeaders.builder()
                           .status(status.code())
                           .set(HttpHeaderNames.CONNECTION, "close")
                           .setObject(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8)
                           .setInt(HttpHeaderNames.CONTENT_LENGTH, data.length())
                           .build();
    writer.writeHeaders(id, 1, headers, false);
    writer.writeData(id, 1, data, true).addListener(ChannelFutureListener.CLOSE);
}
 
Example 7
Source File: AutoIndex.java    From armeria with Apache License 2.0 5 votes vote down vote up
static HttpData listingToHtml(String dirPath, String mappedDirPath, List<String> listing) {
    final Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
    final Escaper urlEscaper = UrlEscapers.urlFragmentEscaper();
    final String escapedDirPath = htmlEscaper.escape(dirPath);
    final StringBuilder buf = new StringBuilder(listing.size() * 64);
    buf.append(PART1);
    buf.append(escapedDirPath);
    buf.append(PART2);
    buf.append(escapedDirPath);
    buf.append(PART3);
    buf.append(listing.size());
    buf.append(PART4);
    if (!"/".equals(mappedDirPath)) {
        buf.append("<li class=\"directory parent\"><a href=\"../\">../</a></li>\n");
    }
    for (String name : listing) {
        buf.append("<li class=\"");
        if (name.charAt(name.length() - 1) == '/') {
            buf.append("directory");
        } else {
            buf.append("file");
        }
        buf.append("\"><a href=\"");
        buf.append(urlEscaper.escape(name));
        buf.append("\">");
        buf.append(name);
        buf.append("</a></li>\n");
    }
    buf.append(PART5);
    return HttpData.ofUtf8(buf.toString());
}
 
Example 8
Source File: ServerSentEvents.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static HttpData toHttpData(ServerSentEvent sse) {
    final StringBuilder sb = new StringBuilder();

    // Write a comment first because a user might want to explain his or her event at first line.
    final String comment = sse.comment();
    if (comment != null) {
        appendField(sb, "", comment, false);
    }

    final String id = sse.id();
    if (id != null) {
        appendField(sb, "id", id, true);
    }

    final String event = sse.event();
    if (event != null) {
        appendField(sb, "event", event, true);
    }

    final String data = sse.data();
    if (data != null) {
        appendField(sb, "data", data, true);
    }

    final Duration retry = sse.retry();
    if (retry != null) {
        // Reconnection time, in milliseconds.
        sb.append("retry:").append(retry.toMillis()).append(LINE_FEED);
    }

    return sb.length() == 0 ? HttpData.empty()
                            : HttpData.ofUtf8(sb.append(LINE_FEED).toString());
}
 
Example 9
Source File: JavascriptStaticService.java    From curiostack with MIT License 4 votes vote down vote up
@Inject
public JavascriptStaticService(Config globalConfig) {
  String rendered =
      globalConfig.getObject("javascriptConfig").render(ConfigRenderOptions.concise());
  response = HttpData.ofUtf8("var CONFIG = " + rendered + ";");
}