aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java
blob: 026f1c3929185951d82374cb85ef65ba57a99e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.client.graphinventory.entities.uri;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

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.aai.entities.uri.AAIUri;
import org.onap.so.client.graphinventory.entities.uri.Depth;
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.GraphInventoryObjectPlurals;
import org.onap.so.client.graphinventory.GraphInventoryObjectType;
import org.springframework.web.util.UriUtils;

public class SimpleUri implements GraphInventoryResourceUri {

	protected UriBuilder internalURI;
	protected final static String relationshipAPI = "/relationship-list/relationship";
	protected final static 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;
	}
	protected SimpleUri(GraphInventoryObjectType type, URI uri) {
		this.type = type;
		this.pluralType = null;
		this.internalURI = UriBuilder.fromPath(uri.getRawPath().replaceAll("/aai/v\\d+", ""));
		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(GraphInventoryObjectPlurals type, UriBuilder builder, Object... values) {
		this.internalURI = builder;
		this.values = values;
		this.type = null;
		this.pluralType = type;
	}
	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;
	}
	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;
	}
	
	@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) {
		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 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++) {
			try {
				encoded[i] = UriUtils.encode(values[i].toString(), StandardCharsets.UTF_8.toString());
			} catch (UnsupportedEncodingException e) {
				encoded[i] = values[i].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 AAIUri) {
			return this.build().equals(((AAIUri)o).build());
		}
		return false;
	}
	
	@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;
	}
	
	protected String getTemplate(GraphInventoryObjectType type) {
		return type.uriTemplate();
	}
	
	protected String getTemplate(GraphInventoryObjectPlurals type) {
		return type.uriTemplate();
	}
	
}