All Downloads are FREE. Search and download functionalities are using the official Maven repository.

li.rudin.mavenjs.servlet.handler.impl.ResourceHandler Maven / Gradle / Ivy

package li.rudin.mavenjs.servlet.handler.impl;

import java.io.InputStream;
import java.net.URL;

import li.rudin.mavenjs.servlet.Servlet;
import li.rudin.mavenjs.servlet.handler.Handler;
import li.rudin.mavenjs.servlet.handler.HandlerResult;

public class ResourceHandler implements Handler
{
	private URL getResource(String name)
	{
		URL url = Thread.currentThread().getContextClassLoader().getResource(name);
		
		if (url == null && name.length() > 1)
			//try without leading /
			url = Thread.currentThread().getContextClassLoader().getResource(name.substring(1));
		
		if (url == null)
			//try locally
			url = Servlet.class.getResource(name);
		
		return url;
	}
	
	@Override
	public HandlerResult processRequest(String url) throws Exception
	{
		URL resourceUrl = getResource(url);
		
		if (resourceUrl != null)
		{
			InputStream stream = resourceUrl.openStream();
			
			//TODO: proper mime type
			String contentType = "text/html";
			if (url.endsWith(".js"))
				contentType = "text/javascript";
			else if (url.endsWith(".css"))
				contentType = "text/css";

			return new HandlerResult(contentType, stream);
		}

		return null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy