Newer
Older
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
package code;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
//import StuffHandler.CustomObject;
import java.nio.file.Files;
import java.nio.file.Path;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpPrincipal;
import java.io.*;
import java.util.*;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
public class InstallHandler implements HttpHandler{
@Override
public void handle(HttpExchange he) throws IOException {
URI requestedUri = he.getRequestURI();
String requestMethod = he.getRequestMethod();
String response = "";
if (requestMethod.compareTo("POST") == 0 || requestMethod.compareTo("post") == 0) {
InputStream is = he.getRequestBody();
String g = readBody(is);
//BufferedWriter f = new BufferedWriter(new FileWriter(dati, true));//////////////////FIX
String[] s = g.split(":");
//String s0 = "\"" + s[0] + "\"";
//String s1 = "\"" + s[1] + "\"";
//CustomObject o=new CustomObject(s0,s1);
//Boolean trovato=false;
/*for(CustomObject o:cose) {
if(o.nome.compareTo(s0)==0) {
trovato=true;
}
}
if(!trovato) {
f.append(s0 + ":" + s1 + "\n");
response = "Done\n";
}
else response="Object already exists!\n";*/
System.out.println(g);
//effettuo chiamata a CloudAppManager
//preso da https://www.baeldung.com/java-http-request
//è una chiamata annidata nella risposta alla webapp
// -richiesta REST da webApp a /install
// -prendo da DB e poi chiamo CloudAppMng su /install
// -attendo risposta da CloudAppMng e chiudo
// -rispondo a webApp e chiudo
// EZ
//
//standard per chiamata in slide
//https://www.dir.uniupo.it/pluginfile.php/948883/mod_resource/content/1/FrameworkProgetto5.pdf
//
//http://127.0.0.1:8080/install
URL url = new URL("http://127.0.0.1:3000");//maybe, se CloudAppe è in localhost porta 8080
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");//fix parametri da mandare
//leggo da DB domini e riempio (magari famo .DAO??)
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());//inserimento param in call
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
//con.setRequestProperty("Content-Type", "application/json");
//String contentType = con.getHeaderField("Content-Type");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
//leggo risposta
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
//finita chiamata a CloudApp
//f.close();
OutputStream os = he.getResponseBody();
he.sendResponseHeaders(status, response.length());//status
os.write(response.getBytes());
os.close();
}
}
private String readBody(InputStream requestBody) {
int req;
StringBuffer sb = new StringBuffer();
try {
while ((req = requestBody.read()) != -1)
sb.append(Character.toString((char) req));
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}