Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / common / Define.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.common;
23
24 import java.util.Map.Entry;
25
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.Access.Level;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.config.Config;
30
31 public class Define {
32     private static String ROOT_NS = null;
33     private static String ROOT_COMPANY = null;
34     private static boolean initialized = false;
35
36     private final static String MSG = ".set(Access access) must be called before use";
37     public static final CharSequence ROOT_NS_TAG = "AAF_NS"; // use for certain Replacements in Location
38     private static final int ROOT_NS_TAG_LEN=ROOT_NS_TAG.length();
39     private static final String ROOT_NS_TAG_DOT = ROOT_NS_TAG +".";
40
41     public static String ROOT_NS() {
42         if (ROOT_NS==null) {
43             throw new RuntimeException(Define.class.getName() + MSG);
44         }
45         return ROOT_NS;
46     }
47     
48     public static String ROOT_COMPANY() {
49         if (ROOT_NS==null) {
50             throw new RuntimeException(Define.class.getName() + MSG);
51         }
52         return ROOT_COMPANY;
53     }
54     
55     public static void set(Access access) throws CadiException {
56         ROOT_NS = access.getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
57         ROOT_COMPANY = access.getProperty(Config.AAF_ROOT_COMPANY,null);
58         if (ROOT_COMPANY==null) {
59             int last = ROOT_NS.lastIndexOf('.');
60             if (last>=0) {
61                 ROOT_COMPANY = ROOT_NS.substring(0, last);
62             } else {
63                 throw new CadiException(Config.AAF_ROOT_COMPANY + " or " + Config.AAF_ROOT_NS + " property with 3 positions is required.");
64             }
65         }
66         
67         for ( Entry<Object, Object> es : access.getProperties().entrySet()) {
68             if (es.getKey().toString().startsWith(ROOT_NS_TAG_DOT)) {
69                 access.getProperties().setProperty(es.getKey().toString(),varReplace(es.getValue().toString()));
70             }
71         }
72
73         initialized = true;
74         access.printf(Level.INIT,"AAF Root NS is %s, and AAF Company Root is %s",ROOT_NS,ROOT_COMPANY);
75     }
76
77     public static String varReplace(final String potential) {
78         int idx = potential.indexOf(ROOT_NS_TAG_DOT);
79         if(idx<0) {
80             return potential;
81         } else if(idx==0) {
82             return ROOT_NS + potential.substring(ROOT_NS_TAG_LEN);
83         } else if('.'==potential.charAt(idx)) {
84             return potential.replace(ROOT_NS_TAG, ROOT_NS);
85         } else {
86             return potential;
87         }
88     }
89
90     public static boolean isInitialized() {
91         return initialized;
92     }
93     
94     public static String getCredType(int type) {
95         switch(type) {
96             case 0:      return "NoCrd";
97             case 1:   return "U/P";
98             case 2:   return "U/P2";
99             case 10:  return "FQI";
100             case 200: return "x509";
101             default:
102                 return "n/a";
103         }
104     }
105     
106 }