2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.rserv;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
31 import org.onap.aaf.misc.env.Trans;
34 public class Routes<TRANS extends Trans> {
35 // Since this must be very, very fast, and only needs one creation, we'll use just an array.
36 private Route<TRANS>[] routes;
40 @SuppressWarnings("unchecked")
42 routes = new Route[10];
46 // This method for setup of Routes only...
48 synchronized Route<TRANS> findOrCreate(HttpMethods meth, String path) {
49 Route<TRANS> rv = null;
50 for (int i=0;i<end;++i) {
51 if (routes[i].resolvesTo(meth,path))rv = routes[i];
55 if (end>=routes.length) {
56 @SuppressWarnings("unchecked")
57 Route<TRANS>[] temp = new Route[end+10];
58 System.arraycopy(routes, 0, temp, 0, routes.length);
62 routes[end++]=rv=new Route<TRANS>(meth,path);
67 public Route<TRANS> derive(HttpServletRequest req, CodeSetter<TRANS> codeSetter) throws IOException, ServletException {
68 Route<TRANS> rv = null;
69 String path = req.getPathInfo();
70 String meth = req.getMethod();
71 //TODO a TREE would be better
72 for (int i=0;rv==null && i<end; ++i) {
73 rv = routes[i].matches(meth,path);
74 if (rv!=null && !codeSetter.matches(rv)) { // potential match, check if has Code
75 rv = null; // not quite, keep going
82 public List<RouteReport> routeReport() {
83 ArrayList<RouteReport> ltr = new ArrayList<>();
84 for (int i=0;i<end;++i) {
85 ltr.add(routes[i].api());