AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / HttpCode.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.rserv;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.onap.aaf.misc.env.Trans;
28
29 /**
30  * HTTP Code element, which responds to the essential "handle Method".
31  * 
32  * Use Native HttpServletRe[quest|sponse] calls for questions like QueryParameters (getParameter, etc)
33  * 
34  * Use local "pathParam" method to obtain in an optimized manner the path parameter, which must be interpreted by originating string
35  * 
36  * i.e. my/path/:id/:other/*
37  * 
38  * @author Jonathan
39  *
40  * @param <TRANS>
41  * @param <T>
42  */
43 public abstract class HttpCode<TRANS extends Trans, CONTEXT> {
44         protected CONTEXT context;
45         private String desc;
46         protected String [] roles;
47         private boolean all;
48         
49         // Package by design... Set by Route when linked
50         Match match;
51         
52         public HttpCode(CONTEXT context, String description, String ... roles) {
53                 this.context = context;
54                 desc = description;
55                 
56                 // Evaluate for "*" once...
57                 all = false;
58                 for(String srole : roles) {
59                         if("*".equals(srole)) {
60                                 all = true;
61                                 break;
62                         }
63                 }
64                 this.roles = all?null:roles;
65         }
66         
67         public abstract void handle(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws Exception;
68         
69         public String desc() {
70                 return desc;
71         }
72         
73         /**
74          * Get the variable element out of the Path Parameter, as set by initial Code
75          * 
76          * @param req
77          * @param key
78          * @return
79          */
80         public String pathParam(HttpServletRequest req, String key) {
81                 String rv = match.param(req.getPathInfo(), key);
82                 if(rv!=null) {
83                         rv = rv.trim();
84                         if(rv.endsWith("/")) {
85                                 rv = rv.substring(0, rv.length()-1);
86                         }
87                 }
88                 return rv;
89         }
90
91         // Note: get Query Params from Request
92         
93         /**
94          * Check for Authorization when set.
95          * 
96          * If no Roles set, then accepts all users
97          * 
98          * @param req
99          * @return
100          */
101         public boolean isAuthorized(HttpServletRequest req) {
102                 if(all)return true;
103                 if(roles!=null) {
104                         for(String srole : roles) {
105                                 if(req.isUserInRole(srole)) return true;
106                         }
107                 }
108                 return false;
109         }
110         
111         public boolean no_cache() {
112                 return false;
113         }
114         
115         public String toString() {
116                 return desc;
117         }
118 }