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.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 public class LegacyURLTransformer {
31 * Instantiates a new legacy URL transformer.
33 private LegacyURLTransformer() {
37 private static class Helper {
39 private static final LegacyURLTransformer INSTANCE = new LegacyURLTransformer();
43 * Gets the single instance of LegacyURLTransformer.
45 * @return single instance of LegacyURLTransformer
47 public static LegacyURLTransformer getInstance() {
48 return Helper.INSTANCE;
52 * Gets the legacy URL.
55 * @return the legacy URL
56 * @throws MalformedURLException the malformed URL exception
58 public URL getLegacyURL(URL url) throws MalformedURLException {
59 String substring = "/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/"
60 + "vserver/(?<vserverKey>[^/]*?$)";
61 String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}";
62 Pattern p = Pattern.compile(substring);
63 String result = url.toString();
64 Matcher m = p.matcher(result);
66 result = m.replaceFirst(replacement);
68 return new URL(result);
72 * Gets the current URL.
75 * @return the current URL
76 * @throws MalformedURLException the malformed URL exception
78 public URL getCurrentURL(URL url) throws MalformedURLException {
79 String substring = "/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)";
80 String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}";
81 Pattern p = Pattern.compile(substring);
82 String result = url.toString();
83 Matcher m = p.matcher(result);
85 result = m.replaceFirst(replacement);
88 return new URL(result);