1
+ package com .baeldung .rest ;
2
+
3
+ import com .sun .net .httpserver .HttpExchange ;
4
+ import com .sun .net .httpserver .HttpHandler ;
5
+ import com .sun .net .httpserver .HttpServer ;
6
+ import java .io .*;
7
+ import java .net .InetSocketAddress ;
8
+ import java .nio .charset .StandardCharsets ;
9
+ import java .util .ArrayList ;
10
+ import java .util .List ;
11
+
12
+ public class RestApiServer implements HttpHandler {
13
+
14
+ private final List <String > users = new ArrayList <>();
15
+
16
+ @ Override
17
+ public void handle (HttpExchange exchange ) throws IOException {
18
+ String method = exchange .getRequestMethod ();
19
+ switch (method ) {
20
+ case "GET" -> handleGet (exchange );
21
+ case "POST" -> handlePost (exchange );
22
+ case "PUT" -> handlePut (exchange );
23
+ case "DELETE" -> handleDelete (exchange );
24
+ default -> sendResponse (exchange , 405 , "Method Not Allowed" );
25
+ }
26
+ }
27
+
28
+ private void handleGet (HttpExchange exchange ) throws IOException {
29
+ sendResponse (exchange , 200 , users .toString ());
30
+ }
31
+
32
+ private void handlePost (HttpExchange exchange ) throws IOException {
33
+ String newUser = readRequestBody (exchange );
34
+ if (!newUser .isBlank ()) {
35
+ users .add (newUser );
36
+ sendResponse (exchange , 201 , "User added: " + newUser );
37
+ } else {
38
+ sendResponse (exchange , 400 , "Invalid user data" );
39
+ }
40
+ }
41
+
42
+ private void handlePut (HttpExchange exchange ) throws IOException {
43
+ String body = readRequestBody (exchange );
44
+ String [] parts = body .split (":" , 2 );
45
+ if (parts .length == 2 ) {
46
+ int index = Integer .parseInt (parts [0 ]);
47
+ String newName = parts [1 ];
48
+ if (index >= 0 && index < users .size ()) {
49
+ users .set (index , newName );
50
+ sendResponse (exchange , 200 , "User updated: " + newName );
51
+ } else {
52
+ sendResponse (exchange , 404 , "User not found" );
53
+ }
54
+ } else {
55
+ sendResponse (exchange , 400 , "Invalid input format" );
56
+ }
57
+ }
58
+
59
+ private void handleDelete (HttpExchange exchange ) throws IOException {
60
+ String body = readRequestBody (exchange );
61
+ int index ;
62
+ try {
63
+ index = Integer .parseInt (body );
64
+ if (index >= 0 && index < users .size ()) {
65
+ String removedUser = users .remove (index );
66
+ sendResponse (exchange , 200 , "User deleted: " + removedUser );
67
+ } else {
68
+ sendResponse (exchange , 404 , "User not found" );
69
+ }
70
+ } catch (NumberFormatException e ) {
71
+ sendResponse (exchange , 400 , "Invalid index" );
72
+ }
73
+ }
74
+
75
+ private void sendResponse (HttpExchange exchange , int statusCode , String response ) throws IOException {
76
+ exchange .sendResponseHeaders (statusCode , response .length ());
77
+ OutputStream os = exchange .getResponseBody ();
78
+ os .write (response .getBytes (StandardCharsets .UTF_8 ));
79
+ os .close ();
80
+ }
81
+
82
+ private String readRequestBody (HttpExchange exchange ) throws IOException {
83
+ InputStream is = exchange .getRequestBody ();
84
+ return new String (is .readAllBytes (), StandardCharsets .UTF_8 );
85
+ }
86
+
87
+ // Helper methods for testing
88
+ public List <String > getUsers () {
89
+ return new ArrayList <>(users ); // Return a copy to prevent external modification
90
+ }
91
+
92
+ public void addUser (String user ) {
93
+ users .add (user );
94
+ }
95
+
96
+ public void clearUsers () {
97
+ users .clear ();
98
+ }
99
+
100
+ public static void main (String [] args ) throws IOException {
101
+ HttpServer server = HttpServer .create (new InetSocketAddress (8080 ), 0 );
102
+ server .createContext ("/users" , new RestApiServer ());
103
+ server .setExecutor (null );
104
+ System .out .println ("Server started at http://localhost:8080/users" );
105
+ server .start ();
106
+ }
107
+ }
0 commit comments