Skip to content
Snippets Groups Projects
InstallHandler.java 4.79 KiB
package code;


import java.net.HttpURLConnection;

import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpServer;
//import StuffHandler.CustomObject;

import db.DBC;
import db.Dominio;

import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;

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.lang.reflect.Array;
import java.util.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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 = "ayyyyNstall\n";
		
		
		String query = requestedUri.getRawQuery();
		String body = readBody(he.getRequestBody());
		
		String user = he.getRequestHeaders().get("user").get(0);
		System.out.println(body);
		
		if (requestMethod.compareTo("POST") == 0 || requestMethod.compareTo("post") == 0) {
			
			
			try {
				JSONObject j=new JSONObject(body);
				System.out.println(j.toString());
				String dm= j.getString("domain");
				System.out.println(
						requestMethod + "\n" + query + "\n" + body + "\n" + response + "\n" + user + "\n" + dm + "\n");
				
				try {

					Dominio d = DBC.getDom(dm);
					if (d!=null) {
						System.out.println("DOMINIO GIA' IN USO");
						response = "DOMINIO GIA' IN USO";
						he.sendResponseHeaders(401, response.length());
						OutputStream os = he.getResponseBody();

						os.write(response.getBytes());
						os.close();
						return;
					}else {
						String s = user + "-A";
						
						JSONArray arr=j.toJSONArray(j.names());
						String usrs= arr.get(3).toString();
						usrs=usrs.concat(","+s);
						j.put("users", usrs);
						//JSONObject person =  jsonArray.getJSONObject(0).getJSONObject("person");
						//person.put("name", "Sammie");
						
						System.out.println("kek"+j);
						DBC.insertDom(j);
						
						
						
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				

			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			
			
			
			

			
			
			
			
			//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();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}