Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class App {
- final static private Logger LOGGER = LoggerFactory.getILoggerFactory().getLogger("requests");
- public static void main(String[] args) {
- secure("src/main/resources/keystore.jks", "password", null, null);
- //folders
- final String LIST_FOLDER_CONTENT = "/folders/:path/list_folder_content";
- final String FOLDER_GET_META_DATA = "/folders/:path/get_metadata";
- final String FOLDER_DELETE = "/folders/:path/delete";
- final String FOLDER_MOVE = "/folders/:path/move";
- final String CREATE_DIRECTORY = "/folders/:path/create_directory";
- //files
- final String FILE_GET_METADATA = "/files/:path/get_metadata";
- final String FILE_DELETE = "/files/:path/delete";
- final String FILE_MOVE = "/files/:path/move";
- final String FILE_RENAME = "/files/:path/rename";
- final String FILE_DOWNLOAD = "/files/:path/download";
- final String FILE_UPLOAD = "/files/:path/upload";
- //users
- final String CREATE_USER = "/users/:user/create_user";
- final String ACCESS = "/users/access";
- final String USER_DELETE = "/users/delete";
- final String LOG_OUT = "/users/log_out";
- final String DB_URL = "jdbc:sqlite:test.db";
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(DB_URL);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Configuration configuration = new DefaultConfiguration().set(connection).set(SQLDialect.SQLITE);
- final Gson gson = new Gson();
- final ResponseTransformer json = gson::toJson;
- final FolderController folderController = new FolderController(new FolderMetadataDao(configuration), new FileMetadataDao(configuration), new FileContentsDao(configuration), new SessionDataDao(configuration), configuration);
- final FileController fileController = new FileController(new FileMetadataDao(configuration), new FileContentsDao(configuration), new SessionDataDao(configuration), new FolderMetadataDao(configuration), configuration);
- final UserController userController = new UserController(new UsersDao(configuration), new FolderMetadataDao(configuration), new SessionDataDao(configuration), configuration);
- port(4567);
- before("/*/", (req, res) -> {
- info(req);
- });
- //folders
- put(CREATE_DIRECTORY, (request, response) -> {
- return folderController.handleCreateNewFolder(setRequest(request, configuration), response);
- }, json);
- put(FOLDER_MOVE, (request, response) -> {
- return folderController.handleMoveFolder(setRequest(request, configuration),response);
- }, json);
- get(FOLDER_GET_META_DATA, (request, response) -> {
- return folderController.handleGetMetadata(setRequest(request, configuration),response);
- }, json);
- get(LIST_FOLDER_CONTENT, (request, response) -> {
- return folderController.handleListFolderContent(setRequest(request, configuration),response);
- }, json);
- delete(FOLDER_DELETE, (request, response) -> {
- folderController.handleDeleteFolder(setRequest(request, configuration), response);
- return "Folder deleted";
- }, json);
- //files
- post(FILE_UPLOAD, (request, response) -> {
- return fileController.handleUpload(setRequest(request, configuration),response);
- }, json);
- get(FILE_DOWNLOAD, (request, response) -> {
- return fileController.handleDownload(setRequest(request, configuration),response);
- }, json);
- get(FILE_GET_METADATA, (request, response) -> {
- return fileController.handleGetMetadata(setRequest(request, configuration),response);
- }, json);
- put(FILE_MOVE, (request, response) -> {
- return fileController.handleMove(setRequest(request, configuration), response);
- }, json);
- put(FILE_RENAME, (request, response) -> {
- return fileController.handleRename(setRequest(request, configuration), response);
- }, json);
- delete(FILE_DELETE, (request, response) -> {
- fileController.handleDelete(setRequest(request, configuration), response);
- return "File deleted";
- }, json);
- //users
- post(CREATE_USER, (request, response) -> {
- userController.handleCreateUser(request,response);
- return "User created";
- }, json);
- get(ACCESS, (request, response) -> {
- return userController.handleAccess(request,response);
- }, json);
- delete(USER_DELETE, (request, response) -> {
- userController.handleDelete(request,response);
- return "User deleted";
- }, json);
- post(LOG_OUT, (request, response) -> {
- userController.handleLogOut(request,response);
- return "Logged out";
- }, json);
- exception(ParameterFormatException.class,(ex,request,response) -> {
- response.status(403);
- response.body(gson.toJson(new ParameterFormatError(request.params())));
- });
- exception(FileAlreadyExists.class,(ex,request,response) -> {
- response.status(409);
- response.body(gson.toJson(ex));
- });
- exception(UserAlreadyExists.class,(ex,request,response) -> {
- response.status(409);
- response.body(gson.toJson(ex));
- });
- exception(FileDoesNotExist.class,(ex, request, response) -> {
- response.status(404);
- response.body(gson.toJson(ex));
- });
- exception(UnsuccessfulAuthorization.class,(ex, request, response) -> {
- response.status(401);
- response.body(gson.toJson(ex));
- });
- exception(UserDoesNotExist.class,(ex, request, response) -> {
- response.status(404);
- response.body(gson.toJson(ex));
- });
- exception(WrongPathFormat.class,(ex,request,response) -> {
- response.status(403);
- response.body(gson.toJson(ex));
- });
- exception(JSONException.class, (ex,request,response) -> {
- response.status(403);
- response.body(gson.toJson(ex));
- });
- }
- private static void info(Request req) {
- LOGGER.info("{}", req);
- }
- private static Request setRequest(Request request, Configuration configuration){
- Integer userId = new SessionDataDao(configuration).fetchBySessionId(request.cookie("session")).get(0).getUserId();
- String userName = new UsersDao(configuration).fetchOneById(userId).getUserName();
- String oldPath = request.params(":path");
- String newPath = request.queryParams("new_path");
- if(!(newPath == null)){
- request.attribute("newPath", "/"+userName+newPath);
- }
- request.attribute("path", "/"+userName+oldPath);
- return request;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement