91ab580b07f58a14cff9a5bd2bf84e3a7eeb6228
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandler / filters / RequestUriFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.apihandler.filters;
22
23 import java.io.IOException;
24 import java.net.URI;
25
26 import javax.ws.rs.container.ContainerRequestContext;
27 import javax.ws.rs.container.ContainerRequestFilter;
28 import javax.ws.rs.container.PreMatching;
29 import javax.ws.rs.core.UriInfo;
30
31 import org.onap.so.apihandlerinfra.Constants;
32
33
34 @PreMatching
35 public class RequestUriFilter implements ContainerRequestFilter {
36
37         private String requestURI;
38         @Override
39         public void filter(ContainerRequestContext context) throws IOException {
40                 UriInfo uriInfo = context.getUriInfo();
41                 URI baseURI = uriInfo.getBaseUri();
42                 requestURI = uriInfo.getPath();
43
44                 if(requestURI.contains("onap/so/infra/serviceInstances")){
45                         requestURI = requestURI.replaceFirst("serviceInstances", "serviceInstantiation");
46                 if(!requestURI.contains(Constants.SERVICE_INSTANCE_PATH)){
47                         //Adds /serviceInstances after the version provided in the URI
48                         requestURI = new StringBuilder(requestURI).insert(requestURI.indexOf(Constants.SERVICE_INSTANTIATION_PATH) + 24, Constants.SERVICE_INSTANCE_PATH).toString();
49                 }
50                 requestURI = baseURI + requestURI;
51                 URI uri = URI.create(requestURI);
52                 context.setRequestUri(uri);
53                 }
54         }
55         public String getRequestUri(){
56                 return requestURI;
57         }
58 }