// Path: opentracing-jaxrs2/src/main/java/io/opentracing/contrib/jaxrs2/internal/CastUtils.java
// public class CastUtils {
//     private static final Logger log = Logger.getLogger(CastUtils.class.getName());
// 
//     private CastUtils() {}
// 
//     /**
//      * Casts given object to the given class.
//      *
//      * @param object
//      * @param clazz
//      * @param <T>
//      * @return casted object, or null if there is any error
//      */
//     public static <T> T cast(Object object, Class<T> clazz) {
//         if (object == null || clazz == null) {
//             return null;
//         }
// 
//         try {
//             return clazz.cast(object);
//         } catch (ClassCastException ex) {
//             log.severe("Cannot cast to " + clazz.getName());
//             return null;
//         }
//     }
// }
// 
// Path: opentracing-jaxrs2/src/main/java/io/opentracing/contrib/jaxrs2/internal/SpanWrapper.java
// public class SpanWrapper {
// 
//     public static final String PROPERTY_NAME = SpanWrapper.class.getName() + ".activeSpanWrapper";
// 
//     private Scope scope;
//     private Span span;
//     private AtomicBoolean finished = new AtomicBoolean();
// 
//     public SpanWrapper(Span span, Scope scope) {
//         this.span = span;
//         this.scope = scope;
// 
//     }
// 
//     public Span get() {
//         return span;
//     }
// 
//     public Scope getScope() {
//         return scope;
//     }
// 
//     public void finish() {
//         if (finished.compareAndSet(false, true)) {
//             span.finish();
//         }
//     }
// 
//     public boolean isFinished() {
//         return finished.get();
//     }
// }


import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.tag.Tags;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
package io.opentracing.contrib.jaxrs2.server;


/**
 * Filter which finishes span after server processing. It is required to be registered.
 *
 * @author Pavol Loffay
 */
public class SpanFinishingFilter implements Filter {

  public SpanFinishingFilter() {
  }

  /**
   * @param tracer
   * @deprecated use no-args constructor
   */
  @Deprecated
  public SpanFinishingFilter(Tracer tracer){
  }

  @Override
  public void init(FilterConfig filterConfig) {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse)response;
    HttpServletRequest httpRequest = (HttpServletRequest)request;

    try {
      chain.doFilter(request, response);
    } catch (Exception ex) {
// Path: opentracing-jaxrs2/src/main/java/io/opentracing/contrib/jaxrs2/internal/CastUtils.java
// public class CastUtils {
//     private static final Logger log = Logger.getLogger(CastUtils.class.getName());
// 
//     private CastUtils() {}
// 
//     /**
//      * Casts given object to the given class.
//      *
//      * @param object
//      * @param clazz
//      * @param <T>
//      * @return casted object, or null if there is any error
//      */
//     public static <T> T cast(Object object, Class<T> clazz) {
//         if (object == null || clazz == null) {
//             return null;
//         }
// 
//         try {
//             return clazz.cast(object);
//         } catch (ClassCastException ex) {
//             log.severe("Cannot cast to " + clazz.getName());
//             return null;
//         }
//     }
// }
// 
// Path: opentracing-jaxrs2/src/main/java/io/opentracing/contrib/jaxrs2/internal/SpanWrapper.java
// public class SpanWrapper {
// 
//     public static final String PROPERTY_NAME = SpanWrapper.class.getName() + ".activeSpanWrapper";
// 
//     private Scope scope;
//     private Span span;
//     private AtomicBoolean finished = new AtomicBoolean();
// 
//     public SpanWrapper(Span span, Scope scope) {
//         this.span = span;
//         this.scope = scope;
// 
//     }
// 
//     public Span get() {
//         return span;
//     }
// 
//     public Scope getScope() {
//         return scope;
//     }
// 
//     public void finish() {
//         if (finished.compareAndSet(false, true)) {
//             span.finish();
//         }
//     }
// 
//     public boolean isFinished() {
//         return finished.get();
//     }
// }


// Path: opentracing-jaxrs2/src/main/java/io/opentracing/contrib/jaxrs2/server/SpanFinishingFilter.java
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.tag.Tags;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

package io.opentracing.contrib.jaxrs2.server;


/**
 * Filter which finishes span after server processing. It is required to be registered.
 *
 * @author Pavol Loffay
 */
public class SpanFinishingFilter implements Filter {

  public SpanFinishingFilter() {
  }

  /**
   * @param tracer
   * @deprecated use no-args constructor
   */
  @Deprecated
  public SpanFinishingFilter(Tracer tracer){
  }

  @Override
  public void init(FilterConfig filterConfig) {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse)response;
    HttpServletRequest httpRequest = (HttpServletRequest)request;

    try {
      chain.doFilter(request, response);
    } catch (Exception ex) {
      SpanWrapper spanWrapper = getSpanWrapper(httpRequest);