Replaced all tabs with spaces in java and pom.xml
[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 import javax.ws.rs.container.ContainerRequestContext;
26 import javax.ws.rs.container.ContainerRequestFilter;
27 import javax.ws.rs.container.PreMatching;
28 import javax.ws.rs.core.UriInfo;
29 import org.onap.so.apihandlerinfra.Constants;
30
31
32 @PreMatching
33 public class RequestUriFilter implements ContainerRequestFilter {
34
35     private String requestURI;
36
37     @Override
38     public void filter(ContainerRequestContext context) throws IOException {
39         UriInfo uriInfo = context.getUriInfo();
40         URI baseURI = uriInfo.getBaseUri();
41         requestURI = uriInfo.getPath();
42
43         if (requestURI.contains("onap/so/infra/serviceInstances")) {
44             requestURI = requestURI.replaceFirst("serviceInstances", "serviceInstantiation");
45             if (!requestURI.contains(Constants.SERVICE_INSTANCE_PATH)) {
46                 // Adds /serviceInstances after the version provided in the URI
47                 requestURI = new StringBuilder(requestURI)
48                         .insert(requestURI.indexOf(Constants.SERVICE_INSTANTIATION_PATH) + 24,
49                                 Constants.SERVICE_INSTANCE_PATH)
50                         .toString();
51             }
52             requestURI = baseURI + requestURI;
53             URI uri = URI.create(requestURI);
54             context.setRequestUri(uri);
55         }
56     }
57
58     public String getRequestUri() {
59         return requestURI;
60     }
61 }