CreateFolderHandler Documentation

Overview

The CreateFolderHandler class is an HTTP handler that enables authenticated users to create new directories within their website's file structure. It provides a secure API endpoint for folder creation operations initiated from the web-based file management interface.


Purpose

This handler is part of the multi-tenant hosting service's file management system, allowing users to organize their content by creating new folders within their allocated storage space. It ensures that only authenticated users can create directories and that they can only do so within their own account boundaries.


Core Functionality

This handler performs the following key operations:


1. Request Validation


2. Authentication & Authorization


3. Path Construction

The handler builds the target directory path based on the authenticated user:

Where:


4. Directory Creation


5. Response Handling

Returns a plain text response indicating the operation result:

Note: If the directory already exists, the handler logs this condition but still returns "Fail" to the client.


Request Format

Method: POST

Content-Type: application/json

Body:

{
  "path": "relative/folder/path"
}

The path value should be relative to the user's static directory (e.g., "images/thumbnails" or "assets/css").


Response Format

Content-Type: text/plain; charset=UTF-8

Body: Either "Success" or "Fail"


Response Codes


Security Features


Error Handling


Limitations


Use Cases

This handler is typically invoked when users:


Dependencies

This handler relies on:

flowchart TD A(["Start handle"]) --> B{"Request method
== POST?"} B -->|No| C["Send 403 Forbidden
Close response body"] B -->|Yes| D["Read request body
into String with UTF-8"] C --> T(["End"]) D --> E["Parse JSON body
using parseJsonToMap"] E --> F["Get sessionId via
getJavaSessionId(exchange)"] F --> G["Get username from
SessionManager.getUsername(sessionId)"] G --> H{"Username
is null?"} H -->|Yes| I["Log: 'Rejected: session not valid'
Send 403 Forbidden
Close response body"] H -->|No| J["Build base path:
USER_DIR + user + /static/ + map.get(path)"] I --> T J --> K{"Username ==
ADMIN_HOST?"} K -->|Yes| L["Override path:
ADMIN_DIR + /static/ + map.get(path)"] K -->|No| M["Keep user path"] L --> N["Initialize msg = 'Fail'
Create Path object: newPath = Paths.get(path)"] M --> N N --> O["TRY: Files.createDirectory(newPath)"] O --> P{"Directory creation
successful?"} P -->|Success| Q["Set msg = 'Success'"] P -->|FileAlreadyExistsException| R["CATCH: Log 'Directory already exist: {path}'
msg remains 'Fail'"] P -->|Other Exception| S["msg remains 'Fail'"] Q --> U R --> U S --> U U["Convert msg to bytes:
response = msg.getBytes()"] U --> V["Set Content-Type header:
text/plain; charset=UTF-8"] V --> W["Send 200 response
with response.length"] W --> X["Write response bytes
to OutputStream"] X --> Y["Close OutputStream"] Y --> T