Skip to content

Instantly share code, notes, and snippets.

@digulla
Last active October 17, 2017 08:12
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 digulla/f010cdc2f409c31a0a8dc3a1bc4259ec to your computer and use it in GitHub Desktop.
Save digulla/f010cdc2f409c31a0a8dc3a1bc4259ec to your computer and use it in GitHub Desktop.
Variant of StandaloneServletContextUriLocator from wro4j which can also load from web resources from the classpath.
package ch.swissquant.gkb.ui.wro;
import static org.apache.commons.lang3.Validate.notNull;
import static org.apache.commons.lang3.Validate.validState;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.manager.factory.standalone.StandaloneContext;
import ro.isdc.wro.manager.factory.standalone.StandaloneContextAware;
import ro.isdc.wro.model.resource.locator.ServletContextUriLocator;
import ro.isdc.wro.util.WroUtil;
/**
* An extension of {@link ServletContextUriLocator} which is used for standalone (or build-time) solution. This class
* existed initially as an anonymous class, but was extracted since it is complex enough to exist on its own.
*
* @author Alex Objelean
* @created 28 Sep 2013
* @since 1.7.2
*/
public class StandaloneServletContextUriLocator2
extends ServletContextUriLocator implements StandaloneContextAware {
private StandaloneContext standaloneContext;
private static final Logger LOG = LoggerFactory.getLogger(StandaloneServletContextUriLocator2.class);
public StandaloneServletContextUriLocator2() {
}
/**
* This implementation will try to locate the provided resource inside contextFolder configured by standaloneContext.
* If a resource cannot be located, the next contextFolder from the list will be tried. The first successful result
* will be returned.
*/
@Override
public InputStream locate(final String uri)
throws IOException {
validState(standaloneContext != null, "Locator was not initialized properly. StandaloneContext missing.");
Exception lastException = null;
final String[] contextFolders = standaloneContext.getContextFolders();
for(final String contextFolder : contextFolders) {
try {
return locateStreamWithContextFolder(uri, contextFolder);
} catch(final IOException e) {
lastException = e;
LOG.debug("Could not locate: {} using contextFolder: {}", uri, contextFolder);
}
}
final String metaInfPath = "META-INF/resources/" + makeRelative(uri);
List<URL> urls = Collections.list(standaloneContext.getClass().getClassLoader().getResources(metaInfPath));
if (urls.isEmpty()) {
final String exceptionMessage = String.format(
"No valid resource '%s' found inside any of contextFolders: %s or inside classpath:%s",
uri,
Arrays.toString(standaloneContext.getContextFolders()),
metaInfPath);
throw new IOException(exceptionMessage, lastException);
}
if (urls.size() > 1) {
LOG.warn("Found several resources when looking for {}, loading the first one: {}", metaInfPath, urls);
}
LOG.debug("Located {} here: {}", uri, urls.get(0));
return urls.get(0).openStream();
}
/**
* TODO this is duplicated code (from super) -> find a way to reuse it.
*/
private InputStream locateStreamWithContextFolder(final String uri, final String contextFolder)
throws IOException, FileNotFoundException {
if (getWildcardStreamLocator().hasWildcard(uri)) {
final String fullPath = WroUtil.getFullPath(uri);
final String realPath = contextFolder + fullPath;
return getWildcardStreamLocator().locateStream(uri, new File(realPath));
}
final String uriWithoutPrefix = makeRelative(uri);
final File file = new File(contextFolder, uriWithoutPrefix);
LOG.debug("Opening file: " + file.getAbsolutePath());
return new FileInputStream(file);
}
/** Remove leading slash */
private String makeRelative(String uri) {
if (uri.startsWith("/")) {
return uri.substring(1);
}
return uri;
}
public void initialize(final StandaloneContext standaloneContext) {
notNull(standaloneContext);
this.standaloneContext = standaloneContext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment