Resolved below sonar issues:
[aai/aai-service.git] / ajsc-aai / src / main / java / org / openecomp / aai / workarounds / LegacyURLTransformer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.workarounds;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 public class LegacyURLTransformer {
29
30     /**
31      * Instantiates a new legacy URL transformer.
32      */
33     private LegacyURLTransformer() {
34
35     }
36
37     private static class Helper {
38
39         private static final LegacyURLTransformer INSTANCE = new LegacyURLTransformer();
40     }
41
42     /**
43      * Gets the single instance of LegacyURLTransformer.
44      *
45      * @return single instance of LegacyURLTransformer
46      */
47     public static LegacyURLTransformer getInstance() {
48         return Helper.INSTANCE;
49     }
50
51     /**
52      * Gets the legacy URL.
53      *
54      * @param url the url
55      * @return the legacy URL
56      * @throws MalformedURLException the malformed URL exception
57      */
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);
65         if (m.find()) {
66             result = m.replaceFirst(replacement);
67         }
68         return new URL(result);
69     }
70
71     /**
72      * Gets the current URL.
73      *
74      * @param url the url
75      * @return the current URL
76      * @throws MalformedURLException the malformed URL exception
77      */
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);
84         if (m.find()) {
85             result = m.replaceFirst(replacement);
86         }
87
88         return new URL(result);
89     }
90 }