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);
}
}
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());
}
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);
}
}
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);
}
}
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;
}
Aggregations