1e60e7967614afa13f2232b6df0b3b717357e4d9
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / api / API_Api.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.locate.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.layer.Result;
32 import org.onap.aaf.auth.locate.AAF_Locate;
33 import org.onap.aaf.auth.locate.LocateCode;
34 import org.onap.aaf.auth.locate.facade.LocateFacade;
35 import org.onap.aaf.auth.locate.mapper.Mapper.API;
36 import org.onap.aaf.auth.rserv.HttpMethods;
37 import org.onap.aaf.cadi.Symm;
38
39 /**
40  * API Apis
41  * @author Jonathan
42  *
43  */
44 public class API_Api {
45     /**
46      * Normal Init level APIs
47      * 
48      * @param gwAPI
49      * @param facade
50      * @throws Exception
51      */
52     public static void init(final AAF_Locate gwAPI, LocateFacade facade) throws Exception {
53         ////////
54         // Overall APIs
55         ///////
56         gwAPI.route(HttpMethods.GET,"/api",API.VOID,new LocateCode(facade,"Document API", true) {
57             @Override
58             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
59                 Result<Void> r = context.getAPI(trans,resp,gwAPI);
60                 switch(r.status) {
61                 case OK:
62                     resp.setStatus(HttpStatus.OK_200);
63                     break;
64                 default:
65                     context.error(trans,resp,r);
66             }
67
68             }
69         });
70
71         ////////
72         // Overall Examples
73         ///////
74         gwAPI.route(HttpMethods.GET,"/api/example/*",API.VOID,new LocateCode(facade,"Document API", true) {
75             @Override
76             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
77                 String pathInfo = req.getPathInfo();
78                 int question = pathInfo.lastIndexOf('?');
79                 
80                 pathInfo = pathInfo.substring(13, question<0?pathInfo.length():question);// IMPORTANT, this is size of "/api/example/"
81                 String nameOrContextType=Symm.base64noSplit.decode(pathInfo);
82 //                String param = req.getParameter("optional");
83                 Result<Void> r = context.getAPIExample(trans,resp,nameOrContextType,
84                         question>=0 && "optional=true".equalsIgnoreCase(req.getPathInfo().substring(question+1))
85                         );
86                 switch(r.status) {
87                     case OK:
88                         resp.setStatus(HttpStatus.OK_200);
89                         break;
90                     default:
91                         context.error(trans,resp,r);
92                 }
93             }
94         });
95
96     }
97 }