package code; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.security.NoSuchAlgorithmException; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONException; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import db.DBC; import db.Dominio; public class StartHandler implements HttpHandler { @Override public void handle(HttpExchange he) throws IOException { URI requestedUri = he.getRequestURI(); System.out.println(requestedUri.toString()); /* * if(he.getRequestHeaders().get("version")==null) { he.sendResponseHeaders(426, * "VERSIONE NON PRESENTE. (USARE -H version:1.0)".length()); OutputStream os = * he.getResponseBody(); * os.write("VERSIONE NON PRESENTE. (USARE -H version:1.0)".getBytes()); * os.close(); return; } else * if(he.getRequestHeaders().get("version").get(0).compareTo("1.0")!=0) { * he.sendResponseHeaders(426, * "CAMBIA VERSIONE. (USARE -H version:1.0)".length()); OutputStream os = * he.getResponseBody(); * os.write("CAMBIA VERSIONE. (USARE -H version:1.0)".getBytes()); os.close(); * return; } */ String requestMethod = he.getRequestMethod(); String query = requestedUri.getRawQuery(); String body = readBody(he.getRequestBody()); String response = ""; //String user = he.getRequestHeaders().get("user").get(0); String token=he.getRequestHeaders().get("Authorization").get(0).substring(7); String user = ""; // se dominio del body ha admin chi fa chiamata allora continua try { //JSONObject tok=new JSONObject(token); //String accessToken=tok.getString("access_token"); String[] tokSplit=token.split("."); if(tokSplit.length!=3)return;//controllo che il token abbia header,body e signature(abbia 2 punti :s) //int scnddot=accessToken.lastIndexOf(".");//dopo questo indice è tutta signature String signature=tokSplit[2]; user=TokenHandler.verificaToken(token,signature); if(user.equals(""))return; } catch (NoSuchAlgorithmException | IOException | JSONException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } if (requestMethod.compareToIgnoreCase("POST") == 0) { String dominio = body.substring(7); System.out.println( requestMethod + "\n" + query + "\n" + body + "\n" + response + "\n" + user + "\n" + dominio + "\n"); try { // Dominio d = DBC.getDom(dominio); // String s = user + "-A"; ArrayList<String> ad= DBC.getDomainsAdmin(dominio); /* * 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 (!ad.contains(user)) {//if (!d.getUsers().contains(s)) { // he.sendResponseHeaders(401,0 ); System.out.println("NON AUTORIZZATO"); response = "NON AUTORIZZATO"; he.sendResponseHeaders(401, response.length()); OutputStream os = he.getResponseBody(); os.write(response.getBytes()); os.close(); // System.out.println("OPERAZIONE NON IMPLEMENTATA"); // he.sendResponseHeaders(501,0); // OutputStream os = he.getResponseBody(); // os.write("OPERAZIONE NON IMPLEMENTATA".getBytes()); // os.close(); return; } } catch (SQLException|JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } URL url = new URL("http://localhost:3000/start");// maybe, se CloudApp è in localhost porta 8080 HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("version", "1.0"); //Map<String, String> parameters = new HashMap<>(); // System.out.println("pino1"); // {“domain”:”nome_dominio”} check utente chiamante ha permessi su dominio // chiamato, chi fa install è admin // parameters.put("param1", "val");// fix parametri da mandare //parameters.put("domain", dominio); con.setDoOutput(true); // System.out.println("pino2"); DataOutputStream out = new DataOutputStream(con.getOutputStream());// inserimento param in call out.writeBytes(body.toString());//(ParameterStringBuilder.getParamsString(parameters));//j.toString(); out.flush(); out.close(); // System.out.println("pino3"); // con.setRequestProperty("Content-Type", "application/json"); // String contentType = con.getHeaderField("Content-Type"); con.setConnectTimeout(5000); con.setReadTimeout(5000); // System.out.println("pino4"); // leggo risposta int status = con.getResponseCode(); Reader streamReader = null; // if (status > 299) { // System.out.println("pino5"); // streamReader = new InputStreamReader(con.getErrorStream()); // BufferedReader in = new BufferedReader(streamReader); // String inputLine; // StringBuffer content = new StringBuffer(); // while ((inputLine = in.readLine()) != null) { // content.append(inputLine); // } // response = content.toString(); // in.close(); // } else { // System.out.println("pino6"); // streamReader = new InputStreamReader(con.getInputStream()); // BufferedReader in = new BufferedReader(streamReader); // String inputLine; // StringBuffer content = new StringBuffer(); // while ((inputLine = in.readLine()) != null) { // content.append(inputLine); // } // response = content.toString(); // in.close(); // } con.disconnect(); he.sendResponseHeaders(status, response.length()); OutputStream os = he.getResponseBody(); os.write(response.getBytes()); os.close(); } // else { // System.out.println("OPERAZIONE NON IMPLEMENTATA"); // he.sendResponseHeaders(501, 0); // OutputStream os = he.getResponseBody(); // os.write("OPERAZIONE NON IMPLEMENTATA".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(); } }