Sonar Fixes: Variable name changes
[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 = req.getParameter(key);
82         if (rv==null) {
83             rv = match.param(req.getPathInfo(), key);
84             if (rv!=null) {
85                 rv = rv.trim();
86                 if (rv.endsWith("/")) {
87                     rv = rv.substring(0, rv.length()-1);
88                 }
89             }
90         }
91         return rv;
92     }
93
94     // Note: get Query Params from Request
95
96     /**
97      * Check for Authorization when set.
98      *
99      * If no Roles set, then accepts all users
100      *
101      * @param req
102      * @return
103      */
104     public boolean isAuthorized(HttpServletRequest req) {
105         if (all)return true;
106         if (roles!=null) {
107             for (String srole : roles) {
108                 if (req.isUserInRole(srole)) return true;
109             }
110         }
111         return false;
112     }
113
114     public boolean noCache() {
115         return false;
116     }
117
118     public String toString() {
119         return desc;
120     }
121 }