DeleteFolderHandler Documentation

Overview

The DeleteFolderHandler class is an HTTP handler that enables authenticated users to delete directories (folders) from their website's file structure. It provides a secure API endpoint for recursive folder deletion 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 remove entire directories and their contents from their allocated storage space. It ensures that only authenticated users can delete folders 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. Recursive Folder Deletion


5. Response Messages

Returns a plain text response indicating the operation result:


Request Format

Method: POST

Content-Type: application/json

Body:

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

The path value must not be empty and should be relative to the user's static directory (e.g., "images/thumbnails" or "old-content").


Response Format

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

Body: Either "Success" or "Fail"


Response Codes


Security Features


Error Handling


Logging

The handler provides console logging for debugging:


Use Cases

This handler is typically invoked when users:


Dependencies

This handler relies on:


Behavior Notes


Safety Considerations

⚠️ Warning: This handler performs recursive deletion, which is a destructive operation. The empty path validation is critical to prevent catastrophic data loss. Consider implementing additional safety measures such as:

flowchart TD A(["Start handle"]) --> B{"Request method
== POST?"} B -->|No| C["Send 403 Forbidden
Close response body"] B -->|Yes| D["Read request body
with UTF-8 encoding"] C --> Z(["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
OR path is empty?"} H -->|Yes| I{"Path is empty?"} H -->|No| J["Build path:
USER_DIR + user + /static/ + map.get(path)"] I -->|Yes| K["Log: 'Rejected: Empty path'"] I -->|No| L["Log: 'Rejected: session not valid'"] K --> M["Send 403 Forbidden
Close response body"] L --> M M --> Z J --> N{"Username ==
ADMIN_HOST?"} N -->|Yes| O["Override path:
ADMIN_DIR + /static/ + map.get(path)"] N -->|No| P["Keep user path"] O --> Q["Log: 'Deleting: {path.toString()}'"] P --> Q Q --> R["Initialize msg = 'Fail'"] R --> S["Call deleteFolder(Paths.get(path))"] S --> T{"deleteFolder
returned true?"} T -->|Yes| U["Set msg = 'Success'"] T -->|No| V["msg remains 'Fail'"] U --> W["Convert msg to bytes"] V --> W W --> X["Set Content-Type header:
text/plain; charset=UTF-8"] X --> Y["Send 200 response
with response.length"] Y --> AA["Write response bytes
to OutputStream"] AA --> AB["Close OutputStream"] AB --> Z