import javax.servlet.http.HttpSession;
import com.vaadin.DefaultDeploymentConfiguration;
+import com.vaadin.sass.ScssStylesheet;
import com.vaadin.server.AbstractCommunicationManager.Callback;
import com.vaadin.server.ServletPortletHelper.ApplicationClassException;
import com.vaadin.server.VaadinSession.SessionStartEvent;
throws IOException, ServletException {
final ServletContext sc = getServletContext();
- URL resourceUrl = sc.getResource(filename);
- if (resourceUrl == null) {
- // try if requested file is found from classloader
-
- // strip leading "/" otherwise stream from JAR wont work
- filename = filename.substring(1);
- resourceUrl = getVaadinService().getClassLoader().getResource(
- filename);
+ URL resourceUrl = findResourceURL(filename, sc);
- if (resourceUrl == null) {
+ if (resourceUrl == null) {
+ // File not found, if this was a css request we still look for a
+ // scss file with the same name
+ if (serveOnTheFlyCompiledScss(filename, request, response, sc)) {
+ return;
+ } else {
// cannot serve requested file
getLogger()
.info("Requested resource ["
+ "] not found from filesystem or through class loader."
+ " Add widgetset and/or theme JAR to your classpath or add files to WebContent/VAADIN folder.");
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
- return;
}
+ return;
+ }
- // security check: do not permit navigation out of the VAADIN
- // directory
- if (!isAllowedVAADINResourceUrl(request, resourceUrl)) {
- getLogger()
- .info("Requested resource ["
- + filename
- + "] not accessible in the VAADIN directory or access to it is forbidden.");
- response.setStatus(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
+ // security check: do not permit navigation out of the VAADIN
+ // directory
+ if (!isAllowedVAADINResourceUrl(request, resourceUrl)) {
+ getLogger()
+ .info("Requested resource ["
+ + filename
+ + "] not accessible in the VAADIN directory or access to it is forbidden.");
+ response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+ return;
}
// Find the modification timestamp
is.close();
}
+ private URL findResourceURL(String filename, ServletContext sc)
+ throws MalformedURLException {
+ URL resourceUrl = sc.getResource(filename);
+ if (resourceUrl == null) {
+ // try if requested file is found from classloader
+
+ // strip leading "/" otherwise stream from JAR wont work
+ if (filename.startsWith("/")) {
+ filename = filename.substring(1);
+ }
+
+ resourceUrl = getVaadinService().getClassLoader().getResource(
+ filename);
+ }
+ return resourceUrl;
+ }
+
+ private boolean serveOnTheFlyCompiledScss(String filename,
+ HttpServletRequest request, HttpServletResponse response,
+ ServletContext sc) throws IOException {
+ if (getVaadinService().getDeploymentConfiguration().isProductionMode()) {
+ // This is not meant for production mode.
+ return false;
+ }
+
+ if (!filename.endsWith(".css")) {
+ return false;
+ }
+
+ String scssFilename = filename.substring(0, filename.length() - 4)
+ + ".scss";
+ URL scssUrl = findResourceURL(scssFilename, sc);
+ if (scssUrl == null) {
+ // Is a css request but no scss file was found
+ return false;
+ }
+ // security check: do not permit navigation out of the VAADIN
+ // directory
+ if (!isAllowedVAADINResourceUrl(request, scssUrl)) {
+ getLogger()
+ .info("Requested resource ["
+ + filename
+ + "] not accessible in the VAADIN directory or access to it is forbidden.");
+ response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+ // Handled, return true so no further processing is done
+ return true;
+ }
+ String realFilename = sc.getRealPath(scssFilename);
+ ScssStylesheet scss = ScssStylesheet.get(realFilename);
+ if (scss == null) {
+ getLogger()
+ .warning(
+ "Scss file "
+ + scssFilename
+ + " exists but ScssStylesheet was not able to find it");
+ return false;
+ }
+ try {
+ getLogger()
+ .fine("Compiling " + realFilename + " for request to "
+ + filename);
+ scss.compile();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ // This is for development mode only so instruct the browser to never
+ // cache it
+ response.setHeader("Cache-Control", "no-cache");
+ final String mimetype = getVaadinService().getMimeType(filename);
+ writeResponse(response, mimetype, scss.toString());
+
+ return true;
+ }
+
/**
* Check whether a URL obtained from a classloader refers to a valid static
* resource in the directory VAADIN.