Sonar fixes related to exceptions
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / server / AbsService.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.server;
23
24 import java.security.NoSuchAlgorithmException;
25 import java.util.Properties;
26
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.SSLSocketFactory;
29 import javax.servlet.Filter;
30
31 import org.onap.aaf.auth.common.Define;
32 import org.onap.aaf.auth.rserv.RServlet;
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Access.Level;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.LocatorException;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
38 import org.onap.aaf.cadi.client.Rcli;
39 import org.onap.aaf.cadi.client.Retryable;
40 import org.onap.aaf.cadi.config.Config;
41 import org.onap.aaf.cadi.http.HTransferSS;
42 import org.onap.aaf.cadi.principal.TaggedPrincipal;
43 import org.onap.aaf.cadi.register.Registrant;
44 import org.onap.aaf.cadi.util.Split;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Trans;
47 import org.onap.aaf.misc.env.impl.BasicEnv;
48
49 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
50         public final Access access;
51         public final ENV env;
52         private AAFConHttp aafCon;
53
54         public final String app_name;
55         public final String app_version;
56         public final String app_interface_version;
57         public final String ROOT_NS;
58
59     public AbsService(final Access access, final ENV env) throws CadiException {
60                 Define.set(access);
61                 ROOT_NS = Define.ROOT_NS();
62                 this.access = access;
63                 this.env = env;
64
65                 String component = access.getProperty(Config.AAF_COMPONENT, null);
66                 final String[] locator_deploy;
67                 
68                 if(component == null) {
69                         locator_deploy = null;
70                 } else {
71                         locator_deploy = Split.splitTrim(':', component);
72                 }
73                         
74                 if(component == null || locator_deploy==null || locator_deploy.length<2) {
75                         throw new CadiException("AAF Component must include the " + Config.AAF_COMPONENT + " property, <fully qualified service name>:<full deployed version (i.e. 2.1.3.13)");
76                 }
77                 final String[] version = Split.splitTrim('.', locator_deploy[1]);
78                 if(version==null || version.length<2) {
79                         throw new CadiException("AAF Component Version must have at least Major.Minor version");
80                 }
81                 app_name = Define.varReplace(locator_deploy[0]);
82                 app_version = locator_deploy[1];
83                 app_interface_version = version[0]+'.'+version[1];
84                 
85                 // Print Cipher Suites Available
86                 if(access.willLog(Level.DEBUG)) {
87                         SSLContext context;
88                         try {
89                                 context = SSLContext.getDefault();
90                         } catch (NoSuchAlgorithmException e) {
91                                 throw new CadiException("SSLContext issue",e);
92                         }
93                         SSLSocketFactory sf = context.getSocketFactory();
94                         StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
95                         boolean first = true;
96                         int count=0;
97                         for( String cs : sf.getSupportedCipherSuites()) {
98                                 if(first)first = false;
99                                 else sb.append(',');
100                                 sb.append(cs);
101                                 if(++count%4==0){sb.append('\n');}
102                         }
103                         access.log(Level.DEBUG,sb);
104                 }
105     }
106
107         protected abstract Filter[] _filters(Object ... additionalTafLurs) throws CadiException,  LocatorException;
108         
109         /**
110          * Overload this method to add new TAF or LURs
111          * 
112          * @return
113          * @throws CadiException
114          * @throws LocatorException
115          */
116         public Filter[] filters() throws CadiException,  LocatorException {
117                 return _filters();
118         }
119
120     public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
121
122         // Lazy Instantiation
123     public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
124                 if(aafCon==null) {
125                         if(access.getProperty(Config.AAF_URL,null)!=null) {
126                                 aafCon = _newAAFConHttp();
127                         } else {
128                                 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
129                         }
130                 }
131                 return aafCon;
132     }
133     
134     /**
135      * Allow to be over ridden for special cases
136      * @return
137      * @throws LocatorException 
138      */
139                 protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
140                         if(aafCon==null) {
141                                 aafCon = new AAFConHttp(access);
142                         }
143                         return aafCon;
144
145                 }
146     
147     // This is a method, so we can overload for AAFAPI
148     public String aaf_url() {
149                 return access.getProperty(Config.AAF_URL, null);
150     }
151     
152         public Rcli<?> client() throws CadiException {
153                 return aafCon.client(Config.AAF_DEFAULT_VERSION);
154         }
155
156         public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
157                 return aafCon.client(Config.AAF_DEFAULT_VERSION).forUser(
158                                 new HTransferSS(p,app_name, aafCon.securityInfo()));
159         }
160
161         public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException  {
162                         return aafCon.hman().best(new HTransferSS(p,app_name, aafCon.securityInfo()), retryable);
163         }
164         
165         protected static final String loadFromArgOrSystem(final Properties props, final String tag, final String args[], final String def) {
166                 String tagEQ = tag + '=';
167                 String value;
168                 for(String arg : args) {
169                         if(arg.startsWith(tagEQ)) {
170                                 props.put(tag, value=arg.substring(tagEQ.length()));
171                                 return value;
172                         }
173                 }
174                 // check System.properties
175                 value = System.getProperty(tag);
176                 if(value!=null) { 
177                         props.put(tag, value);
178                         return value;
179                 }
180                 
181                 if(def!=null) {
182                         props.put(tag,def);
183                 }
184                 return def;
185         }
186
187 }