/*
 * #%L
 * netrelay
 * %%
 * Copyright (C) 2015 Braintags GmbH
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */
package de.braintags.netrelay.controller;

import java.util.Properties;

import de.braintags.netrelay.routing.RouterDefinition;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;

/**
 * A controller to define serving of static contents. Internally a {@link StaticHandler} is used
 * <br>
 * Config-Parameter:<br/>
 * <UL>
 * <LI>{@value #CACHE_ENABLED_PROPERTY}<br/>
 * <LI>{@value #CACHE_TIMEOUT_PROPERTY}<br/>
 * </UL>
 * <br>
 * Request-Parameter:<br/>
 * <br/>
 * Result-Parameter:<br/>
 * <br/>
 *
 * @author Michael Remme
 */
public class StaticController extends AbstractController {
  private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory
      .getLogger(StaticController.class);

  /**
   * The property, by which one can switch on / off the caching of static contents
   */
  public static final String CACHE_ENABLED_PROPERTY = "cacheEnabled";

  /**
   * The property to define the timeout of cached elements, if caching is enabled
   */
  public static final String CACHE_TIMEOUT_PROPERTY = "cacheTimeout";
  public static final String WEBROOT = "webroot";
  private StaticHandler staticHandler;

  @Override
  public void initProperties(final Properties properties) {
    if (properties.containsKey(WEBROOT)) {
      staticHandler = StaticHandler.create(properties.getProperty(WEBROOT));
    } else {
      staticHandler = StaticHandler.create();
    }

    if (properties.containsKey(CACHE_ENABLED_PROPERTY)) {
      staticHandler.setCachingEnabled(Boolean.valueOf(properties.getProperty(CACHE_ENABLED_PROPERTY, "true")));
    }
    if (properties.containsKey(CACHE_TIMEOUT_PROPERTY)) {
      staticHandler.setCacheEntryTimeout(Integer.parseInt(properties.getProperty(CACHE_TIMEOUT_PROPERTY)));
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see io.vertx.core.Handler#handle(java.lang.Object)
   */
  @Override
  public void handleController(final RoutingContext event) {
    if (LOGGER.isDebugEnabled())
      LOGGER.debug("handling " + getClass().getName() + " for " + event.request().path());
    event.response().headers().set(HttpHeaders.CONTENT_ENCODING, HttpHeaders.IDENTITY);
    staticHandler.handle(event);
  }

  /**
   * Creates a default definition for the current instance
   *
   * @return
   */
  public static RouterDefinition createDefaultRouterDefinition() {
    RouterDefinition def = new RouterDefinition();
    def.setName(StaticController.class.getSimpleName());
    def.setBlocking(false);
    def.setController(StaticController.class);
    def.setHandlerProperties(getDefaultProperties());
    def.setRoutes(new String[] { "/static/*" });
    return def;
  }

  /**
   * Get the default properties for an implementation of StaticController
   *
   * @return
   */
  public static Properties getDefaultProperties() {
    Properties json = new Properties();
    json.put(CACHE_ENABLED_PROPERTY, "true");
    return json;
  }

}

@@ -397,101 +397,263 @@
 es;%0A
-%0Aimport de.braintags.netrelay.routing.RouterDefinition;%0Aimport io.vertx.core.http.HttpHeaders
+import java.util.Set;%0A%0Aimport com.google.common.collect.ImmutableSet;%0A%0Aimport de.braintags.netrelay.routing.RouterDefinition;%0Aimport de.braintags.vertx.util.HttpContentType;%0Aimport io.vertx.core.http.HttpHeaders;%0Aimport io.vertx.core.http.impl.MimeMapping
 ;%0Aim
@@ -1670,16 +1670,416 @@
 broot%22;%0A
+%0A  private static final Set%3CHttpContentType%3E COMPRESSED_CONTENT_TYPES = ImmutableSet%0A      .of(removeParams(HttpContentType.APPLICATION_JAVASCRIPT), removeParams(HttpContentType.TEXT_CSS),%0A          removeParams(HttpContentType.IMAGE_SVG));%0A%0A  private static HttpContentType removeParams(final HttpContentType parsed) %7B%0A    return new HttpContentType(parsed.getMainType(), parsed.getSubType());%0A  %7D%0A%0A
   privat
@@ -2715,16 +2715,85 @@
 );%0A    %7D
+%0A%0A    // FIXME: remove this%0A    staticHandler.setMaxAgeSeconds(3600);
 %0A  %7D%0A%0A  
@@ -3075,24 +3075,512 @@
 ().path());%0A
+%0A    boolean compressed = false;%0A    String contentType = MimeMapping.getMimeTypeForFilename(event.request().path());%0A    if (contentType != null) %7B%0A      try %7B%0A        HttpContentType parsed = HttpContentType.parse(contentType);%0A        if (COMPRESSED_CONTENT_TYPES.contains(removeParams(parsed))) %7B%0A          compressed = true;%0A        %7D%0A      %7D catch (Throwable t) %7B%0A        LOGGER.error(%22unable to detect mime type: %22 + event.request().path());%0A      %7D%0A    %7D%0A    if (!compressed) %7B%0A  
     event.re
@@ -3655,16 +3655,22 @@
 NTITY);%0A
+    %7D%0A
     stat

fix static handler