All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.propagation.carriers.ServletRequestAttrsInjectAdapter Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the Javax Servlet package.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules.propagation.carriers;

import com.undefinedlabs.scope.jdk.reflection.ReflectionContext;
import io.opentracing.propagation.TextMap;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

public class ServletRequestAttrsInjectAdapter implements TextMap {

  private final Object servletRequest;

  // Must be child of ServletRequest
  public ServletRequestAttrsInjectAdapter(final Object servletRequest) {
    this.servletRequest = servletRequest;
  }

  @Override
  public Iterator> iterator() {
    throw new UnsupportedOperationException("Should be used only with tracer#inject()");
  }

  @Override
  public void put(String key, String value) {
    try {
      final ReflectionContext reflectionContext = ReflectionContext.INSTANCE;
      final String requestClassName = this.servletRequest.getClass().getName();
      final ClassLoader requestClassLoader = this.servletRequest.getClass().getClassLoader();
      final Method setAttributeMethod =
          reflectionContext.getScopeMethod(
              requestClassName, requestClassLoader, "setAttribute", String.class, Object.class);

      setAttributeMethod.invoke(servletRequest, key, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy