c1ad6337cc3b7a1985edae4a97250e922e1add34
[aaf/cadi.git] / client / src / main / java / com / att / cadi / client / RawClient.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.client;\r
24 \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
30 \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
34 \r
35 public abstract class RawClient {\r
36         protected static String aafid, aafpass, aafurl;\r
37         protected static Symm symm;\r
38         \r
39         protected static boolean init(PrintStream out) {\r
40                 try {\r
41                         String propfile = System.getProperty(Config.CADI_PROP_FILES);\r
42                         if(propfile==null) {\r
43                                 propfile = "raw.props";\r
44                         }\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
52                                         try {\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
65                                         } finally {\r
66                                                 ps.close();\r
67                                         }\r
68                                 }\r
69                         } else {\r
70                                 FileInputStream fis = new FileInputStream(propfile);\r
71                                 try {\r
72                                         System.getProperties().load(fis);\r
73                                 } finally {\r
74                                         fis.close();\r
75                                 }\r
76                                 \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
82 \r
83                                 if(cadiKeyFile==null || aafid==null || aafpass==null || aafurl==null ) {\r
84                                         out.print(Config.CADI_KEYFILE);\r
85                                         out.print(", ");\r
86                                         out.print(Config.CADI_KEYFILE);\r
87                                         out.print(", ");\r
88                                         out.print(Config.CADI_KEYFILE);\r
89                                         out.print(", ");\r
90                                         out.print(Config.CADI_KEYFILE);\r
91                                         out.print(" need to be set in ");\r
92                                         out.println(propfile);\r
93                                 } else {\r
94                                         fis = new FileInputStream(cadiKeyFile);\r
95                                         try {\r
96                                                 symm = Symm.obtain(fis);\r
97                                         } finally {\r
98                                                 fis.close();\r
99                                         }\r
100                                 }\r
101                                 return true;\r
102                         }\r
103                 } catch (Exception e) {\r
104                         e.printStackTrace(out);\r
105                 }\r
106                 return false;\r
107 \r
108         }\r
109         \r
110         public abstract String call(final PrintStream out, final String meth, final String path) throws Exception;\r
111         \r
112         public static void main(String[] args) {\r
113                 // Sonar idiocy\r
114                 PrintStream out = System.out;\r
115 \r
116                 try {\r
117                         if(init(out)) {\r
118                                 if(args.length<2) {\r
119                                         System.out.println("Parameters: <Method> <path>");\r
120                                 } else {\r
121                                         RawClient client = new DME2();\r
122                                         out.println(client.call(out,args[0],args[1]));\r
123                                 }\r
124                         }               \r
125                 } catch (Exception e) {\r
126                         e.printStackTrace(out);\r
127                 }\r
128         }\r
129         \r
130         protected static class DME2 extends RawClient {\r
131 \r
132                 public String call(final PrintStream out, final String meth, final String path) {\r
133                         try {\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
138                                 \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
144                                         int c;\r
145                                         StringBuilder sb = new StringBuilder();\r
146                                         while((c=System.in.read()) >=0) {\r
147                                                 sb.append((char)c);\r
148                                         }\r
149                                         client.setPayload(sb.toString());\r
150                                 }\r
151                                 return client.sendAndWait(10000);\r
152                         } catch (Exception e) {\r
153                                 e.printStackTrace(out);\r
154                                 return "";\r
155                         }\r
156                 }\r
157         }\r
158 }\r