-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathHelloServer.java
116 lines (97 loc) · 4.41 KB
/
HelloServer.java
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
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
import org.nanohttpd.protocols.http.IHTTPSession;
import org.nanohttpd.protocols.http.NanoHTTPD;
import org.nanohttpd.protocols.http.request.Method;
import org.nanohttpd.protocols.http.response.Response;
import org.nanohttpd.util.ServerRunner;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class HelloServer extends NanoHTTPD {
/**
* logger to log to.
*/
private static final Logger LOG = Logger.getLogger(HelloServer.class.getName());
public static void main(String[] args) {
ServerRunner.run(HelloServer.class);
}
public HelloServer() {
super(8085);
}
@Override
public Response serve(IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
String msg;
HelloServer.LOG.info(method + " '" + uri + "' ");
if (method.toString().equals("GET") && uri.equals("/build")) {
Map<String, String> parms = session.getParms();
msg = "<html><body><h1>Hello. Building of a report with " + parms.get("reportId") + "id starts.</h1></body></html>\n";
CompletableFuture.runAsync(() -> {
try {
String id = parms.get("reportId");
Thread.sleep(5000); // LOCK
String url = "http://localhost:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Tested Server");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String msg2= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope/\"" +
"xmlns:wsdl=\"http://www.pleutre.org/mysample\"" +
"xmlns:xd=\"http://www.w3.org/2000/09/xmldsig#\">" +
"<soap:Header/>" +
"<soap:Body>" +
"<wsdl:MySampleResponse>" +
"<wsdl:FunctionalIdentifier>"+ id +"</wsdl:FunctionalIdentifier>" +
"<wsdl:StatusMessage>VALID</wsdl:StatusMessage>" +
"</wsdl:MySampleResponse>" +
"</soap:Body>" +
"</soap:Envelope>";
con.setRequestProperty("content-length",Integer.toString(msg2.length()));
// Send post request
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
writer.write( msg2 );
writer.flush();
writer.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
return Response.newFixedLengthResponse(msg);
}
else
{
msg = "<html><body><h1>Hello too, but its wrong page or method</h1></body></html>\n";
return Response.newFixedLengthResponse(msg);
}
}
}