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