2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.server;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.Properties;
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.SSLSocketFactory;
29 import javax.servlet.Filter;
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;
49 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
50 public final Access access;
52 private AAFConHttp aafCon;
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;
59 public AbsService(final Access access, final ENV env) throws CadiException {
61 ROOT_NS = Define.ROOT_NS();
65 String component = access.getProperty(Config.AAF_COMPONENT, null);
66 final String[] locator_deploy;
68 if(component == null) {
69 locator_deploy = null;
71 locator_deploy = Split.splitTrim(':', component);
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)");
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");
81 app_name = Define.varReplace(locator_deploy[0]);
82 app_version = locator_deploy[1];
83 app_interface_version = version[0]+'.'+version[1];
85 // Print Cipher Suites Available
86 if(access.willLog(Level.DEBUG)) {
89 context = SSLContext.getDefault();
90 } catch (NoSuchAlgorithmException e) {
91 throw new CadiException("SSLContext issue",e);
93 SSLSocketFactory sf = context.getSocketFactory();
94 StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
97 for( String cs : sf.getSupportedCipherSuites()) {
98 if(first)first = false;
101 if(++count%4==0){sb.append('\n');}
103 access.log(Level.DEBUG,sb);
107 protected abstract Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException;
110 * Overload this method to add new TAF or LURs
113 * @throws CadiException
114 * @throws LocatorException
116 public Filter[] filters() throws CadiException, LocatorException {
120 public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
122 // Lazy Instantiation
123 public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
125 if(access.getProperty(Config.AAF_URL,null)!=null) {
126 aafCon = _newAAFConHttp();
128 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
135 * Allow to be over ridden for special cases
137 * @throws LocatorException
139 protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
141 aafCon = new AAFConHttp(access);
147 // This is a method, so we can overload for AAFAPI
148 public String aaf_url() {
149 return access.getProperty(Config.AAF_URL, null);
152 public Rcli<?> client() throws CadiException {
153 return aafCon.client(Config.AAF_DEFAULT_VERSION);
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()));
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);
165 protected static final String loadFromArgOrSystem(final Properties props, final String tag, final String args[], final String def) {
166 String tagEQ = tag + '=';
168 for(String arg : args) {
169 if(arg.startsWith(tagEQ)) {
170 props.put(tag, value=arg.substring(tagEQ.length()));
174 // check System.properties
175 value = System.getProperty(tag);
177 props.put(tag, value);