Search in sources :

Example 11 with ParseException

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

the class SpacesResource method createSpace.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Space successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a space", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createSpace(@ApiParam(required = true) CreateSpacePojo createSpacePojo) throws ServiceInvocationException {
    Space space = createSpace(createSpacePojo.getName());
    try {
        Gson gson = new Gson();
        String spaceJSON = gson.toJson(createSpacePojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        try {
            JSONObject obj = (JSONObject) p.parse(spaceJSON);
            obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_9, obj.toJSONString());
        } catch (ParseException e) {
            logger.warning("Problems with monitoring event...");
            e.printStackTrace();
        }
        try {
            SpaceSubscription rmiResult = (SpaceSubscription) Context.get().invoke(new ServiceNameVersion(NoracleAgentService.class.getCanonicalName(), NoracleService.API_VERSION), "subscribeToSpace", space.getSpaceId(), space.getSpaceSecret());
        } catch (Exception ex) {
            logger.warning("Error, while trying to subscribe to space: " + ex.getMessage());
        }
        // TODO: return https instead of http
        URI uri = new URI(null, null, RESOURCE_NAME + "/" + space.getSpaceId(), null);
        Response response = Response.created(uri).build();
        return response;
    } catch (URISyntaxException e) {
        logger.warning("URISyntaxException: " + e.getMessage());
        throw new InternalServerErrorException(e);
    }
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) Gson(com.google.gson.Gson) NoracleAgentService(i5.las2peer.services.noracleService.NoracleAgentService) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ParseException(net.minidev.json.parser.ParseException) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) JSONObject(net.minidev.json.JSONObject) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription) ApiResponses(io.swagger.annotations.ApiResponses)

Example 12 with ParseException

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

the class QuestionRelationsResource method changeQuestionRelation.

@PUT
@Path("/{relationId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Changes a question relation", response = QuestionRelation.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No relation id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Relation Not Found", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public QuestionRelation changeQuestionRelation(@PathParam("relationId") String relationId, @ApiParam(required = true) ChangeQuestionRelationPojo changeQuestionRelationPojo) throws ServiceInvocationException {
    Gson gson = new Gson();
    String changeRelationPojoJson = gson.toJson(changeQuestionRelationPojo);
    JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
    try {
        JSONObject obj = (JSONObject) p.parse(changeRelationPojoJson);
        obj.put("relId", relationId);
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_4, obj.toJSONString());
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // Try to log training data
    try {
        String from = getQuestionText(changeQuestionRelationPojo.getQuestionId1());
        String to = getQuestionText(changeQuestionRelationPojo.getQuestionId2());
        JSONObject trainingData = new JSONObject();
        trainingData.put("from", from);
        trainingData.put("to", to);
        if (changeQuestionRelationPojo.getDirected())
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_42, trainingData.toString());
        else
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_43, trainingData.toString());
    } catch (ServiceInvocationException e) {
    /* getDirected will return null at this point */
    // Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_42,
    // changeQuestionRelationPojo.getDirected().toString());
    }
    return changeQuestionRelation(relationId, changeQuestionRelationPojo.getName(), changeQuestionRelationPojo.getQuestionId1(), changeQuestionRelationPojo.getQuestionId2(), changeQuestionRelationPojo.getDirected());
}
Also used : JSONObject(net.minidev.json.JSONObject) Gson(com.google.gson.Gson) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with ParseException

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

the class GeoJsonQueryResponseTransformer method createGeoJSON.

private Object createGeoJSON(Metacard metacard) throws CatalogTransformerException {
    if (metacardTransformer == null) {
        throw new CatalogTransformerException("The metacard transformer cannot be null");
    }
    BinaryContent rawContent = metacardTransformer.transform(metacard, null);
    JSONParser jsonParser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
    try {
        return jsonParser.parse(rawContent.getInputStream());
    } catch (ParseException | UnsupportedEncodingException e) {
        throw new CatalogTransformerException("Unable to parse transformed metacard content as JSON", e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) JSONParser(net.minidev.json.parser.JSONParser) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(net.minidev.json.parser.ParseException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 14 with ParseException

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

the class Replay method get.

@Override
public TestCase get() {
    try {
        String json = in.readLine();
        if (json == null) {
            return null;
        }
        TestCase tc = TestCase.fromJSON(constraints, json);
        if (restriction.size() == 1) {
            if (restriction.contains(Restriction.CONTINUOUS) && !tc.impl().setContinuous(true)) {
                throw new IllegalArgumentException("Cannot be CONTINUOUS");
            } else if (!tc.impl().setContinuous(false)) {
                throw new IllegalArgumentException("Cannot be DISCRETE");
            }
        }
        return tc;
    } catch (IOException | ParseException | JSONConverterException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : TestCase(org.btrplace.safeplace.testing.TestCase) IOException(java.io.IOException) ParseException(net.minidev.json.parser.ParseException) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 15 with ParseException

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

the class TestCase method fromJSON.

public static TestCase fromJSON(List<Constraint> cstrs, String c) throws ParseException, JSONConverterException {
    JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
    JSONObject o = (JSONObject) p.parse(new StringReader(c));
    String cId = o.getAsString("constraint");
    Optional<Constraint> opt = cstrs.stream().filter(x -> x.id().equals(cId)).findFirst();
    if (!opt.isPresent()) {
        throw new IllegalArgumentException("Unknown constraint '" + cId + "'");
    }
    Constraint cstr = opt.get();
    InstanceConverter ic = new InstanceConverter();
    ic.getConstraintsConverter().register(new ScheduleConverter());
    ReconfigurationPlanConverter rc = new ReconfigurationPlanConverter();
    Instance i = ic.fromJSON(o.getAsString("instance"));
    ReconfigurationPlan plan = rc.fromJSON(o.getAsString("plan"));
    TestCase tc = new TestCase(i, plan, cstr);
    List<Constant> l = new ArrayList<>();
    for (Object x : (JSONArray) o.get("args")) {
        l.add(Constant.fromJSON((JSONObject) x));
    }
    tc.args(l);
    if (cstr.isSatConstraint()) {
        tc.impl(cstr.instantiate(l.stream().map(x -> x.eval(null)).collect(Collectors.toList())));
    }
    if (tc.impl() != null) {
        tc.impl().setContinuous((Boolean) o.get("continuous"));
    }
    return tc;
}
Also used : Constant(org.btrplace.safeplace.spec.term.Constant) ModelView(org.btrplace.model.view.ModelView) JSONConverterException(org.btrplace.json.JSONConverterException) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) Collectors(java.util.stream.Collectors) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) Objects(java.util.Objects) Constraint(org.btrplace.safeplace.spec.Constraint) List(java.util.List) StringReader(java.io.StringReader) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) ParseException(net.minidev.json.parser.ParseException) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Optional(java.util.Optional) Instance(org.btrplace.model.Instance) Collections(java.util.Collections) SatConstraint(org.btrplace.model.constraint.SatConstraint) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Constraint(org.btrplace.safeplace.spec.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) Instance(org.btrplace.model.Instance) Constant(org.btrplace.safeplace.spec.term.Constant) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) StringReader(java.io.StringReader) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject)

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