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.locate.api;
24 import java.net.ConnectException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.locate.AAF_Locate;
32 import org.onap.aaf.auth.locate.BasicAuthCode;
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.CadiException;
38 import org.onap.aaf.cadi.client.Future;
39 import org.onap.aaf.cadi.client.Rcli;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.oauth.OAuth2Principal;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.TimeTaken;
48 * API Apis.. using Redirect for mechanism
53 public class API_Proxy {
56 * Normal Init level APIs
62 public static void init(final AAF_Locate gwAPI, LocateFacade facade) {
64 String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null);
68 // But DO NOT transfer BasicAuth case... wastes resources.
70 final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade);
72 gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) {
74 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
75 if ("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) {
76 bac.handle(trans, req, resp);
78 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
80 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
82 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
83 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
84 ft.get(10000); // Covers return codes and err messages
89 } catch (CadiException | APIException e) {
98 gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) {
100 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
101 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
103 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
105 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
106 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);
107 ft.get(10000); // Covers return codes and err messages
111 } catch (CadiException | APIException e) {
112 trans.error().log(e);
119 gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) {
121 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
122 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
124 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
126 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
127 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
128 ft.get(10000); // Covers return codes and err messages
132 } catch (CadiException | APIException e) {
133 trans.error().log(e);
140 gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) {
142 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
143 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
145 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
147 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
148 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
149 ft.get(10000); // Covers return codes and err messages
153 } catch (CadiException | APIException e) {
154 trans.error().log(e);