Java Code Examples for com.linecorp.armeria.common.RequestContext#currentOrNull()

The following examples show how to use com.linecorp.armeria.common.RequestContext#currentOrNull() . 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: ServiceRequestContext.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the server-side context of the {@link Request} that is being handled in the current thread.
 * If the context is a {@link ClientRequestContext}, {@link ClientRequestContext#root()} is returned.
 *
 * @return the {@link ServiceRequestContext} available in the current thread,
 *         or {@code null} if unavailable.
 */
@Nullable
static ServiceRequestContext currentOrNull() {
    final RequestContext ctx = RequestContext.currentOrNull();
    if (ctx == null) {
        return null;
    }

    final ServiceRequestContext root = ctx.root();
    if (root != null) {
        return root;
    }

    throw new IllegalStateException(
            "The current context is not a server-side context and does not have a root " +
            "which means that the context is not invoked by a server request. ctx: " + ctx);
}
 
Example 2
Source File: ClientRequestContext.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the client-side context of the {@link Request} that is being handled in the current thread.
 *
 * @return the {@link ClientRequestContext} available in the current thread, or {@code null} if unavailable.
 * @throws IllegalStateException if the current context is not a {@link ClientRequestContext}.
 */
@Nullable
static ClientRequestContext currentOrNull() {
    final RequestContext ctx = RequestContext.currentOrNull();
    if (ctx == null) {
        return null;
    }
    checkState(ctx instanceof ClientRequestContext,
               "The current context is not a client-side context: %s", ctx);
    return (ClientRequestContext) ctx;
}
 
Example 3
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public String get(String key) {
    final RequestContext ctx = RequestContext.currentOrNull();
    if (ctx != null) {
        final String value = RequestScopedMdc.get(ctx, key);
        if (value != null) {
            return value;
        }
    }

    return delegate.get(key);
}
 
Example 4
Source File: DefaultClientRequestContext.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nullable
private static ServiceRequestContext serviceRequestContext() {
    final RequestContext current = RequestContext.currentOrNull();
    return current != null ? current.root() : null;
}
 
Example 5
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> getCopyOfContextMap() {
    final Map<String, String> threadLocalMap = getDelegateContextMap();
    final RequestContext ctx = RequestContext.currentOrNull();
    if (ctx == null) {
        // No context available
        if (threadLocalMap != null) {
            return maybeCloneThreadLocalMap(threadLocalMap);
        } else {
            return Object2ObjectMaps.emptyMap();
        }
    }

    // Retrieve the request-scoped properties.
    // Note that this map is 1) unmodifiable and shared 2) or modifiable yet unshared,
    // which means it's OK to return as it is or mutate it.
    final Map<String, String> requestScopedMap = getAll(ctx);
    if (threadLocalMap == null || threadLocalMap.isEmpty()) {
        // No thread-local map available
        return requestScopedMap;
    }

    // Thread-local map available
    if (requestScopedMap.isEmpty()) {
        // Only thread-local map available
        return maybeCloneThreadLocalMap(threadLocalMap);
    }

    // Both thread-local and request-scoped map available
    final Object2ObjectOpenHashMap<String, String> merged;
    if (requestScopedMap instanceof Object2ObjectOpenHashMap) {
        // Reuse the mutable copy returned by getAll() for less memory footprint.
        merged = (Object2ObjectOpenHashMap<String, String>) requestScopedMap;
        threadLocalMap.forEach(merged::putIfAbsent);
    } else {
        merged = new Object2ObjectOpenHashMap<>(threadLocalMap.size() + requestScopedMap.size());
        merged.putAll(threadLocalMap);
        merged.putAll(requestScopedMap);
    }
    return merged;
}
 
Example 6
Source File: ClientRequestContextTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static void assertCurrentCtx(@Nullable RequestContext ctx) {
    final RequestContext current = RequestContext.currentOrNull();
    assertThat(current).isSameAs(ctx);
}
 
Example 7
Source File: ServiceRequestContextTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static void assertCurrentCtx(@Nullable RequestContext ctx) {
    final RequestContext current = RequestContext.currentOrNull();
    assertThat(current).isSameAs(ctx);
}
 
Example 8
Source File: RequestContextExporter.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link Map} whose key is an export key set through {@code add*()} in
 * {@link RequestContextExporterBuilder} and value is extracted from {@link RequestContext}.
 * Note that this method returns an empty {@link Map} if current {@link RequestContext} is {@code null}.
 */
public Map<String, String> export() {
    final RequestContext ctx = RequestContext.currentOrNull();
    return ctx != null ? export(ctx) : ImmutableMap.of();
}