@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(); }
@POST @Path("session") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public String login(@Context HttpServletRequest request, @FormParam("json") String jsonString) throws IcatException { logger.debug(jsonString); if (jsonString == null) { throw new IcatException(IcatExceptionType.BAD_PARAMETER, "json must not be null"); } String plugin = null; Map<String, String> credentials = new HashMap<>(); try (JsonParser parser = Json.createParser(new ByteArrayInputStream(jsonString.getBytes()))) { String key = null; boolean inCredentials = false; while (parser.hasNext()) { JsonParser.Event event = parser.next(); if (event == Event.KEY_NAME) { key = parser.getString(); } else if (event == Event.VALUE_STRING) { if (inCredentials) { credentials.put(key, parser.getString()); } else { if (key.equals("plugin")) { plugin = parser.getString(); } } } else if (event == Event.START_ARRAY && key.equals("credentials")) { inCredentials = true; } else if (event == Event.END_ARRAY) { inCredentials = false; } } } Authenticator authenticator = authPlugins.get(plugin); if (authenticator == null) { throw new IcatException( IcatException.IcatExceptionType.SESSION, "Authenticator mnemonic " + plugin + " not recognised"); } logger.debug("Using " + plugin + " to authenticate"); String userName = authenticator.authenticate(credentials, request.getRemoteAddr()).getUserName(); String sessionId = beanManager.login(userName, lifetimeMinutes, manager, userTransaction); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("sessionId", sessionId).writeEnd(); gen.close(); return baos.toString(); }
@PUT @Path("session/{sessionId}") public void refresh(@PathParam("sessionId") String sessionId) throws IcatException { beanManager.refresh(sessionId, lifetimeMinutes, manager, userTransaction); }
@DELETE @Path("session/{sessionId}") public void logout(@PathParam("sessionId") String sessionId) throws IcatException { beanManager.logout(sessionId, manager, userTransaction); }
@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(); }