Skip to content

Instantly share code, notes, and snippets.

@leonardinius
Created January 10, 2011 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardinius/773173 to your computer and use it in GitHub Desktop.
Save leonardinius/773173 to your computer and use it in GitHub Desktop.
import com.atlassian.jira.ComponentManager;
import com.atlassian.plugin.webresource.WebResourceManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import javax.servlet.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class IncludeResourcesFilter implements Filter
{
// ------------------------------ FIELDS ------------------------------
private static final String DEPENDENCIES = "resources";
private static final char SEPARATOR_CHAR = ';';
private List<String> resourceDependencies = Collections.emptyList();
// --------------------- GETTER / SETTER METHODS ---------------------
public List<String> getResourceDependencies()
{
return resourceDependencies;
}
// ------------------------ INTERFACE METHODS ------------------------
// --------------------- Interface Filter ---------------------
public void init(final FilterConfig filterConfig) throws ServletException
{
resourceDependencies = ImmutableList.copyOf(getDependencies(filterConfig));
}
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain)
throws IOException, ServletException
{
doIncludeResources(getWebResourceManager(), getResourceDependencies());
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy()
{
}
// -------------------------- OTHER METHODS --------------------------
private void doIncludeResources(WebResourceManager webResourceManager, Iterable<String> resourceDependencies)
{
for (String dependency : resourceDependencies)
{
webResourceManager.requireResource(dependency);
}
}
private List<String> getDependencies(FilterConfig filterConfig)
{
List<String> dependencies = Lists.newArrayListWithCapacity(2);
for (String dependency : getResourceDependencies(filterConfig))
{
if (StringUtils.isNotBlank(dependency))
{
dependencies.add(dependency);
}
}
return dependencies;
}
private List<String> getResourceDependencies(FilterConfig filterConfig)
{
return Arrays.asList(
StringUtils.split(StringUtils.defaultString(filterConfig.getInitParameter(DEPENDENCIES)), SEPARATOR_CHAR));
}
private WebResourceManager getWebResourceManager() {return ComponentManager.getInstance().getWebResourceManager();}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment