Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Restlet v2 Message#getAttributes calls #6796

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,67 @@

package io.opentelemetry.instrumentation.restlet.v2_0.internal;

import static java.util.Collections.emptySet;

import io.opentelemetry.context.propagation.TextMapGetter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nullable;
import org.restlet.Message;
import org.restlet.Request;
import org.restlet.util.Series;

final class RestletHeadersGetter implements TextMapGetter<Request> {

private static final MethodHandle GET_ATTRIBUTES;

static {
MethodHandle getAttributes = null;

MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
getAttributes =
lookup.findVirtual(Message.class, "getAttributes", MethodType.methodType(Map.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
// changed the return type to ConcurrentMap in version 2.1
try {
getAttributes =
lookup.findVirtual(
Message.class, "getAttributes", MethodType.methodType(ConcurrentMap.class));
} catch (NoSuchMethodException | IllegalAccessException ex) {
// ignored
}
}

GET_ATTRIBUTES = getAttributes;
}

@Override
public Iterable<String> keys(Request carrier) {
return getHeaders(carrier).getNames();
Series<?> headers = getHeaders(carrier);
return headers == null ? emptySet() : headers.getNames();
}

@Override
public String get(Request carrier, String key) {
Series<?> headers = getHeaders(carrier);
return headers.getFirstValue(key, /* ignoreCase = */ true);
return headers == null ? null : headers.getFirstValue(key, /* ignoreCase = */ true);
}

@SuppressWarnings("unchecked")
@Nullable
static Series<?> getHeaders(Message carrier) {
return (Series<?>) carrier.getAttributes().get("org.restlet.http.headers");
if (GET_ATTRIBUTES == null) {
return null;
}
try {
Map<String, Object> attributes = (Map<String, Object>) GET_ATTRIBUTES.invoke(carrier);
return (Series<?>) attributes.get("org.restlet.http.headers");
} catch (Throwable e) {
return null;
}
}
}