ListAllFiles Handler Documentation

Overview

The `ListAllFiles` class is a specialized HTTP handler that provides authenticated users with a structured inventory of their website files. It serves as a critical component of the file management system, enabling users to browse and organize their content through the web-based administrative interface.

Purpose

This handler is designed for a multi-tenant hosting service where each registered user manages their own domain and associated files. It allows authenticated users to retrieve a categorized listing of all files within their account's directory structure, facilitating file management operations through the web interface.

Core Functionality

1. Request Validation


2. Authentication & Authorization


3. User Path Resolution

The handler determines the appropriate file path based on the authenticated user:


4. File Categorization

Files are automatically categorized into seven distinct types:


5. JSON Response Structure

Returns a structured JSON response with files organized by category:

[{
"folder": ["directory1/", "directory2/"],
"html": ["index.html", "about.html"],
"php": ["contact.php"],
"css": ["style.css"],
"img": ["logo.png", "banner.jpg"],
"js": ["script.js"],
"video": ["intro.mp4"],
"status": "ok"
}]


6. Error Handling


Request Format

Method: POST
Content-Type: application/json
Body:

{
"path": "subdirectory/path" // Optional: relative path within user's directory
}


Response Codes


Security Features


Dependencies

This handler relies on several helper methods and components:


Use Case

This handler is typically called by the administrative web interface when users:

flowchart TD A(["Start"]) --> B{"Request method
== POST?"} B -->|No| C["Send 405 Method Not Allowed
Close response"] B -->|Yes| D["Read request body
into String"] D --> E["Parse JSON into Map
using parseJsonToMap"] E --> F["Get sessionId from exchange
via getJavaSessionId"] F --> G["Get username from
SessionManager.getUsername"] G --> H{"Username
is null?"} H -->|Yes| I["Send 403 Forbidden
Close response body"] H -->|No| J["Set base userPath for user"] J --> K{"Username ==
norlund_johan_lukas_com?"} K -->|Yes| L["Set userPath =
ADMIN_DIR + /static/"] K -->|No| M["Set userPath =
USER_DIR + user +/static/"] L --> N M --> N N{"map.get(path) not
empty and not null?"} N -->|Yes| O["Append map path to userPath"] N -->|No| P["Keep userPath as is"] O --> Q["Log: 'Listing files in: {userPath}'"] P --> Q Q --> R["Initialize StringBuilder json
Set success = true
Start JSON array and object"] R --> S["TRY: Call filesList(userPath)"] S --> T{"RuntimeException
thrown?"} T -->|Yes| U["CATCH: Log error
Set response = failure JSON
Set success = false"] T -->|No| V["Start iterating through types array:
folder, html, php, css, img, js, video"] V --> W["Append type name to JSON
Start array for this type"] W --> X["Iterate through all files
from filesList"] X --> Y{"File matches
current type?"} Y -->|folder| Z1["Check: file.endsWith('/')"] Y -->|html/php/css/js| Z2["Check: file.endsWith('.type')"] Y -->|img| Z3["Check against
IMAGE_EXTENSIONS array"] Y -->|video| Z4["Check against
VIDEO_EXTENSIONS array"] Z1 --> AA{"Match found?"} Z2 --> AA Z3 --> AA Z4 --> AA AA -->|Yes| AB["Append file to JSON array
with comma separator"] AA -->|No| AC["Skip this file"] AB --> AD{"More files for
this type?"} AC --> AD AD -->|Yes| X AD -->|No| AE["Close JSON array for type
Add comma"] AE --> AF{"More types to
process?"} AF -->|Yes| V AF -->|No| AG{"success
== true?"} U --> AG AG -->|Yes| AH["Append: 'status': 'ok'
Close JSON object and array
Set response = json.toString"] AG -->|No| AI["Keep response = failure JSON"] AH --> AJ["Log: 'Sending back: {response}'"] AI --> AJ AJ --> AK["Set Content-Type:
application/json"] AK --> AL["Send 200 response
with response.length"] AL --> AM["Write response bytes
to OutputStream"] AM --> AN["Close OutputStream"] AN --> AO(["End"]) C --> AO I --> AO