Search in sources :

Example 11 with JsonParseException

use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.

the class Dataverses method addDataverse.

@POST
@Path("{identifier}")
public Response addDataverse(String body, @PathParam("identifier") String parentIdtf) {
    Dataverse d;
    JsonObject dvJson;
    try (StringReader rdr = new StringReader(body)) {
        dvJson = Json.createReader(rdr).readObject();
        d = jsonParser().parseDataverse(dvJson);
    } catch (JsonParsingException jpe) {
        LOGGER.log(Level.SEVERE, "Json: {0}", body);
        return error(Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
    } catch (JsonParseException ex) {
        Logger.getLogger(Dataverses.class.getName()).log(Level.SEVERE, "Error parsing dataverse from json: " + ex.getMessage(), ex);
        return error(Response.Status.BAD_REQUEST, "Error parsing the POSTed json into a dataverse: " + ex.getMessage());
    }
    try {
        if (!parentIdtf.isEmpty()) {
            Dataverse owner = findDataverseOrDie(parentIdtf);
            d.setOwner(owner);
        }
        // set the dataverse - contact relationship in the contacts
        for (DataverseContact dc : d.getDataverseContacts()) {
            dc.setDataverse(d);
        }
        AuthenticatedUser u = findAuthenticatedUserOrDie();
        d = execCommand(new CreateDataverseCommand(d, createDataverseRequest(u), null, null));
        return created("/dataverses/" + d.getAlias(), json(d));
    } catch (WrappedResponse ww) {
        Throwable cause = ww.getCause();
        StringBuilder sb = new StringBuilder();
        if (cause == null) {
            return ww.refineResponse("cause was null!");
        }
        while (cause.getCause() != null) {
            cause = cause.getCause();
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                }
            }
        }
        String error = sb.toString();
        if (!error.isEmpty()) {
            LOGGER.log(Level.INFO, error);
            return ww.refineResponse(error);
        }
        return ww.getResponse();
    } catch (EJBException ex) {
        Throwable cause = ex;
        StringBuilder sb = new StringBuilder();
        sb.append("Error creating dataverse.");
        while (cause.getCause() != null) {
            cause = cause.getCause();
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                }
            }
        }
        LOGGER.log(Level.SEVERE, sb.toString());
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error creating dataverse: " + sb.toString());
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Error creating dataverse", ex);
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error creating dataverse: " + ex.getMessage());
    }
}
Also used : JsonObject(javax.json.JsonObject) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) JsonString(javax.json.JsonString) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) CreateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand) Dataverse(edu.harvard.iq.dataverse.Dataverse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) ConstraintViolationException(javax.validation.ConstraintViolationException) ConstraintViolation(javax.validation.ConstraintViolation) StringReader(java.io.StringReader) ConstraintViolationException(javax.validation.ConstraintViolationException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 12 with JsonParseException

use of edu.harvard.iq.dataverse.util.json.JsonParseException in project dataverse by IQSS.

the class HarvestingClients method modifyHarvestingClient.

@PUT
@Path("{nickName}")
public Response modifyHarvestingClient(String jsonBody, @PathParam("nickName") String nickName, @QueryParam("key") String apiKey) throws IOException, JsonParseException {
    HarvestingClient harvestingClient = null;
    try {
        harvestingClient = harvestingClientService.findByNickname(nickName);
    } catch (Exception ex) {
        // We don't care what happened; we'll just assume we couldn't find it.
        harvestingClient = null;
    }
    if (harvestingClient == null) {
        return error(Response.Status.NOT_FOUND, "Harvesting client " + nickName + " not found.");
    }
    String ownerDataverseAlias = harvestingClient.getDataverse().getAlias();
    try (StringReader rdr = new StringReader(jsonBody)) {
        DataverseRequest req = createDataverseRequest(findUserOrDie());
        JsonObject json = Json.createReader(rdr).readObject();
        String newDataverseAlias = jsonParser().parseHarvestingClient(json, harvestingClient);
        if (newDataverseAlias != null && !newDataverseAlias.equals("") && !newDataverseAlias.equals(ownerDataverseAlias)) {
            return error(Response.Status.BAD_REQUEST, "Bad \"dataverseAlias\" supplied. Harvesting client " + nickName + " belongs to the dataverse " + ownerDataverseAlias);
        }
        HarvestingClient managedHarvestingClient = execCommand(new UpdateHarvestingClientCommand(req, harvestingClient));
        return created("/datasets/" + nickName, harvestingConfigAsJson(managedHarvestingClient));
    } catch (JsonParseException ex) {
        return error(Response.Status.BAD_REQUEST, "Error parsing harvesting client: " + ex.getMessage());
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) UpdateHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateHarvestingClientCommand) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) IOException(java.io.IOException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)12 StringReader (java.io.StringReader)11 JsonObject (javax.json.JsonObject)11 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)6 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)6 JsonReader (javax.json.JsonReader)6 Path (javax.ws.rs.Path)6 Gson (com.google.gson.Gson)5 POST (javax.ws.rs.POST)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 Dataset (edu.harvard.iq.dataverse.Dataset)4 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 EJBException (javax.ejb.EJBException)4 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)3 CreateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand)3 IOException (java.io.IOException)3 ConstraintViolation (javax.validation.ConstraintViolation)3 GsonBuilder (com.google.gson.GsonBuilder)2 DatasetField (edu.harvard.iq.dataverse.DatasetField)2