1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.client;
\r
25 import java.io.File;
\r
26 import java.io.FileInputStream;
\r
27 import java.io.FileOutputStream;
\r
28 import java.io.PrintStream;
\r
29 import java.net.URI;
\r
31 import com.att.aft.dme2.api.DME2Client;
\r
32 import com.att.cadi.Symm;
\r
33 import com.att.cadi.config.Config;
\r
35 public abstract class RawClient {
\r
36 protected static String aafid, aafpass, aafurl;
\r
37 protected static Symm symm;
\r
39 protected static boolean init(PrintStream out) {
\r
41 String propfile = System.getProperty(Config.CADI_PROP_FILES);
\r
42 if(propfile==null) {
\r
43 propfile = "raw.props";
\r
45 File pfile = new File(propfile);
\r
46 if(!pfile.exists()) {
\r
47 if(propfile.equals("raw.props")) {
\r
48 out.println("Creating 'raw.props'. Edit for proper values, then run again. Alternatively, set "
\r
49 + Config.CADI_PROP_FILES+" to a cadi properties file");
\r
50 FileOutputStream fos = new FileOutputStream(pfile);
\r
51 PrintStream ps = new PrintStream(fos);
\r
53 ps.println("# Use http://www.bing.com/maps to figure out LAT/LONG of an Address");
\r
54 ps.println("AFT_LATITUDE=38.432930");
\r
55 ps.println("AFT_LONGITUDE=-90.432480");
\r
56 ps.println("AFT_ENVIRONMENT=AFTUAT");
\r
57 ps.print(Config.AAF_URL);
\r
58 ps.println("=aaf_url=https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=DEV/routeOffer=BAU_SE");
\r
59 ps.print(Config.CADI_KEYFILE);
\r
60 ps.println("=<keyfile. use java -jar cadi-core*.jar in lib dir>");
\r
61 ps.println(Config.AAF_MECHID);
\r
62 ps.print("=<your id>");
\r
63 ps.println(Config.AAF_MECHPASS);
\r
64 ps.print("=<your encrypted password. use java -jar cadi-core*.jar in lib dir>");
\r
70 FileInputStream fis = new FileInputStream(propfile);
\r
72 System.getProperties().load(fis);
\r
77 String cadiKeyFile = System.getProperty(Config.CADI_KEYFILE);
\r
78 aafid = System.getProperty(Config.AAF_MECHID);
\r
79 aafpass = System.getProperty(Config.AAF_MECHPASS);
\r
80 aafurl = System.getProperty(Config.AAF_URL);
\r
81 out.println("Contacting: " + aafurl);
\r
83 if(cadiKeyFile==null || aafid==null || aafpass==null || aafurl==null ) {
\r
84 out.print(Config.CADI_KEYFILE);
\r
86 out.print(Config.CADI_KEYFILE);
\r
88 out.print(Config.CADI_KEYFILE);
\r
90 out.print(Config.CADI_KEYFILE);
\r
91 out.print(" need to be set in ");
\r
92 out.println(propfile);
\r
94 fis = new FileInputStream(cadiKeyFile);
\r
96 symm = Symm.obtain(fis);
\r
103 } catch (Exception e) {
\r
104 e.printStackTrace(out);
\r
110 public abstract String call(final PrintStream out, final String meth, final String path) throws Exception;
\r
112 public static void main(String[] args) {
\r
114 PrintStream out = System.out;
\r
118 if(args.length<2) {
\r
119 System.out.println("Parameters: <Method> <path>");
\r
121 RawClient client = new DME2();
\r
122 out.println(client.call(out,args[0],args[1]));
\r
125 } catch (Exception e) {
\r
126 e.printStackTrace(out);
\r
130 protected static class DME2 extends RawClient {
\r
132 public String call(final PrintStream out, final String meth, final String path) {
\r
134 DME2Client client = new DME2Client(new URI(aafurl),10000);
\r
135 client.setCredentials(aafid, symm.depass(aafpass));
\r
136 client.setMethod(meth);
\r
137 client.setContext(path);
\r
139 if("GET".equalsIgnoreCase(meth) ||
\r
140 "DELETE".equalsIgnoreCase(meth)) {
\r
141 client.setPayload("");
\r
142 } else if("POST".equalsIgnoreCase(meth) ||
\r
143 "PUT".equalsIgnoreCase(meth)) {
\r
145 StringBuilder sb = new StringBuilder();
\r
146 while((c=System.in.read()) >=0) {
\r
147 sb.append((char)c);
\r
149 client.setPayload(sb.toString());
\r
151 return client.sendAndWait(10000);
\r
152 } catch (Exception e) {
\r
153 e.printStackTrace(out);
\r