Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active December 22, 2015 16:09
Show Gist options
  • Save kevin-lee/6497632 to your computer and use it in GitHub Desktop.
Save kevin-lee/6497632 to your computer and use it in GitHub Desktop.
A solution or at least a workaround to the problem described at https://github.com/webjars/jquery/pull/8 for Spring framework users. To use this, you shouldn't use <mvc:resources> or ResourceHandler. It works for both name-version.number.extension and name.extension. e.g.) /webjars/jquery/1.10.2/jquery-1.10.2.min.map, /webjars/jquery/1.10.2/jque…
/**
* Copyright 2013 Lee, Seong Hyun (Kevin)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lckymn.kevin.lee.spring.mvc;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author Lee, SeongHyun (Kevin)
* @version 0.0.1 (2010-08-21)
*/
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter
{
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/real-webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
/**
* Copyright 2013 Lee, Seong Hyun (Kevin)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lckymn.kevin.lee.spring.mvc;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author Lee, SeongHyun (Kevin)
* @version 0.0.1 (2010-09-09)
*/
@Controller
public class WebJarsController
{
private static final String PATH_TO_ACCESS_WEB_JARS_RESOURCE = "/real-webjars";
private static final Pattern VERSION_PATTERN = Pattern.compile("^[\\d]+([\\.][\\d]+)*");
private static final String WEB_JARS = "/webjars";
private static final int WEB_JARS_LENGTH = WEB_JARS.length();
private void forwardToWebJarResource(final String path, final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException
{
request.getRequestDispatcher(path)
.forward(request, response);
}
private String getProjectAndVersion(final HttpServletRequest request, final String name)
{
final String uri = request.getRequestURI();
final int start = uri.indexOf(WEB_JARS) + WEB_JARS_LENGTH;
final int end = uri.lastIndexOf(name);
final String projectAndVersion = uri.substring(start, end);
return projectAndVersion;
}
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = { WEB_JARS
+ "/**/{name}-{nameRest}.min.{extension}" })
protected final void getWebJarsMinifiedResourceWithOrWithoutVersion(@PathVariable final String name,
@PathVariable final String nameRest, @PathVariable final String extension, final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException
{
final String projectAndVersion = getProjectAndVersion(request, name);
/* @formatter:off */
final String path =
VERSION_PATTERN.matcher(nameRest)
.find() ?
MessageFormat.format(PATH_TO_ACCESS_WEB_JARS_RESOURCE + "{0}{1}.min.{2}",
projectAndVersion, name, extension) :
MessageFormat.format(PATH_TO_ACCESS_WEB_JARS_RESOURCE + "{0}{1}-{2}.min.{3}",
projectAndVersion, name, nameRest, extension);
/* @formatter:on */
forwardToWebJarResource(path, request, response);
}
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = { WEB_JARS
+ "/**/{name}-{nameRest}.{extension}" })
protected final void getWebJarsResourceWithOrWithoutVersion(@PathVariable final String name,
@PathVariable final String nameRest, @PathVariable final String extension, final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException
{
final String projectAndVersion = getProjectAndVersion(request, name);
/* @formatter:off */
final String path =
VERSION_PATTERN.matcher(nameRest)
.find() ?
MessageFormat.format(PATH_TO_ACCESS_WEB_JARS_RESOURCE + "{0}{1}.{2}",
projectAndVersion, name, extension) :
MessageFormat.format(PATH_TO_ACCESS_WEB_JARS_RESOURCE + "{0}{1}-{2}.{3}",
projectAndVersion, name, nameRest, extension);
/* @formatter:on */
forwardToWebJarResource(path, request, response);
}
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = { WEB_JARS
+ "/**/{name:.*[^-][\\D]+.*}.{extension}" })
protected final void getWebJarsResource(@PathVariable final String name, @PathVariable final String extension,
final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
{
final String projectAndVersion = getProjectAndVersion(request, name);
final String path =
MessageFormat.format(PATH_TO_ACCESS_WEB_JARS_RESOURCE + "{0}{1}.{2}", projectAndVersion, name, extension);
forwardToWebJarResource(path, request, response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment