Skip to content
Snippets Groups Projects
StartHandler.java 6.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    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;
    
    A C's avatar
    A C committed
    import java.security.NoSuchAlgorithmException;
    
    A C's avatar
    A C committed
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    
    A C's avatar
    A C committed
    import org.json.JSONException;
    
    
    A C's avatar
    A C committed
    import com.sun.net.httpserver.Headers;
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    
    
    A C's avatar
    A C committed
    import db.DBC;
    
    A C's avatar
    A C committed
    import db.Dominio;
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    public class StartHandler implements HttpHandler {
    
    	@Override
    	public void handle(HttpExchange he) throws IOException {
    		URI requestedUri = he.getRequestURI();
    		System.out.println(requestedUri.toString());
    		/*
    
    A C's avatar
    A C committed
    		 * 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; }
    		 */
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    		String requestMethod = he.getRequestMethod();
    		String query = requestedUri.getRawQuery();
    		String body = readBody(he.getRequestBody());
    
    A C's avatar
    A C committed
    		String response = "";
    
    A C's avatar
    A C committed
    		//String user = he.getRequestHeaders().get("user").get(0);
    		String token=he.getRequestHeaders().get("Authorization").get(0).substring(7);
    		String user = "";
    
    A C's avatar
    A C committed
    
    		// se dominio del body ha admin chi fa chiamata allora continua
    
    A C's avatar
    A C committed
    		
    		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();
    		}
    		
    		
    
    A C's avatar
    A C committed
    		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");
    
    A C's avatar
    A C committed
    			try {
    
    
    A C's avatar
    A C committed
    //				Dominio d = DBC.getDom(dominio);
    //				String s = user + "-A";
    				ArrayList<String> ad= DBC.getDomainsAdmin(dominio);
    
    A C's avatar
    A C committed
    				/*
    				 * 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; }
    				 */
    
    A C's avatar
    A C committed
    				if (!ad.contains(user)) {//if (!d.getUsers().contains(s)) {
    
    A C's avatar
    A C committed
    					// he.sendResponseHeaders(401,0 );
    					System.out.println("NON AUTORIZZATO");
    					response = "NON AUTORIZZATO";
    					he.sendResponseHeaders(401, response.length());
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    					OutputStream os = he.getResponseBody();
    
    A C's avatar
    A C committed
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    					os.write(response.getBytes());
    					os.close();
    
    A C's avatar
    A C committed
    
    					// System.out.println("OPERAZIONE NON IMPLEMENTATA");
    					// he.sendResponseHeaders(501,0);
    					// OutputStream os = he.getResponseBody();
    					// os.write("OPERAZIONE NON IMPLEMENTATA".getBytes());
    					// os.close();
    
    					return;
    				}
    
    
    A C's avatar
    A C committed
    			} catch (SQLException|JSONException e) {
    
    A C's avatar
    A C committed
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    
    
    A C's avatar
    A C committed
    			URL url = new URL("http://localhost:3002/start");// maybe, se CloudApp è in localhost porta 8080
    
    A C's avatar
    A C committed
    			HttpURLConnection con = (HttpURLConnection) url.openConnection();
    			con.setRequestMethod("POST");
    			con.setRequestProperty("Content-Type", "application/json");
    			con.setRequestProperty("version", "1.0");
    
    
    A C's avatar
    A C committed
    			//Map<String, String> parameters = new HashMap<>();
    
    A C's avatar
    A C committed
    			// 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
    
    A C's avatar
    A C committed
    			//parameters.put("domain", dominio);
    
    A C's avatar
    A C committed
    
    			con.setDoOutput(true);
    			// System.out.println("pino2");
    			DataOutputStream out = new DataOutputStream(con.getOutputStream());// inserimento param in call
    
    A C's avatar
    A C committed
    			out.writeBytes(body.toString());//(ParameterStringBuilder.getParamsString(parameters));//j.toString();
    
    A C's avatar
    A C committed
    
    			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;
    
    
    A C's avatar
    A C committed
    //			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();
    //			}
    
    A C's avatar
    A C committed
    
    			con.disconnect();
    
    			he.sendResponseHeaders(status, response.length());
    			OutputStream os = he.getResponseBody();
    			os.write(response.getBytes());
    			os.close();
    
    		}
    
    
    A C's avatar
    A C committed
    //		else {
    //			System.out.println("OPERAZIONE NON IMPLEMENTATA");
    //			he.sendResponseHeaders(501, 0);
    //			OutputStream os = he.getResponseBody();
    //			os.write("OPERAZIONE NON IMPLEMENTATA".getBytes());
    //			os.close();
    //		}
    
    A C's avatar
    A C committed
    	}
    
    	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();