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