AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / Routes.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
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
22 package org.onap.aaf.auth.rserv;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30
31 import org.onap.aaf.misc.env.Trans;
32
33
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;
37         private int end;
38         
39
40         @SuppressWarnings("unchecked")
41         public Routes() {
42                 routes = new Route[10];
43                 end = 0;
44         }
45         
46         // This method for setup of Routes only...
47         // Package on purpose
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];
52                 }
53                 
54                 if(rv==null) {
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);
59                                 routes = temp;
60                         }
61                         
62                         routes[end++]=rv=new Route<TRANS>(meth,path);
63                 }
64                 return rv;
65         }
66         
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
76                         }
77                 }
78                 //TODO a Default?
79                 return rv;
80         }
81         
82         public List<RouteReport> routeReport() {
83                 ArrayList<RouteReport> ltr = new ArrayList<RouteReport>();
84                 for(int i=0;i<end;++i) {
85                         ltr.add(routes[i].api());
86                 }
87                 return ltr;
88         }
89 }