Example usage for java.security AccessControlException AccessControlException

List of usage examples for java.security AccessControlException AccessControlException

Introduction

In this page you can find the example usage for java.security AccessControlException AccessControlException.

Prototype

public AccessControlException(String s) 

Source Link

Document

Constructs an AccessControlException with the specified, detailed message.

Usage

From source file:servlets.Samples_servlets.java

/**
 * */*w  w w  .  ja  v a 2s  . c o m*/
 * This function retrieves the registered samples for a given LIMS. The
 * function requires a valid LIMS type, the URL for the service, and the
 * user credentials.
 *
 * @param request
 * @param response
 * @throws IOException
 */
private void get_external_samples_list(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    JsonArray samples = new JsonArray();

    try {
        /**
         * *******************************************************
         * STEP 1 CHECK IF THE USER IS LOGGED CORRECTLY IN THE APP. IF ERROR
         * --> throws exception if not valid session, GO TO STEP 5b ELSE -->
         * GO TO STEP 2
         * *******************************************************
         */
        Map<String, Cookie> cookies = this.getCookies(request);
        String loggedUser = cookies.get("loggedUser").getValue();
        String sessionToken = cookies.get("sessionToken").getValue();

        if (!checkAccessPermissions(loggedUser, sessionToken)) {
            throw new AccessControlException("Your session is invalid. User or session token not allowed.");
        }

        //Read the JSON file
        String external_sample_type = request.getParameter("external_sample_type");
        File file = new File(DATA_LOCATION + File.separator + "extensions" + File.separator + "external_sources"
                + File.separator + external_sample_type);
        JsonObject lims_data;
        if (file.isFile()) {
            lims_data = new JsonParser().parse(new BufferedReader(new FileReader(file))).getAsJsonObject();
        } else {
            throw new FileNotFoundException(
                    "JSON file for selected LIMS cannot be found. File name is " + external_sample_type);
        }

        String get_all_url = lims_data.get("get_all_url").getAsString();
        String human_readable_url = lims_data.get("human_readable_url").getAsString();
        String id_field = lims_data.get("id_field").getAsString();
        String name_field = lims_data.get("name_field").getAsString();
        String list_samples_field = lims_data.get("list_samples_field").getAsString();
        String apikey_param = "";
        if (lims_data.get("apikey_param") != null) {
            apikey_param = lims_data.get("apikey_param").getAsString();
        }

        //Request the list of samples for the selected LIMS
        String external_sample_url = request.getParameter("external_sample_url");
        //Adapt URL
        if (!(external_sample_url.startsWith("http://") || external_sample_url.startsWith("https://"))) {
            external_sample_url = "http://" + external_sample_url;
        }
        if (external_sample_url.endsWith("/")) {
            external_sample_url = external_sample_url.substring(0, external_sample_url.length() - 1);
        }

        get_all_url = get_all_url.replace("$${APP_URL}", external_sample_url);
        human_readable_url = human_readable_url.replace("$${APP_URL}", external_sample_url);

        //Prepare request
        HttpClient client = new DefaultHttpClient();
        HttpGet _request = new HttpGet(get_all_url);
        // Set LIMS credentials
        if (request.getParameter("credentials") != null) {
            _request.setHeader("Authorization", "Basic " + request.getParameter("credentials"));
        } else if (request.getParameter("apikey") != null) {
            URIBuilder uri = new URIBuilder(get_all_url);
            uri.addParameter(apikey_param, request.getParameter("apikey"));
            _request = new HttpGet(uri.build());
        }

        //Send request
        HttpResponse _response = client.execute(_request);
        JsonElement json_response = new JsonParser().parse(EntityUtils.toString(_response.getEntity()));

        if (json_response.isJsonObject()) {
            JsonArray sample_list = json_response.getAsJsonObject().get(list_samples_field).getAsJsonArray();
            JsonObject object;
            for (JsonElement element : sample_list) {
                object = new JsonObject();
                object.add("id", element.getAsJsonObject().get(id_field));
                object.add("name", element.getAsJsonObject().get(name_field));
                object.add("url", new JsonPrimitive(human_readable_url.replace("$${SAMPLE_ID}",
                        element.getAsJsonObject().get(id_field).getAsString())));
                samples.add(object);
            }
        }
    } catch (Exception e) {
        ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_external_samples_list",
                e.getMessage());
    } finally {
        /**
         * *******************************************************
         * STEP 3b CATCH ERROR. GO TO STEP 4
         * *******************************************************
         */
        if (ServerErrorManager.errorStatus()) {
            response.setStatus(400);
            response.getWriter().print(ServerErrorManager.getErrorResponse());
        } else {
            /**
             * *******************************************************
             * STEP 3A WRITE SUCCESS RESPONSE. GO TO STEP 4
             * *******************************************************
             */
            JsonObject obj = new JsonObject();
            obj.add("samples", samples);
            response.getWriter().print(obj.toString());
        }
    }
}

From source file:servlets.Samples_servlets.java

/**
 * *//from w ww  . ja v a 2s .c om
 * This function retrieves the details for a specific sample from a LIMS.
 *
 * @param request
 * @param response
 * @throws IOException
 */
private void get_external_samples_details(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    DAO dao_instance = null;
    JsonObject sample_details = new JsonObject();

    try {
        /**
         * *******************************************************
         * STEP 1 CHECK IF THE USER IS LOGGED CORRECTLY IN THE APP. IF ERROR
         * --> throws exception if not valid session, GO TO STEP 5b ELSE -->
         * GO TO STEP 2
         * *******************************************************
         */
        Map<String, Cookie> cookies = this.getCookies(request);
        String loggedUser = cookies.get("loggedUser").getValue();
        String sessionToken = cookies.get("sessionToken").getValue();

        if (!checkAccessPermissions(loggedUser, sessionToken)) {
            throw new AccessControlException("Your session is invalid. User or session token not allowed.");
        }

        //Load the sample information
        String biocondition_id = request.getParameter("biocondition_id");
        dao_instance = DAOProvider.getDAOByName("Biocondition");
        boolean loadRecursive = true;
        Object[] params = { loadRecursive };
        BioCondition biocondition = (BioCondition) dao_instance.findByID(biocondition_id, params);

        //Read the JSON file
        File file = new File(DATA_LOCATION + File.separator + "extensions" + File.separator + "external_sources"
                + File.separator + biocondition.getExternalSampleType());
        JsonObject lims_data;
        if (file.isFile()) {
            lims_data = new JsonParser().parse(new BufferedReader(new FileReader(file))).getAsJsonObject();
        } else {
            throw new FileNotFoundException("JSON file for selected LIMS cannot be found. File name is "
                    + biocondition.getExternalSampleType());
        }

        String api_readable_url = lims_data.get("api_readable_url").getAsString();
        String human_readable_url = lims_data.get("human_readable_url").getAsString();
        String sample_details_field = lims_data.get("sample_details_field").getAsString();
        String apikey_param = "";
        if (lims_data.get("apikey_param") != null) {
            apikey_param = lims_data.get("apikey_param").getAsString();
        }

        //Request the list of samples for the selected LIMS
        String external_sample_url = biocondition.getExternalSampleURL();
        //Adapt URL
        if (!(external_sample_url.startsWith("http://") || external_sample_url.startsWith("https://"))) {
            external_sample_url = "http://" + external_sample_url;
        }
        if (external_sample_url.endsWith("/")) {
            external_sample_url = external_sample_url.substring(0, external_sample_url.length() - 1);
        }

        api_readable_url = api_readable_url.replace("$${APP_URL}", external_sample_url).replace("$${SAMPLE_ID}",
                biocondition.getExternalSampleID());
        human_readable_url = human_readable_url.replace("$${APP_URL}", external_sample_url);

        //Prepare request
        HttpClient client = new DefaultHttpClient();
        HttpGet _request = new HttpGet(api_readable_url);
        // Set LIMS credentials
        if (request.getParameter("credentials") != null) {
            _request.setHeader("Authorization", "Basic " + request.getParameter("credentials"));
        } else if (request.getParameter("apikey") != null) {
            URIBuilder uri = new URIBuilder(api_readable_url);
            uri.addParameter(apikey_param, request.getParameter("apikey"));
            _request = new HttpGet(uri.build());
        }

        //Send request
        HttpResponse _response = client.execute(_request);
        JsonElement json_response = new JsonParser().parse(EntityUtils.toString(_response.getEntity()));

        if (json_response.isJsonObject()) {
            JsonElement _sample_details = json_response.getAsJsonObject().get(sample_details_field);
            if (_sample_details.isJsonObject()) {
                sample_details = _sample_details.getAsJsonObject();
            } else if (_sample_details.isJsonArray()) {
                sample_details = _sample_details.getAsJsonArray().get(0).getAsJsonObject();
            }
        }
    } catch (Exception e) {
        ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_external_samples_details",
                e.getMessage());
    } finally {
        /**
         * *******************************************************
         * STEP 3b CATCH ERROR. GO TO STEP 4
         * *******************************************************
         */
        if (ServerErrorManager.errorStatus()) {
            response.setStatus(400);
            response.getWriter().print(ServerErrorManager.getErrorResponse());
        } else {
            /**
             * *******************************************************
             * STEP 3A WRITE SUCCESS RESPONSE. GO TO STEP 4
             * *******************************************************
             */
            JsonObject obj = new JsonObject();
            obj.add("sample_details", sample_details);
            response.getWriter().print(obj.toString());
        }
    }
}

From source file:servlets.Samples_servlets.java

private void get_all_samples_handler(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from  w  ww . j a  v a  2s  . c om*/
        DAO dao_instance = null;
        ArrayList<Object> bioconditionsList = null;
        ArrayList<String> study_samples = null;
        try {

            Map<String, Cookie> cookies = this.getCookies(request);

            String loggedUser, loggedUserID, sessionToken;
            loggedUser = cookies.get("loggedUser").getValue();
            loggedUserID = cookies.get("loggedUserID").getValue();
            sessionToken = cookies.get("sessionToken").getValue();

            /**
             * *******************************************************
             * STEP 1 CHECK IF THE USER IS LOGGED CORRECTLY IN THE APP. IF
             * ERROR --> throws exception if not valid session, GO TO STEP
             * 5b ELSE --> GO TO STEP 2
             * *******************************************************
             */
            if (!checkAccessPermissions(loggedUser, sessionToken)) {
                throw new AccessControlException("Your session is invalid. User or session token not allowed.");
            }

            /**
             * *******************************************************
             * STEP 2 Get ALL THE ANALYSIS Object from DB. IF ERROR -->
             * throws MySQL exception, GO TO STEP 3b ELSE --> GO TO STEP 3
             * *******************************************************
             */
            boolean loadRecursive = "1".equals(request.getParameter("recursive"));

            String experiment_id = cookies.get("currentExperimentID").getValue();

            Object[] params = { loadRecursive };
            dao_instance = DAOProvider.getDAOByName("BioCondition");
            bioconditionsList = dao_instance.findAll(params);

            study_samples = ((BioCondition_JDBCDAO) dao_instance).findSamplesIDByExperimentID(experiment_id);
            ArrayList<String> bioconditionIds = new ArrayList<String>();
            for (String sample_id : study_samples) {
                if (sample_id.contains(".")) {
                    sample_id = sample_id.split("\\.")[0];
                }
                bioconditionIds.add("BC" + sample_id.substring(2));
            }

            for (int i = bioconditionsList.size() - 1; i >= 0; i--) {
                if (((BioCondition) bioconditionsList.get(i)).isOwner(loggedUserID)
                        || ((BioCondition) bioconditionsList.get(i)).isPublic() || bioconditionIds
                                .contains(((BioCondition) bioconditionsList.get(i)).getBioConditionID())) {
                    continue;
                }
                bioconditionsList.remove(i);
            }

        } catch (Exception e) {
            ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_all_samples_handler",
                    e.getMessage());
        } finally {
            /**
             * *******************************************************
             * STEP 3b CATCH ERROR. GO TO STEP 4
             * *******************************************************
             */
            if (ServerErrorManager.errorStatus()) {
                response.setStatus(400);
                response.getWriter().print(ServerErrorManager.getErrorResponse());
            } else {
                /**
                 * *******************************************************
                 * STEP 3A WRITE RESPONSE ERROR. GO TO STEP 4
                 * *******************************************************
                 */
                String bioconditionsJSON = "{\"samples\" : [";
                for (int i = 0; i < bioconditionsList.size(); i++) {
                    bioconditionsJSON += ((BioCondition) bioconditionsList.get(i)).toJSON()
                            + ((i < bioconditionsList.size() - 1) ? "," : "");
                }
                bioconditionsJSON += "], \"samples_current_study\" : [";
                for (int i = 0; i < study_samples.size(); i++) {
                    bioconditionsJSON += "\"" + study_samples.get(i) + "\""
                            + ((i < study_samples.size() - 1) ? "," : "");
                }
                bioconditionsJSON += "]}";

                response.getWriter().print(bioconditionsJSON);
            }
            /**
             * *******************************************************
             * STEP 4 Close connection.
             * ********************************************************
             */
            if (dao_instance != null) {
                dao_instance.closeConnection();
            }
        }
        //CATCH IF THE ERROR OCCURRED IN ROLL BACK OR CONNECTION CLOSE 
    } catch (Exception e) {
        ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_all_samples_handler",
                e.getMessage());
        response.setStatus(400);
        response.getWriter().print(ServerErrorManager.getErrorResponse());
    }
}