[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-core / src / main / java / com / att / cssa / rserv / Route.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.List;\r
27 \r
28 import javax.servlet.ServletException;\r
29 import javax.servlet.http.HttpServletRequest;\r
30 import javax.servlet.http.HttpServletResponse;\r
31 \r
32 import com.att.inno.env.Env;\r
33 import com.att.inno.env.TimeTaken;\r
34 import com.att.inno.env.Trans;\r
35 \r
36 public class Route<TRANS extends Trans> {\r
37         public final String auditText;\r
38         public final HttpMethods meth;\r
39         public final String path;\r
40         \r
41         private Match match;\r
42         // package on purpose\r
43         private final TypedCode<TRANS> content;\r
44         private final boolean isGet;\r
45         \r
46         public Route(HttpMethods meth, String path) {\r
47                 this.path = path;\r
48                 auditText = meth.name() + ' ' + path;\r
49                 this.meth = meth; // Note: Using Spark def for now.\r
50                 isGet = meth.compareTo(HttpMethods.GET) == 0;\r
51                 match = new Match(path);\r
52                 content = new TypedCode<TRANS>();\r
53         }\r
54         \r
55         public void add(HttpCode<TRANS,?> code, String ... others) {\r
56                 code.match = match;\r
57                 content.add(code, others);\r
58         }\r
59         \r
60 //      public void add(HttpCode<TRANS,?> code, Class<?> cls, String version, String ... others) {\r
61 //              code.match = match;\r
62 //              content.add(code, cls, version, others);\r
63 //      }\r
64 //\r
65         public HttpCode<TRANS,?> getCode(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {\r
66                 // Type is associated with Accept for GET (since it is what is being returned\r
67                 // We associate the rest with ContentType.\r
68                 // FYI, thought about this a long time before implementing this way.\r
69                 String compare;\r
70 //              String special[]; // todo, expose Charset (in special) to outside\r
71                 if(isGet) {\r
72                         compare = req.getHeader("Accept"); // Accept is used for read, as we want to agree on what caller is ready to handle\r
73                 } else {\r
74                         compare = req.getContentType(); // Content type used to declare what data is being created, updated or deleted (might be used for key)\r
75                 }\r
76 \r
77                 Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> hl = content.prep(trans, compare);\r
78                 if(hl==null) {\r
79                         resp.setStatus(406); // NOT_ACCEPTABLE\r
80                 } else {\r
81                         if(isGet) { // Set Content Type to expected content\r
82                                 if("*".equals(hl.x) || "*/*".equals(hl.x)) {// if wild-card, then choose first kind of type\r
83                                         resp.setContentType(content.first());\r
84                                 } else {\r
85                                         resp.setContentType(hl.x);\r
86                                 }\r
87                         }\r
88                         return hl.y.x;\r
89                 }\r
90                 return null;\r
91         }\r
92         \r
93         public Route<TRANS> matches(String method, String path) {\r
94                 return meth.name().equalsIgnoreCase(method) && match.match(path)?this:null;\r
95         }\r
96         \r
97         public TimeTaken start(Trans trans, String auditText, HttpCode<TRANS,?> code, String type) {\r
98                 StringBuilder sb = new StringBuilder(auditText);\r
99                 sb.append(", ");\r
100                 sb.append(code.desc());\r
101                 sb.append(", Content: ");\r
102                 sb.append(type);\r
103                 return trans.start(sb.toString(), Env.SUB);\r
104         }\r
105 \r
106         // Package on purpose.. for "find/Create" routes only\r
107         boolean resolvesTo(HttpMethods hm, String p) {\r
108                 return(path.equals(p) && hm.equals(meth));\r
109         }\r
110         \r
111         public String toString() {\r
112                 return auditText + ' ' + content; \r
113         }\r
114 \r
115         public String report(HttpCode<TRANS, ?> code) {\r
116                 StringBuilder sb = new StringBuilder();\r
117                 sb.append(auditText);\r
118                 sb.append(' ');\r
119                 content.relatedTo(code, sb);\r
120                 return sb.toString();\r
121         }\r
122 \r
123         public RouteReport api() {\r
124                 RouteReport tr = new RouteReport();\r
125                 tr.meth = meth;\r
126                 tr.path = path;\r
127                 content.api(tr);\r
128                 return tr;\r
129         }\r
130 \r
131 \r
132         /**\r
133          * contentRelatedTo (For reporting) list routes that will end up at a specific Code\r
134          * @return\r
135          */\r
136         public String contentRelatedTo(HttpCode<TRANS, ?> code) {\r
137                 StringBuilder sb = new StringBuilder(path);\r
138                 sb.append(' ');\r
139                 content.relatedTo(code, sb);\r
140                 return sb.toString();\r
141         }\r
142 }\r