1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cssa.rserv;
\r
25 import java.io.IOException;
\r
26 import java.util.ArrayList;
\r
27 import java.util.List;
\r
29 import javax.servlet.ServletException;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
32 import com.att.inno.env.Trans;
\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
41 @SuppressWarnings("unchecked")
\r
43 routes = new Route[10];
\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
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
63 routes[end++]=rv=new Route<TRANS>(meth,path);
\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
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