/*
 * Copyright (c) 2008. All rights reserved.
 */
package ro.isdc.wro.http;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.isdc.wro.config.Context;
import ro.isdc.wro.config.jmx.WroConfiguration;
import ro.isdc.wro.http.support.ServletContextAttributeHelper;

/** This is a copy of ro.isdc.wro.http.FixedWroContextFilter with a fix for https://code.google.com/p/wro4j/issues/detail?id=824
 * This class should be removed when that bug is fixed
 * @author candrews
 *
 */
public class FixedWroContextFilter
  implements Filter {
  private FilterConfig filterConfig;

  /**
   * {@inheritDoc}
   */
  public void init(final FilterConfig filterConfig)
      throws ServletException {
    this.filterConfig = filterConfig;
  }
  
  /**
   * {@inheritDoc}
   */
  public final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
      throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;
    Context.set(Context.webContext(request, response, this.filterConfig), getWroConfiguration());
    final String correlationId = Context.getCorrelationId();
    try {
      chain.doFilter(request, response);
    } finally {
      Context.setCorrelationId(correlationId);
      Context.unset();
    }
  }

  /**
   * @return the {@link WroConfiguration} extracted from {@link ServletContext} if exist or default one otherwise. 
   */
  private WroConfiguration getWroConfiguration() {
    final WroConfiguration configAttribute = getServletContextAttributeHelper().getWroConfiguration();
    final WroConfiguration config = configAttribute != null ? configAttribute : new WroConfiguration();
    return config;
  }

  /**
   * @VisibleForTesting
   * @return the instance responsible for {@link WroConfiguration} lookup.
   */
  ServletContextAttributeHelper getServletContextAttributeHelper() {
    return ServletContextAttributeHelper.create(filterConfig);
  }
  
  /**
   * {@inheritDoc}
   */
  public void destroy() {
    Context.destroy();
  }
}

/*
 * Copyright (c) 2008. All rights reserved.
 */
package ro.isdc.wro.http;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.isdc.wro.config.Context;
import ro.isdc.wro.config.jmx.WroConfiguration;
import ro.isdc.wro.http.support.ServletContextAttributeHelper;

/** This is a copy of ro.isdc.wro.http.FixedWroContextFilter with a fix for https://code.google.com/p/wro4j/issues/detail?id=824
 * This class should be removed when that bug is fixed
 * @author candrews
 *
 */
public class FixedWroContextFilter
  implements Filter {
  private FilterConfig filterConfig;

  /**
   * {@inheritDoc}
   */
  public void init(final FilterConfig filterConfig)
      throws ServletException {
    this.filterConfig = filterConfig;
  }
  
  /**
   * {@inheritDoc}
   */
  public final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
      throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;
    final String oldCorrelationId = Context.isContextSet()?Context.getCorrelationId():null;
    Context.set(Context.webContext(request, response, this.filterConfig), getWroConfiguration());
    final String correlationId = Context.getCorrelationId();
    try {
      chain.doFilter(request, response);
    } finally {
      Context.setCorrelationId(correlationId);
      Context.unset();
      if(oldCorrelationId != null) Context.setCorrelationId(oldCorrelationId);
    }
  }

  /**
   * @return the {@link WroConfiguration} extracted from {@link ServletContext} if exist or default one otherwise. 
   */
  private WroConfiguration getWroConfiguration() {
    final WroConfiguration configAttribute = getServletContextAttributeHelper().getWroConfiguration();
    final WroConfiguration config = configAttribute != null ? configAttribute : new WroConfiguration();
    return config;
  }

  /**
   * @VisibleForTesting
   * @return the instance responsible for {@link WroConfiguration} lookup.
   */
  ServletContextAttributeHelper getServletContextAttributeHelper() {
    return ServletContextAttributeHelper.create(filterConfig);
  }
  
  /**
   * {@inheritDoc}
   */
  public void destroy() {
    Context.destroy();
  }
}

Use the same fix as I just submitted to wro4j for issue https://code.google.com/p/wro4j/issues/detail?id=824