Sonar Fixes: Variable name changes
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / Acceptor.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 java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.onap.aaf.misc.env.Trans;
29
30 /**
31  * Find Acceptable Paths and place them where TypeCode can evaluate.
32  *
33  * If there are more than one, TypeCode will choose based on "q" value
34  * @author Jonathan
35  *
36  * @param <TRANS>
37  */
38 class Acceptor<TRANS extends Trans>  {
39     private List<Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>>> types;
40     List<Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>>> acceptable;
41
42     public Acceptor(List<Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>>> types) {
43         this.types = types;
44         acceptable = new ArrayList<>();
45     }
46
47     private boolean eval(HttpCode<TRANS,?> code, String str, List<String> props) {
48
49         boolean ok = false;
50         boolean any = false;
51         for (Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type : types) {
52             ok = true;
53             if (type.x.equals(str)) {
54                 for (Iterator<String> iter = props.iterator();ok && iter.hasNext();) {
55                     ok = props(type,iter.next(),iter.next());
56                 }
57                 if (ok) {
58                     any = true;
59                     acceptable.add(type);
60                 }
61             }
62         }
63
64         return any;
65     }
66
67     /**
68      * Evaluate Properties
69      * @param type
70      * @param tag
71      * @param value
72      * @return
73      */
74     private boolean props(Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type, String tag, String value) {
75         boolean rv = false;
76         if (type.y!=null) {
77             for (Pair<String,Object> prop : type.y.y){
78                 if (tag.equals(prop.x)) {
79                     if ( "charset".equals(tag)) {
80                         return prop.x==null?false:prop.y.equals(value.toLowerCase()); // return True if Matched
81                     } else if ("version".equals(tag)) {
82                         return prop.y.equals(new Version(value)); // Note: Version Class knows Minor Version encoding
83                     } else if (tag.equals(Content.Q)) { // replace Q value
84                         try {
85                             type.y.y.get(0).y=Float.parseFloat(value);
86                         } catch (NumberFormatException e) {
87                             rv=false; // need to do something to make Sonar happy. But nothing to do.
88                         }
89                         return true;
90                     } else {
91                         return value.equals(prop.y);
92                     }
93                 }
94             }
95         }
96         return rv;
97     }
98
99     /**
100      * parse
101      *
102      * Note: I'm processing by index to avoid lots of memory creation, which speeds things
103      * up for this time critical section of code.
104      * @param code
105      * @param cntnt
106      * @return
107      */
108     protected boolean parse(HttpCode<TRANS, ?> code, String cntnt) {
109         byte[] bytes = cntnt.getBytes();
110
111         int cis;
112         int cie=-1;
113         int cend;
114         int sis;
115         int sie;
116         int send;
117         String name;
118         ArrayList<String> props = new ArrayList<>();
119         do {
120             // Clear these in case more than one Semi
121             props.clear(); // on loop, do not want mixed properties
122             name=null;
123
124             cis = cie+1; // find comma start
125             while (cis<bytes.length && Character.isSpaceChar(bytes[cis]))++cis;
126             cie = cntnt.indexOf(',',cis); // find comma end
127             cend = cie<0?bytes.length:cie; // If no comma, set comma end to full length, else cie
128             while (cend>cis && Character.isSpaceChar(bytes[cend-1]))--cend;
129             // Start SEMIS
130             sie=cis-1;
131             do {
132                 sis = sie+1;  // semi start is one after previous end
133                 while (sis<bytes.length && Character.isSpaceChar(bytes[sis]))++sis;
134                 sie = cntnt.indexOf(';',sis);
135                 send = sie>cend || sie<0?cend:sie;  // if the Semicolon is after the comma, or non-existent, use comma end, else keep
136                 while (send>sis && Character.isSpaceChar(bytes[send-1]))--send;
137                 if (name==null) { // first entry in Comma set is the name, not a property
138                     name = new String(bytes,sis,send-sis);
139                 } else { // We've looped past the first Semi, now process as properties
140                     // If there are additional elements (more entities within Semi Colons)
141                     // apply Properties
142                     int eq = cntnt.indexOf('=',sis);
143                     if (eq>sis && eq<send) {
144                         props.add(new String(bytes,sis,eq-sis));
145                         props.add(new String(bytes,eq+1,send-(eq+1)));
146                     }
147                 }
148                 // End Property
149             } while (sie<=cend && sie>=cis); // End SEMI processing
150             // Now evaluate Comma set and return if true
151             if (eval(code,name,props))return true; // else loop again to check next comma
152         } while (cie>=0); // loop to next comma
153         return false; // didn't get even one match
154     }
155
156 }