Skip to content

Commit 082106d

Browse files
mourjobuger
authored andcommitted
Fix Java middleware example (#358)
* Add encoding/decoding HTTP msgs to Java middleware example. * Add clojure middleware example.
1 parent ecd7e3a commit 082106d

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

examples/middleware/echo.clj

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(ns echo.core
2+
(:gen-class)
3+
(:require [clojure.string :as cs]
4+
[clojure.java.io :as io])
5+
(:import org.apache.commons.codec.binary.Hex
6+
java.io.BufferedReader
7+
java.io.IOException
8+
java.io.InputStreamReader))
9+
10+
11+
(defn transform-http-msg
12+
"Function that transforms/filters the incoming HTTP messages."
13+
[headers body]
14+
;; do actual transformations here
15+
[headers body])
16+
17+
18+
(defn decode-hex-string
19+
"Decode an Hex-encoded string."
20+
[s]
21+
(String. (Hex/decodeHex (.toCharArray s))))
22+
23+
24+
(defn encode-hex-string
25+
"Encode a string to a hex-encoded string."
26+
[^String s]
27+
(String. (Hex/encodeHex (.getBytes s))))
28+
29+
30+
(defn -main
31+
[& args]
32+
(let [br (BufferedReader. (InputStreamReader. System/in))]
33+
(try
34+
(loop [hex-line (.readLine br)]
35+
(let [decoded-req (decode-hex-string hex-line)
36+
37+
;; empty line separates headers from body
38+
http-request (partition-by empty? (cs/split-lines decoded-req))
39+
headers (first http-request)
40+
41+
;; HTTP messages can contain no body:
42+
body (when (= 3 (count http-request)) (last http-request))
43+
[new-headers new-body] (transform-http-msg headers body)]
44+
45+
(println (encode-hex-string (str (cs/join "\n" headers)
46+
(when body
47+
(str "\n\n"
48+
(cs/join "\n" body)))))))
49+
(when-let [line (.readLine br)]
50+
(recur line)))
51+
(catch IOException e nil))))
52+
53+

examples/middleware/echo.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
import java.io.IOException;
33
import java.io.InputStreamReader;
44

5-
public class echo {
6-
public static void main(String[] args) {
5+
import org.apache.commons.codec.DecoderException;
6+
import org.apache.commons.codec.binary.Hex;
7+
8+
9+
public class Echo {
10+
public static String decodeHexString(String s) throws DecoderException {
11+
return new String(Hex.decodeHex(s.toCharArray()));
12+
}
13+
14+
public static String encodeHexString(String s) {
15+
return new String(Hex.encodeHex(s.getBytes()));
16+
}
17+
18+
public static String transformHTTPMessage(String req) {
19+
// do actual transformations here
20+
return req;
21+
}
22+
23+
public static void main(String[] args) throws DecoderException {
724
if(args != null){
825
for(String arg : args){
926
System.out.println(arg);
@@ -17,11 +34,15 @@ public static void main(String[] args) {
1734

1835
try {
1936
while ((line = stdin.readLine()) != null) {
37+
String decodedLine = decodeHexString(line);
38+
39+
String transformedLine = transformHTTPMessage(decodedLine);
2040

21-
System.out.println(line);
41+
String encodedLine = encodeHexString(transformedLine);
42+
System.out.println(encodedLine);
2243

2344
}
2445
} catch (IOException e) {
2546
}
2647
}
27-
}
48+
}

0 commit comments

Comments
 (0)