summaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/openecomp/mso/client/aai/entities/uri/SimpleUri.java
blob: f3e6fad48b80c3b7a1c28cead5a9cf23ca5595c8 (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
/*-
 * ============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.openecomp.mso.client.aai.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.openecomp.mso.client.aai.AAIObjectPlurals;
import org.openecomp.mso.client.aai.AAIObjectType;
import org.openecomp.mso.client.aai.entities.uri.parsers.UriParser;
import org.openecomp.mso.client.aai.entities.uri.parsers.UriParserSpringImpl;
import org.springframework.web.util.UriUtils;

public class SimpleUri implements AAIResourceUri {

	protected UriBuilder internalURI;
	protected final static String relationshipAPI = "/relationship-list/relationship";
	protected final static String relatedTo = "/related-to";
	protected final Object[] values;
	protected final AAIObjectType type;
	protected final AAIObjectPlurals pluralType;
	protected SimpleUri(AAIObjectType type, Object... values) {
		this.type = type;
		this.pluralType = null;
		this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
		this.values = values;
	}
	protected SimpleUri(AAIObjectType 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(AAIObjectType type, UriBuilder builder, Object... values) {
		this.internalURI = builder;
		this.values = values;
		this.type = type;
		this.pluralType = null;
	}
	protected SimpleUri(AAIObjectPlurals type, UriBuilder builder, Object... values) {
		this.internalURI = builder;
		this.values = values;
		this.type = null;
		this.pluralType = type;
	}
	protected SimpleUri(AAIObjectPlurals type) {
		this.type = null;
		this.pluralType = type;
		this.internalURI = UriBuilder.fromPath(this.getTemplate(type));
		this.values = new Object[0];
	}
	
	@Override
	public SimpleUri relationshipAPI() {
		this.internalURI = internalURI.path(relationshipAPI);
		return this;
	}
	
	@Override
	public SimpleUri relatedTo(AAIObjectPlurals plural) {
		
		this.internalURI = internalURI.path(relatedTo).path(plural.partialUri());
		return this;
	}
	@Override
	public SimpleUri relatedTo(AAIObjectType 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.queryParam("resource-version", version);
		return this;
	}
	
	@Override
	public SimpleUri queryParam(String name, String... values) {
		this.internalURI = internalURI.queryParam(name, values);
		return this;
	}
	
	@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 AAIObjectType 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 SimpleUri depth(Depth depth) {
		this.internalURI.queryParam("depth", depth.toString());
		return this;
	}
	@Override
	public SimpleUri nodesOnly(boolean nodesOnly) {
		if (nodesOnly) {
			this.internalURI.queryParam("nodes-only", "");
		}
		return this;
	}
	
	protected String getTemplate(AAIObjectType type) {
		return type.uriTemplate();
	}
	
	protected String getTemplate(AAIObjectPlurals type) {
		return type.uriTemplate();
	}
	
}