2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.aai.workarounds;
23 import java.net.MalformedURLException;
25 import java.net.URISyntaxException;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 public class LegacyURITransformer {
31 private final Pattern legacyUriPattern = Pattern.compile("/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)");
32 private final Pattern currentUriPattern = Pattern.compile("/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/vserver/(?<vserverKey>[^/]*?$)");
34 * Instantiates a new legacy URL transformer.
36 private LegacyURITransformer() {
40 private static class Helper {
41 private static final LegacyURITransformer INSTANCE = new LegacyURITransformer();
45 * Gets the single instance of LegacyURLTransformer.
47 * @return single instance of LegacyURLTransformer
49 public static LegacyURITransformer getInstance() {
50 return Helper.INSTANCE;
54 * Gets the legacy URL.
57 * @return the legacy URL
58 * @throws URISyntaxException
59 * @throws MalformedURLException the malformed URL exception
61 public URI getLegacyURI(URI url) throws URISyntaxException {
62 String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}";
63 String result = url.toString();
64 Matcher m = currentUriPattern.matcher(result);
66 result = m.replaceFirst(replacement);
70 return new URI(result);
75 * Gets the current URL.
78 * @return the current URL
79 * @throws URISyntaxException
80 * @throws MalformedURLException the malformed URL exception
82 public URI getCurrentURI(URI url) throws URISyntaxException {
83 String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}";
84 String result = url.toString();
85 Matcher m = legacyUriPattern.matcher(result);
87 result = m.replaceFirst(replacement);
91 return new URI(result);