@GET @Path("session/{sessionId}") @Produces(MediaType.APPLICATION_JSON) public String getSession(@PathParam("sessionId") String sessionId) throws IcatException { String userName = beanManager.getUserName(sessionId, manager); double remainingMinutes = beanManager.getRemainingMinutes(sessionId, manager); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject() .write("userName", userName) .write("remainingMinutes", remainingMinutes) .writeEnd(); gen.close(); return baos.toString(); }
@POST @Path("entityManager") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public String create(@FormParam("sessionId") String sessionId, @FormParam("entities") String json) throws IcatException { List<EntityBaseBean> entities = new ArrayList<>(); try (JsonParser parser = Json.createParser(new ByteArrayInputStream(json.getBytes()))) { EventChecker checker = new EventChecker(parser); checker.get(Event.START_ARRAY); while (true) { Event event = checker.get(Event.START_OBJECT, Event.END_ARRAY); if (event == Event.END_ARRAY) { break; } checker.get(Event.KEY_NAME); String entityName = parser.getString(); checker.get(Event.START_OBJECT); entities.add(parseEntity(checker, entityName)); checker.get(Event.END_OBJECT); } } catch (JsonException e) { throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage()); } String userName = beanManager.getUserName(sessionId, manager); List<CreateResponse> createResponses = beanManager.createMany(userName, entities, manager, userTransaction); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (JsonGenerator gen = Json.createGenerator(baos)) { gen.writeStartArray(); for (CreateResponse createResponse : createResponses) { transmitter.processMessage(createResponse.getNotificationMessage()); gen.write(createResponse.getPk()); } gen.writeEnd(); } catch (JMSException e) { throw new IcatException( IcatException.IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage()); } return baos.toString(); }
@GET @Path("entityManager") @Produces(MediaType.APPLICATION_JSON) public String search( @QueryParam("sessionId") String sessionId, @QueryParam("query") String query, @QueryParam("id") Long id) throws IcatException { if (sessionId == null) { throw new IcatException(IcatExceptionType.BAD_PARAMETER, "sessionId is not set"); } if (query == null) { throw new IcatException(IcatExceptionType.BAD_PARAMETER, "query is not set"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); String userName = beanManager.getUserName(sessionId, manager); if (id == null) { gen.writeStartArray(); for (Object result : beanManager.search(userName, query, manager, userTransaction)) { if (result == null) { gen.writeNull(); } else if (result instanceof EntityBaseBean) { gen.writeStartObject(); gen.writeStartObject(result.getClass().getSimpleName()); jsonise((EntityBaseBean) result, gen); gen.writeEnd(); gen.writeEnd(); } else if (result instanceof Long) { gen.write((Long) result); } else if (result instanceof Double) { if (Double.isNaN((double) result)) { gen.writeNull(); } else { gen.write((Double) result); } } else if (result instanceof String) { gen.write((String) result); } else if (result instanceof Boolean) { gen.write((Boolean) result); } else { throw new IcatException( IcatException.IcatExceptionType.INTERNAL, "Don't know how to jsonise " + result.getClass()); } } gen.writeEnd(); } else { EntityBaseBean result = beanManager.get(userName, query, id, manager, userTransaction); gen.writeStartObject(); gen.writeStartObject(result.getClass().getSimpleName()); jsonise(result, gen); gen.writeEnd(); gen.writeEnd(); } gen.close(); return baos.toString(); }