2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.rserv;
24 import java.io.IOException;
25 import java.util.List;
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;
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;
42 public abstract class RServlet<TRANS extends Trans> implements Servlet {
43 private Routes<TRANS> routes = new Routes<TRANS>();
45 private ServletConfig config;
48 public void init(ServletConfig config) throws ServletException {
53 public ServletConfig getServletConfig() {
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);
64 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
65 HttpServletRequest request = (HttpServletRequest)req;
66 HttpServletResponse response = (HttpServletResponse)res;
68 @SuppressWarnings("unchecked")
69 TRANS trans = (TRANS)req.getAttribute(TransFilter.TRANS_TAG);
71 response.setStatus(404); // Not Found, because it didn't go through TransFilter
76 HttpCode<TRANS,?> code=null;
77 String ct = req.getContentType();
78 TimeTaken tt = trans.start("Resolve to Code", Env.SUB);
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);
86 String method = request.getMethod();
87 trans.checkpoint("No Route matches "+ method + ' ' + request.getPathInfo());
88 response.setStatus(404); // Not Found
90 // Find best Code in Route based on "Accepts (Get) or Content-Type" (if exists)
91 code = codesetter.code();// route.getCode(trans, request, response);
97 if (route!=null && code!=null) {
98 StringBuilder sb = new StringBuilder(72);
99 sb.append(route.auditText);
101 sb.append(code.desc());
103 sb.append(", ContentType: ");
106 tt = trans.start(sb.toString(),Env.SUB);
109 code.handle(trans, request, response);
110 response.flushBuffer();
111 } catch (ServletException e) {
112 trans.error().log(e);
114 } catch (Exception e) {
115 trans.error().log(e,request.getMethod(),request.getPathInfo());
116 throw new ServletException(e);
124 public String getServletInfo() {
125 return "RServlet for Jetty";
129 * Allow Service to instantiate certain actions after service starts up
130 * @throws LocatorException
131 * @throws CadiException
132 * @throws APIException
134 public void postStartup(String hostname, int port) throws APIException {
138 public void destroy() {
141 public String applicationJSON(Class<?> cls, String version) {
142 StringBuilder sb = new StringBuilder();
143 sb.append("application/");
144 sb.append(cls.getSimpleName());
146 sb.append(";charset=utf-8");
147 sb.append(";version=");
149 return sb.toString();
152 public String applicationXML(Class<?> cls, String version) {
153 StringBuilder sb = new StringBuilder();
154 sb.append("application/");
155 sb.append(cls.getSimpleName());
157 sb.append(";charset=utf-8");
158 sb.append(";version=");
160 return sb.toString();
163 public List<RouteReport> routeReport() {
164 return routes.routeReport();