@Override public DataStore deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String dataStoreType = value.getNode("type").getString(value.getKey().toString()); Optional<DataStoreFactory> factory = DataStoreFactories.get(dataStoreType); if (!factory.isPresent()) { throw new ObjectMappingException("Unknown DataStore type " + dataStoreType); } try { return factory.get().createDataStore(value.getKey().toString(), value); } catch (PermissionsLoadingException e) { throw new ObjectMappingException(e); } }
@Override public Object[] getPath() { LinkedList<Object> pathElements = new LinkedList<>(); ConfigurationNode ptr = this; if (ptr.getParent() == null) { return new Object[] {this.getKey()}; } do { pathElements.addFirst(ptr.getKey()); } while ((ptr = ptr.getParent()).getParent() != null); return pathElements.toArray(); }
@Override @SuppressWarnings("unchecked") // i continue to hate generics public Enum deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String enumConstant = value.getString(); if (enumConstant == null) { throw new ObjectMappingException("No value present in node " + value); } Optional<Enum> ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), enumConstant); // XXX: intellij says this cast is optional but it isnt if (!ret.isPresent()) { throw new ObjectMappingException("Invalid enum constant provided for " + value.getKey() + ": " + "Expected a value of enum " + type + ", got " + enumConstant); } return ret.get(); }
@Override @SuppressWarnings("unchecked") // i continue to hate generics public Enum deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String enumConstant = value.getString(); if (enumConstant == null) { throw new ObjectMappingException("No value present in node " + value); } Optional<Enum> ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), enumConstant); // XXX: intellij says this cast is optional but it isnt if (!ret.isPresent()) { throw new ObjectMappingException("Invalid enum constant provided for " + value.getKey() + ": " + "Expected a value of enum " + type + ", got " + enumConstant); } return ret.get(); }
@Override @SuppressWarnings("unchecked") // i continue to hate generics public Enum deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String enumConstant = value.getString(); if (enumConstant == null) { throw new ObjectMappingException("No value present in node " + value); } Optional<Enum> ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), enumConstant); // XXX: intellij says this cast is optional but it isnt if (!ret.isPresent()) { throw new ObjectMappingException("Invalid enum constant provided for " + value.getKey() + ": " + "Expected a value of enum " + type + ", got " + enumConstant); } return ret.get(); }
@Override public URI deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URL deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUrl = value.getString(); if (plainUrl == null) { throw new ObjectMappingException("No value present in node " + value); } URL url; try { url = new URL(plainUrl); } catch (MalformedURLException e) { throw new ObjectMappingException("Invalid URL string provided for " + value.getKey() + ": got " + plainUrl); } return url; }
@NonNull @Override public Object[] getPath() { LinkedList<Object> pathElements = new LinkedList<>(); ConfigurationNode pointer = this; if (pointer.getParent() == null) { return new Object[]{this.getKey()}; } do { pathElements.addFirst(pointer.getKey()); } while ((pointer = pointer.getParent()).getParent() != null); return pathElements.toArray(); }
@Override @SuppressWarnings("unchecked") // Correct types are verified in the constructor public String serialize(ConfigurationNode node) throws PermissionsLoadingException { Objects.requireNonNull(node, "node"); try { ((ObjectMapper) factory.mapper).bind(this).serialize(node); } catch (ObjectMappingException e) { throw new PermissionsLoadingException(t("Error while serializing backend %s", node.getKey()), e); } return factory.type; }
@NonNull @Override public Object[] getPath() { LinkedList<Object> pathElements = new LinkedList<>(); ConfigurationNode pointer = this; if (pointer.getParent() == null) { return new Object[]{this.getKey()}; } do { pathElements.addFirst(pointer.getKey()); } while ((pointer = pointer.getParent()).getParent() != null); return pathElements.toArray(); }
private static void generateValue(JsonWriter generator, ConfigurationNode node) throws IOException { if (node.hasMapChildren()) { generateObject(generator, node); } else if (node.hasListChildren()) { generateArray(generator, node); } else if (node.getKey() == null && node.getValue() == null) { generator.beginObject(); generator.endObject(); } else { Object value = node.getValue(); if (value instanceof Double) { generator.value((Double) value); } else if (value instanceof Float) { generator.value((Float) value); } else if (value instanceof Long) { generator.value((Long) value); } else if (value instanceof Integer) { generator.value((Integer) value); } else if (value instanceof Boolean) { generator.value((Boolean) value); } else { generator.value(value.toString()); } } }
private static void generateValue(JsonWriter generator, ConfigurationNode node) throws IOException { if (node.hasMapChildren()) { generateObject(generator, node); } else if (node.hasListChildren()) { generateArray(generator, node); } else if (node.getKey() == null && node.getValue() == null) { generator.beginObject(); generator.endObject(); } else { Object value = node.getValue(); if (value instanceof Double) { generator.value((Double) value); } else if (value instanceof Float) { generator.value((Float) value); } else if (value instanceof Long) { generator.value((Long) value); } else if (value instanceof Integer) { generator.value((Integer) value); } else if (value instanceof Boolean) { generator.value((Boolean) value); } else { generator.value(value.toString()); } } }
@Override public UserPermissionStruct deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String username = value.getKey().toString(); String password = value.getNode("password").getString(); TreeNode perms = SecurityService.permissionTreeFromConfig(value.getNode("permissions")); return new UserPermissionStruct(username, password, perms); }
private static <T extends ConfigurationNode> Iterator<VisitedNodeImpl<T>> getChildren(VisitedNodeImpl<T> from) { T node = from.getNode(); switch (node.getValueType()) { case LIST: { Object[] path = from.getRawPath(); return Iterators.transform(node.getChildrenList().iterator(), child -> { Objects.requireNonNull(child); //noinspection unchecked T castedChild = ((T) child); Object[] childPath = calculatePath(path, child.getKey()); return new VisitedNodeImpl<>(childPath, castedChild); }); } case MAP: { Object[] path = from.getRawPath(); return Iterators.transform(node.getChildrenMap().entrySet().iterator(), child -> { Objects.requireNonNull(child); //noinspection unchecked T castedChild = ((T) child.getValue()); Object[] childPath = calculatePath(path, child.getKey()); return new VisitedNodeImpl<>(childPath, castedChild); }); } default: return Collections.emptyIterator(); } }
/** * Returns a permissions tree representing the passed configuration node * @param config The configuration node which represents a permission set * @return A tree structure representing the config node */ public static TreeNode permissionTreeFromConfig(ConfigurationNode config) { if (config == null || config.getValue() == null) { return new TreeNode(false); } if (!config.hasMapChildren()) { if (config.getValue().getClass() == Boolean.class) return new TreeNode(config.getKey().toString(), config.getBoolean()); else { TreeNode node = new TreeNode(config.getKey().toString(), true); node.addChild(new TreeNode("*", true)); return node; } } TreeNode root = new TreeNode(config.getKey().toString(), true); for (Map.Entry<Object, ? extends ConfigurationNode> entry : config.getChildrenMap().entrySet()) { if (entry.getKey().toString().equalsIgnoreCase(".")) { root.setValue(entry.getValue().getBoolean()); continue; } root.addChild(permissionTreeFromConfig(entry.getValue())); } return root; }
@Override public URI deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URI deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URL deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUrl = value.getString(); if (plainUrl == null) { throw new ObjectMappingException("No value present in node " + value); } URL url; try { url = new URL(plainUrl); } catch (MalformedURLException e) { throw new ObjectMappingException("Invalid URL string provided for " + value.getKey() + ": got " + plainUrl); } return url; }
@Override public URL deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUrl = value.getString(); if (plainUrl == null) { throw new ObjectMappingException("No value present in node " + value); } URL url; try { url = new URL(plainUrl); } catch (MalformedURLException e) { throw new ObjectMappingException("Invalid URL string provided for " + value.getKey() + ": got " + plainUrl); } return url; }
@Override public PermissionStruct deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String key = value.getKey().toString(); TreeNode perms = WebAPI.getSecurityService().permissionTreeFromConfig(value.getNode("permissions")); int rateLimit = value.getNode("rateLimit").getInt(); return new PermissionStruct(key, perms, rateLimit); }