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.hello;
24 import java.io.BufferedReader;
25 import java.io.InputStreamReader;
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.hello.AAF_Hello.API;
33 import org.onap.aaf.auth.rserv.HttpCode;
34 import org.onap.aaf.auth.rserv.HttpMethods;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
43 public class API_Hello {
46 private static final String APPLICATION_JSON = "application/json";
47 protected static final byte[] NOT_JSON = "Data does not look like JSON".getBytes();
49 // Hide Public Constructor
50 private API_Hello() {}
53 * Normal Init level APIs
59 public static void init(final AAF_Hello oauthHello) throws Exception {
64 oauthHello.route(HttpMethods.GET,"/hello/:perm*",API.TOKEN,new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"Hello OAuth"){
66 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
67 resp.setStatus(200 /* OK */);
68 ServletOutputStream os = resp.getOutputStream();
69 os.print("Hello AAF ");
70 String perm = pathParam(req, "perm");
71 if (perm!=null && perm.length()>0) {
73 os.print(req.getUserPrincipal().getName());
74 TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
76 if (req.isUserInRole(perm)) {
79 os.print(" does not have ");
84 os.print("Permission: ");
90 trans.info().printf("Said 'Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
101 oauthHello.route(oauthHello.env,HttpMethods.POST,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Create") {
103 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
104 BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
105 StringBuilder sb = new StringBuilder();
107 sb.append(br.readLine());
109 String content = sb.toString();
110 trans.info().printf("Content from %s: %s\n", pathParam(req, ":id"),content);
111 if (content.startsWith("{") && content.endsWith("}")) {
112 resp.setStatus(201 /* OK */);
114 resp.getOutputStream().write(NOT_JSON);
124 oauthHello.route(oauthHello.env,HttpMethods.GET,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Read") {
126 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
127 resp.setStatus(200 /* OK */);
128 StringBuilder sb = new StringBuilder("{\"resp\": \"Hello REST AAF\",\"principal\": \"");
129 sb.append(req.getUserPrincipal().getName());
131 String perm = pathParam(req, "perm");
132 trans.info().printf("Read request from %s: %s\n", pathParam(req, ":id"),perm);
133 if (perm!=null && perm.length()>0) {
134 TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
136 sb.append(",\"validation\": { \"permission\" : \"");
138 sb.append("\",\"has\" : \"");
139 sb.append(req.isUserInRole(perm));
146 ServletOutputStream os = resp.getOutputStream();
147 os.println(sb.toString());
148 trans.info().printf("Said 'RESTful Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
155 oauthHello.route(oauthHello.env,HttpMethods.PUT,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Update") {
157 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
158 BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
159 StringBuilder sb = new StringBuilder();
161 sb.append(br.readLine());
163 String content = sb.toString();
164 trans.info().printf("Content from %s: %s\n", pathParam(req, ":id"),content);
165 if (content.startsWith("{") && content.endsWith("}")) {
166 resp.setStatus(200 /* OK */);
167 resp.getOutputStream().print(content);
169 resp.getOutputStream().write(NOT_JSON);
179 oauthHello.route(oauthHello.env,HttpMethods.DELETE,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Delete") {
181 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
182 trans.info().printf("Delete requested on %s\n", pathParam(req, ":id"));
183 resp.setStatus(200 /* OK */);