Advertisement
Coriic

Untitled

Jan 24th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. public class App {
  2.  
  3.     final static private Logger LOGGER = LoggerFactory.getILoggerFactory().getLogger("requests");
  4.  
  5.     public static void main(String[] args) {
  6.         secure("src/main/resources/keystore.jks", "password", null, null);
  7.         //folders
  8.         final String LIST_FOLDER_CONTENT = "/folders/:path/list_folder_content";
  9.         final String FOLDER_GET_META_DATA = "/folders/:path/get_metadata";
  10.         final String FOLDER_DELETE = "/folders/:path/delete";
  11.         final String FOLDER_MOVE = "/folders/:path/move";
  12.         final String CREATE_DIRECTORY = "/folders/:path/create_directory";
  13.  
  14.         //files
  15.         final String FILE_GET_METADATA = "/files/:path/get_metadata";
  16.         final String FILE_DELETE = "/files/:path/delete";
  17.         final String FILE_MOVE = "/files/:path/move";
  18.         final String FILE_RENAME = "/files/:path/rename";
  19.         final String FILE_DOWNLOAD = "/files/:path/download";
  20.         final String FILE_UPLOAD = "/files/:path/upload";
  21.  
  22.         //users
  23.         final String CREATE_USER = "/users/:user/create_user";
  24.         final String ACCESS = "/users/access";
  25.         final String USER_DELETE = "/users/delete";
  26.         final String LOG_OUT = "/users/log_out";
  27.  
  28.         final String DB_URL = "jdbc:sqlite:test.db";
  29.  
  30.         Connection connection = null;
  31.         try {
  32.             connection = DriverManager.getConnection(DB_URL);
  33.         } catch (SQLException e) {
  34.             e.printStackTrace();
  35.         }
  36.         Configuration configuration = new DefaultConfiguration().set(connection).set(SQLDialect.SQLITE);
  37.  
  38.         final Gson gson = new Gson();
  39.         final ResponseTransformer json = gson::toJson;
  40.        
  41.         final FolderController folderController = new FolderController(new FolderMetadataDao(configuration), new FileMetadataDao(configuration), new FileContentsDao(configuration), new SessionDataDao(configuration), configuration);
  42.         final FileController fileController = new FileController(new FileMetadataDao(configuration), new FileContentsDao(configuration), new SessionDataDao(configuration), new FolderMetadataDao(configuration), configuration);
  43.         final UserController userController = new UserController(new UsersDao(configuration), new FolderMetadataDao(configuration), new SessionDataDao(configuration), configuration);
  44.  
  45.         port(4567);
  46.  
  47.         before("/*/", (req, res) -> {
  48.             info(req);
  49.         });
  50.  
  51.         //folders
  52.         put(CREATE_DIRECTORY, (request, response) -> {
  53.             return folderController.handleCreateNewFolder(setRequest(request, configuration), response);
  54.         }, json);
  55.  
  56.         put(FOLDER_MOVE, (request, response) -> {
  57.             return folderController.handleMoveFolder(setRequest(request, configuration),response);
  58.         }, json);
  59.  
  60.         get(FOLDER_GET_META_DATA, (request, response) -> {
  61.             return folderController.handleGetMetadata(setRequest(request, configuration),response);
  62.         }, json);
  63.  
  64.         get(LIST_FOLDER_CONTENT, (request, response) -> {
  65.             return folderController.handleListFolderContent(setRequest(request, configuration),response);
  66.         }, json);
  67.  
  68.         delete(FOLDER_DELETE, (request, response) -> {
  69.             folderController.handleDeleteFolder(setRequest(request, configuration), response);
  70.             return "Folder deleted";
  71.         }, json);
  72.  
  73.         //files
  74.         post(FILE_UPLOAD, (request, response) -> {
  75.             return fileController.handleUpload(setRequest(request, configuration),response);
  76.         }, json);
  77.  
  78.         get(FILE_DOWNLOAD, (request, response) -> {
  79.             return fileController.handleDownload(setRequest(request, configuration),response);
  80.         }, json);
  81.  
  82.         get(FILE_GET_METADATA, (request, response) -> {
  83.             return fileController.handleGetMetadata(setRequest(request, configuration),response);
  84.         }, json);
  85.  
  86.         put(FILE_MOVE, (request, response) -> {
  87.             return fileController.handleMove(setRequest(request, configuration), response);
  88.         }, json);
  89.  
  90.         put(FILE_RENAME, (request, response) -> {
  91.             return fileController.handleRename(setRequest(request, configuration), response);
  92.         }, json);
  93.  
  94.         delete(FILE_DELETE, (request, response) -> {
  95.             fileController.handleDelete(setRequest(request, configuration), response);
  96.             return "File deleted";
  97.         }, json);
  98.  
  99.         //users
  100.         post(CREATE_USER, (request, response) -> {
  101.             userController.handleCreateUser(request,response);
  102.             return "User created";
  103.         }, json);
  104.  
  105.         get(ACCESS, (request, response) -> {
  106.             return userController.handleAccess(request,response);
  107.         }, json);
  108.  
  109.         delete(USER_DELETE, (request, response) -> {
  110.             userController.handleDelete(request,response);
  111.             return "User deleted";
  112.         }, json);
  113.  
  114.         post(LOG_OUT, (request, response) -> {
  115.             userController.handleLogOut(request,response);
  116.             return "Logged out";
  117.         }, json);
  118.  
  119.         exception(ParameterFormatException.class,(ex,request,response) -> {
  120.             response.status(403);
  121.             response.body(gson.toJson(new ParameterFormatError(request.params())));
  122.         });
  123.  
  124.         exception(FileAlreadyExists.class,(ex,request,response) -> {
  125.             response.status(409);
  126.             response.body(gson.toJson(ex));
  127.         });
  128.  
  129.         exception(UserAlreadyExists.class,(ex,request,response) -> {
  130.             response.status(409);
  131.             response.body(gson.toJson(ex));
  132.         });
  133.  
  134.         exception(FileDoesNotExist.class,(ex, request, response) -> {
  135.             response.status(404);
  136.             response.body(gson.toJson(ex));
  137.         });
  138.  
  139.         exception(UnsuccessfulAuthorization.class,(ex, request, response) -> {
  140.             response.status(401);
  141.             response.body(gson.toJson(ex));
  142.         });
  143.  
  144.         exception(UserDoesNotExist.class,(ex, request, response) -> {
  145.             response.status(404);
  146.             response.body(gson.toJson(ex));
  147.         });
  148.  
  149.         exception(WrongPathFormat.class,(ex,request,response) -> {
  150.             response.status(403);
  151.             response.body(gson.toJson(ex));
  152.         });
  153.  
  154.         exception(JSONException.class, (ex,request,response) -> {
  155.             response.status(403);
  156.             response.body(gson.toJson(ex));
  157.         });
  158.  
  159.     }
  160.  
  161.     private static void info(Request req) {
  162.         LOGGER.info("{}", req);
  163.     }
  164.  
  165.     private static Request setRequest(Request request, Configuration configuration){
  166.         Integer userId = new SessionDataDao(configuration).fetchBySessionId(request.cookie("session")).get(0).getUserId();
  167.         String userName = new UsersDao(configuration).fetchOneById(userId).getUserName();
  168.         String oldPath = request.params(":path");
  169.         String newPath = request.queryParams("new_path");
  170.         if(!(newPath == null)){
  171.             request.attribute("newPath", "/"+userName+newPath);
  172.         }
  173.         request.attribute("path", "/"+userName+oldPath);
  174.         return request;
  175.     }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement