Skip to content
Snippets Groups Projects
StopHandler.java 5.01 KiB
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;

public class StopHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange he) throws IOException {

		// URI requestedUri = he.getRequestURI();
		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 requestMethod = he.getRequestMethod();
		// String query = requestedUri.getRawQuery();
		String body = Helper.readBody(he.getRequestBody());
		String user;// he.getRequestHeaders().get("user").get(0);

		if ((user = Helper.checkTokenGetUser(he)) == null) {
			Helper.sendCors(he, 401);
			return;
		}

		String domain;
		try {
			domain = new JSONObject(body).getString("domain");

			// Dominio d = DBC.getDom(dominio);
			// String s = user + "-A";

			ArrayList<String> ad = DBC.getDomainsAdmin(user);

			/*
			 * 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(domain)) {
				Helper.sendCors(he, 401);
				return;
			}

		} 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()+"stop");// 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??)
		 * //parameters.put("domain", dominio);
		 *
		 * con.setDoOutput(true);
		 * DataOutputStream out = new DataOutputStream(con.getOutputStream());//
		 * inserimento param in call
		 * //out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
		 * out.writeBytes(body.toString());//era dominio
		 * 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();
		 */

		HttpURLConnection con = Helper.sendMessageToCloudapp("stop", body);
		int status = con.getResponseCode();
		// FIXME serve avere anche il content?
		String cloudappResponse = Helper.getResponseFromConnection(con);
		con.disconnect();

		// 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();
		// }
		//
		// 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
		Helper.sendCors(he, status);
		// cambio lo stato nel DB
		DBC.setStatoDomain(domain, 0);

		// else {
		// System.out.println("OPERAZIONE NON IMPLEMENTATA");
		// he.sendResponseHeaders(501, 0);
		// OutputStream os = he.getResponseBody();
		// os.write("OPERAZIONE NON IMPLEMENTATA".getBytes());
		// os.close();
		// }

	}

}