import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class EclDirectExample {
public static String execute(String clusterAddress,
String clusterName,
String eclCode) {
try {
String urlString = "http://" + clusterAddress + ":8008/EclDirect/RunEcl";
// Construct data
String data = URLEncoder.encode("eclText", "UTF-8")
+ "=" + URLEncoder.encode(eclCode, "UTF-8")
+ "&" + URLEncoder.encode("cluster", "UTF-8")
+ "=" + URLEncoder.encode(clusterName, "UTF-8");
// Send data
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
return convertInputStreamToString(conn.getInputStream());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String convertInputStreamToString(InputStream ists)
throws IOException {
if (ists != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader r1 = new BufferedReader(
new InputStreamReader(ists, "UTF-8"));
while ((line = r1.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
ists.close();
}
return sb.toString();
} else {
return "";
}
}
public static void main(String args[]) {
String eclCode = "OUTPUT('Hello World');";
System.out.println(
execute("192.168.59.129", "thor", eclCode));
}
}