Index: ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/src/main/java/org/xwiki/container/servlet/filters/WebJarFilter.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/src/main/java/org/xwiki/container/servlet/filters/WebJarFilter.java (revision ) +++ ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/src/main/java/org/xwiki/container/servlet/filters/WebJarFilter.java (revision ) @@ -0,0 +1,102 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.container.servlet.filters; + +import java.io.IOException; +import java.io.InputStream; + +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 org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.xwiki.classloader.ClassLoaderManager; +import org.xwiki.classloader.NamespaceURLClassLoader; +import org.xwiki.component.manager.ComponentLookupException; +import org.xwiki.component.manager.ComponentManager; + +public class WebJarFilter implements Filter +{ + public static final String WEBJAR_ACTION = "webjar"; + + public static final String WEBJAR_PATH = String.format("/%s/", WEBJAR_ACTION); + + public static final String WEBJAR_RESOURCE_PREFIX = "META-INF/resources/webjars/"; + + private NamespaceURLClassLoader classLoader; + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException + { + if (!(request instanceof HttpServletRequest)) { + chain.doFilter(request, response); + return; + } + + // The format is /xwiki/bin/webjar/, e.g. angular.js> and it'll be looked in the classpath as: + // "/META-INF/resources/webjars/" + + // Extract the resource name from the URL + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + String resourceName = StringUtils.substringAfter(httpServletRequest.getRequestURL().toString(), WEBJAR_PATH); + + InputStream resourceStream = this.classLoader.getResourceAsStream( + String.format("%s%s", WEBJAR_RESOURCE_PREFIX, resourceName)); + + if (resourceStream != null) { + try { + IOUtils.copy(resourceStream, response.getOutputStream()); + } catch (IOException e) { + throw new IOException(String.format("Failed to read resource [%s ", resourceName), e); + } finally { + IOUtils.closeQuietly(resourceStream); + } + } + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException + { + ComponentManager componentManager = + (ComponentManager) filterConfig.getServletContext().getAttribute(ComponentManager.class.getName()); + + ClassLoaderManager classLoaderManager; + try { + classLoaderManager = componentManager.getInstance(ClassLoaderManager.class); + } catch (ComponentLookupException e) { + throw new ServletException("Failed to locate ClasLoader Manager component required to initialize the " + + "WebJar Filter", e); + } + + this.classLoader = classLoaderManager.getURLClassLoader("wiki:xwiki", false); + } + + @Override + public void destroy() + { + // Nothing to do + } +} Index: ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/pom.xml (date 1375281727000) +++ ../xwiki-platform/xwiki-platform-core/xwiki-platform-containers/xwiki-platform-container-servlet/pom.xml (revision ) @@ -58,6 +58,11 @@ ${commons.version} + org.xwiki.commons + xwiki-commons-classloader-api + ${commons.version} + + org.xwiki.platform xwiki-platform-url-api ${project.version} Index: ../xwiki-platform/xwiki-platform-core/xwiki-platform-web/src/main/webapp/WEB-INF/web.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ../xwiki-platform/xwiki-platform-core/xwiki-platform-web/src/main/webapp/WEB-INF/web.xml (date 1375281727000) +++ ../xwiki-platform/xwiki-platform-core/xwiki-platform-web/src/main/webapp/WEB-INF/web.xml (revision ) @@ -104,6 +104,11 @@ + + WebJarFilter + org.xwiki.container.servlet.filters.WebJarFilter + + @@ -169,6 +174,11 @@ REQUEST INCLUDE FORWARD + + + + WebJarFilter + /webjar/*