<commit_before>package com.continuuity.gateway.router;

import com.continuuity.common.conf.Constants;
import com.continuuity.common.utils.ImmutablePair;
import com.google.common.collect.ImmutableMap;

import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class to match the request path to corresponding service like app-fabric, or metrics service.
 */

public final class RouterPathLookup {
  private static final String VERSION = Constants.Gateway.GATEWAY_VERSION;

  private static final String COMMON_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)/" +
    "(start|debug|stop|status|history|runtimeargs)";

  private static final String PROGRAMINFO_PATH = VERSION +
    "/?/(flows|procedures|mapreduce|workflows)/?$";

  private static final String ALLAPPINFO_PATH = VERSION +
    "/?/apps/?$";

  private static final String APPINFO_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_-]+)/?(flows|procedures|mapreduce|workflows)?/?$";
  private static final String DELETE_PATH = VERSION +
    "/?/apps/?";
  private static final String DEPLOY_PATH = VERSION +
    "/?/apps/?([A-Za-z0-9_]+)?/?$";

  private static final String WEBAPP_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/webapp/(status|start|stop)";

  private static final String DEPLOY_STATUS_PATH = VERSION +
    "/?/deploy/status/?";
  private static final String METRICS_PATH = "^" + VERSION +
    "/metrics";
  private static final String LOGHANDLER_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)/logs";

  private static final String FLOWLET_INSTANCE_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/flows/([A-Za-z0-9_]+)/flowlets/([A-Za-z0-9_]+)/instances";

  private static final String TRANSACTIONS_STATE_PATH = VERSION +
    "/transactions/state";

  private static final String TRANSACTION_ID_PATH = VERSION +
    "/transactions/([A-Za-z0-9_]+)/invalidate";

  private static final String SCHEDULER_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/workflows/([A-Za-z0-9_]+)/" +
    "(schedules|nextruntime)";

  private static final String LIVEINFO_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures)/([A-Za-z0-9_]+)/live-info";

  //TODO: Consolidate this!!!
  private static final String SPEC_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)";

  private enum AllowedMethod {
    GET, PUT, POST, DELETE
  }

  private static final ImmutableMap<ImmutablePair<? extends Set<AllowedMethod>, Pattern>, String> ROUTING_MAP =
    ImmutableMap.<ImmutablePair<? extends Set<AllowedMethod>, Pattern>, String>builder()
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(COMMON_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(SCHEDULER_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.range(AllowedMethod.PUT, AllowedMethod.POST), Pattern.compile(DEPLOY_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(DEPLOY_STATUS_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(FLOWLET_INSTANCE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(SPEC_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(LIVEINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.range(AllowedMethod.GET, AllowedMethod.POST), Pattern.compile(METRICS_PATH)),
           Constants.Service.METRICS)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(LOGHANDLER_PATH)),
           Constants.Service.METRICS)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.DELETE), Pattern.compile(DELETE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(PROGRAMINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(ALLAPPINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(APPINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      // todo change to Constants.Service.DATASET_MANAGER
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.POST),
                            Pattern.compile(TRANSACTIONS_STATE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      // todo change to Constants.Service.DATASET_MANAGER
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.POST), Pattern.compile(TRANSACTION_ID_PATH)),
          Constants.Service.APP_FABRIC_HTTP)
      .build();

  public static String getRoutingPath(String requestPath, String method) {<commit_msg>Added regex in routerpath lookup<commit_after>package com.continuuity.gateway.router;

import com.continuuity.common.conf.Constants;
import com.continuuity.common.utils.ImmutablePair;
import com.google.common.collect.ImmutableMap;

import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class to match the request path to corresponding service like app-fabric, or metrics service.
 */

public final class RouterPathLookup {
  private static final String VERSION = Constants.Gateway.GATEWAY_VERSION;

  private static final String COMMON_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)/" +
    "(start|debug|stop|status|history|runtimeargs)";

  private static final String PROGRAMINFO_PATH = VERSION +
    "/?/(flows|procedures|mapreduce|workflows)/?$";

  private static final String ALLAPPINFO_PATH = VERSION +
    "/?/apps/?$";

  private static final String APPINFO_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_-]+)/?(flows|procedures|mapreduce|workflows)?/?$";
  private static final String DELETE_PATH = VERSION +
    "/?/apps/?";
  private static final String DEPLOY_PATH = VERSION +
    "/?/apps/?([A-Za-z0-9_]+)?/?$";

  private static final String WEBAPP_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/webapp/(status|start|stop)";

  private static final String DEPLOY_STATUS_PATH = VERSION +
    "/?/deploy/status/?";
  private static final String METRICS_PATH = "^" + VERSION +
    "/metrics";
  private static final String LOGHANDLER_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)/logs";

  private static final String FLOWLET_INSTANCE_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/flows/([A-Za-z0-9_]+)/flowlets/([A-Za-z0-9_]+)/instances";

  private static final String TRANSACTIONS_STATE_PATH = VERSION +
    "/transactions/state";

  private static final String TRANSACTION_ID_PATH = VERSION +
    "/transactions/([A-Za-z0-9_]+)/invalidate";

  private static final String SCHEDULER_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/workflows/([A-Za-z0-9_]+)/" +
    "(schedules|nextruntime)";

  private static final String LIVEINFO_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures)/([A-Za-z0-9_]+)/live-info";

  //TODO: Consolidate this!!!
  private static final String SPEC_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/(flows|procedures|mapreduce|workflows)/([A-Za-z0-9_]+)";

  private static final String PROMOTE_PATH = VERSION +
    "/?/apps/([A-Za-z0-9_]+)/promote";
  private static final String RESET_PATH = VERSION +
    "/unrecoverable/reset";

  private enum AllowedMethod {
    GET, PUT, POST, DELETE
  }

  private static final ImmutableMap<ImmutablePair<? extends Set<AllowedMethod>, Pattern>, String> ROUTING_MAP =
    ImmutableMap.<ImmutablePair<? extends Set<AllowedMethod>, Pattern>, String>builder()
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(COMMON_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(SCHEDULER_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.range(AllowedMethod.PUT, AllowedMethod.POST), Pattern.compile(DEPLOY_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(DEPLOY_STATUS_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(FLOWLET_INSTANCE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(SPEC_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.PUT), Pattern.compile(LIVEINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.range(AllowedMethod.GET, AllowedMethod.POST), Pattern.compile(METRICS_PATH)),
           Constants.Service.METRICS)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(LOGHANDLER_PATH)),
           Constants.Service.METRICS)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.DELETE), Pattern.compile(DELETE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(PROGRAMINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(ALLAPPINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET), Pattern.compile(APPINFO_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      // todo change to Constants.Service.DATASET_MANAGER
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.GET, AllowedMethod.POST),
                            Pattern.compile(TRANSACTIONS_STATE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      // todo change to Constants.Service.DATASET_MANAGER
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.POST), Pattern.compile(TRANSACTION_ID_PATH)),
          Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.POST), Pattern.compile(RESET_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .put(ImmutablePair.of(EnumSet.of(AllowedMethod.POST), Pattern.compile(PROMOTE_PATH)),
           Constants.Service.APP_FABRIC_HTTP)
      .build();

  public static String getRoutingPath(String requestPath, String method) {
{'fraction_non_alphanumeric': 0.14124919272995665, 'fraction_numerical': 0.00645816034689547, 'mean_word_length': 4.082044069385842, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3525120', 'n_tokens_mistral': 3807, 'n_tokens_neox': 3743, 'n_words': 518}