1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cssa.rserv;
\r
25 import java.io.IOException;
\r
26 import java.util.ArrayList;
\r
27 import java.util.HashMap;
\r
28 import java.util.List;
\r
30 import javax.servlet.ServletException;
\r
32 import com.att.inno.env.Env;
\r
33 import com.att.inno.env.TimeTaken;
\r
34 import com.att.inno.env.Trans;
\r
38 * TypedCode organizes implementation code based on the Type and Version of code it works with so that it can
\r
39 * be located quickly at runtime based on the "Accept" HTTP Header.
\r
41 * FYI: For those in the future wondering why I would create a specialized set of "Pair" for the data content:
\r
42 * 1) TypeCode is used in Route, and this code is used for every transaction... it needs to be blazingly fast
\r
43 * 2) The actual number of objects accessed is quite small and built at startup. Arrays are best
\r
44 * 3) I needed a small, well defined tree where each level is a different Type. Using a "Pair" Generic definitions,
\r
45 * I created type-safety at each level, which you can't get from a TreeSet, etc.
\r
46 * 4) Chaining through the Network is simply object dereferencing, which is as fast as Java can go.
\r
47 * 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
\r
48 * read both in code, and in the debugger. However, TypeSafety allows your IDE (Eclipse) to help you make the
\r
49 * choices. Also, make sure you have a good "toString()" method on each object so you can see what's happening
\r
50 * in the IDE Debugger.
\r
52 * Empirically, this method of obtaining routes proved to be much faster than the HashSet implementations available in otherwise
\r
53 * competent Open Source.
\r
57 public class TypedCode<TRANS extends Trans> extends Content<TRANS> {
\r
58 private List<Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String, Object>>>>> types;
\r
60 public TypedCode() {
\r
61 types = new ArrayList<Pair<String,Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>>();
\r
65 * Construct Typed Code based on ContentType parameters passed in
\r
71 public TypedCode<TRANS> add(HttpCode<TRANS,?> code, String ... others) {
\r
72 StringBuilder sb = new StringBuilder();
\r
73 boolean first = true;
\r
74 for(String str : others) {
\r
82 parse(code, sb.toString());
\r
88 protected Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> types(HttpCode<TRANS,?> code, String str) {
\r
89 Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String, Object>>>> type = null;
\r
90 ArrayList<Pair<String, Object>> props = new ArrayList<Pair<String,Object>>();
\r
91 // Want Q percentage is to be first in the array everytime. If not listed, 1.0 is default
\r
92 props.add(new Pair<String,Object>(Q,1f));
\r
93 Pair<HttpCode<TRANS,?>, List<Pair<String,Object>>> cl = new Pair<HttpCode<TRANS,?>, List<Pair<String,Object>>>(code, props);
\r
94 // // breakup "plus" stuff, i.e. application/xaml+xml
\r
95 // int plus = str.indexOf('+');
\r
97 type = new Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>(str, cl);
\r
101 // int prev = str.indexOf('/')+1;
\r
102 // String first = str.substring(0,prev);
\r
104 // while(prev!=0) {
\r
105 // nstr = first + (plus>-1?str.substring(prev,plus):str.substring(prev));
\r
106 // type = new Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>>(nstr, cl);
\r
107 // types.add(type);
\r
109 // plus = str.indexOf('+',prev);
\r
116 protected boolean props(Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type, String tag, String value) {
\r
117 if(tag.equals(Q)) { // reset the Q value (first in array)
\r
120 type.y.y.get(0).y=Float.parseFloat(value);
\r
122 } catch (NumberFormatException e) {
\r
123 rv=false; // Note: this awkward syntax forced by Sonar, which doesn't like doing nothing with Exception
\r
124 // which is what should happen
\r
127 return type.y.y.add(new Pair<String,Object>(tag,"version".equals(tag)?new Version(value):value));
\r
130 public Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> prep(TRANS trans, String compare) throws IOException, ServletException {
\r
131 Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> c,rv=null;
\r
132 if(types.size()==1 && "".equals((c=types.get(0)).x)) { // if there are no checks for type, skip
\r
135 if(compare==null || compare.length()==0) {
\r
136 rv = types.get(0); // first code is used
\r
138 Acceptor<TRANS> acc = new Acceptor<TRANS>(types);
\r
140 TimeTaken tt = trans.start(compare, Env.SUB);
\r
142 accepted = acc.parse(null, compare);
\r
147 switch(acc.acceptable.size()) {
\r
149 // // TODO best Status Code?
\r
150 // resp.setStatus(HttpStatus.NOT_ACCEPTABLE_406);
\r
153 rv = acc.acceptable.get(0);
\r
155 default: // compare Q values to get Best Match
\r
156 float bestQ = -1.0f;
\r
157 Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> bestT = null;
\r
158 for(Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> type : acc.acceptable) {
\r
159 Float f = (Float)type.y.y.get(0).y; // first property is always Q
\r
166 // When it is a GET, the matched type is what is returned, so set ContentType
\r
167 // if(isGet)resp.setContentType(bestT.x); // set ContentType of Code<TRANS,?>
\r
173 trans.checkpoint("No Match found for Accept");
\r
181 * Print on String Builder content related to specific Code
\r
183 * This is for Reporting and Debugging purposes, so the content is not cached.
\r
185 * If code is "null", then all content is matched
\r
190 public StringBuilder relatedTo(HttpCode<TRANS, ?> code, StringBuilder sb) {
\r
191 boolean first = true;
\r
192 for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> pair : types) {
\r
193 if(code==null || pair.y.x == code) {
\r
200 for(Pair<String,Object> prop : pair.y.y) {
\r
201 // Don't print "Q". it's there for internal use, but it is only meaningful for "Accepts"
\r
202 if(!prop.x.equals(Q) || !prop.y.equals(1f) ) {
\r
214 public List<Pair<String, Object>> getContent(HttpCode<TRANS,?> code) {
\r
215 for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> pair : types) {
\r
216 if(pair.y.x == code) {
\r
223 public String toString() {
\r
224 return relatedTo(null,new StringBuilder()).toString();
\r
227 public void api(RouteReport tr) {
\r
228 // Need to build up a map, because Prop entries can be in several places.
\r
229 HashMap<HttpCode<?,?>,StringBuilder> psb = new HashMap<HttpCode<?,?>,StringBuilder>();
\r
230 StringBuilder temp;
\r
233 // Read through Code/TypeCode trees for all accepted Typecodes
\r
234 for(Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> tc : types) {
\r
235 // If new, then it's new Code set, create prefix content
\r
236 if((temp=psb.get(tc.y.x))==null) {
\r
237 psb.put(tc.y.x,temp=new StringBuilder());
\r
238 if(tr.desc==null) {
\r
239 tr.desc = tc.y.x.desc();
\r
246 // add all properties
\r
247 for(Pair<String, Object> props : tc.y.y) {
\r
249 temp.append(props.x);
\r
251 temp.append(props.y);
\r
254 // Gather all ContentType possibilities for the same code together
\r
256 for(StringBuilder sb : psb.values()) {
\r
257 tr.contextTypes.add(sb.toString());
\r
261 public String first() {
\r
262 if(types.size()>0) {
\r
263 return types.get(0).x;
\r