Update AAF Version 1.0.0
[aaf/authz.git] / authz-core / src / main / java / com / att / cssa / rserv / Routes.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cssa.rserv;\r
24 \r
25 import java.io.IOException;\r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 \r
29 import javax.servlet.ServletException;\r
30 import javax.servlet.http.HttpServletRequest;\r
31 \r
32 import com.att.inno.env.Trans;\r
33 \r
34 \r
35 public class Routes<TRANS extends Trans> {\r
36         // Since this must be very, very fast, and only needs one creation, we'll use just an array.\r
37         private Route<TRANS>[] routes;\r
38         private int end;\r
39         \r
40 \r
41         @SuppressWarnings("unchecked")\r
42         public Routes() {\r
43                 routes = new Route[10];\r
44                 end = 0;\r
45         }\r
46         \r
47         // This method for setup of Routes only...\r
48         // Package on purpose\r
49         synchronized Route<TRANS> findOrCreate(HttpMethods  meth, String path) {\r
50                 Route<TRANS> rv = null;\r
51                 for(int i=0;i<end;++i) {\r
52                         if(routes[i].resolvesTo(meth,path))rv = routes[i];\r
53                 }\r
54                 \r
55                 if(rv==null) {\r
56                         if(end>=routes.length) {\r
57                                 @SuppressWarnings("unchecked")\r
58                                 Route<TRANS>[] temp = new Route[end+10];\r
59                                 System.arraycopy(routes, 0, temp, 0, routes.length);\r
60                                 routes = temp;\r
61                         }\r
62                         \r
63                         routes[end++]=rv=new Route<TRANS>(meth,path);\r
64                 }\r
65                 return rv;\r
66         }\r
67         \r
68         public Route<TRANS> derive(HttpServletRequest req, CodeSetter<TRANS> codeSetter)  throws IOException, ServletException {\r
69                 Route<TRANS> rv = null;\r
70                 String path = req.getPathInfo();\r
71                 String meth = req.getMethod();\r
72                 //TODO a TREE would be better\r
73                 for(int i=0;rv==null && i<end; ++i) {\r
74                         rv = routes[i].matches(meth,path);\r
75                         if(rv!=null && !codeSetter.matches(rv)) { // potential match, check if has Code \r
76                                 rv = null; // not quite, keep going\r
77                         }\r
78                 }\r
79                 //TODO a Default?\r
80                 return rv;\r
81         }\r
82         \r
83         public List<RouteReport> routeReport() {\r
84                 ArrayList<RouteReport> ltr = new ArrayList<RouteReport>();\r
85                 for(int i=0;i<end;++i) {\r
86                         ltr.add(routes[i].api());\r
87                 }\r
88                 return ltr;\r
89         }\r
90 }\r