DeleteFileHandler Documentation

Overview

The DeleteFileHandler class is an HTTP handler that enables authenticated users to delete files from their website's file structure. It provides a secure API endpoint for file 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 unwanted or outdated files from their allocated storage space. It ensures that only authenticated users can delete files 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 file path based on the authenticated user:

Where:


4. File Existence Verification


5. File Deletion


6. Response Messages

Returns a plain text response indicating the operation result:


Request Format

Method: POST

Content-Type: application/json

Body:

{
  "path": "relative/directory/path/",
  "filename": "file.ext"
}

The path value should be relative to the user's static directory and may be empty for files in the root. The filename is the name of the file to delete (e.g., "image.png", "style.css").


Response Format

Content-Type: text/plain (implicit)

Body: One of the three response messages described above


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

flowchart TD A(["Start handle"]) --> B{"Request method
== POST?"} B -->|No| C["Send 405 Method Not Allowed"] B -->|Yes| D["Read request body using
InputStreamReader with UTF-8
BufferedReader"] C --> Z(["End"]) D --> E["Collect body lines into String"] E --> F["Log: 'Parsing body...'
Parse JSON using parseJsonToMap"] F --> G["Initialize response =
'An error occured deleting the file'"] G --> H["Get sessionId via
getJavaSessionId(exchange)"] H --> I["Get username from
SessionManager.getUsername(sessionId)"] I --> J{"Username
is null?"} J -->|Yes| K["Send 403 Forbidden
Close response body"] J -->|No| L["Log: 'Recived path: {map.get(path)}'"] K --> Z L --> M["Build base userPath:
USER_DIR + user + /static/"] M --> N{"Username ==
ADMIN_HOST?"} N -->|Yes| O["Override userPath:
ADMIN_DIR + /static/"] N -->|No| P["Keep user base path"] O --> Q P --> Q Q{"map.get(path) not
empty and not null?"} Q -->|Yes| R["Append path to userPath"] Q -->|No| S["Keep userPath as is"] R --> T["Append filename:
userPath += map.get(filename)"] S --> T T --> U["Create Path object:
path = Paths.get(userPath)"] U --> V["Log: 'Delete target: {path.toString()}'"] V --> W{"Files.exists(path)?"} W -->|No| X["Set response =
'The file does not exist'"] W -->|Yes| Y["TRY: Files.delete(path)"] Y --> AA{"Deletion
successful?"} AA -->|Success| AB["Set response =
'The file was deleted successfully'"] AA -->|IOException| AC["CATCH: Log error message
response remains default error"] AB --> AD AC --> AD X --> AD AD["Send 200 response
with response.length()"] AD --> AE["Write response bytes
to OutputStream"] AE --> AF["Close OutputStream"] AF --> Z