Skip to content
Snippets Groups Projects
StartHandler.java 3.92 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 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();
		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;
		}

		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;
		}

		/*
		 * URL url = new URL(Helper.getCloudappURL()+"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();
		 */
		HttpURLConnection con = Helper.sendMessageToCloudapp("start", body);
		int status = con.getResponseCode();
		// FIXME serve avere anche il content?
		String cloudappResponse = Helper.getResponseFromConnection(con);
		con.disconnect();

		Helper.sendCors(he, status);
		// cambio lo stato nel DB
		DBC.setStatoDomain(domain, 1);

	}

}