-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay19s1.java
More file actions
189 lines (163 loc) · 6.78 KB
/
Day19s1.java
File metadata and controls
189 lines (163 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.*;
/**
* Basit HTTP Sunucusu ve REST API Örneği
* Bu program, temel bir HTTP sunucusu oluşturur ve basit bir REST API sunar.
* Kullanıcı bilgilerini yönetmek için CRUD operasyonları içerir.
*/
public class Day19s1 {
private static final int PORT = 8080;
private static final Map<Integer, User> users = new ConcurrentHashMap<>();
private static int nextUserId = 1;
public static void main(String[] args) {
System.out.println("*** BASİT HTTP SUNUCUSU VE REST API ***");
System.out.println("Sunucu " + PORT + " portunda başlatılıyor...");
// Örnek kullanıcılar ekle
users.put(nextUserId++, new User("Ahmet", "ahmet@email.com"));
users.put(nextUserId++, new User("Ayşe", "ayse@email.com"));
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Sunucu başlatıldı. Bağlantılar bekleniyor...");
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(() -> handleClient(clientSocket)).start();
}
} catch (IOException e) {
System.err.println("Sunucu hatası: " + e.getMessage());
e.printStackTrace();
}
}
private static void handleClient(Socket clientSocket) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
// HTTP isteğini oku
String requestLine = in.readLine();
if (requestLine == null) return;
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
String path = requestParts[1];
// İstek gövdesini oku
StringBuilder body = new StringBuilder();
String line;
while ((line = in.readLine()) != null && !line.isEmpty()) {
body.append(line).append("\n");
}
// Yanıt oluştur
String response = handleRequest(method, path, body.toString());
// HTTP yanıtını gönder
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: application/json");
out.println("Access-Control-Allow-Origin: *");
out.println();
out.println(response);
} catch (IOException e) {
System.err.println("İstemci hatası: " + e.getMessage());
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String handleRequest(String method, String path, String body) {
try {
switch (method) {
case "GET":
return handleGet(path);
case "POST":
return handlePost(body);
case "PUT":
return handlePut(path, body);
case "DELETE":
return handleDelete(path);
default:
return createErrorResponse("Desteklenmeyen HTTP metodu: " + method);
}
} catch (Exception e) {
return createErrorResponse("İşlem hatası: " + e.getMessage());
}
}
private static String handleGet(String path) {
if (path.equals("/api/users")) {
return createSuccessResponse(users.values());
} else if (path.startsWith("/api/users/")) {
int id = Integer.parseInt(path.substring("/api/users/".length()));
User user = users.get(id);
if (user != null) {
return createSuccessResponse(user);
}
return createErrorResponse("Kullanıcı bulunamadı");
}
return createErrorResponse("Geçersiz endpoint");
}
private static String handlePost(String body) {
try {
User newUser = parseUser(body);
int id = nextUserId++;
users.put(id, newUser);
return createSuccessResponse("Kullanıcı oluşturuldu. ID: " + id);
} catch (Exception e) {
return createErrorResponse("Kullanıcı oluşturma hatası: " + e.getMessage());
}
}
private static String handlePut(String path, String body) {
if (!path.startsWith("/api/users/")) {
return createErrorResponse("Geçersiz endpoint");
}
try {
int id = Integer.parseInt(path.substring("/api/users/".length()));
if (!users.containsKey(id)) {
return createErrorResponse("Kullanıcı bulunamadı");
}
User updatedUser = parseUser(body);
users.put(id, updatedUser);
return createSuccessResponse("Kullanıcı güncellendi");
} catch (Exception e) {
return createErrorResponse("Kullanıcı güncelleme hatası: " + e.getMessage());
}
}
private static String handleDelete(String path) {
if (!path.startsWith("/api/users/")) {
return createErrorResponse("Geçersiz endpoint");
}
try {
int id = Integer.parseInt(path.substring("/api/users/".length()));
if (users.remove(id) != null) {
return createSuccessResponse("Kullanıcı silindi");
}
return createErrorResponse("Kullanıcı bulunamadı");
} catch (Exception e) {
return createErrorResponse("Kullanıcı silme hatası: " + e.getMessage());
}
}
private static User parseUser(String json) {
// Basit JSON parsing
json = json.replaceAll("[{}\"]", "");
String[] parts = json.split(",");
String name = parts[0].split(":")[1].trim();
String email = parts[1].split(":")[1].trim();
return new User(name, email);
}
private static String createSuccessResponse(Object data) {
return "{\"success\": true, \"data\": " + data + "}";
}
private static String createErrorResponse(String message) {
return "{\"success\": false, \"error\": \"" + message + "\"}";
}
private static class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public String toString() {
return "{\"name\": \"" + name + "\", \"email\": \"" + email + "\"}";
}
}
}