diff options
Diffstat (limited to 'common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java')
-rw-r--r-- | common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java | 239 |
1 files changed, 18 insertions, 221 deletions
diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java index ffe47c5c51..aff7392efe 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java @@ -23,254 +23,50 @@ package org.onap.so.client.graphinventory.entities.uri; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.io.Serializable; import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.regex.Pattern; import javax.ws.rs.core.UriBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.onap.so.client.graphinventory.Format; import org.onap.so.client.graphinventory.GraphInventoryObjectPlurals; import org.onap.so.client.graphinventory.GraphInventoryObjectType; -import org.onap.so.client.graphinventory.entities.uri.parsers.UriParser; -import org.onap.so.client.graphinventory.entities.uri.parsers.UriParserSpringImpl; -import org.onap.so.client.graphinventory.exceptions.IncorrectNumberOfUriKeys; -import org.springframework.web.util.UriUtils; -public class SimpleUri implements GraphInventoryResourceUri, Serializable { +public abstract class SimpleUri<T extends GraphInventorySingleResourceUri<?, ?, ?, ?>, PT extends GraphInventoryPluralResourceUri<?, ?>, S extends GraphInventoryObjectType, P extends GraphInventoryObjectPlurals> + extends SimpleBaseUri<T, T, S> implements GraphInventorySingleResourceUri<T, PT, S, P> { private static final long serialVersionUID = -337701171277616439L; - - protected transient UriBuilder internalURI; protected static final String relationshipAPI = "/relationship-list/relationship"; protected static final String relatedTo = "/related-to"; - protected final Object[] values; - protected final GraphInventoryObjectType type; - protected final GraphInventoryObjectPlurals pluralType; - - protected SimpleUri(GraphInventoryObjectType type, Object... values) { - this.type = type; - this.pluralType = null; - this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); - this.values = values; - validateValuesSize(this.getTemplate(type), values); - } - - protected SimpleUri(GraphInventoryObjectType type, URI uri) { - this.type = type; - this.pluralType = null; - this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll(getPrefixPattern().toString(), "")); - this.values = new Object[0]; - } - protected SimpleUri(GraphInventoryObjectType type, UriBuilder builder, Object... values) { - this.internalURI = builder; - this.values = values; - this.type = type; - this.pluralType = null; + protected SimpleUri(S type, Object... values) { + super(type, values); } - protected SimpleUri(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) { - this.internalURI = builder; - this.values = values; - this.type = null; - this.pluralType = type; - } + protected SimpleUri(S type, URI uri) { + super(type, uri); - protected SimpleUri(GraphInventoryObjectPlurals type) { - this.type = null; - this.pluralType = type; - this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); - this.values = new Object[0]; } - protected SimpleUri(GraphInventoryObjectPlurals type, Object... values) { - this.type = null; - this.pluralType = type; - this.internalURI = UriBuilder.fromPath(this.getTemplate(type)); - this.values = values; - validateValuesSize(this.getTemplate(type), values); - } + protected SimpleUri(S type, UriBuilder builder, Object... values) { + super(type, builder, values); - protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectType childType, - Object... childValues) { - this.type = childType; - this.pluralType = null; - this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri()); - this.values = childValues; - validateValuesSize(childType.partialUri(), values); } - protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectPlurals childType) { - this.type = null; - this.pluralType = childType; - this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri()); - this.values = new Object[0]; + protected SimpleUri(T parentUri, S childType, Object... childValues) { + super(parentUri, childType, childValues); } - protected void setInternalURI(UriBuilder builder) { - this.internalURI = builder; + protected SimpleUri(SimpleBaseUri<T, T, S> copy) { + super(copy); } @Override - public SimpleUri relationshipAPI() { - this.internalURI = internalURI.path(relationshipAPI); - return this; - } - - @Override - public SimpleUri relatedTo(GraphInventoryObjectPlurals plural) { - - this.internalURI = internalURI.path(relatedTo).path(plural.partialUri()); - return this; - } - - @Override - public SimpleUri relatedTo(GraphInventoryObjectType type, String... values) { - this.internalURI = - internalURI.path(relatedTo).path(UriBuilder.fromPath(type.partialUri()).build(values).toString()); - return this; - } - - @Override - public SimpleUri resourceVersion(String version) { + public T resourceVersion(String version) { this.internalURI = internalURI.replaceQueryParam("resource-version", version); - return this; - } - - @Override - public SimpleUri queryParam(String name, String... values) { - this.internalURI = internalURI.queryParam(name, values); - return this; - } - - @Override - public SimpleUri replaceQueryParam(String name, String... values) { - this.internalURI = internalURI.replaceQueryParam(name, values); - return this; - } - - @Override - public SimpleUri resultIndex(int index) { - this.internalURI = internalURI.replaceQueryParam("resultIndex", index); - return this; - } - - @Override - public SimpleUri resultSize(int size) { - this.internalURI = internalURI.replaceQueryParam("resultSize", size); - return this; - } - - @Override - public SimpleUri limit(int size) { - return this.resultIndex(0).resultSize(size); - } - - @Override - public URI build() { - return build(this.values); - } - - protected Pattern getPrefixPattern() { - return Pattern.compile("/.*?/v\\d+"); - } - - protected URI build(Object... values) { - // This is a workaround because resteasy does not encode URIs correctly - final String[] encoded = new String[values.length]; - for (int i = 0; i < values.length; i++) { - encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString()); - } - return internalURI.buildFromEncoded(encoded); - } - - @Override - public Map<String, String> getURIKeys() { - return this.getURIKeys(this.build().toString()); - } - - protected Map<String, String> getURIKeys(String uri) { - UriParser parser; - if (this.type != null) { - if (!("".equals(this.getTemplate(type)))) { - parser = new UriParserSpringImpl(this.getTemplate(type)); - } else { - return new HashMap<>(); - } - } else { - parser = new UriParserSpringImpl(this.getTemplate(pluralType)); - } - - - return parser.parse(uri); - } - - @Override - public SimpleUri clone() { - if (this.type != null) { - return new SimpleUri(this.type, this.internalURI.clone(), values); - } else { - return new SimpleUri(this.pluralType, this.internalURI.clone(), values); - } - } - - @Override - public GraphInventoryObjectType getObjectType() { - return this.type; - } - - @Override - public boolean equals(Object o) { - if (o instanceof GraphInventoryUri) { - return this.build().equals(((GraphInventoryUri) o).build()); - } - return false; + return (T) this; } @Override - public int hashCode() { - return new HashCodeBuilder().append(this.build()).toHashCode(); - } - - - @Override - public SimpleUri depth(Depth depth) { - this.internalURI.replaceQueryParam("depth", depth.toString()); - return this; - } - - @Override - public SimpleUri nodesOnly(boolean nodesOnly) { - if (nodesOnly) { - this.internalURI.replaceQueryParam("nodes-only", ""); - } - return this; - } - - @Override - public SimpleUri format(Format format) { - this.internalURI.replaceQueryParam("format", format); - return this; - } - - public void validateValuesSize(String template, Object... values) { - UriParser parser = new UriParserSpringImpl(template); - Set<String> variables = parser.getVariables(); - if (variables.size() != values.length) { - throw new IncorrectNumberOfUriKeys(String.format("Expected %s variables: %s", variables.size(), variables)); - } - } - - protected String getTemplate(GraphInventoryObjectType type) { - return type.uriTemplate(); - } - - protected String getTemplate(GraphInventoryObjectPlurals type) { - return type.uriTemplate(); + public T relationshipAPI() { + this.internalURI = internalURI.path(relationshipAPI); + return (T) this; } private void writeObject(ObjectOutputStream oos) throws IOException { @@ -283,4 +79,5 @@ public class SimpleUri implements GraphInventoryResourceUri, Serializable { String uri = ois.readUTF(); this.setInternalURI(UriBuilder.fromUri(uri)); } + } |