Update project structure for aaf/cadi
[aaf/cadi.git] / client / src / main / java / org / onap / aaf / 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 org.onap.aaf.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 org.onap.aaf.cadi.Symm;\r
32 import org.onap.aaf.cadi.config.Config;\r
33 \r
34 import com.att.aft.dme2.api.DME2Client;\r
35 \r
36 public abstract class RawClient {\r
37         protected static String aafid, aafpass, aafurl;\r
38         protected static Symm symm;\r
39         \r
40         protected static boolean init(PrintStream out) {\r
41                 try {\r
42                         String propfile = System.getProperty(Config.CADI_PROP_FILES);\r
43                         if(propfile==null) {\r
44                                 propfile = "raw.props";\r
45                         }\r
46                         File pfile = new File(propfile);\r
47                         if(!pfile.exists())  {\r
48                                 if(propfile.equals("raw.props")) {\r
49                                         out.println("Creating 'raw.props'.  Edit for proper values, then run again.  Alternatively, set "\r
50                                         + Config.CADI_PROP_FILES+" to a cadi properties file");\r
51                                         FileOutputStream fos = new FileOutputStream(pfile);\r
52                                         PrintStream ps = new PrintStream(fos);\r
53                                         try {\r
54                                                 ps.println("# Use http://www.bing.com/maps to figure out LAT/LONG of an Address");\r
55                                                 ps.println("AFT_LATITUDE=38.432930");\r
56                                                 ps.println("AFT_LONGITUDE=-90.432480");\r
57                                                 ps.println("AFT_ENVIRONMENT=AFTUAT");\r
58                                                 ps.print(Config.AAF_URL);\r
59                                                 ps.println("=aaf_url=https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=DEV/routeOffer=BAU_SE");\r
60                                                 ps.print(Config.CADI_KEYFILE);\r
61                                                 ps.println("=<keyfile.  use java -jar cadi-core*.jar in lib dir>");\r
62                                                 ps.println(Config.AAF_MECHID);\r
63                                                 ps.print("=<your id>");\r
64                                                 ps.println(Config.AAF_MECHPASS);\r
65                                                 ps.print("=<your encrypted password.  use java -jar cadi-core*.jar in lib dir>");\r
66                                         } finally {\r
67                                                 ps.close();\r
68                                         }\r
69                                 }\r
70                         } else {\r
71                                 FileInputStream fis = new FileInputStream(propfile);\r
72                                 try {\r
73                                         System.getProperties().load(fis);\r
74                                 } finally {\r
75                                         fis.close();\r
76                                 }\r
77                                 \r
78                                 String cadiKeyFile = System.getProperty(Config.CADI_KEYFILE);\r
79                                 aafid = System.getProperty(Config.AAF_MECHID);\r
80                                 aafpass = System.getProperty(Config.AAF_MECHPASS);\r
81                                 aafurl = System.getProperty(Config.AAF_URL);\r
82                                 out.println("Contacting: " + aafurl);\r
83 \r
84                                 if(cadiKeyFile==null || aafid==null || aafpass==null || aafurl==null ) {\r
85                                         out.print(Config.CADI_KEYFILE);\r
86                                         out.print(", ");\r
87                                         out.print(Config.CADI_KEYFILE);\r
88                                         out.print(", ");\r
89                                         out.print(Config.CADI_KEYFILE);\r
90                                         out.print(", ");\r
91                                         out.print(Config.CADI_KEYFILE);\r
92                                         out.print(" need to be set in ");\r
93                                         out.println(propfile);\r
94                                 } else {\r
95                                         fis = new FileInputStream(cadiKeyFile);\r
96                                         try {\r
97                                                 symm = Symm.obtain(fis);\r
98                                         } finally {\r
99                                                 fis.close();\r
100                                         }\r
101                                 }\r
102                                 return true;\r
103                         }\r
104                 } catch (Exception e) {\r
105                         e.printStackTrace(out);\r
106                 }\r
107                 return false;\r
108 \r
109         }\r
110         \r
111         public abstract String call(final PrintStream out, final String meth, final String path) throws Exception;\r
112         \r
113         public static void main(String[] args) {\r
114                 // Sonar idiocy\r
115                 PrintStream out = System.out;\r
116 \r
117                 try {\r
118                         if(init(out)) {\r
119                                 if(args.length<2) {\r
120                                         System.out.println("Parameters: <Method> <path>");\r
121                                 } else {\r
122                                         RawClient client = new DME2();\r
123                                         out.println(client.call(out,args[0],args[1]));\r
124                                 }\r
125                         }               \r
126                 } catch (Exception e) {\r
127                         e.printStackTrace(out);\r
128                 }\r
129         }\r
130         \r
131         protected static class DME2 extends RawClient {\r
132 \r
133                 public String call(final PrintStream out, final String meth, final String path) {\r
134                         try {\r
135                                 DME2Client client = new DME2Client(new URI(aafurl),10000);\r
136                                 client.setCredentials(aafid, symm.depass(aafpass));\r
137                                 client.setMethod(meth);\r
138                                 client.setContext(path);\r
139                                 \r
140                                 if("GET".equalsIgnoreCase(meth) ||\r
141                                    "DELETE".equalsIgnoreCase(meth)) {\r
142                                         client.setPayload("");\r
143                                 } else if("POST".equalsIgnoreCase(meth) ||\r
144                                                   "PUT".equalsIgnoreCase(meth)) {\r
145                                         int c;\r
146                                         StringBuilder sb = new StringBuilder();\r
147                                         while((c=System.in.read()) >=0) {\r
148                                                 sb.append((char)c);\r
149                                         }\r
150                                         client.setPayload(sb.toString());\r
151                                 }\r
152                                 return client.sendAndWait(10000);\r
153                         } catch (Exception e) {\r
154                                 e.printStackTrace(out);\r
155                                 return "";\r
156                         }\r
157                 }\r
158         }\r
159 }\r