-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCodeServer.java
More file actions
151 lines (136 loc) · 5.53 KB
/
MorseCodeServer.java
File metadata and controls
151 lines (136 loc) · 5.53 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
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class MorseCodeServer {
private static final Map<Character, String> ENC = new HashMap<>();
private static final Map<String, Character> DEC = new HashMap<>();
static {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
String[] morse = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..",
".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----"
};
for (int i = 0; i < alphabet.length(); i++) {
ENC.put(alphabet.charAt(i), morse[i]);
DEC.put(morse[i], alphabet.charAt(i));
}
// Space between words -> slash
ENC.put(' ', "/");
DEC.put("/", ' ');
}
public static void main(String[] args) throws Exception {
int port = 8081; // default
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/encode", exchange -> {
if ("OPTIONS".equalsIgnoreCase(exchange.getRequestMethod())) {
handlePreflight(exchange);
return;
}
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
sendCors(exchange, 405, "Method Not Allowed");
return;
}
String body = readBody(exchange);
String result = encode(body);
sendText(exchange, 200, result);
});
// Decode endpoint
server.createContext("/decode", exchange -> {
if ("OPTIONS".equalsIgnoreCase(exchange.getRequestMethod())) {
handlePreflight(exchange);
return;
}
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
sendCors(exchange, 405, "Method Not Allowed");
return;
}
String body = readBody(exchange);
String result;
try {
result = decode(body);
} catch (IllegalArgumentException ex) {
result = "Error: " + ex.getMessage();
sendText(exchange, 400, result);
return;
}
sendText(exchange, 200, result);
});
server.setExecutor(null);
System.out.println("MorseCodeServer running on http://localhost:" + port + "/");
server.start();
}
private static String encode(String input) {
if (input == null) return "";
StringBuilder out = new StringBuilder();
for (char ch : input.toUpperCase().toCharArray()) {
if (ENC.containsKey(ch)) {
out.append(ENC.get(ch)).append(' ');
}
}
return out.toString().trim();
}
private static String decode(String morse) {
if (morse == null || morse.trim().isEmpty()) return "";
StringBuilder out = new StringBuilder();
String[] tokens = morse.trim().replaceAll("\\s+", " ").split(" ");
for (String token : tokens) {
Character c = DEC.get(token);
if (c != null) {
out.append(c);
} else {
throw new IllegalArgumentException("Unknown Morse token: '" + token + "'");
}
}
return out.toString();
}
private static String readBody(HttpExchange exchange) throws IOException {
try (InputStream is = exchange.getRequestBody()) {
byte[] buf = is.readAllBytes();
return new String(buf, StandardCharsets.UTF_8).trim();
}
}
private static void handlePreflight(HttpExchange exchange) throws IOException {
Headers h = exchange.getResponseHeaders();
addCors(h);
h.add("Access-Control-Allow-Methods", "POST, OPTIONS");
h.add("Access-Control-Allow-Headers", "Content-Type");
exchange.sendResponseHeaders(204, -1);
exchange.close();
}
private static void sendText(HttpExchange exchange, int code, String text) throws IOException {
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
Headers h = exchange.getResponseHeaders();
h.add("Content-Type", "text/plain; charset=utf-8");
addCors(h);
exchange.sendResponseHeaders(code, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
private static void sendCors(HttpExchange exchange, int code, String msg) throws IOException {
byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
Headers h = exchange.getResponseHeaders();
addCors(h);
h.add("Content-Type", "text/plain; charset=utf-8");
exchange.sendResponseHeaders(code, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
private static void addCors(Headers h) {
h.add("Access-Control-Allow-Origin", "*");
}
}