Search in sources :

Example 6 with ParseException

use of net.minidev.json.parser.ParseException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionsResource method changeQuestionText.

@PUT
@Path("/{questionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "The updated question from the network", response = Question.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No question id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Question Not Found", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Question changeQuestionText(@PathParam("questionId") String questionId, ChangeQuestionPojo changeQuestionPojo) throws ServiceInvocationException {
    Gson gson = new Gson();
    String createRelationPojoJson = gson.toJson(changeQuestionPojo);
    JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
    try {
        JSONObject obj = (JSONObject) p.parse(createRelationPojoJson);
        obj.put("qId", questionId);
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_6, obj.toJSONString());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return changeQuestionText(questionId, changeQuestionPojo.getText());
}
Also used : JSONObject(net.minidev.json.JSONObject) Gson(com.google.gson.Gson) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with ParseException

use of net.minidev.json.parser.ParseException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionsResource method createQuestion.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Question successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No question text given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a question", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createQuestion(@PathParam("spaceId") String questionSpaceId, @ApiParam(required = true) CreateQuestionPojo createQuestionPojo) throws ServiceInvocationException {
    Question question = createQuestion(questionSpaceId, createQuestionPojo.getText());
    try {
        Gson gson = new Gson();
        String createRelationPojoJson = gson.toJson(createQuestionPojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        JSONObject obj = new JSONObject();
        JSONObject attributes = new JSONObject();
        obj.put("functionName", "createQuestion");
        obj.put("serviceAlias", "distributed-noracle");
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        obj.put("qid", question.getQuestionId());
        attributes.put("spaceId", questionSpaceId);
        attributes.put("userId", Context.getCurrent().getMainAgent().getIdentifier());
        attributes.put("body", p.parse(createRelationPojoJson));
        attributes.put("result", question.getQuestionId());
        obj.put("attributes", attributes);
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_5, obj.toJSONString());
        return Response.created(new URI(null, null, SpacesResource.RESOURCE_NAME + "/" + questionSpaceId + "/" + RESOURCE_NAME + "/" + question.getQuestionId(), null)).build();
    } catch (URISyntaxException | ParseException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) Gson(com.google.gson.Gson) JSONParser(net.minidev.json.parser.JSONParser) URISyntaxException(java.net.URISyntaxException) ParseException(net.minidev.json.parser.ParseException) URI(java.net.URI) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with ParseException

use of net.minidev.json.parser.ParseException in project scheduler by btrplace.

the class JSONObjectConverter method fromJSON.

/**
 * Un-serialize an object from a stream.
 * The stream must be close afterward
 *
 * @param r the stream to read
 * @return the resulting object
 * @throws JSONConverterException if the stream cannot be parsed
 */
default E fromJSON(Reader r) throws JSONConverterException {
    try {
        JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
        Object o = p.parse(r);
        if (!(o instanceof JSONObject)) {
            throw new JSONConverterException("Unable to parse a JSON object");
        }
        return fromJSON((JSONObject) o);
    } catch (ParseException ex) {
        throw new JSONConverterException(ex);
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject) ParseException(net.minidev.json.parser.ParseException)

Example 9 with ParseException

use of net.minidev.json.parser.ParseException in project ddf by codice.

the class GeoNamesWebService method query.

private Object query(String urlStr) {
    final String response;
    try {
        WebClient client = createWebClient(urlStr);
        response = client.acceptEncoding(StandardCharsets.UTF_8.name()).accept("application/json").get(String.class);
    } catch (WebApplicationException | ProcessingException e) {
        LOGGER.debug("Error while making GeoNames request.", e);
        return null;
    }
    try {
        JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
        return parser.parse(response);
    } catch (ParseException e) {
        LOGGER.debug("Error while parsing JSON message from GeoNames service.", e);
        return null;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) WebClient(org.apache.cxf.jaxrs.client.WebClient) ProcessingException(javax.ws.rs.ProcessingException)

Example 10 with ParseException

use of net.minidev.json.parser.ParseException in project ddf by codice.

the class GeoNamesWebService method webQuery.

private Object webQuery(String urlStr) {
    final String response;
    try {
        WebClient client = createWebClient(urlStr);
        response = client.acceptEncoding(StandardCharsets.UTF_8.name()).accept(MediaType.APPLICATION_JSON).get(String.class);
    } catch (WebApplicationException | ProcessingException e) {
        LOGGER.debug("Error while making GeoNames request: {}", urlStr, e);
        return null;
    }
    try {
        JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
        return parser.parse(response);
    } catch (ParseException e) {
        LOGGER.debug("Error while parsing JSON message from GeoNames service.", e);
        return null;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) WebClient(org.apache.cxf.jaxrs.client.WebClient) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

ParseException (net.minidev.json.parser.ParseException)15 JSONParser (net.minidev.json.parser.JSONParser)13 JSONObject (net.minidev.json.JSONObject)11 Gson (com.google.gson.Gson)8 ApiResponses (io.swagger.annotations.ApiResponses)8 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)3 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 ProcessingException (javax.ws.rs.ProcessingException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 JSONConverterException (org.btrplace.json.JSONConverterException)2 BinaryContent (ddf.catalog.data.BinaryContent)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1