@Override
 protected void startContexts(Manager manager) {
   super.startContexts(manager);
   manager.getContext(RequestContext.class).activate(UUID.randomUUID().toString());
 }
  @Override
  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      throws IOException, ServletException {
    if (req instanceof HttpServletRequest && resp instanceof HttpServletResponse) {
      HttpServletRequest httpReq = ((HttpServletRequest) req);
      HttpServletResponse httpResp = ((HttpServletResponse) resp);

      String requestEnrichment = httpReq.getHeader(ENRICHMENT_REQUEST);

      if (requestEnrichment != null && !"null".equals(requestEnrichment)) {

        final AtomicReference<NonWritingServletOutputStream> stream =
            new AtomicReference<NonWritingServletOutputStream>();
        final AtomicReference<NonWritingPrintWriter> writer =
            new AtomicReference<NonWritingPrintWriter>();

        HttpServletResponseWrapper responseWrapper =
            new HttpServletResponseWrapper((HttpServletResponse) resp) {
              @Override
              public ServletOutputStream getOutputStream() throws IOException {
                stream.set(new NonWritingServletOutputStream());
                return stream.get();
              }

              @Override
              public PrintWriter getWriter() throws IOException {
                writer.set(NonWritingPrintWriter.newInstance());
                return writer.get();
              }
            };

        String responseEnrichment = "null";

        try {
          RequestPayload requestPayload =
              SerializationUtils.deserializeFromBase64(requestEnrichment);
          ServerAssertion serverAssertion = requestPayload.getAssertion();

          ManagerBuilder builder =
              ManagerBuilder.from().extension(Class.forName(DEFAULT_EXTENSION_CLASS));
          Manager manager = builder.create();
          manager.start();
          manager.bind(ApplicationScoped.class, Manager.class, manager);
          manager.inject(this);

          req.setAttribute(
              WarpCommons.LIFECYCLE_MANAGER_STORE_REQUEST_ATTRIBUTE, lifecycleManagerStore);

          manager.fire(new BeforeSuite());
          manager.fire(new BeforeRequest(req));
          lifecycleManagerStore.get().bind(ServletRequest.class, req);

          assertionRegistry.get().registerAssertion(serverAssertion);

          lifecycleManager.get().fireLifecycleEvent(new BeforeServletEvent());

          try {
            chain.doFilter(req, responseWrapper);

            lifecycleManager.get().fireLifecycleEvent(new AfterServletEvent());

            // request successfully finished
            TestResult firstFailedResult = testResultStore.get().getFirstFailed();
            if (firstFailedResult == null) {
              ResponsePayload responsePayload = new ResponsePayload(serverAssertion);
              responseEnrichment = SerializationUtils.serializeToBase64(responsePayload);
              httpResp.setHeader(ENRICHMENT_RESPONSE, responseEnrichment);
            } else {
              Throwable throwable = firstFailedResult.getThrowable();
              if (throwable instanceof InvocationTargetException) {
                throwable = throwable.getCause();
              }
              ResponsePayload responsePayload = new ResponsePayload(throwable);
              enrichResponse(httpResp, responsePayload);
            }
          } catch (Exception e) {
            log.log(Level.SEVERE, "The error occured during request execution", e);
            throw e;
          } finally {
            assertionRegistry.get().unregisterAssertion(serverAssertion);

            lifecycleManagerStore.get().unbind(ServletRequest.class, req);
            manager.fire(new AfterRequest(req));
            manager.fire(new AfterSuite());
          }

        } catch (Throwable e) {
          // exception occured during request execution
          ResponsePayload responsePayload = new ResponsePayload(e);
          responseEnrichment = SerializationUtils.serializeToBase64(responsePayload);
          httpResp.setHeader(ENRICHMENT_RESPONSE, responseEnrichment);
          httpResp.sendError(500);
        }

        if (writer.get() != null) {
          writer.get().finallyWriteAndClose(resp.getOutputStream());
        }
        if (stream.get() != null) {
          stream.get().finallyWriteAndClose(resp.getOutputStream());
        }

        return;
      }
    }

    chain.doFilter(req, resp);
  }