Example usage for org.apache.commons.validator.routines UrlValidator isValid

List of usage examples for org.apache.commons.validator.routines UrlValidator isValid

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines UrlValidator isValid.

Prototype

public boolean isValid(String value) 

Source Link

Document

Checks if a field has a valid url address.

Usage

From source file:org.asqatasun.webapp.validator.CreateContractFormValidator.java

/**
 *
 * @param userSubscriptionCommand/*ww w  .  j av  a 2s .com*/
 * @param errors
 * @return
 */
private boolean checkContractUrl(CreateContractCommand createContractCommand, Errors errors) {
    String url = createContractCommand.getContractUrl().trim();
    if (StringUtils.isBlank(url)) {
        return true;
    }
    String[] schemes = { "http", "https" };
    long validatorOptions = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_LOCAL_URLS;
    UrlValidator urlValidator = new UrlValidator(schemes, validatorOptions);
    if (!urlValidator.isValid(url)) {
        errors.rejectValue(CONTRACT_URL_KEY, INVALID_URL_KEY);
        return false;
    }
    return true;
}

From source file:org.asqatasun.webapp.validator.CreateUserFormValidator.java

/**
 *
 * @param userSubscriptionCommand//from  w  ww .ja  v  a  2  s  .  co m
 * @param errors
 * @return
 */
private boolean checkSiteUrl(CreateUserCommand userSubscriptionCommand, Errors errors) {
    if (!checkSiteUrl) {
        return true;
    }
    if (userSubscriptionCommand.getSiteUrl() == null || userSubscriptionCommand.getSiteUrl().trim().isEmpty()) {
        errors.rejectValue(SITE_URL_KEY, MISSING_URL_KEY);
        return false;
    } else {
        String url = userSubscriptionCommand.getSiteUrl().trim();
        String[] schemes = { "http", "https" };
        UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
        if (!urlValidator.isValid(url)) {
            errors.rejectValue(SITE_URL_KEY, INVALID_URL_KEY);
            return false;
        }
    }
    return true;
}

From source file:org.asqatasun.websnapshot.urlmanager.utils.UrlUtils.java

/**
 *
 * @param url//  w w  w . j  a v a  2s.c  om
 * @return
 */
public static boolean checkIfURLIsValid(String url) {
    String[] schemes = { "http", "https" };
    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_2_SLASHES);
    return urlValidator.isValid(url);
}

From source file:org.jaqpot.core.service.resource.AlgorithmResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON, "text/uri-list" })
@Path("/{id}")
@ApiOperation(value = "Creates Model", notes = "Applies Dataset and Parameters on Algorithm and creates Model.", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response trainModel(@ApiParam(name = "title", required = true) @FormParam("title") String title,
        @ApiParam(name = "description", required = true) @FormParam("description") String description,
        @ApiParam(name = "dataset_uri", defaultValue = DEFAULT_DATASET) @FormParam("dataset_uri") String datasetURI,
        @ApiParam(name = "prediction_feature", defaultValue = DEFAULT_PRED_FEATURE) @FormParam("prediction_feature") String predictionFeature,
        @FormParam("parameters") String parameters,
        @ApiParam(name = "transformations", defaultValue = DEFAULT_TRANSFORMATIONS) @FormParam("transformations") String transformations,
        @ApiParam(name = "scaling", defaultValue = STANDARIZATION) @FormParam("scaling") String scaling, //, allowableValues = SCALING + "," + STANDARIZATION
        @ApiParam(name = "doa", defaultValue = DEFAULT_DOA) @FormParam("doa") String doa,
        @PathParam("id") String algorithmId, @HeaderParam("subjectid") String subjectId)
        throws QuotaExceededException, ParameterIsNullException, ParameterInvalidURIException,
        ParameterTypeException, ParameterRangeException, ParameterScopeException {
    UrlValidator urlValidator = new UrlValidator();

    Algorithm algorithm = algorithmHandler.find(algorithmId);
    if (algorithm == null) {
        throw new NotFoundException("Could not find Algorithm with id:" + algorithmId);
    }/*from   www .java 2s  .c om*/

    //Dataset validation should happen only in regression and classification algorithms
    if (algorithm.getOntologicalClasses().contains("ot:Regression")
            || algorithm.getOntologicalClasses().contains("ot:Classification")) {

        if (datasetURI == null) {
            throw new ParameterIsNullException("datasetURI");
        }

        if (!urlValidator.isValid(datasetURI)) {
            throw new ParameterInvalidURIException("Not valid Dataset URI.");
        }

        String datasetId = datasetURI.split("dataset/")[1];
        Dataset datasetMeta = datasetHandler.findMeta(datasetId);

        if (datasetMeta.getTotalRows() != null && datasetMeta.getTotalRows() < 2) {
            throw new BadRequestException("Cannot train model on dataset with less than 2 rows.");
        }
    }

    //Prediction validation should not happen in enm:NoTarget  algorithms
    if (algorithm.getOntologicalClasses().contains("enm:NoTarget")) {
        if (predictionFeature == null) {
            throw new ParameterIsNullException("predictionFeature");
        }
        if (!urlValidator.isValid(predictionFeature)) {
            throw new ParameterInvalidURIException("Not valid Prediction Feature URI.");
        }
    }

    if (title == null) {
        throw new ParameterIsNullException("title");
    }
    if (description == null) {
        throw new ParameterIsNullException("description");
    }

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long modelCount = modelHandler.countAllOfCreator(user.getId());
    int maxAllowedModels = new UserFacade(user).getMaxModels();

    if (modelCount > maxAllowedModels) {
        LOG.info(String.format("User %s has %d models while maximum is %d", user.getId(), modelCount,
                maxAllowedModels));
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + modelCount + " models. "
                + "No more than " + maxAllowedModels + " are allowed with your subscription.");
    }

    Map<String, Object> options = new HashMap<>();
    options.put("title", title);
    options.put("description", description);
    options.put("dataset_uri", datasetURI);
    options.put("prediction_feature", predictionFeature);
    options.put("subjectid", subjectId);
    options.put("algorithmId", algorithmId);
    options.put("parameters", parameters);
    options.put("base_uri", uriInfo.getBaseUri().toString());
    options.put("creator", securityContext.getUserPrincipal().getName());

    Map<String, String> transformationAlgorithms = new LinkedHashMap<>();
    if (transformations != null && !transformations.isEmpty()) {
        transformationAlgorithms.put(uriInfo.getBaseUri().toString() + "algorithm/pmml",
                "{\"transformations\" : \"" + transformations + "\"}");
    }
    if (scaling != null && !scaling.isEmpty()) {
        transformationAlgorithms.put(scaling, "");
    }
    if (doa != null && !doa.isEmpty()) {
        transformationAlgorithms.put(doa, "");
    }
    if (!transformationAlgorithms.isEmpty()) {
        String transformationAlgorithmsString = serializer.write(transformationAlgorithms);
        LOG.log(Level.INFO, "Transformations:{0}", transformationAlgorithmsString);
        options.put("transformations", transformationAlgorithmsString);
    }

    parameterValidator.validate(parameters, algorithm.getParameters());

    //return Response.ok().build();
    Task task = trainingService.initiateTraining(options, securityContext.getUserPrincipal().getName());

    return Response.ok(task).build();
}

From source file:org.jaqpot.core.service.resource.DatasetResource.java

@POST
@Path("/{id}/qprf")
@ApiOperation("Creates QPRF Report")
@Authorize//  w ww .  j  av a  2s. c o m
public Response createQPRFReport(
        @ApiParam(value = "Authorization token") @HeaderParam("subjectid") String subjectId,
        @PathParam("id") String id, @FormParam("substance_uri") String substanceURI,
        @FormParam("title") String title, @FormParam("description") String description)
        throws QuotaExceededException {

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long reportCount = reportHandler.countAllOfCreator(user.getId());
    int maxAllowedReports = new UserFacade(user).getMaxReports();

    if (reportCount > maxAllowedReports) {
        LOG.info(String.format("User %s has %d algorithms while maximum is %d", user.getId(), reportCount,
                maxAllowedReports));
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + reportCount + " reports. "
                + "No more than " + maxAllowedReports + " are allowed with your subscription.");
    }

    Dataset ds = datasetHandler.find(id);
    if (ds == null) {
        throw new NotFoundException("Dataset with id:" + id + " was not found on the server.");
    }
    if (ds.getByModel() == null || ds.getByModel().isEmpty()) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    Model model = modelHandler.find(ds.getByModel());
    if (model == null) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    String datasetURI = model.getDatasetUri();
    if (datasetURI == null || datasetURI.isEmpty()) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }
    Dataset trainingDS = client.target(datasetURI).request().accept(MediaType.APPLICATION_JSON)
            .header("subjectid", subjectId).get(Dataset.class);
    if (trainingDS == null) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }

    if (model.getTransformationModels() != null) {
        for (String transModelURI : model.getTransformationModels()) {
            Model transModel = modelHandler.find(transModelURI.split("model/")[1]);
            if (transModel == null) {
                throw new NotFoundException(
                        "Transformation model with id:" + transModelURI + " was not found.");
            }
            try {
                trainingDS = jpdiClient
                        .predict(trainingDS, transModel, trainingDS.getMeta(), UUID.randomUUID().toString())
                        .get();
            } catch (InterruptedException ex) {
                LOG.log(Level.SEVERE, "JPDI Training procedure interupted", ex);
                throw new InternalServerErrorException("JPDI Training procedure interupted", ex);
            } catch (ExecutionException ex) {
                LOG.log(Level.SEVERE, "Training procedure execution error", ex.getCause());
                throw new InternalServerErrorException("JPDI Training procedure error", ex.getCause());
            } catch (CancellationException ex) {
                throw new InternalServerErrorException("Procedure was cancelled");
            }
        }
    }

    List<String> retainableFeatures = new ArrayList<>(model.getIndependentFeatures());
    retainableFeatures.addAll(model.getDependentFeatures());

    trainingDS.getDataEntry().parallelStream().forEach(dataEntry -> {
        dataEntry.getValues().keySet().retainAll(retainableFeatures);
    });

    DataEntry dataEntry = ds.getDataEntry().stream()
            .filter(de -> de.getCompound().getURI().equals(substanceURI)).findFirst()
            .orElseThrow(() -> new BadRequestException(""));

    trainingDS.getDataEntry().add(dataEntry);
    trainingDS.getMeta().setCreators(new HashSet<>(Arrays.asList(user.getId())));

    Map<String, Object> parameters = new HashMap<>();

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(substanceURI)) {
        Dataset structures = client.target(substanceURI + "/structures").request()
                .accept(MediaType.APPLICATION_JSON).header("subjectid", subjectId).get(Dataset.class);
        List<Map<String, String>> structuresList = structures.getDataEntry().stream().map(de -> {
            String compound = de.getCompound().getURI();
            String casrn = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23CASRNDefault"))
                    .orElse("").toString();
            String einecs = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23EINECSDefault"))
                    .orElse("").toString();
            String iuclid5 = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUCLID5_UUIDDefault"))
                    .orElse("").toString();
            String inchi = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23InChI_stdDefault"))
                    .orElse("").toString();
            String reach = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23REACHRegistrationDateDefault"))
                    .orElse("").toString();
            String iupac = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUPACNameDefault"))
                    .orElse("").toString();

            Map<String, String> structuresMap = new HashMap<>();
            structuresMap.put("Compound", compound);
            structuresMap.put("CasRN", casrn);
            structuresMap.put("EC number", einecs);
            structuresMap.put("REACH registration date", reach);
            structuresMap.put("IUCLID 5 Reference substance UUID", iuclid5);
            structuresMap.put("Std. InChI", inchi);
            structuresMap.put("IUPAC name", iupac);

            return structuresMap;
        }).collect(Collectors.toList());
        parameters.put("structures", structuresList);
    } else {
        List<Map<String, String>> structuresList = new ArrayList<>();
        Map<String, String> structuresMap = new HashMap<>();
        structuresMap.put("Compound", "");
        structuresMap.put("CasRN", "");
        structuresMap.put("EC number", "");
        structuresMap.put("REACH registration date", "");
        structuresMap.put("IUCLID 5 Reference substance UUID", "");
        structuresMap.put("Std. InChI", "");
        structuresMap.put("IUPAC name", "");
        structuresList.add(structuresMap);
        parameters.put("structures", structuresList);
    }

    parameters.put("predictedFeature", model.getPredictedFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid predicted feature")));

    parameters.put("algorithm", algorithmHandler.find(model.getAlgorithm().getId()));
    parameters.put("substanceURI", substanceURI);
    if (model.getLinkedModels() != null && !model.getLinkedModels().isEmpty()) {
        Model doa = modelHandler.find(model.getLinkedModels().get(0).split("model/")[1]);
        if (doa != null) {
            parameters.put("doaURI", doa.getPredictedFeatures().get(0));
            parameters.put("doaMethod", doa.getAlgorithm().getId());
        }
    }
    TrainingRequest request = new TrainingRequest();

    request.setDataset(trainingDS);
    request.setParameters(parameters);
    request.setPredictionFeature(model.getDependentFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid prediction feature")));

    Report report = client.target("http://147.102.82.32:8094/pws/qprf").request()
            .header("Content-Type", MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
            .post(Entity.json(request), Report.class);

    report.setMeta(MetaInfoBuilder.builder().addTitles(title).addDescriptions(description)
            .addCreators(securityContext.getUserPrincipal().getName()).build());
    report.setId(new ROG(true).nextString(15));
    report.setVisible(Boolean.TRUE);
    reportHandler.create(report);

    return Response.ok(report).build();
}

From source file:org.jaqpot.core.service.resource.DatasetResource.java

@POST
@Path("/{id}/qprf-dummy")
@ApiOperation("Creates QPRF Report")
@Authorize//from  w ww  .  j  a  v a2s  .  c o  m
public Response createQPRFReportDummy(
        @ApiParam(value = "Authorization token") @HeaderParam("subjectid") String subjectId,
        @PathParam("id") String id, @FormParam("substance_uri") String substanceURI,
        @FormParam("title") String title, @FormParam("description") String description) {

    Dataset ds = datasetHandler.find(id);
    if (ds == null) {
        throw new NotFoundException("Dataset with id:" + id + " was not found on the server.");
    }
    if (ds.getByModel() == null || ds.getByModel().isEmpty()) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    Model model = modelHandler.find(ds.getByModel());
    if (model == null) {
        throw new BadRequestException("Selected dataset was not produced by a valid model.");
    }
    String datasetURI = model.getDatasetUri();
    if (datasetURI == null || datasetURI.isEmpty()) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }
    Dataset trainingDS = client.target(datasetURI).request().accept(MediaType.APPLICATION_JSON)
            .header("subjectid", subjectId).get(Dataset.class);
    if (trainingDS == null) {
        throw new BadRequestException(
                "The model that created this dataset does not point to a valid training dataset.");
    }

    if (model.getTransformationModels() != null) {
        for (String transModelURI : model.getTransformationModels()) {
            Model transModel = modelHandler.find(transModelURI.split("model/")[1]);
            if (transModel == null) {
                throw new NotFoundException(
                        "Transformation model with id:" + transModelURI + " was not found.");
            }
            try {
                trainingDS = jpdiClient
                        .predict(trainingDS, transModel, trainingDS.getMeta(), UUID.randomUUID().toString())
                        .get();
            } catch (InterruptedException ex) {
                LOG.log(Level.SEVERE, "JPDI Training procedure interupted", ex);
                throw new InternalServerErrorException("JPDI Training procedure interupted", ex);
            } catch (ExecutionException ex) {
                LOG.log(Level.SEVERE, "Training procedure execution error", ex.getCause());
                throw new InternalServerErrorException("JPDI Training procedure error", ex.getCause());
            } catch (CancellationException ex) {
                throw new InternalServerErrorException("Procedure was cancelled");
            }
        }
    }

    List<String> retainableFeatures = new ArrayList<>(model.getIndependentFeatures());
    retainableFeatures.addAll(model.getDependentFeatures());

    trainingDS.getDataEntry().parallelStream().forEach(dataEntry -> {
        dataEntry.getValues().keySet().retainAll(retainableFeatures);
    });

    DataEntry dataEntry = ds.getDataEntry().stream()
            .filter(de -> de.getCompound().getURI().equals(substanceURI)).findFirst()
            .orElseThrow(() -> new BadRequestException(""));

    trainingDS.getDataEntry().add(dataEntry);

    Map<String, Object> parameters = new HashMap<>();

    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(substanceURI)) {
        Dataset structures = client.target(substanceURI + "/structures").request()
                .accept(MediaType.APPLICATION_JSON).header("subjectid", subjectId).get(Dataset.class);
        List<Map<String, String>> structuresList = structures.getDataEntry().stream().map(de -> {
            String compound = de.getCompound().getURI();
            String casrn = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23CASRNDefault"))
                    .orElse("").toString();
            String einecs = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23EINECSDefault"))
                    .orElse("").toString();
            String iuclid5 = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUCLID5_UUIDDefault"))
                    .orElse("").toString();
            String inchi = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23InChI_stdDefault"))
                    .orElse("").toString();
            String reach = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23REACHRegistrationDateDefault"))
                    .orElse("").toString();
            String iupac = Optional.ofNullable(de.getValues().get(
                    "https://apps.ideaconsult.net/enmtest/feature/http%3A%2F%2Fwww.opentox.org%2Fapi%2F1.1%23IUPACNameDefault"))
                    .orElse("").toString();

            Map<String, String> structuresMap = new HashMap<>();
            structuresMap.put("Compound", compound);
            structuresMap.put("CasRN", casrn);
            structuresMap.put("EC number", einecs);
            structuresMap.put("REACH registration date", reach);
            structuresMap.put("IUCLID 5 Reference substance UUID", iuclid5);
            structuresMap.put("Std. InChI", inchi);
            structuresMap.put("IUPAC name", iupac);

            return structuresMap;
        }).collect(Collectors.toList());
        parameters.put("structures", structuresList);
    } else {
        List<Map<String, String>> structuresList = new ArrayList<>();
        Map<String, String> structuresMap = new HashMap<>();
        structuresMap.put("Compound", "");
        structuresMap.put("CasRN", "");
        structuresMap.put("EC number", "");
        structuresMap.put("REACH registration date", "");
        structuresMap.put("IUCLID 5 Reference substance UUID", "");
        structuresMap.put("Std. InChI", "");
        structuresMap.put("IUPAC name", "");
        structuresList.add(structuresMap);
        parameters.put("structures", structuresList);
    }

    parameters.put("predictedFeature", model.getPredictedFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid predicted feature")));

    parameters.put("algorithm", algorithmHandler.find(model.getAlgorithm().getId()));
    parameters.put("substanceURI", substanceURI);
    if (model.getLinkedModels() != null && !model.getLinkedModels().isEmpty()) {
        Model doa = modelHandler.find(model.getLinkedModels().get(0).split("model/")[1]);
        if (doa != null) {
            parameters.put("doaURI", doa.getPredictedFeatures().get(0));
            parameters.put("doaMethod", doa.getAlgorithm().getId());
        }
    }
    TrainingRequest request = new TrainingRequest();

    request.setDataset(trainingDS);
    request.setParameters(parameters);
    request.setPredictionFeature(model.getDependentFeatures().stream().findFirst()
            .orElseThrow(() -> new BadRequestException("Model does not have a valid prediction feature")));

    return Response.ok(request).build();
    //        Report report = client.target("http://147.102.82.32:8094/pws/qprf")
    //                .request()
    //                .header("Content-Type", MediaType.APPLICATION_JSON)
    //                .accept(MediaType.APPLICATION_JSON)
    //                .post(Entity.json(request), Report.class);
    //
    //        report.setMeta(MetaInfoBuilder.builder()
    //                .addTitles(title)
    //                .addDescriptions(description)
    //                .addCreators(securityContext.getUserPrincipal().getName())
    //                .build()
    //        );
    //        report.setId(new ROG(true).nextString(15));
    //        report.setVisible(Boolean.TRUE);
    //        reportHandler.create(report);
    //
    //        return Response.ok(report).build();
}

From source file:org.jaqpot.core.service.resource.ModelResource.java

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Path("/{id}")
@ApiOperation(value = "Creates Prediction", notes = "Creates Prediction", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response makePrediction(
        // defaultValue = DEFAULT_DATASET
        @ApiParam(name = "dataset_uri", required = true) @FormParam("dataset_uri") String datasetURI,
        @FormParam("visible") Boolean visible, @PathParam("id") String id,
        @HeaderParam("subjectid") String subjectId) throws GeneralSecurityException, QuotaExceededException,
        ParameterIsNullException, ParameterInvalidURIException {

    if (datasetURI == null) {
        throw new ParameterIsNullException("datasetURI");
    }/*from  ww w.  j  av a  2  s  .  c  om*/
    if (id == null) {
        throw new ParameterIsNullException("id");
    }

    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(datasetURI)) {
        throw new ParameterInvalidURIException("Not valid dataset URI.");
    }

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long datasetCount = datasetHandler.countAllOfCreator(user.getId());
    int maxAllowedDatasets = new UserFacade(user).getMaxDatasets();

    if (datasetCount > maxAllowedDatasets) {
        LOG.info(String.format("User %s has %d datasets while maximum is %d", user.getId(), datasetCount,
                maxAllowedDatasets));
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + datasetCount + " datasets. "
                + "No more than " + maxAllowedDatasets + " are allowed with your subscription.");
    }

    Model model = modelHandler.find(id);
    if (model == null) {
        throw new NotFoundException("Model not found.");
    }
    String datasetId = datasetURI.split("dataset/")[1];
    Dataset datasetMeta = datasetHandler.findMeta(datasetId);
    List<String> requiredFeatures = retrieveRequiredFeatures(model);

    parameterValidator.validateDataset(datasetMeta, requiredFeatures);

    Map<String, Object> options = new HashMap<>();
    options.put("dataset_uri", datasetURI);
    options.put("subjectid", subjectId);
    options.put("modelId", id);
    options.put("creator", securityContext.getUserPrincipal().getName());
    options.put("base_uri", uriInfo.getBaseUri().toString());
    Task task = predictionService.initiatePrediction(options);
    return Response.ok(task).build();
}

From source file:org.jaqpot.core.service.resource.ValidationResource.java

@POST
@Path("/training_test_cross")
@ApiOperation(value = "Creates Validation Report", notes = "Creates Validation Report", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response crossValidateAlgorithm(@FormParam("algorithm_uri") String algorithmURI,
        @FormParam("training_dataset_uri") String datasetURI,
        @FormParam("algorithm_params") String algorithmParameters,
        @FormParam("prediction_feature") String predictionFeature,
        @ApiParam(name = "transformations", defaultValue = DEFAULT_TRANSFORMATIONS) @FormParam("transformations") String transformations,
        @ApiParam(name = "scaling", defaultValue = STANDARIZATION) @FormParam("scaling") String scaling, //, allowableValues = SCALING + "," + STANDARIZATION
        @FormParam("folds") Integer folds, @FormParam("stratify") String stratify,
        @FormParam("seed") Integer seed, @HeaderParam("subjectId") String subjectId)
        throws QuotaExceededException, JMSException, ParameterInvalidURIException, ParameterIsNullException {
    if (algorithmURI == null)
        throw new ParameterIsNullException("algorithmURI");
    if (datasetURI == null)
        throw new ParameterIsNullException("datasetURI");
    if (folds == null)
        throw new ParameterIsNullException("folds");

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long reportCount = reportHandler.countAllOfCreator(user.getId());
    int maxAllowedReports = new UserFacade(user).getMaxReports();

    if (reportCount > maxAllowedReports) {
        LOG.info(String.format("User %s has %d algorithms while maximum is %d", user.getId(), reportCount,
                maxAllowedReports));/*w  ww  .  j a  v  a2  s.  c  o m*/
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + reportCount + " reports. "
                + "No more than " + maxAllowedReports + " are allowed with your subscription.");
    }

    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(algorithmURI)) {
        throw new ParameterInvalidURIException("Not valid algorithm URI.");
    }
    if (!urlValidator.isValid(datasetURI)) {
        throw new ParameterInvalidURIException("Not valid dataset URI.");
    }
    if (!urlValidator.isValid(predictionFeature)) {
        throw new ParameterInvalidURIException("Not valid prediction feature URI.");
    }
    if (transformations != null && !transformations.isEmpty() && !urlValidator.isValid(transformations)) {
        throw new ParameterInvalidURIException("Not valid transformation URI.");
    }
    if (scaling != null && !scaling.isEmpty() && !urlValidator.isValid(scaling)) {
        throw new ParameterInvalidURIException("Not valid scaling URI.");
    }
    if ((stratify != null && !stratify.isEmpty() && !stratify.equals("random") && !stratify.equals("normal"))) {
        throw new BadRequestException("Not valid stratify option - choose between random and normal");
    }

    Task task = new Task(new ROG(true).nextString(12));
    task.setMeta(MetaInfoBuilder.builder().setCurrentDate()
            .addTitles("Validation on algorithm: " + algorithmURI).addComments("Validation task created")
            .addDescriptions("Validation task using algorithm " + algorithmURI + " and dataset " + datasetURI)
            .addCreators(securityContext.getUserPrincipal().getName()).build());
    task.setType(Task.Type.VALIDATION);
    task.setHttpStatus(202);
    task.setStatus(Task.Status.QUEUED);
    task.setVisible(Boolean.TRUE);
    Map<String, Object> options = new HashMap<>();
    options.put("taskId", task.getId());
    options.put("algorithm_uri", algorithmURI);
    options.put("dataset_uri", datasetURI);
    options.put("algorithm_params", algorithmParameters);
    options.put("prediction_feature", predictionFeature);
    options.put("folds", folds);
    options.put("stratify", stratify);
    options.put("seed", seed);
    options.put("creator", user.getId());
    options.put("subjectId", subjectId);

    Map<String, String> transformationAlgorithms = new LinkedHashMap<>();
    if (transformations != null && !transformations.isEmpty()) {
        transformationAlgorithms.put(uriInfo.getBaseUri().toString() + "algorithm/pmml",
                "{\"transformations\" : \"" + transformations + "\"}");
    }
    if (scaling != null && !scaling.isEmpty()) {
        transformationAlgorithms.put(scaling, "");
    }
    if (!transformationAlgorithms.isEmpty()) {
        String transformationAlgorithmsString = serializer.write(transformationAlgorithms);
        LOG.log(Level.INFO, "Transformations:{0}", transformationAlgorithmsString);
        options.put("transformations", transformationAlgorithmsString);
    }

    taskHandler.create(task);
    jmsContext.createProducer().setDeliveryDelay(1000).send(crossValidationQueue, options);

    return Response.ok(task).build();
}

From source file:org.jaqpot.core.service.resource.ValidationResource.java

@POST
@Path("/training_test_split")
@ApiOperation(value = "Creates Validation Report", notes = "Creates Validation Report", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response splitValidateAlgorithm(@FormParam("algorithm_uri") String algorithmURI,
        @FormParam("training_dataset_uri") String datasetURI,
        @FormParam("algorithm_params") String algorithmParameters,
        @FormParam("prediction_feature") String predictionFeature,
        @ApiParam(name = "transformations", defaultValue = DEFAULT_TRANSFORMATIONS) @FormParam("transformations") String transformations,
        @ApiParam(name = "scaling", defaultValue = STANDARIZATION) @FormParam("scaling") String scaling, //, allowableValues = SCALING + "," + STANDARIZATION          
        @ApiParam(name = "split_ratio", required = true) @FormParam("split_ratio") Double splitRatio,
        @FormParam("stratify") String stratify, @FormParam("seed") Integer seed,
        @HeaderParam("subjectId") String subjectId)
        throws QuotaExceededException, JMSException, ParameterInvalidURIException, ParameterIsNullException {
    if (algorithmURI == null)
        throw new ParameterIsNullException("algorithmURI");
    if (datasetURI == null)
        throw new ParameterIsNullException("datasetURI");
    if (splitRatio == null)
        throw new ParameterIsNullException("splitRatio");

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long reportCount = reportHandler.countAllOfCreator(user.getId());
    int maxAllowedReports = new UserFacade(user).getMaxReports();

    if (reportCount > maxAllowedReports) {
        LOG.info(String.format("User %s has %d reports while maximum is %d", user.getId(), reportCount,
                maxAllowedReports));//w  ww . ja va 2 s .  c o m
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + reportCount + " reports. "
                + "No more than " + maxAllowedReports + " are allowed with your subscription.");
    }

    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(algorithmURI)) {
        throw new ParameterInvalidURIException("Not valid algorithm URI.");
    }
    if (!urlValidator.isValid(datasetURI)) {
        throw new ParameterInvalidURIException("Not valid dataset URI.");
    }
    if (!urlValidator.isValid(predictionFeature)) {
        throw new ParameterInvalidURIException("Not valid prediction feature URI.");
    }
    if (transformations != null && !transformations.isEmpty() && !urlValidator.isValid(transformations)) {
        throw new ParameterInvalidURIException("Not valid transformation URI.");
    }
    if (scaling != null && !scaling.isEmpty() && !urlValidator.isValid(scaling)) {
        throw new ParameterInvalidURIException("Not valid scaling URI.");
    }
    if ((stratify != null && !stratify.isEmpty() && !stratify.equals("random") && !stratify.equals("normal"))) {
        throw new BadRequestException("Not valid stratify option - choose between random and normal");
    }

    Task task = new Task(new ROG(true).nextString(12));
    task.setMeta(MetaInfoBuilder.builder().setCurrentDate()
            .addTitles("Validation on algorithm: " + algorithmURI).addComments("Validation task created")
            .addDescriptions("Validation task using algorithm " + algorithmURI + " and dataset " + datasetURI)
            .addCreators(securityContext.getUserPrincipal().getName()).build());
    task.setType(Task.Type.VALIDATION);
    task.setHttpStatus(202);
    task.setStatus(Task.Status.QUEUED);
    task.setVisible(Boolean.TRUE);
    Map<String, Object> options = new HashMap<>();
    options.put("taskId", task.getId());
    options.put("algorithm_uri", algorithmURI);
    options.put("dataset_uri", datasetURI);
    options.put("algorithm_params", algorithmParameters);
    options.put("prediction_feature", predictionFeature);
    options.put("scaling", scaling);
    options.put("split_ratio", splitRatio);
    options.put("stratify", stratify);
    options.put("seed", seed);
    options.put("type", "SPLIT");
    options.put("subjectId", subjectId);

    Map<String, String> transformationAlgorithms = new LinkedHashMap<>();
    if (transformations != null && !transformations.isEmpty()) {
        transformationAlgorithms.put(uriInfo.getBaseUri().toString() + "algorithm/pmml",
                "{\"transformations\" : \"" + transformations + "\"}");
    }
    if (scaling != null && !scaling.isEmpty()) {
        transformationAlgorithms.put(scaling, "");
    }
    if (!transformationAlgorithms.isEmpty()) {
        String transformationAlgorithmsString = serializer.write(transformationAlgorithms);
        LOG.log(Level.INFO, "Transformations:{0}", transformationAlgorithmsString);
        options.put("transformations", transformationAlgorithmsString);
    }

    taskHandler.create(task);
    System.out.println(splitValidationQueue.getTopicName());
    jmsContext.createProducer().setDeliveryDelay(1000).send(splitValidationQueue, options);
    return Response.ok(task).build();
}

From source file:org.jaqpot.core.service.resource.ValidationResource.java

@POST
@Path("/test_set_validation")
@ApiOperation(value = "Creates Validation Report", notes = "Creates Validation Report", response = Task.class)
@org.jaqpot.core.service.annotations.Task
public Response externalValidateAlgorithm(@FormParam("model_uri") String modelURI,
        @FormParam("test_dataset_uri") String datasetURI, @HeaderParam("subjectId") String subjectId)
        throws QuotaExceededException, ParameterIsNullException, ParameterInvalidURIException {
    if (modelURI == null)
        throw new ParameterIsNullException("modelURI");
    if (datasetURI == null)
        throw new ParameterIsNullException("datasetURI");

    User user = userHandler.find(securityContext.getUserPrincipal().getName());
    long reportCount = reportHandler.countAllOfCreator(user.getId());
    int maxAllowedReports = new UserFacade(user).getMaxReports();

    if (reportCount > maxAllowedReports) {
        LOG.info(String.format("User %s has %d algorithms while maximum is %d", user.getId(), reportCount,
                maxAllowedReports));//from  ww  w .ja  v a 2s .c o m
        throw new QuotaExceededException("Dear " + user.getId()
                + ", your quota has been exceeded; you already have " + reportCount + " reports. "
                + "No more than " + maxAllowedReports + " are allowed with your subscription.");
    }

    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(modelURI)) {
        throw new ParameterInvalidURIException("Not valid model URI.");
    }
    if (!urlValidator.isValid(datasetURI)) {
        throw new ParameterInvalidURIException("Not valid dataset URI.");
    }

    Task task = new Task(new ROG(true).nextString(12));
    task.setMeta(MetaInfoBuilder.builder().setCurrentDate().addTitles("Validation on model: " + modelURI)
            .addComments("Validation task created")
            .addDescriptions("Validation task using model " + modelURI + " and dataset " + datasetURI)
            .addCreators(securityContext.getUserPrincipal().getName()).build());
    task.setType(Task.Type.VALIDATION);
    task.setHttpStatus(202);
    task.setStatus(Task.Status.QUEUED);
    task.setVisible(Boolean.TRUE);
    Map<String, Object> options = new HashMap<>();
    options.put("taskId", task.getId());
    options.put("model_uri", modelURI);
    options.put("subjectId", subjectId);
    options.put("dataset_uri", datasetURI);
    options.put("base_uri", uriInfo.getBaseUri().toString());
    options.put("type", "EXTERNAL");
    options.put("subjectId", subjectId);
    options.put("creator", user.getId());

    taskHandler.create(task);
    jmsContext.createProducer().setDeliveryDelay(1000).send(externalValidationQueue, options);

    return Response.ok(task).build();
}