-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACSUtility.java
187 lines (137 loc) · 5.9 KB
/
ACSUtility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.ge.lotus.automation.acs;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
public class ACSUtility {
private final String REFERER = "http://localhost:8270";
static Properties prop = new Properties();
static InputStream input = null;
public static void addNellcoreDevice(String ParameterName,String device,String parameterValue) throws Exception {
startDeviceinACS(ParameterName,device);
Thread.currentThread().sleep(5000);
new ACSUtility().sendPost(getFinalACSXMLString(ParameterName,parameterValue));
Thread.currentThread().sleep(5000);
removeDeviceinACS(ParameterName);
}
public static void startDeviceinACS(String parameter, String deviceName) throws Exception{
removeDeviceinACS(parameter);
ACSUtility acsUtility = new ACSUtility();
acsUtility.sendPost(getFinalStringForAddingDevice(parameter,deviceName));
acsUtility.sendPost(getFinalStringForStartSimulator(parameter));
}
public static void removeDeviceinACS(String parameter) throws Exception{
ACSUtility acsUtility = new ACSUtility();
acsUtility.sendPost(getFinalStringForRemovingDevice(parameter));
System.out.println("Device Romoved for "+parameter);
}
public static String getFinalACSXMLString( String parameterName,String parameterValue) throws IOException{
String comPort = null;
comPort = getValue(parameterName.replaceAll("\\s+","")+"ComPort");
String finalXMLString = getValue("parameter_1");
finalXMLString += comPort+getValue("parameter_2");
finalXMLString += parameterName+getValue("parameter_3");
finalXMLString += parameterValue+getValue("parameter_4");
return finalXMLString;
}
public static String getFinalStringForStartSimulator( String parameterName) throws IOException{
String comPort = null;
comPort = getValue(parameterName.replaceAll("\\s+","")+"ComPort");
String finalXMLString = getValue("starting_simulator1");
finalXMLString += comPort+getValue("starting_simulator2");
return finalXMLString;
}
public static String getFinalStringForAddingDevice( String parameterName,String deviceName) throws IOException{
String comPort = null;
comPort = getValue(parameterName.replaceAll("\\s+","")+"ComPort");
String finalXMLString = getValue("Adding_Device1");
finalXMLString += comPort+getValue("Adding_Device2");
finalXMLString += deviceName+getValue("Adding_Device3");
return finalXMLString;
}
public static String getFinalStringForRemovingDevice( String parameterName) throws IOException{
String comPort = null;
comPort = getValue(parameterName.replaceAll("\\s+","")+"ComPort");
String finalXMLString = getValue("Remove_device1");
finalXMLString += comPort+getValue("Remove_device2");
return finalXMLString;
}
public static String getValue(String key) throws IOException{
input = new FileInputStream("SimulationUrl.properties");
// load a properties file
prop.load(input);
String value = prop.getProperty(key);
return value;
}
// HTTP GET request
private String sendGet() throws Exception {
String url = REFERER;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("Referer", "http://localhost:8270/gui/");
con.setRequestProperty("Accept-Language", "en-US,en");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("charset", "UTF-8");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
return response.toString();
}
// HTTP POST request
public String sendPost(String Command) throws Exception {
String url = REFERER;
String urlParameters = Command;
//http://stackoverflow.com/questions/23517139/java-lang-classcastexception-com-sun-net-ssl-internal-www-protocol-https-httpsu
URL obj = new URL(null, url, new sun.net.www.protocol.http.Handler());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
con.setUseCaches( false );
con.setInstanceFollowRedirects(true);
//add reqest header
con.setRequestMethod("POST");
con.setRequestProperty("Referer", "http://localhost:8270");
con.setRequestProperty("Accept-Language", "en-US,en");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("charset", "UTF-8");
// Send post request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
//System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
return response.toString();
}
}