aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/interceptors/pre/RetiredInterceptor.java
blob: 9a33b05370ec59b17c3a20d6b525b3616793b5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.interceptors.pre;

import org.onap.aai.exceptions.AAIException;
import org.onap.aai.interceptors.AAIContainerFilter;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.service.RetiredService;
import org.onap.aai.util.AAIConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Can cache this so if the uri was already cached then it won't run the string
// matching each time but only does it for the first time

@Provider
@PreMatching
@Priority(AAIRequestFilterPriority.RETIRED_SERVICE)
public class RetiredInterceptor extends AAIContainerFilter implements ContainerRequestFilter {

    private static final Pattern VERSION_PATTERN = Pattern.compile("v\\d+|latest");

    private RetiredService retiredService;

    private String basePath;

    @Autowired
    public RetiredInterceptor(RetiredService retiredService, @Value("${schema.uri.base.path}") String basePath){
        this.retiredService = retiredService;
        this.basePath = basePath;
        if(!basePath.endsWith("/")){
            this.basePath = basePath + "/";
        }
    }
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {

        String requestURI = containerRequestContext.getUriInfo().getAbsolutePath().getPath();

        String version = extractVersionFromPath(requestURI);

        List<Pattern> retiredAllVersionList = retiredService.getRetiredAllVersionList();


        if(checkIfUriRetired(containerRequestContext, retiredAllVersionList, version, requestURI, "")){
            return;
        }

        List<Pattern> retiredVersionList = retiredService.getRetiredPatterns();

        checkIfUriRetired(containerRequestContext, retiredVersionList, version, requestURI);
    }

    public boolean checkIfUriRetired(ContainerRequestContext containerRequestContext,
                                     List<Pattern> retiredPatterns,
                                     String version,
                                     String requestURI,
                                     String message){


        for(Pattern retiredPattern : retiredPatterns){
            if(retiredPattern.matcher(requestURI).matches()){
                AAIException e;

                if(message == null){
                    e = new AAIException("AAI_3007");
                } else {
                    e = new AAIException("AAI_3015");
                }

                ArrayList<String> templateVars = new ArrayList<>();

                if (templateVars.isEmpty()) {
                    templateVars.add("PUT");
                    if(requestURI != null){
                        requestURI = requestURI.replaceAll(basePath, "");
                    }
                    templateVars.add(requestURI);
                    if(message == null){
                        templateVars.add(version);
                        templateVars.add(AAIConfig.get("aai.default.api.version", ""));
                    }
                }

                Response response = Response
                        .status(e.getErrorObject().getHTTPResponseCode())
                        .entity(
                                ErrorLogHelper
                                        .getRESTAPIErrorResponse(
                                                containerRequestContext.getAcceptableMediaTypes(), e, templateVars
                                        )
                        )
                        .build();

                containerRequestContext.abortWith(response);

                return true;
            }
        }

        return false;
    }

    public boolean checkIfUriRetired(ContainerRequestContext containerRequestContext,
                                     List<Pattern> retiredPatterns,
                                     String version,
                                     String requestURI){
        return checkIfUriRetired(containerRequestContext, retiredPatterns, version, requestURI, null);
    }

    protected String extractVersionFromPath(String requestURI) {
        Matcher versionMatcher = VERSION_PATTERN.matcher(requestURI);
        String version = null;

        if(versionMatcher.find()){
            version = versionMatcher.group(0);
        }
        return version;
    }

}