Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / api / API_Find.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.common.Define;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.layer.Result;
33 import org.onap.aaf.auth.locate.AAF_Locate;
34 import org.onap.aaf.auth.locate.LocateCode;
35 import org.onap.aaf.auth.locate.facade.LocateFacade;
36 import org.onap.aaf.auth.locate.mapper.Mapper.API;
37 import org.onap.aaf.auth.rserv.HttpMethods;
38 import org.onap.aaf.misc.env.util.Split;
39
40 /**
41  * API Apis.. using Redirect for mechanism
42  *
43  * @author Jonathan
44  *
45  */
46 public class API_Find {
47     /**
48      * Normal Init level APIs
49      *
50      * @param gwAPI
51      * @param facade
52      * @throws Exception
53      */
54     public static void init(final AAF_Locate gwAPI, LocateFacade facade) throws Exception {
55         ////////
56         // Overall APIs
57         ///////
58
59         final LocateCode locationInfo = new LocateCode(facade,"Location Information", true) {
60             @Override
61             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
62                 String service = pathParam(req, ":service");
63                 String version = pathParam(req, ":version");
64                 String other = pathParam(req, ":other");
65                 if (service.indexOf(':')>=0) {
66                     String split[] = Split.split(':', service);
67                     switch(split.length) {
68                         case 3:
69                             other=split[2];
70                         case 2:
71                             version = split[1];
72                             service = split[0];
73                     }
74                 }
75                 service=Define.varReplace(service);
76                 Result<Void> r = context.getEndpoints(trans,resp,
77                     req.getPathInfo(), // use as Key
78                     service,version,other
79                 );
80                 switch(r.status) {
81                     case OK:
82                         resp.setStatus(HttpStatus.OK_200);
83                         break;
84                     default:
85                         context.error(trans,resp,r);
86                 }
87             }
88         };
89
90         gwAPI.route(HttpMethods.GET,"/locate/:service/:version",API.ENDPOINTS,locationInfo);
91         gwAPI.route(HttpMethods.GET,"/locate/:service/:version/:other",API.ENDPOINTS,locationInfo);
92         gwAPI.route(HttpMethods.GET,"/locate/:service",API.ENDPOINTS,locationInfo);
93
94
95         gwAPI.route(HttpMethods.GET,"/download/agent", API.VOID, new LocateCode(facade,"Redirect to latest Agent",false) {
96             @Override
97             public void handle(AuthzTrans arg0, HttpServletRequest arg1, HttpServletResponse arg2) throws Exception {
98             }
99         });
100
101         gwAPI.route(HttpMethods.PUT,"/registration",API.MGMT_ENDPOINTS,new LocateCode(facade,"Put Location Information", true) {
102             @Override
103             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
104                 Result<Void> r = context.putMgmtEndpoints(trans,req,resp);
105                 switch(r.status) {
106                     case OK:
107                         resp.setStatus(HttpStatus.OK_200);
108                         break;
109                     default:
110                         context.error(trans,resp,r);
111                 }
112
113             }
114         });
115
116         gwAPI.route(HttpMethods.DELETE,"/registration",API.MGMT_ENDPOINTS,new LocateCode(facade,"Remove Location Information", true) {
117             @Override
118             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
119                 Result<Void> r = context.removeMgmtEndpoints(trans,req,resp);
120                 switch(r.status) {
121                     case OK:
122                         resp.setStatus(HttpStatus.OK_200);
123                         break;
124                     default:
125                         context.error(trans,resp,r);
126                 }
127
128             }
129         });
130
131     }
132 }