<commit_before>package umm3601;

import org.apache.xmlbeans.impl.common.ReaderInputStream;
import org.bson.Document;
import spark.Route;
import spark.utils.IOUtils;
import com.mongodb.util.JSON;
import umm3601.digitalDisplayGarden.Authentication.Auth;
import umm3601.digitalDisplayGarden.Authentication.Cookie;
import umm3601.digitalDisplayGarden.Authentication.UnauthorizedUserException;
import umm3601.digitalDisplayGarden.Photos;
import umm3601.digitalDisplayGarden.PlantController;

import java.io.*;
import java.awt.image.RenderedImage;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Properties;

import static spark.Spark.*;

import umm3601.digitalDisplayGarden.ExcelParser;
import umm3601.digitalDisplayGarden.QRCodes;

import javax.imageio.ImageIO;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.Part;


public class Server {

    public static final String API_URL = "https://dorfner.congrue.xyz:2538";

    public static final String clientBaseURL = "http://localhost:9000/";

    public static String databaseName = "test";

    private static String excelTempDir = "/tmp/digital-display-garden";

    private static String clientId;
    private static String clientSecret;

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {

        String configFileLocation;
        if (args.length == 0) {
            configFileLocation = "config.properties";
        } else {
            configFileLocation = args[0];
        }
        readConfig(configFileLocation);

        port(2538);

        // This users looks in the folder `public` for the static web artifacts,
        // which includes all the HTML, CSS, and JS files generated by the Angular
        // build. This `public` directory _must_ be somewhere in the classpath;
        // a problem which is resolved in `server/build.gradle`.
        staticFiles.location("/public");

        PlantController plantController = new PlantController(databaseName);
        Photos photos = new Photos(databaseName);
        Auth auth = new Auth(clientId, clientSecret);

        options("/*", (request, response) -> {

            String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
            if (accessControlRequestHeaders != null) {
                response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
            }

            String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
            if (accessControlRequestMethod != null) {
                response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
            }
 
            return "OK";
        });

        before((request, response) -> {
            response.header("Access-Control-Allow-Credentials", "true");
            response.header("Access-Control-Allow-Origin", "http://localhost:9000");
        });

        // Redirects for the "home" page
        redirect.get("", "/");

        Route clientRoute = (req, res) -> {
            InputStream stream = plantController.getClass().getResourceAsStream("/public/index.html");
            return IOUtils.toString(stream);
        };
        Route notFoundRoute = (req, res) -> {
            res.type("text");
            res.status(404);
            return "Sorry, we couldn't find that!";
        };

        get("/", clientRoute);

        get("api/helloWorld", (req, res) ->{
            String cookie = req.cookie("ddg");
            if (!auth.authorized(cookie)) {
                res.redirect(auth.getAuthURL("api/helloWorld"));
            }
            res.type("text/plain");

           return "Hello World!";
        });

        get("callback", (req, res) ->{
           Map<String, String[]> params = req.queryMap().toMap();
           String state = params.get("state")[0];
           String code = params.get("code")[0];
            try {
                String originatingURL = auth.verifyCallBack(state, code);
                if (null != originatingURL) {
                    Cookie c = auth.getCookie();
                    res.cookie(c.name, c.value, c.max_age);
                    res.redirect(originatingURL);
                    System.out.println("good");
                    return ""; // not reached
                } else {
                    System.out.println("bad");
                    res.status(403);
                    return "?????"; // todo: return a reasonable message
                }
            } catch (UnauthorizedUserException e) {
                res.redirect("http://localhost:9000/admin/incorrectAccount");
                return ""; // not reached
            }
        });

        get("api/check-authorization", (req, res) -> {
            res.type("application/json");
            String cookie = req.cookie("ddg");
            Document returnDoc = new Document();
            returnDoc.append("authorized", auth.authorized(cookie));
            return JSON.serialize(returnDoc);
        });

        get("api/authorize", (req,res) -> {
            String originatingURLs[] = req.queryMap().toMap().get("originatingURL");
            String originatingURL;
            if (originatingURLs == null) {
                originatingURL = API_URL; // todo: what should the default be?
            } else {
                originatingURL = originatingURLs[0];
            }
            res.redirect(auth.getAuthURL(originatingURL));
            // I think we could return an arbitrary value since the redirect prevents this from being used
            return res;
        });

        // List plants
        get("api/plants", (req, res) -> {
            System.out.println(req.cookie("garbage"));
            res.type("application/json");
            return plantController.listPlants(req.queryMap().toMap(), plantController.getLiveUploadId());
        });

        //Get a plant
        get("api/plants/:plantID", (req, res) -> {
            res.type("application/json");
            String id = req.params("plantID");
            return plantController.getPlantByPlantID(id, plantController.getLiveUploadId());
        });

        //Get feedback counts for a plant
        get("api/plants/:plantID/counts", (req, res) -> {
            res.type("application/json");
            String id = req.params("plantID");
            return plantController.getFeedbackForPlantByPlantID(id, plantController.getLiveUploadId());
        });<commit_msg>Removing debugging code from Server.java<commit_after>package umm3601;

import org.apache.xmlbeans.impl.common.ReaderInputStream;
import org.bson.Document;
import spark.Route;
import spark.utils.IOUtils;
import com.mongodb.util.JSON;
import umm3601.digitalDisplayGarden.Authentication.Auth;
import umm3601.digitalDisplayGarden.Authentication.Cookie;
import umm3601.digitalDisplayGarden.Authentication.UnauthorizedUserException;
import umm3601.digitalDisplayGarden.Photos;
import umm3601.digitalDisplayGarden.PlantController;

import java.io.*;
import java.awt.image.RenderedImage;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Properties;

import static spark.Spark.*;

import umm3601.digitalDisplayGarden.ExcelParser;
import umm3601.digitalDisplayGarden.QRCodes;

import javax.imageio.ImageIO;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.Part;


public class Server {

    public static final String API_URL = "https://dorfner.congrue.xyz:2538";

    public static final String clientBaseURL = "http://localhost:9000/";

    public static String databaseName = "test";

    private static String excelTempDir = "/tmp/digital-display-garden";

    private static String clientId;
    private static String clientSecret;

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {

        String configFileLocation;
        if (args.length == 0) {
            configFileLocation = "config.properties";
        } else {
            configFileLocation = args[0];
        }
        readConfig(configFileLocation);

        port(2538);

        // This users looks in the folder `public` for the static web artifacts,
        // which includes all the HTML, CSS, and JS files generated by the Angular
        // build. This `public` directory _must_ be somewhere in the classpath;
        // a problem which is resolved in `server/build.gradle`.
        staticFiles.location("/public");

        PlantController plantController = new PlantController(databaseName);
        Photos photos = new Photos(databaseName);
        Auth auth = new Auth(clientId, clientSecret);

        options("/*", (request, response) -> {

            String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
            if (accessControlRequestHeaders != null) {
                response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
            }

            String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
            if (accessControlRequestMethod != null) {
                response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
            }
 
            return "OK";
        });

        before((request, response) -> {
            response.header("Access-Control-Allow-Credentials", "true");
            response.header("Access-Control-Allow-Origin", "http://localhost:9000");
        });

        // Redirects for the "home" page
        redirect.get("", "/");

        Route clientRoute = (req, res) -> {
            InputStream stream = plantController.getClass().getResourceAsStream("/public/index.html");
            return IOUtils.toString(stream);
        };
        Route notFoundRoute = (req, res) -> {
            res.type("text");
            res.status(404);
            return "Sorry, we couldn't find that!";
        };

        get("/", clientRoute);

        get("callback", (req, res) ->{
           Map<String, String[]> params = req.queryMap().toMap();
           String state = params.get("state")[0];
           String code = params.get("code")[0];
            try {
                String originatingURL = auth.verifyCallBack(state, code);
                if (null != originatingURL) {
                    Cookie c = auth.getCookie();
                    res.cookie(c.name, c.value, c.max_age);
                    res.redirect(originatingURL);
                    System.out.println("good");
                    return ""; // not reached
                } else {
                    System.out.println("bad");
                    res.status(403);
                    return "?????"; // todo: return a reasonable message
                }
            } catch (UnauthorizedUserException e) {
                res.redirect("http://localhost:9000/admin/incorrectAccount");
                return ""; // not reached
            }
        });

        get("api/check-authorization", (req, res) -> {
            res.type("application/json");
            String cookie = req.cookie("ddg");
            Document returnDoc = new Document();
            returnDoc.append("authorized", auth.authorized(cookie));
            return JSON.serialize(returnDoc);
        });

        get("api/authorize", (req,res) -> {
            String originatingURLs[] = req.queryMap().toMap().get("originatingURL");
            String originatingURL;
            if (originatingURLs == null) {
                originatingURL = API_URL; // todo: what should the default be?
            } else {
                originatingURL = originatingURLs[0];
            }
            res.redirect(auth.getAuthURL(originatingURL));
            // I think we could return an arbitrary value since the redirect prevents this from being used
            return res;
        });

        // List plants
        get("api/plants", (req, res) -> {
            res.type("application/json");
            return plantController.listPlants(req.queryMap().toMap(), plantController.getLiveUploadId());
        });

        //Get a plant
        get("api/plants/:plantID", (req, res) -> {
            res.type("application/json");
            String id = req.params("plantID");
            return plantController.getPlantByPlantID(id, plantController.getLiveUploadId());
        });

        //Get feedback counts for a plant
        get("api/plants/:plantID/counts", (req, res) -> {
            res.type("application/json");
            String id = req.params("plantID");
            return plantController.getFeedbackForPlantByPlantID(id, plantController.getLiveUploadId());
        });
{'fraction_non_alphanumeric': 0.11904575060817704, 'fraction_numerical': 0.009887781527112924, 'mean_word_length': 2.2828438948995364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926082', 'n_tokens_mistral': 3415, 'n_tokens_neox': 3218, 'n_words': 728}