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