<commit_before>package org.projectspinoza.dd.xmetadata.web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.projectspinoza.dd.xmetadata.RoutingHandler;
import org.projectspinoza.dd.xmetadata.XmetaApi;
import org.projectspinoza.dd.xmetadata.XmetaResult;
import org.projectspinoza.dd.xmetadata.core.PostgreMysqlApi;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTOptions;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.JWTAuthHandler;


public class XmetaVerticle extends AbstractVerticle implements RoutingHandler{
  private static Logger LOG = LogManager.getRootLogger();
  private Router router;
  private JWTAuth authProvider;
  
  @Override
  public void start(Future<Void> startFuture) throws Exception {
    super.start(startFuture);
    router = Router.router(vertx);
    
    router.route().handler(BodyHandler.create());
    authProvider = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject()
        .put("type", "jceks")
        .put("path", "keystore.jceks")
        .put("password", "<PASSWORD>")));
    
    router.route("/xmeta/*").handler(JWTAuthHandler.create(authProvider, "/auth"));
    
    router.post("/auth").consumes("application/json").handler(this::auth);
    router.get("/xmeta/api/v1/").handler(this::info);
    router.get("/xmeta/api/v1/list/db").handler(this::listDatabases);
    router.get("/xmeta/api/v1/list/table/:database").handler(this::listTables);
    router.get("/xmeta/api/v1/list/column/:database/:table").handler(this::listColumns);
    router.get("/xmeta/api/v1/list/columnWithType/:database/:table").handler(this::listColumnsWithType);
    
    router.get("/xmeta/api/v1/get/columnType/:database/:table/:column").handler(this::getColumnType);
    router.get("/xmeta/api/v1/get/columnLength/:database/:table/:column").handler(this::getColumnLength);
    router.get("/xmeta/api/v1/get/columnAtPos/:database/:table/:columnIndex").handler(this::getColumnAtPosition);
    router.get("/xmeta/api/v1/get/primaryKey/:database/:table").handler(this::getPrimaryKey);
    router.get("/xmeta/api/v1/get/foreignKey/:database/:table").handler(this::getForeignKey);
    router.get("/xmeta/api/v1/get/foreignKeysRefTable/:database/:table").handler(this::getForeignKeysReferencedTable);
    router.get("/xmeta/api/v1/get/indexes/:database/:table").handler(this::getIndexes);
    router.get("/xmeta/api/v1/get/allIdxRefTable/:database/:table").handler(this::getAllIndexesAndReferencedTable);
    
    router.get("/xmeta/api/v1/exists/db/:db").handler(this::databaseExists);
    router.get("/xmeta/api/v1/exists/table/:database/:table").handler(this::tableExists);
    router.get("/xmeta/api/v1/exists/column/:database/:table/:column").handler(this::columnExists);
    
    router.get("/xmeta/api/v1/get/supports/:feature").handler(this::supportsFeature);
    
    router.get("/xmeta/api/v1/info/db/").handler(this::getDatabaseInfo);
    router.get("/xmeta/api/v1/info/dbdriver/").handler(this::getDatabaseDriverInfo);
    
    
    vertx.createHttpServer().requestHandler(router::accept).listen(8181);
  }

  @Override
  public void stop(Future<Void> stopFuture) throws Exception {
    super.stop(stopFuture);
  }

  @Override
  public void info(RoutingContext routingContext) {
    
    List<String> availableRoutes = new ArrayList<String>();
    
    List<Route> allRoutes = router.getRoutes();
    for(Route r : allRoutes){
      if(r.getPath() != null && !(r.getPath().isEmpty())){
        availableRoutes.add(r.getPath());
      }
    }
    System.out.println("allRoutes: " + availableRoutes.size());
    XmetaResult<List<String>> r = new XmetaResult<List<String>>();
    r.setStatus(200);
    r.setTitle("ListRoutes");
    r.setResult(availableRoutes);
    Buffer responseData = toBuffer(r);
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
    
    response.end(responseData);
  }

  @Override
  public void listDatabases(RoutingContext routingContext) {
    XmetaApi api = getApiHandler(routingContext.user().principal());
    Buffer responseData = toBuffer(api.listDatabases());
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
    
    api.closeConnection();
    response.end(responseData);
  }

  @Override
  public void listTables(RoutingContext routingContext) {
    XmetaApi api = getApiHandler(routingContext.user().principal());
    
    Buffer responseData = toBuffer(api.listTables(routingContext.request().getParam("database")));
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
   
    api.closeConnection();<commit_msg>minor modification<commit_after>package org.projectspinoza.dd.xmetadata.web;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTOptions;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.JWTAuthHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.projectspinoza.dd.xmetadata.RoutingHandler;
import org.projectspinoza.dd.xmetadata.XmetaApi;
import org.projectspinoza.dd.xmetadata.XmetaResult;
import org.projectspinoza.dd.xmetadata.core.PostgreMysqlApi;

import com.fasterxml.jackson.databind.ObjectMapper;


public class XmetaVerticle extends AbstractVerticle implements RoutingHandler{
  private static Logger LOG = LogManager.getRootLogger();
  private Router router;
  private JWTAuth authProvider;
  
  @Override
  public void start(Future<Void> startFuture) throws Exception {
    super.start(startFuture);
    router = Router.router(vertx);
    
    router.route().handler(BodyHandler.create());
    authProvider = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject()
        .put("type", "jceks")
        .put("path", "keystore.jceks")
        .put("password", "<PASSWORD>")));
    
    router.route("/xmeta/*").handler(JWTAuthHandler.create(authProvider, "/auth"));
    
    router.post("/auth").consumes("application/json").handler(this::auth);
    router.get("/xmeta/api/v1/").handler(this::info);
    router.get("/xmeta/api/v1/list/db").handler(this::listDatabases);
    router.get("/xmeta/api/v1/list/table/:database").handler(this::listTables);
    router.get("/xmeta/api/v1/list/column/:database/:table").handler(this::listColumns);
    router.get("/xmeta/api/v1/list/columnWithType/:database/:table").handler(this::listColumnsWithType);
    
    router.get("/xmeta/api/v1/get/columnType/:database/:table/:column").handler(this::getColumnType);
    router.get("/xmeta/api/v1/get/columnLength/:database/:table/:column").handler(this::getColumnLength);
    router.get("/xmeta/api/v1/get/columnAtPos/:database/:table/:columnIndex").handler(this::getColumnAtPosition);
    router.get("/xmeta/api/v1/get/primaryKey/:database/:table").handler(this::getPrimaryKey);
    router.get("/xmeta/api/v1/get/foreignKey/:database/:table").handler(this::getForeignKey);
    router.get("/xmeta/api/v1/get/foreignKeysRefTable/:database/:table").handler(this::getForeignKeysReferencedTable);
    router.get("/xmeta/api/v1/get/indexes/:database/:table").handler(this::getIndexes);
    router.get("/xmeta/api/v1/get/allIdxRefTable/:database/:table").handler(this::getAllIndexesAndReferencedTable);
    
    router.get("/xmeta/api/v1/exists/db/:db").handler(this::databaseExists);
    router.get("/xmeta/api/v1/exists/table/:database/:table").handler(this::tableExists);
    router.get("/xmeta/api/v1/exists/column/:database/:table/:column").handler(this::columnExists);
    
    router.get("/xmeta/api/v1/get/supports/:feature").handler(this::supportsFeature);
    
    router.get("/xmeta/api/v1/info/db/").handler(this::getDatabaseInfo);
    router.get("/xmeta/api/v1/info/dbdriver/").handler(this::getDatabaseDriverInfo);
    
    
    vertx.createHttpServer().requestHandler(router::accept).listen(8181);
  }

  @Override
  public void stop(Future<Void> stopFuture) throws Exception {
    super.stop(stopFuture);
  }

  @Override
  public void info(RoutingContext routingContext) {
    
    List<String> availableRoutes = new ArrayList<String>();
    
    List<Route> allRoutes = router.getRoutes();
    for(Route r : allRoutes){
      if(r.getPath() != null && !(r.getPath().isEmpty())){
        availableRoutes.add(r.getPath());
      }
    }
    System.out.println("allRoutes: " + availableRoutes.size());
    XmetaResult<List<String>> r = new XmetaResult<List<String>>();
    r.setStatus(200);
    r.setTitle("ListRoutes");
    r.setResult(availableRoutes);
    Buffer responseData = toBuffer(r);
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
    
    response.end(responseData);
  }

  @Override
  public void listDatabases(RoutingContext routingContext) {
    XmetaApi api = getApiHandler(routingContext.user().principal());
    Buffer responseData = toBuffer(api.listDatabases());
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
    
    api.closeConnection();
    response.end(responseData);
  }

  @Override
  public void listTables(RoutingContext routingContext) {
    String dbName = routingContext.request().getParam("database");
    routingContext.user().principal().put("db_name", dbName);
    XmetaApi api = getApiHandler(routingContext.user().principal());
    
    Buffer responseData = toBuffer(api.listTables(dbName));
    HttpServerResponse response = routingContext.response();
    response.setStatusCode(200);
    response.headers()
    .add("Content-Length", responseData.length()+"")
    .add("Content-Type", "application/json");
   
    api.closeConnection();
{'fraction_non_alphanumeric': 0.16012669364772128, 'fraction_numerical': 0.006510645785676579, 'mean_word_length': 4.703462117410938, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6346844', 'n_tokens_mistral': 3640, 'n_tokens_neox': 3669, 'n_words': 427}