---

# (DD03) Business Logic — Example.basicAuth() [19 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.basicAuth()

This method demonstrates how to call an HTTP endpoint that is protected by Basic Authentication using the Java 11 `HttpClient` API. It builds a client with an `Authenticator`, supplies a fixed username and password pair, creates a GET request to the target web URI, and submits the request synchronously. The method then writes the HTTP status code and response body to the console, making it a simple end-to-end connectivity and credential-validation example rather than a reusable service operation.

From a business perspective, this method acts as a technical sample for authenticated outbound communication with a remote web service. Its responsibility is limited to one authentication scenario: Basic Auth over HTTPS. The flow follows a straightforward builder-and-dispatch pattern, where the client is configured first, the request is assembled second, and the response is consumed last. In the wider system, it serves as a reference implementation for developers who need to understand how authenticated calls are performed with Java 11 networking primitives.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["basicAuth()"]
    STEP1["Create HttpClient builder"]
    STEP2["Attach Authenticator"]
    STEP3["Return fixed credentials username and password"]
    STEP4["Build HttpClient"]
    STEP5["Create HttpRequest builder"]
    STEP6["Set target URI https://labs.consol.de"]
    STEP7["Configure GET method"]
    STEP8["Build HttpRequest"]
    STEP9["Send request synchronously"]
    STEP10["Capture HttpResponse as String body"]
    STEP11["Print status code"]
    STEP12["Print response body"]
    END_NODE["Return"]

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> STEP4
    STEP4 --> STEP5
    STEP5 --> STEP6
    STEP6 --> STEP7
    STEP7 --> STEP8
    STEP8 --> STEP9
    STEP9 --> STEP10
    STEP10 --> STEP11
    STEP11 --> STEP12
    STEP12 --> END_NODE
```

## 3. Parameter Analysis

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

External state and instance data referenced by this method:
- No instance fields are read.
- The method uses fixed literal values for the username, password, and target URI.
- Console output is the only observable side effect.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| R | `HttpClient.send` | N/A | External HTTPS endpoint `https://labs.consol.de` | Sends an outbound authenticated GET request and reads the remote response body. |
| R | `HttpResponse.statusCode` | N/A | HTTP response metadata | Reads the returned HTTP status code for operational verification. |
| R | `HttpResponse.body` | N/A | HTTP response payload | Reads the response content as a string for display. |
| C | `System.out.println` | N/A | Console output | Writes the response status and payload to standard output for diagnostics. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | No internal Java caller found in the scanned source set | `io.github.biezhi.java11.http.Example.basicAuth` | `HttpClient.send [R] https://labs.consol.de` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENCE] `(client construction)` (L133-L138)

> Creates an HTTP client with a custom authenticator so outbound requests can be automatically supplied with credentials.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newBuilder()` |
| 2 | EXEC | `.authenticator(new Authenticator() { ... })` |
| 3 | CALL | `getPasswordAuthentication()` |
| 4 | RETURN | `new PasswordAuthentication("username", "password".toCharArray())` |
| 5 | CALL | `.build()` |

**Block 1.1** — [METHOD OVERRIDE] `(Authenticator.getPasswordAuthentication)` (L135-L137)

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return new PasswordAuthentication("username", "password".toCharArray());` |

**Block 2** — [SEQUENCE] `(request construction)` (L140-L144)

> Builds the outbound GET request for the target public HTTPS endpoint.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpRequest request = HttpRequest.newBuilder()` |
| 2 | EXEC | `.uri(new URI("https://labs.consol.de"))` |
| 3 | EXEC | `.GET()` |
| 4 | CALL | `.build()` |

**Block 3** — [SEQUENCE] `(request execution and output)` (L146-L148)

> Sends the request synchronously, then prints the HTTP status and body for verification.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());` |
| 2 | CALL | `client.send(request, HttpResponse.BodyHandlers.ofString())` |
| 3 | EXEC | `HttpResponse.BodyHandlers.ofString()` |
| 4 | CALL | `response.statusCode()` |
| 5 | CALL | `response.body()` |
| 6 | CALL | `System.out.println(response.statusCode());` |
| 7 | CALL | `System.out.println(response.body());` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `basicAuth` | Method | Demonstration method for sending a Basic Authentication protected HTTP request. |
| `HttpClient` | Technical term | Java 11 HTTP client used to create and send outbound web requests. |
| `Authenticator` | Technical term | Callback used by the HTTP client to supply credentials when a server requests authentication. |
| `PasswordAuthentication` | Technical term | Container for username and password credentials supplied to the authenticator. |
| `GET` | HTTP method | Read-only request used to retrieve content from the remote endpoint. |
| `URI` | Technical term | Uniform Resource Identifier that identifies the target HTTPS address. |
| `HTTPS` | Acronym | HTTP over TLS; encrypted transport for secure web communication. |
| `Basic Authentication` | Business term | Credential scheme that transmits a username and password to authorize access to a protected endpoint. |
| `response.statusCode()` | Technical term | HTTP status returned by the remote server, used to confirm success or failure. |
| `response.body()` | Technical term | Response payload returned by the remote server, displayed for inspection. |
| `labs.consol.de` | External endpoint | Public sample host used here as the authenticated request target. |
| `standard output` | Technical term | Console stream used to display diagnostic results. |
