/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.ows;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.geoserver.ows.util.EncodingInfo;
import org.geoserver.ows.util.XmlCharsetDetector;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/**
* Controller which publishes files through a web interface from the classpath
* <p>
* To use this controller, it should be mapped to a particular url in the url mapping of the spring
* dispatcher servlet. Example:
*
* <pre>
* <code>
* <bean id="filePublisher" class="org.geoserver.ows.FilePublisher"/>
* <bean id="dispatcherMappings"
* <property name="alwaysUseFullPath" value="true"/>
* class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
* <property name="mappings">
* <prop key="/schemas/** /*.xsd">filePublisher</prop>
* <prop key="/schemas/** /*.dtd">filePublisher</prop>
* <prop key="/styles/*">filePublisher</prop>
* </property>
* </bean>
* </code>
* </pre>
*
* @author Justin Deoliveira, The Open Planning Project
* @author Andrea Aime - GeoSolutions
*/
public class ClasspathPublisher extends AbstractURLPublisher {
Class clazz;
/**
* Creates the new classpath publisher.
*
* @param clazz the class used to perform classpath lookups with
* {@link Class#getResource(String)}
*/
public ClasspathPublisher(Class clazz) {
this.clazz = clazz;
}
public ClasspathPublisher() {
this.clazz = ClasspathPublisher.class;
}
@Override
protected URL getUrl(HttpServletRequest request) {
String ctxPath = request.getContextPath();
String reqPath = request.getRequestURI();
reqPath = reqPath.substring(ctxPath.length());
// try a few lookups
URL url = clazz.getResource(reqPath);
if (url == null && !reqPath.startsWith("/")) {
url = clazz.getResource("/");
}
if (url == null && (reqPath.length() > 1) && reqPath.startsWith("/")) {
url = clazz.getResource(reqPath.substring(1));
}
return url;
}
}