summaryrefslogtreecommitdiffstats
path: root/authz-core/src/main/java/org/onap/aaf/cssa/rserv/TypedCode.java
blob: e1aaf1d69eea08cc715be311bc718d4b26555737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.cssa.rserv;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.onap.aaf.inno.env.Env;
import org.onap.aaf.inno.env.TimeTaken;
import org.onap.aaf.inno.env.Trans;


/**
 * TypedCode organizes implementation code based on the Type and Version of code it works with so that it can
 * be located quickly at runtime based on the "Accept" HTTP Header.
 *
 * FYI: For those in the future wondering why I would create a specialized set of "Pair" for the data content:
 *   1) TypeCode is used in Route, and this code is used for every transaction... it needs to be blazingly fast
 *   2) The actual number of objects accessed is quite small and built at startup.  Arrays are best
 *   3) I needed a small, well defined tree where each level is a different Type.  Using a "Pair" Generic definitions, 
 *      I created type-safety at each level, which you can't get from a TreeSet, etc.
 *   4) Chaining through the Network is simply object dereferencing, which is as fast as Java can go.
 *   5) The drawback is that in your code is that all the variables are named "x" and "y", which can be a bit hard to
 *   	read both in code, and in the debugger.  However, TypeSafety allows your IDE (Eclipse) to help you make the 
 *      choices.  Also, make sure you have a good "toString()" method on each object so you can see what's happening
 *      in the IDE Debugger.
 *   
 * Empirically, this method of obtaining routes proved to be much faster than the HashSet implementations available in otherwise
 * competent Open Source.
 *
 * @param <TRANS>
 */
public class TypedCode<TRANS extends Trans> extends Content<TRANS> {
		private List<Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String, Object>>>>> types;

		public TypedCode() {
			types = new ArrayList<Pair<String,Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>>();
		}
		
		/**
		 * Construct Typed Code based on ContentType parameters passed in
		 * 
		 * @param code
		 * @param others
		 * @return
		 */
		public TypedCode<TRANS> add(HttpCode<TRANS,?> code, String ... others) {
			StringBuilder sb = new StringBuilder();
			boolean first = true;
			for(String str : others) {
				if(first) {
					first = false; 
				} else {
					sb.append(',');
				}
				sb.append(str);
			}
			parse(code, sb.toString());
			
			return this;
		}
		
		@Override
		protected Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> types(HttpCode<TRANS,?> code, String str) {
			Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String, Object>>>> type = null;
			ArrayList<Pair<String, Object>> props = new ArrayList<Pair<String,Object>>();
			// Want Q percentage is to be first in the array everytime.  If not listed, 1.0 is default
			props.add(new Pair<String,Object>(Q,1f));
			Pair<HttpCode<TRANS,?>, List<Pair<String,Object>>> cl = new Pair<HttpCode<TRANS,?>, List<Pair<String,Object>>>(code, props);
//			// breakup "plus" stuff, i.e. application/xaml+xml
//			int plus = str.indexOf('+');
//			if(plus<0) {
				type = new Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>(str, cl);
				types.add(type);
				return type;
//			} else {
//				int prev = str.indexOf('/')+1;
//				String first = str.substring(0,prev);
//				String nstr;
//				while(prev!=0) {
//					nstr = first + (plus>-1?str.substring(prev,plus):str.substring(prev));
//					type = new Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>(nstr, cl);
//					types.add(type);
//					prev = plus+1;
//					plus = str.indexOf('+',prev);
//				}
//			return type;
//			}
		}

		@Override
		protected boolean props(Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type, String tag, String value) {
			if(tag.equals(Q)) { // reset the Q value (first in array)
				boolean rv = true;
				try {
					type.y.y.get(0).y=Float.parseFloat(value);
					return rv;
				} catch (NumberFormatException e) {
					rv=false; // Note: this awkward syntax forced by Sonar, which doesn't like doing nothing with Exception
							  // which is what should happen
				}
			}
			return type.y.y.add(new Pair<String,Object>(tag,"version".equals(tag)?new Version(value):value));
		}
		
		public Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> prep(TRANS trans, String compare) throws IOException, ServletException {
			Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> c,rv=null;
			if(types.size()==1 && "".equals((c=types.get(0)).x)) { // if there are no checks for type, skip
				rv = c;
			} else {
				if(compare==null || compare.length()==0) {
					rv = types.get(0); // first code is used
				} else {
					Acceptor<TRANS> acc = new Acceptor<TRANS>(types);
					boolean accepted;
					TimeTaken tt = trans.start(compare, Env.SUB);
					try {
						accepted = acc.parse(null, compare);
					} finally {
						tt.done();
					}
					if(accepted) {
						switch(acc.acceptable.size()) {
							case 0:	
//								// TODO best Status Code?
//								resp.setStatus(HttpStatus.NOT_ACCEPTABLE_406);
								break;
							case 1: 
								rv = acc.acceptable.get(0);
								break;
							default: // compare Q values to get Best Match
								float bestQ = -1.0f;
								Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> bestT = null;
								for(Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type : acc.acceptable) {
									Float f = (Float)type.y.y.get(0).y; // first property is always Q
									if(f>bestQ) {
										bestQ=f;
										bestT = type;
									}
								}
								if(bestT!=null) {
									// When it is a GET, the matched type is what is returned, so set ContentType
//									if(isGet)resp.setContentType(bestT.x); // set ContentType of Code<TRANS,?>
//									rv = bestT.y.x;
									rv = bestT;
								}
						}
					} else {
						trans.checkpoint("No Match found for Accept");
					}
				}
			}
			return rv;
		}
		
		/**
		 * Print on String Builder content related to specific Code
		 * 
		 * This is for Reporting and Debugging purposes, so the content is not cached.
		 * 
		 * If code is "null", then all content is matched
		 * 
		 * @param code
		 * @return
		 */
		public StringBuilder relatedTo(HttpCode<TRANS, ?> code, StringBuilder sb) {
			boolean first = true;
			for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> pair : types) {
				if(code==null || pair.y.x == code) {
					if(first) {
						first = false;
					} else {
						sb.append(',');
					}
					sb.append(pair.x);
					for(Pair<String,Object> prop : pair.y.y) {
						// Don't print "Q".  it's there for internal use, but it is only meaningful for "Accepts"
						if(!prop.x.equals(Q) || !prop.y.equals(1f) ) {
							sb.append(';');
							sb.append(prop.x);
							sb.append('=');
							sb.append(prop.y);
						}
					}
				}
			}
			return sb;
		}
		
		public List<Pair<String, Object>> getContent(HttpCode<TRANS,?> code) {
			for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> pair : types) {
				if(pair.y.x == code) {
					return pair.y.y;
				}
			}
			return null;
		}
	
		public String toString() {
			return relatedTo(null,new StringBuilder()).toString();
		}
		
		public void api(RouteReport tr) {
			// Need to build up a map, because Prop entries can be in several places.
			HashMap<HttpCode<?,?>,StringBuilder> psb = new HashMap<HttpCode<?,?>,StringBuilder>();
			StringBuilder temp;
			tr.desc = null;
			
			// Read through Code/TypeCode trees for all accepted Typecodes
			for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> tc : types) {
				// If new, then it's new Code set, create prefix content
				if((temp=psb.get(tc.y.x))==null) {
					psb.put(tc.y.x,temp=new StringBuilder());
					if(tr.desc==null) {
						tr.desc = tc.y.x.desc();
					}
				} else {
					temp.append(',');
				}
				temp.append(tc.x);

				// add all properties
				for(Pair<String, Object> props : tc.y.y) {
					temp.append(';');
					temp.append(props.x);
					temp.append('=');
					temp.append(props.y);
				}
			}
			// Gather all ContentType possibilities for the same code together
			
			for(StringBuilder sb : psb.values()) {
				tr.contextTypes.add(sb.toString());
			}
		}

		public String first() {
			if(types.size()>0) {
				return types.get(0).x;
			}
			return null;
		}
		
	}