2775af0717701dbb343cb66cdee38fd586bc2607
[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.URI;
25 import java.net.URL;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import com.att.aft.dme2.internal.javaxwsrs.core.UriBuilder;
30
31 public class LegacyURLTransformer {
32
33         /**
34          * Instantiates a new legacy URL transformer.
35          */
36         private LegacyURLTransformer() {
37                 
38         }
39         
40         private static class Helper {
41                 private static final LegacyURLTransformer INSTANCE = new LegacyURLTransformer();
42         }
43         
44         /**
45          * Gets the single instance of LegacyURLTransformer.
46          *
47          * @return single instance of LegacyURLTransformer
48          */
49         public static LegacyURLTransformer getInstance() {
50                 return Helper.INSTANCE;
51         }
52         
53         /**
54          * Gets the legacy URL.
55          *
56          * @param url the url
57          * @return the legacy URL
58          * @throws MalformedURLException the malformed URL exception
59          */
60         public URL getLegacyURL(URL url) throws MalformedURLException {
61                 String substring = "/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/vserver/(?<vserverKey>[^/]*?$)";
62                 String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}";
63                 Pattern p = Pattern.compile(substring);
64                 String result = url.toString();
65                 Matcher m = p.matcher(result);
66                 if (m.find()) {
67                         result = m.replaceFirst(replacement);
68
69                 }
70                 URL resultUrl = new URL(result);
71                 return resultUrl;
72                 
73         }
74         
75         /**
76          * Gets the current URL.
77          *
78          * @param url the url
79          * @return the current URL
80          * @throws MalformedURLException the malformed URL exception
81          */
82         public URL getCurrentURL(URL url) throws MalformedURLException {
83                 String substring = "/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)";
84                 String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}";
85                 Pattern p = Pattern.compile(substring);
86                 String result = url.toString();
87                 Matcher m = p.matcher(result);
88                 if (m.find()) {
89                         result = m.replaceFirst(replacement);
90
91                 }
92                 
93                 URL resultUrl = new URL(result);
94                 return resultUrl;
95         }
96 }