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.SSLServerSocketFactory;
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.Defaults;
38 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
39 import org.onap.aaf.cadi.client.Rcli;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.http.HTransferSS;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal;
44 import org.onap.aaf.cadi.register.Registrant;
45 import org.onap.aaf.cadi.util.Split;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.Trans;
48 import org.onap.aaf.misc.env.impl.BasicEnv;
50 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
51 public final Access access;
53 private AAFConHttp aafCon;
55 public final String appName;
56 public final String appVersion;
57 public final String rootNs;
59 public AbsService(final Access access, final ENV env) throws CadiException {
61 rootNs = Define.ROOT_NS();
65 String str = access.getProperty(Config.AAF_LOCATOR_ENTRIES, null);
66 String[] scomp = Split.splitTrim(',', str);
68 throw new CadiException(Config.AAF_LOCATOR_ENTRIES + " must be defined.");
70 str = rootNs + '.' + scomp[0];
74 str = access.getProperty(Config.AAF_LOCATOR_VERSION, null);
76 str = Defaults.AAF_VERSION;
77 env.setProperty(Config.AAF_LOCATOR_VERSION, str);
79 appVersion = access.getProperty(Config.AAF_DEPLOYED_VERSION, str);
81 // Print Cipher Suites Available
82 if (access.willLog(Level.DEBUG)) {
85 context = SSLContext.getDefault();
86 } catch (NoSuchAlgorithmException e) {
87 throw new CadiException("SSLContext issue",e);
89 SSLServerSocketFactory sf = context.getServerSocketFactory();
90 StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
93 for ( String cs : sf.getSupportedCipherSuites()) {
94 if (first)first = false;
97 if (++count%4==0){sb.append('\n');}
99 access.log(Level.DEBUG,sb);
103 public void setProtocol(String proto) {
104 env.setProperty(Config.AAF_LOCATOR_PROTOCOL, proto);
107 public void setSubprotocol(String subproto) {
108 env.setProperty(Config.AAF_LOCATOR_SUBPROTOCOL, subproto);
111 protected abstract Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException;
114 * Overload this method to add new TAF or LURs
117 * @throws CadiException
118 * @throws LocatorException
120 public Filter[] filters() throws CadiException, LocatorException {
124 public abstract Registrant<ENV>[] registrants(final int actualPort) throws CadiException, LocatorException;
126 // Lazy Instantiation
127 public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
129 if (access.getProperty(Config.AAF_URL,null)!=null) {
130 aafCon = _newAAFConHttp();
132 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
139 * Allow to be over ridden for special cases
141 * @throws LocatorException
143 protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
145 aafCon = new AAFConHttp(access);
151 // This is a method, so we can overload for AAFAPI
152 public String aaf_url() {
153 return access.getProperty(Config.AAF_URL, null);
156 public Rcli<?> client() throws CadiException {
157 return aafCon.client();
160 public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
161 return aafCon.client().forUser(
162 new HTransferSS(p, appName, aafCon.securityInfo()));
165 public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException {
166 return aafCon.hman().best(new HTransferSS(p, appName, aafCon.securityInfo()), retryable);
169 protected static final String loadFromArgOrSystem(final Properties props, final String tag, final String args[], final String def) {
170 String tagEQ = tag + '=';
172 for (String arg : args) {
173 if (arg.startsWith(tagEQ)) {
174 props.put(tag, value=arg.substring(tagEQ.length()));
178 // check System.properties
179 value = System.getProperty(tag);
181 props.put(tag, value);