# (DD02) 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 an end-to-end HTTP client invocation against a protected web endpoint using Basic Authentication. It configures a Java 11 `HttpClient` with an `Authenticator`, supplies a fixed username and password, and then performs a synchronous `GET` request to `https://labs.consol.de`.

From a business perspective, the method represents a connectivity and authentication sample rather than a domain transaction. Its purpose is to show how the application can access an external service that requires credential-based access, retrieve the response body, and inspect the HTTP status code for success or failure handling.

The method follows a simple builder-and-delegation pattern: build the client, build the request, send the request, and print the response. The only conditional behavior exists inside the JDK authentication callback, where the client asks for credentials and the method returns the configured username/password pair. In the larger system, this method acts as a standalone example entry point within the HTTP sample package and does not delegate to project-specific services, persistence, or reusable business components.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
START(["basicAuth()"])
B1["Create HttpClient builder"]
B2["Attach Authenticator callback"]
B3["Override getPasswordAuthentication()"]
B4["Return credentials username and password"]
B5["Build HttpRequest for https://labs.consol.de"]
B6["Send request with BodyHandlers.ofString()"]
B7["Read response statusCode()"]
B8["Read response body()"]
END_NODE(["Return / Next"])
START --> B1
B1 --> B2
B2 --> B3
B3 --> B4
B4 --> B5
B5 --> B6
B6 --> B7
B7 --> B8
B8 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method accepts no input parameters. Its behavior is fully determined by internal literals, the configured authenticator, and the fixed target URL. |

**Instance fields / external state read:** None. The method does not reference class fields, shared caches, database state, or request-scoped context. It relies only on local variables, the JDK HTTP client API, and the remote endpoint response.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `HttpClient.send` | N/A | External HTTP endpoint `https://labs.consol.de` | Sends a synchronous HTTP GET request and reads the remote response body for authentication verification. |
| R | `Authenticator.getPasswordAuthentication` | N/A | JDK authentication callback | Reads credentials from the method-defined authentication handler when the HTTP client requests authorization data. |
| R | `HttpResponse.statusCode` | N/A | Response metadata | Reads the HTTP status code to confirm whether the authenticated request succeeded. |
| R | `HttpResponse.body` | N/A | Response payload | Reads the response payload as a string for console output and inspection. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Direct entry point from `Example` example class | `Example.basicAuth()` | `HttpClient.send [R] External HTTP endpoint` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] (L133-L151)

> Builds a basic-auth-enabled HTTP client, creates a GET request, sends it to the remote site, and prints the outcome.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newBuilder()` |
| 2 | EXEC | `.authenticator(new Authenticator() { ... })` // attach credential callback |
| 3 | EXEC | `protected PasswordAuthentication getPasswordAuthentication()` // JDK requests credentials |
| 4 | RETURN | `return new PasswordAuthentication("username", "password".toCharArray());` |
| 5 | CALL | `build()` // finalize the HttpClient |
| 6 | SET | `HttpRequest request = HttpRequest.newBuilder()` |
| 7 | EXEC | `.uri(new URI("https://labs.consol.de"))` // configure target endpoint |
| 8 | EXEC | `.GET()` // configure HTTP method |
| 9 | CALL | `build()` // finalize the request |
| 10 | SET | `HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());` |
| 11 | CALL | `client.send(request, HttpResponse.BodyHandlers.ofString())` |
| 12 | EXEC | `HttpResponse.BodyHandlers.ofString()` // define string response body handling |
| 13 | EXEC | `System.out.println(response.statusCode());` // print HTTP status |
| 14 | CALL | `response.statusCode()` |
| 15 | EXEC | `System.out.println(response.body());` // print response content |
| 16 | CALL | `response.body()` |
| 17 | RETURN | `}` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `basicAuth` | Method | Demonstration method that performs an authenticated HTTP request using Basic Authentication. |
| `HttpClient` | Technical term | Java 11 HTTP client used to send synchronous or asynchronous web requests. |
| `Authenticator` | Technical term | JDK callback that supplies credentials when a remote server demands authentication. |
| `PasswordAuthentication` | Technical term | JDK credential container holding the username and password used for authorization. |
| `BodyHandlers.ofString()` | Technical term | Response body handler that converts the HTTP payload into a Java `String`. |
| `statusCode()` | Technical term | HTTP response status accessor used to determine the server result code. |
| `body()` | Technical term | HTTP response payload accessor used to read the returned content. |
| Basic Authentication | Business term | Standard HTTP authentication scheme where the client provides a username and password to access a protected resource. |
| `https://labs.consol.de` | External endpoint | Target demo website used by the sample to validate authenticated HTTP access. |
| `GET` | HTTP method | Read-only request method used to retrieve remote content without modifying server state. |
| `URI` | Technical term | Uniform Resource Identifier that identifies the remote HTTP resource being requested. |
