Skip to content
Snippets Groups Projects
Home.java 4.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    package code;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    
    public class Home implements HttpHandler {
    
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            String requestMethod = exchange.getRequestMethod();
            if (Helper.compareText(requestMethod, "GET")) {
                List<String> strlist = new ArrayList<>();
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                String response = null;
    
    Elisa Giglio's avatar
    Elisa Giglio committed
                response = getHomePage();
                strlist.add("text/html");
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                if(response != null && !Helper.compareText(response, "fail")){
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                    exchange.getResponseHeaders().put("content-type", strlist);
                    exchange.sendResponseHeaders(200, response.getBytes().length);
                    OutputStream os = exchange.getResponseBody();
                    os.write(response.getBytes());
                    os.close();
                } else {
                    exchange.sendResponseHeaders(500, response.getBytes().length);
                    OutputStream os = exchange.getResponseBody();
                    os.write(response.getBytes());
                    os.close();
                }
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            } else {
                Helper.methodNotAllowed(exchange);
            }
        }
    
    
        private static String getHomePage() {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            String line;
    //        String pageIDString = pageid.toString();
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            String page = Server.CLIENT_PATH+"/index.html";// + pageIDString.substring(0, pageIDString.length() - 1) + ".txt";// entro nella cartella
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
            // html e leggo il file
            // txt
    
            StringBuilder answer = new StringBuilder();
            if (getExtension(page).length() == 0)
                page += ".html";
    
            BufferedReader bufferedReader = null;
            try {
                FileReader fileReader = new FileReader(page);
    
                bufferedReader = new BufferedReader(fileReader);
                boolean isComment = false;
                while ((line = bufferedReader.readLine()) != null) {
                	line = line.trim();
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                	if(line.startsWith("<!--") && line.endsWith("-->")) {
                		continue;
                	}
                	if(line.startsWith("<!--")) {
                		isComment = true;
                		continue;
                	}
                	if(line.endsWith("-->")) {
                		isComment = false;
                		continue;
                	}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
                	if(!isComment && line.length()>0)
                    	answer.append(line).append("\n");
                }
            } catch (FileNotFoundException ex) {
                System.out.println("Unable to open file '" + page + "'");
                return "fail";
            } catch (IOException ex) {
                System.out.println("Error reading file '" + page + "'");
                return "fail";
            } finally {
                try{
                    if(bufferedReader != null)
                        bufferedReader.close();
                } catch (IOException ex){
                    System.out.println("Error closing bufferedReader");
                }
            }
            return answer.toString();
        }
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        private static String getKeycloak(){
            String page = Server.CLIENT_PATH+"/keycloak.json";
            BufferedReader bufferedReader = null;
            StringBuilder answer = new StringBuilder();
            try {
                FileReader fileReader = new FileReader(page);
                bufferedReader = new BufferedReader(fileReader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    answer.append(line.trim());
                }
    
                JSONObject js = new JSONObject(answer.toString());
                return js.toString();
            } catch (FileNotFoundException ex) {
                System.out.println("Unable to open file '" + page + "'");
                return "fail";
            } catch (IOException ex) {
                System.out.println("Error reading file '" + page + "'");
                return "fail";
            } catch (JSONException e) {
                System.out.println("The file doesn't contain a JSON '" + page + "'");
                return "fail";
            } finally {
                try{
                    if(bufferedReader != null)
                        bufferedReader.close();
                } catch (IOException ex){
                    System.out.println("Error closing bufferedReader");
                }
            }
        }
    
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
        private static String getExtension(String file) {
            int i = file.length() - 1;
            while (i > 0 && file.charAt(i) != '.' && file.charAt(i) != '/')
                i--;
            if (file.charAt(i) == '.')
                return file.substring(i + 1);
            else
                return "";
        }
    
    }