@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN}) public static Result getAll() { ExpressionList<Basestation> exp = QueryHelper.buildQuery(Basestation.class, Basestation.FIND.where()); List<JsonHelper.Tuple> tuples = exp.findList().stream().map(basestation -> new JsonHelper.Tuple(basestation, new ControllerHelper.Link("self", controllers.routes.BasestationController.get(basestation.getId()).absoluteURL(request())))).collect(Collectors.toList()); // TODO: add links when available List<ControllerHelper.Link> links = new ArrayList<>(); links.add(new ControllerHelper.Link("self", controllers.routes.BasestationController.getAll().absoluteURL(request()))); links.add(new ControllerHelper.Link("total", controllers.routes.BasestationController.getTotal().absoluteURL(request()))); try { JsonNode result = JsonHelper.createJsonNode(tuples, links, Basestation.class); String[] totalQuery = request().queryString().get("total"); if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) { ExpressionList<Basestation> countExpression = QueryHelper.buildQuery(Basestation.class, Basestation.FIND.where(), true); String root = Basestation.class.getAnnotation(JsonRootName.class).value(); ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount()); } return ok(result); } catch(JsonProcessingException ex) { play.Logger.error(ex.getMessage(), ex); return internalServerError(); } }
@Authentication({User.Role.READONLY_ADMIN, User.Role.ADMIN}) public static Result getAll() { ExpressionList<Assignment> exp = QueryHelper.buildQuery(Assignment.class, Assignment.FIND.where()); List<JsonHelper.Tuple> tuples = exp.findList().stream().map(assignment -> new JsonHelper.Tuple(assignment, new ControllerHelper.Link("self", controllers.routes.AssignmentController.get(assignment.getId()).absoluteURL(request())))).collect(Collectors.toList()); // TODO: add links when available List<ControllerHelper.Link> links = new ArrayList<>(); links.add(new ControllerHelper.Link("self", controllers.routes.AssignmentController.getAll().absoluteURL(request()))); links.add(new ControllerHelper.Link("total", controllers.routes.AssignmentController.getTotal().absoluteURL(request()))); try { JsonNode result = JsonHelper.createJsonNode(tuples, links, Assignment.class); String[] totalQuery = request().queryString().get("total"); if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) { ExpressionList<Assignment> countExpression = QueryHelper.buildQuery(Assignment.class, Assignment.FIND.where(), true); String root = Assignment.class.getAnnotation(JsonRootName.class).value(); ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount()); } return ok(result); } catch(JsonProcessingException ex) { play.Logger.error(ex.getMessage(), ex); return internalServerError(); } }
/** * Parse response into data model object. * * @param unwrap Unwrap datamodel * @return Response datamodel * @throws IOException */ public T parseResponse() throws IOException { ObjectReader reader = MAPPER.readerFor(datamodel).without(Feature.AUTO_CLOSE_SOURCE); if (datamodel.isAnnotationPresent(JsonRootName.class)) { reader = reader.with(DeserializationFeature.UNWRAP_ROOT_VALUE); } try (final InputStream is = response.getStream()) { final T result = reader.readValue(is); /* * ObjectReader.readValue might not consume the entire inputstream, * we skip everything after the json root element. * This is needed for proper http connection reuse (keep alive). */ is.skip(Long.MAX_VALUE); return result; } }
/** * Build the constructor. * * @param mappedTypeClass the type to map * * @return the constructor method */ private MethodSpec buildConstructor( JClassType mappedTypeClass ) { Optional<JsonRootName> jsonRootName = findFirstEncounteredAnnotationsOnAllHierarchy( configuration, mappedTypeClass, JsonRootName.class ); String rootName; if ( !jsonRootName.isPresent() || Strings.isNullOrEmpty( jsonRootName.get().value() ) ) { rootName = mappedTypeClass.getSimpleSourceName(); } else { rootName = jsonRootName.get().value(); } return MethodSpec.constructorBuilder() .addModifiers( Modifier.PUBLIC ) .addStatement( "super($S)", rootName ) .build(); }
@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN}) public static Result getAll() { ExpressionList<User> exp = QueryHelper.buildQuery(User.class, User.FIND.where()); List<JsonHelper.Tuple> tuples = exp.findList().stream().map(user -> new JsonHelper.Tuple(user, new ControllerHelper.Link("self", controllers.routes.UserController.get(user.getId()).absoluteURL(request())))).collect(Collectors.toList()); // TODO: add links when available List<ControllerHelper.Link> links = new ArrayList<>(); links.add(new ControllerHelper.Link("self", controllers.routes.UserController.getAll().absoluteURL(request()))); links.add(new ControllerHelper.Link("total", controllers.routes.UserController.getTotal().absoluteURL(request()))); links.add(new ControllerHelper.Link("me", controllers.routes.UserController.currentUser().absoluteURL(request()))); try { JsonNode result = JsonHelper.createJsonNode(tuples, links, User.class); String[] totalQuery = request().queryString().get("total"); if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) { ExpressionList<User> countExpression = QueryHelper.buildQuery(User.class, User.FIND.where(), true); String root = User.class.getAnnotation(JsonRootName.class).value(); ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount()); } return ok(result); } catch(JsonProcessingException ex) { play.Logger.error(ex.getMessage(), ex); return internalServerError(); } }
@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN}) public static Result getAll() { ExpressionList<Drone> exp = QueryHelper.buildQuery(Drone.class, Drone.FIND.where(),false); List<JsonHelper.Tuple> tuples = exp.findList().stream().map(drone -> new JsonHelper.Tuple(drone, new ControllerHelper.Link("self", controllers.routes.DroneController.get(drone.getId()).absoluteURL(request())))).collect(Collectors.toList()); // TODO: add links when available List<ControllerHelper.Link> links = new ArrayList<>(); links.add(new ControllerHelper.Link("self", controllers.routes.DroneController.getAll().absoluteURL(request()))); links.add(new ControllerHelper.Link("total", controllers.routes.DroneController.getTotal().absoluteURL(request()))); links.add(new ControllerHelper.Link("types", controllers.routes.DroneController.getSuportedTypes().absoluteURL(request()))); try { JsonNode result = JsonHelper.createJsonNode(tuples, links, Drone.class); String[] totalQuery = request().queryString().get("total"); if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) { ExpressionList<Drone> countExpression = QueryHelper.buildQuery(Drone.class, Drone.FIND.where(), true); String root = Drone.class.getAnnotation(JsonRootName.class).value(); ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount()); } return ok(result); } catch(JsonProcessingException ex) { play.Logger.error(ex.getMessage(), ex); return internalServerError(); } }
public static JsonNode removeRootElement(JsonNode node, Class clazz, boolean isList) throws InvalidJSONException { JsonRootName annotation = (JsonRootName) clazz.getAnnotation(JsonRootName.class); String rootElement = annotation.value(); JsonNode rootNode = isList ? node.get(rootElement).get(DEFAULT_ROOT_ELEMENT) : node.get(rootElement); if(rootNode == null) throw new InvalidJSONException("Invalid json: no such element (" + (isList ? DEFAULT_ROOT_ELEMENT : annotation.value()) + ")"); return rootNode; }
public static JsonNode addRootElement(JsonNode node, Class clazz) { JsonRootName annotation = (JsonRootName) clazz.getAnnotation(JsonRootName.class); String rootElement = annotation.value(); ObjectNode nodeWithRoot = Json.newObject(); nodeWithRoot.put(rootElement, node); return nodeWithRoot; }
@Override public String getItemResourceRelFor(Class<?> type) { Class<?> baseType = determineBaseType(type); JsonRootName rootName = getAnnotationByType(baseType, JsonRootName.class); return (rootName == null) ? defaultRelProvider.getItemResourceRelFor(baseType) : rootName.value(); }
public PropertyName findRootName(AnnotatedClass paramAnnotatedClass) { JsonRootName localJsonRootName = (JsonRootName)paramAnnotatedClass.getAnnotation(JsonRootName.class); if (localJsonRootName == null) return null; return new PropertyName(localJsonRootName.value()); }
private Optional<String> fromJsonRoot(Class<?> type) { return stream(type.getAnnotationsByType(JsonRootName.class)) .findFirst() .map(JsonRootName::value); }
private static String rootNameFor(Class<?> type) { JsonRootName rootName = type.getAnnotation(JsonRootName.class); return rootName == null ? type.getSimpleName() : rootName.value(); }