7f94acbabb030ec492c9853c290dfba49e20c3c6
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / RServlet.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.rserv;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import javax.servlet.Servlet;
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.LocatorException;
37 import org.onap.aaf.misc.env.APIException;
38 import org.onap.aaf.misc.env.Env;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
41
42 public abstract class RServlet<TRANS extends Trans> implements Servlet {
43     private Routes<TRANS> routes = new Routes<TRANS>();
44
45     private ServletConfig config;
46
47     @Override
48     public void init(ServletConfig config) throws ServletException {
49         this.config = config;
50     }
51
52     @Override
53     public ServletConfig getServletConfig() {
54         return config;
55     }
56
57     public void route(Env env, HttpMethods meth, String path, HttpCode<TRANS, ?> code, String ... moreTypes) {
58         Route<TRANS> r = routes.findOrCreate(meth,path);
59         r.add(code,moreTypes);
60         env.init().log(r.report(code),code);
61     }
62
63     @Override
64     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
65         HttpServletRequest request = (HttpServletRequest)req;
66         HttpServletResponse response = (HttpServletResponse)res;
67
68         @SuppressWarnings("unchecked")
69         TRANS trans = (TRANS)req.getAttribute(TransFilter.TRANS_TAG);
70         if (trans==null) {
71             response.setStatus(404); // Not Found, because it didn't go through TransFilter
72             return;
73         }
74
75         Route<TRANS> route;
76         HttpCode<TRANS,?> code=null;
77         String ct = req.getContentType();
78         TimeTaken tt = trans.start("Resolve to Code", Env.SUB);
79         try {
80             // routes have multiple code sets.  This object picks the best code set
81             // based on Accept or Content-Type
82             CodeSetter<TRANS> codesetter = new CodeSetter<TRANS>(trans,request,response);
83             // Find declared route
84             route = routes.derive(request, codesetter);
85             if (route==null) {
86                 String method = request.getMethod();
87                 trans.checkpoint("No Route matches "+ method + ' ' + request.getPathInfo());
88                 response.setStatus(404); // Not Found
89             } else {
90                 // Find best Code in Route based on "Accepts (Get) or Content-Type" (if exists)
91                 code = codesetter.code();// route.getCode(trans, request, response);
92             }
93         } finally {
94             tt.done();
95         }
96
97         if (route!=null && code!=null) {
98             StringBuilder sb = new StringBuilder(72);
99             sb.append(route.auditText);
100             sb.append(',');
101             sb.append(code.desc());
102             if (ct!=null) {
103                 sb.append(", ContentType: ");
104                 sb.append(ct);
105             }
106             tt = trans.start(sb.toString(),Env.SUB);
107             try {
108                 /*obj = */
109                 code.handle(trans, request, response);
110                 response.flushBuffer();
111             } catch (ServletException e) {
112                 trans.error().log(e);
113                 throw e;
114             } catch (Exception e) {
115                 trans.error().log(e,request.getMethod(),request.getPathInfo());
116                 throw new ServletException(e);
117             } finally {
118                 tt.done();
119             }
120         }
121     }
122
123     @Override
124     public String getServletInfo() {
125         return "RServlet for Jetty";
126     }
127
128     /**
129      * Allow Service to instantiate certain actions after service starts up
130      * @throws LocatorException
131      * @throws CadiException
132      * @throws APIException
133      */
134     public void postStartup(String hostname, int port) throws APIException {
135     }
136
137     @Override
138     public void destroy() {
139     }
140
141     public String applicationJSON(Class<?> cls, String version) {
142         StringBuilder sb = new StringBuilder();
143         sb.append("application/");
144         sb.append(cls.getSimpleName());
145         sb.append("+json");
146         sb.append(";charset=utf-8");
147         sb.append(";version=");
148         sb.append(version);
149         return sb.toString();
150     }
151
152     public String applicationXML(Class<?> cls, String version) {
153         StringBuilder sb = new StringBuilder();
154         sb.append("application/");
155         sb.append(cls.getSimpleName());
156         sb.append("+xml");
157         sb.append(";charset=utf-8");
158         sb.append(";version=");
159         sb.append(version);
160         return sb.toString();
161     }
162
163     public List<RouteReport> routeReport() {
164         return routes.routeReport();
165     }
166 }