package code; import java.io.IOException; import java.net.HttpURLConnection; import java.sql.SQLException; import java.util.ArrayList; import org.json.JSONException; import org.json.JSONObject; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import db.DBC; import db.Dominio; public class DeleteHandler implements HttpHandler { @Override public void handle(HttpExchange he) throws IOException { String requestMethod = he.getRequestMethod(); if (requestMethod.compareToIgnoreCase("options") == 0) { Helper.sendCors(he, 200); return; } if (requestMethod.compareToIgnoreCase("POST") != 0) { Helper.sendCors(he, 405); return; } // String query = requestedUri.getRawQuery(); String body = Helper.readBody(he.getRequestBody()); String user; if ((user = Helper.checkTokenGetUser(he)) == null) { Helper.sendCors(he, 401); return; } try { String dominio = new JSONObject(body).getString("domain"); Dominio d = DBC.getDom(dominio); // String s = user + "-A"; /* * for(Dominio d : doms){ if( (d.getDomain() == dominio) && * d.getUsers().contains(s)) //something here * System.out.println("OPERAZIONE NON IMPLEMENTATA"); * he.sendResponseHeaders(401,response.length()); OutputStream os = * he.getResponseBody(); os.write("NON AUTORIZZATO".getBytes()); os.close(); * return; } */ if (d == null) { Helper.sendCors(he, 404); return; } ArrayList<String> ad = DBC.getDomainsAdmin(user); if (!ad.contains(dominio)) { Helper.sendCors(he, 401); return; } DBC.delDom(dominio); } catch (SQLException | JSONException e) { e.printStackTrace(); return; } // 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(Helper.getCloudappURL()+"delete");//maybe, se CloudAppe è * in localhost porta 8080 * //aggiungere 3000/delete * HttpURLConnection con = (HttpURLConnection) url.openConnection(); * con.setRequestMethod("POST"); * con.setRequestProperty("Content-Type", "application/json"); * con.setRequestProperty("Accept", "application/json"); * con.setDoOutput(true); * con.setConnectTimeout(5000); * con.setReadTimeout(5000); * * DataOutputStream out = new * DataOutputStream(con.getOutputStream());//inserimento param in call * out.writeBytes(body); * out.flush(); * out.close(); * * //con.setRequestProperty("Content-Type", "application/json"); * //String contentType = con.getHeaderField("Content-Type"); * * //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(); */ HttpURLConnection con = Helper.sendMessageToCloudapp("install", body); int status = con.getResponseCode(); // FIXME serve avere anche il content? String cloudappResponse = Helper.getResponseFromConnection(con); con.disconnect(); Helper.sendCors(he, status); } }