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;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.SSLSocketFactory;
28 import javax.servlet.Filter;
30 import org.onap.aaf.auth.common.Define;
31 import org.onap.aaf.auth.rserv.RServlet;
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.Access.Level;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
37 import org.onap.aaf.cadi.client.Rcli;
38 import org.onap.aaf.cadi.client.Retryable;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.http.HTransferSS;
41 import org.onap.aaf.cadi.principal.TaggedPrincipal;
42 import org.onap.aaf.cadi.register.Registrant;
43 import org.onap.aaf.cadi.util.Split;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Trans;
46 import org.onap.aaf.misc.env.impl.BasicEnv;
48 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
49 public final Access access;
51 private AAFConHttp aafCon;
53 public final String app_name;
54 public final String app_version;
55 public final String app_interface_version;
56 public final String ROOT_NS;
58 public AbsService(final Access access, final ENV env) throws CadiException {
60 ROOT_NS = Define.ROOT_NS();
64 String component = access.getProperty(Config.AAF_COMPONENT, null);
65 final String[] locator_deploy;
67 if(component == null) {
68 locator_deploy = null;
70 locator_deploy = Split.splitTrim(':', component);
73 if(component == null || locator_deploy==null || locator_deploy.length<2) {
74 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 final String[] version = Split.splitTrim('.', locator_deploy[1]);
77 if(version==null || version.length<2) {
78 throw new CadiException("AAF Component Version must have at least Major.Minor version");
80 app_name = Define.varReplace(locator_deploy[0]);
81 app_version = locator_deploy[1];
82 app_interface_version = version[0]+'.'+version[1];
84 // Print Cipher Suites Available
85 if(access.willLog(Level.DEBUG)) {
88 context = SSLContext.getDefault();
89 } catch (NoSuchAlgorithmException e) {
90 throw new CadiException("SSLContext issue",e);
92 SSLSocketFactory sf = context.getSocketFactory();
93 StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
96 for( String cs : sf.getSupportedCipherSuites()) {
97 if(first)first = false;
100 if(++count%4==0){sb.append('\n');}
102 access.log(Level.DEBUG,sb);
106 public abstract Filter[] filters() throws CadiException, LocatorException;
109 public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
111 // Lazy Instantiation
112 public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
114 if(access.getProperty(Config.AAF_URL,null)!=null) {
115 aafCon = _newAAFConHttp();
117 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
124 * Allow to be over ridden for special cases
126 * @throws LocatorException
128 protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
131 aafCon = new AAFConHttp(access);
134 } catch (APIException e) {
135 throw new CadiException(e);
139 // This is a method, so we can overload for AAFAPI
140 public String aaf_url() {
141 return access.getProperty(Config.AAF_URL, null);
144 public Rcli<?> client() throws CadiException {
145 return aafCon.client(Config.AAF_DEFAULT_VERSION);
148 public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
149 return aafCon.client(Config.AAF_DEFAULT_VERSION).forUser(
150 new HTransferSS(p,app_name, aafCon.securityInfo()));
153 public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException {
154 return aafCon.hman().best(new HTransferSS(p,app_name, aafCon.securityInfo()), retryable);