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

The following examples show how to use com.linecorp.armeria.common.HttpData#empty() . 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: SamlMetadataServiceFunction.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, AggregatedHttpRequest req,
                          String defaultHostname, SamlPortConfig portConfig) {
    final HttpData metadata = metadataMap.computeIfAbsent(defaultHostname, h -> {
        try {
            final Element element =
                    SamlMessageUtil.serialize(buildMetadataEntityDescriptorElement(h, portConfig));
            final HttpData newMetadata = HttpData.ofUtf8(nodeToString(element));
            logger.debug("SAML service provider metadata has been prepared for: {}.", h);
            return newMetadata;
        } catch (Throwable cause) {
            logger.warn("{} Unexpected metadata request.", ctx, cause);
            return HttpData.empty();
        }
    });

    if (metadata != HttpData.empty()) {
        return HttpResponse.of(HTTP_HEADERS, metadata);
    } else {
        return HttpResponse.of(HttpStatus.NOT_FOUND);
    }
}
 
Example 2
Source File: ZlibStreamDecoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpData finish() {
    if (decoder.finish()) {
        return fetchDecoderOutput();
    } else {
        return HttpData.empty();
    }
}
 
Example 3
Source File: ZlibStreamDecoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
private HttpData fetchDecoderOutput() {
    ByteBuf decoded = null;
    for (;;) {
        final ByteBuf buf = decoder.readInbound();
        if (buf == null) {
            break;
        }
        if (!buf.isReadable()) {
            buf.release();
            continue;
        }
        if (decoded == null) {
            decoded = buf;
        } else {
            try {
                decoded.writeBytes(buf);
            } finally {
                buf.release();
            }
        }
    }

    if (decoded == null) {
        return HttpData.empty();
    }

    return PooledHttpData.wrap(decoded);
}
 
Example 4
Source File: ByteArrayResponseConverterFunction.java    From armeria with Apache License 2.0 5 votes vote down vote up
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 5
Source File: StringResponseConverterFunction.java    From armeria with Apache License 2.0 5 votes vote down vote up
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 6
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 7
Source File: ServerSentEvents.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static <T> HttpData toHttpData(
        Function<? super T, ? extends ServerSentEvent> converter, T content) {
    final ServerSentEvent sse = converter.apply(content);
    return sse == null ? HttpData.empty() : toHttpData(sse);
}