AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-service / src / main / java / org / onap / aaf / authz / service / api / API_Api.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 org.onap.aaf.authz.service.api;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 import javax.servlet.http.HttpServletResponse;\r
27 \r
28 import org.onap.aaf.authz.env.AuthzTrans;\r
29 import org.onap.aaf.authz.facade.AuthzFacade;\r
30 import org.onap.aaf.authz.layer.Result;\r
31 import org.onap.aaf.authz.service.AuthAPI;\r
32 import org.onap.aaf.authz.service.Code;\r
33 import org.onap.aaf.authz.service.mapper.Mapper.API;\r
34 import org.onap.aaf.cssa.rserv.HttpMethods;\r
35 \r
36 import com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
37 import org.onap.aaf.cadi.Symm;\r
38 \r
39 /**\r
40  * API Apis\r
41  *\r
42  */\r
43 public class API_Api {\r
44         // Hide Public Constructor\r
45         private API_Api() {}\r
46         \r
47         /**\r
48          * Normal Init level APIs\r
49          * \r
50          * @param authzAPI\r
51          * @param facade\r
52          * @throws Exception\r
53          */\r
54         public static void init(final AuthAPI authzAPI, AuthzFacade facade) throws Exception {\r
55                 ////////\r
56                 // Overall APIs\r
57                 ///////\r
58                 authzAPI.route(HttpMethods.GET,"/api",API.API,new Code(facade,"Document API", true) {\r
59                         @Override\r
60                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {\r
61                                 Result<Void> r = context.getAPI(trans,resp,authzAPI);\r
62                                 if(r.isOK()) {\r
63                                         resp.setStatus(HttpStatus.OK_200);\r
64                                 } else {\r
65                                         context.error(trans,resp,r);\r
66                                 }\r
67                         }\r
68                 });\r
69 \r
70                 ////////\r
71                 // Overall Examples\r
72                 ///////\r
73                 authzAPI.route(HttpMethods.GET,"/api/example/*",API.VOID,new Code(facade,"Document API", true) {\r
74                         @Override\r
75                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {\r
76                                 String pathInfo = req.getPathInfo();\r
77                                 int question = pathInfo.lastIndexOf('?');\r
78                                 \r
79                                 pathInfo = pathInfo.substring(13, question<0?pathInfo.length():question);// IMPORTANT, this is size of "/api/example/"\r
80                                 String nameOrContextType=Symm.base64noSplit.decode(pathInfo);\r
81                                 Result<Void> r = context.getAPIExample(trans,resp,nameOrContextType,\r
82                                                 question>=0 && "optional=true".equalsIgnoreCase(req.getPathInfo().substring(question+1))\r
83                                                 );\r
84                                 if(r.isOK()) {\r
85                                         resp.setStatus(HttpStatus.OK_200);\r
86                                 } else {\r
87                                         context.error(trans,resp,r);\r
88                                 }\r
89                         }\r
90                 });\r
91 \r
92         }\r
93 }\r