/*
 * Copyright (C) 2016 Cognifide Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.knotx.server;

import io.knotx.server.configuration.KnotxFlowConfiguration;
import io.knotx.server.configuration.KnotxServerConfiguration;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.rxjava.ext.web.RoutingContext;

public class SupportedMethodsAndPathsHandler implements Handler<RoutingContext> {

  private static final Logger LOGGER = LoggerFactory
      .getLogger(SupportedMethodsAndPathsHandler.class);

  private final KnotxFlowConfiguration defaultFlow;

  private final KnotxFlowConfiguration customFlow;

  private SupportedMethodsAndPathsHandler(KnotxServerConfiguration configuration) {
    this.defaultFlow = configuration.getDefaultFlow();
    this.customFlow = configuration.getCustomFlow();
  }

  public static SupportedMethodsAndPathsHandler create(KnotxServerConfiguration configuration) {
    return new SupportedMethodsAndPathsHandler(configuration);
  }

  @Override
  public void handle(RoutingContext context) {
    if (shouldRejectMethod(context.request().method())) {
      LOGGER.warn("Requested method {} is not supported based on configuration",
          context.request().method());
      context.fail(HttpResponseStatus.METHOD_NOT_ALLOWED.code());
    } else if (shouldRejectPath(context.request().path())) {
      LOGGER.warn("Requested path {} is not supported based on configuration",
          context.request().path());
      context.fail(HttpResponseStatus.NOT_FOUND.code());
    } else {
      context.next();
    }
  }

  private boolean shouldRejectMethod(HttpMethod method) {
    return isMethodDisallowedInFlow(method, defaultFlow) || isMethodDisallowedInFlow(method, customFlow);
  }

  private boolean isMethodDisallowedInFlow(HttpMethod method, KnotxFlowConfiguration flow) {
    return flow.getEngineRouting() != null && flow.getEngineRouting().keySet().stream().noneMatch(supportedMethod -> supportedMethod == method);
  }

  private boolean shouldRejectPath(String path) {
    return isPathNotPresentInFlow(path, defaultFlow) && isPathNotPresentInFlow(path, customFlow);
  }

  private boolean isPathNotPresentInFlow(String path, KnotxFlowConfiguration flow) {
    return flow.getEngineRouting().values().stream().noneMatch(
        routingEntries -> routingEntries.stream()
            .anyMatch(item -> path.matches(item.path()))
    );
  }
}

@@ -1728,58 +1728,333 @@
     
-if (shouldRejectMethod(context.request().method())
+String path = context.request().path();%0A    HttpMethod method = context.request().method();%0A    boolean pathNotAllowedInDefaultFlow = isPathNotPresentInFlow(path, defaultFlow);%0A    boolean pathNotAllowedInCustomFlow = isPathNotPresentInFlow(path, customFlow);%0A    if (pathNotAllowedInDefaultFlow && pathNotAllowedInCustomFlow
 ) %7B%0A
@@ -2082,22 +2082,20 @@
 quested 
-method
+path
  %7B%7D is n
@@ -2135,45 +2135,13 @@
 on%22,
-%0A          context.request().method()
+ path
 );%0A 
@@ -2181,25 +2181,16 @@
 tus.
-METHOD_NOT_ALLOWE
+NOT_FOUN
 D.co
@@ -2215,49 +2215,173 @@
 if (
-shouldRejectPath(context.request().path()
+!pathNotAllowedInDefaultFlow && isMethodDisallowedInFlow(method, defaultFlow)) %7B%0A      if (!pathNotAllowedInCustomFlow && isMethodDisallowedInFlow(method, customFlow
 )) %7B
@@ -2379,32 +2379,34 @@
 mFlow)) %7B%0A      
+ 
+ 
 LOGGER.warn(%22Req
@@ -2404,36 +2404,38 @@
 warn(%22Requested 
-path
+method
  %7B%7D is not suppo
@@ -2467,46 +2467,20 @@
 on%22,
-%0A          context.request().path()
+ method
 );%0A
+  
     
@@ -2509,32 +2509,41 @@
 ponseStatus.
-NOT_FOUN
+METHOD_NOT_ALLOWE
 D.code());%0A 
@@ -2533,32 +2533,40 @@
 LLOWED.code());%0A
+      %7D%0A
     %7D else %7B%0A   
@@ -2599,177 +2599,8 @@
  %7D%0A%0A
-  private boolean shouldRejectMethod(HttpMethod method) %7B%0A    return isMethodDisallowedInFlow(method, defaultFlow) %7C%7C isMethodDisallowedInFlow(method, customFlow);%0A  %7D%0A%0A
   pr
@@ -2842,161 +2842,8 @@
  %7D%0A%0A
-  private boolean shouldRejectPath(String path) %7B%0A    return isPathNotPresentInFlow(path, defaultFlow) && isPathNotPresentInFlow(path, customFlow);%0A  %7D%0A%0A
   pr
@@ -2925,32 +2925,67 @@
 ow) %7B%0A    return
+ flow.getEngineRouting() != null &&
  flow.getEngineR

Customize request routing outside Knots: removed unused loggers