# (DD06) Business Logic — Example.downloadFile() [13 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `io.github.biezhi.java11.http.Example` |
| Layer | Utility |
| Module | `http` (Package: `io.github.biezhi.java11.http`) |

## 1. Role

### Example.downloadFile()

This method demonstrates a simple HTTP file download workflow using the Java 11 `HttpClient` API. It constructs a client, builds an HTTP GET request for a fixed external website, and streams the response body directly into a temporary file on the local file system. In business terms, it represents a file acquisition utility rather than a domain transaction: the method retrieves remote content and persists it locally for later inspection or use.

The implementation follows a straightforward request-orchestration pattern. It does not branch by business type, and it does not delegate into a service layer or database layer; instead, it coordinates standard library APIs for network access, file creation, and console output. Its role in the larger system is illustrative and reusable as a reference example for HTTP download behavior in the `http` package.

The method also exposes the core lifecycle of a download operation: create client, define target URL, allocate a destination file, execute the request, and report the result. Because the destination is a temporary file, the operation is intentionally transient and suitable for sample code, testing, or troubleshooting network/file handling.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["downloadFile()"])
    CLIENT(["Create HttpClient with newHttpClient()"])
    REQUEST(["Build GET HttpRequest for https://labs.consol.de/"])
    TEMPFILE(["Create temp file consol-labs-home.html"])
    SEND(["Send request with BodyHandlers.ofFile(tempFile)"])
    PRINT_STATUS(["Print response status code"])
    PRINT_BODY(["Print downloaded file path"])
    END_NODE(["Return"])

    START --> CLIENT
    CLIENT --> REQUEST
    REQUEST --> TEMPFILE
    TEMPFILE --> SEND
    SEND --> PRINT_STATUS
    PRINT_STATUS --> PRINT_BODY
    PRINT_BODY --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

External state read by the method:
- The hard-coded download target `https://labs.consol.de/`
- The local temporary-file location returned by `Files.createTempFile(...)`
- Standard output via `System.out.println(...)`

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `HttpClient.send(...)` | N/A | Remote HTTP resource `https://labs.consol.de/` | Reads remote content by issuing an HTTP GET request and streams the response body into a local file |
| C | `Files.createTempFile(...)` | N/A | Local temporary file `consol-labs-home.html` | Creates a new temporary file to store the downloaded HTML payload |

Notes:
- No application-specific SC/CBS method is invoked in this method.
- No database table access is present in the source code.
- The only external interaction is network retrieval plus local file creation.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | `Example.main` / demo entry point | `Example.main` -> `Example.downloadFile` | `HttpClient.send(...) [R] Remote HTTP resource` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(linear flow)` (L88-L100)

> Performs the complete download sequence from client creation through response reporting.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newHttpClient();` // creates the HTTP client used for the download |
| 2 | SET | `HttpRequest request = HttpRequest.newBuilder()` // begins request construction |
| 3 | EXEC | `.uri(new URI("https://labs.consol.de/"))` // sets the remote download target |
| 4 | EXEC | `.GET()` // configures the request as an HTTP GET |
| 5 | EXEC | `.build();` // finalizes the immutable request object |
| 6 | SET | `Path tempFile = Files.createTempFile("consol-labs-home", ".html");` // allocates a local temporary file for the downloaded content |
| 7 | SET | `HttpResponse<Path> response = client.send(request, HttpResponse.BodyHandlers.ofFile(tempFile));` // executes the request and stores the response body in the temporary file |
| 8 | EXEC | `System.out.println(response.statusCode());` // prints the HTTP status code for operational visibility |
| 9 | EXEC | `System.out.println(response.body());` // prints the resulting file path returned by the body handler |
| 10 | RETURN | `}` // exits the method |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `HttpClient` | Technical term | Java 11 HTTP client used to send outbound web requests |
| `HttpRequest` | Technical term | Immutable request definition containing target URI and HTTP method |
| `BodyHandlers.ofFile(...)` | Technical term | Response handler that writes the received body to a file |
| `downloadFile` | Method name | Demonstration method that downloads remote HTML content to a temporary local file |
| `tempFile` | Field / local variable | Temporary storage location for downloaded content |
| `GET` | HTTP method | Read-only retrieval operation for remote resources |
| `https://labs.consol.de/` | External endpoint | Fixed sample web resource used as the download target |
| `System.out.println` | Technical term | Console output used to display status and file path |
| `HTML` | Business term | HyperText Markup Language — web document format saved by the download |
