Skip to content
Browse files

1. Add getBatchStatus, endRequests, purgeBatch methods 2. Update getR…

…esponse and sendRequest to support partial send and get 3. Add more test cases to accompany the sample library udpate 4. Format the source code
  • Loading branch information...
1 parent 384ced5 commit a77622149b03a695bd0dc4a8693902bff4081b25 Nelson Wang committed Oct 10, 2011
Showing with 488 additions and 114 deletions.
  1. +333 −109 src/java/SoaWebApiClient/src/SoaWebApiClient.java
  2. +155 −5 src/java/SoaWebApiClient/src/SoaWebApiClientTest.java
View
442 src/java/SoaWebApiClient/src/SoaWebApiClient.java
@@ -1,4 +1,3 @@
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -22,8 +21,8 @@
import javax.net.ssl.X509TrustManager;
public class SoaWebApiClient {
-
- private String headnode;
+
+ private String headnode;
private String brokernode;
private String username;
private String password;
@@ -33,76 +32,87 @@
private int sessionId = -1;
private String userdataSeparator;
private String responseSeparator;
-
- public SoaWebApiClient(String headnode, String servicename, String username, String password) {
+
+ public SoaWebApiClient(String headnode, String servicename,
+ String username, String password) {
this.headnode = headnode;
this.servicename = servicename;
this.username = username;
this.password = password;
-
+
String plainauth = String.format("%s:%s", this.username, this.password);
- this.basicauthinfo = new sun.misc.BASE64Encoder().encode(plainauth.getBytes());
-
+ this.basicauthinfo = new sun.misc.BASE64Encoder().encode(plainauth
+ .getBytes());
+
SSLContext sc;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, getTrustManager(), new java.security.SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- HttpsURLConnection.setDefaultHostnameVerifier(
- new HostnameVerifier() {
- public boolean verify(String urlHostname, javax.net.ssl.SSLSession _session) {
- return true;
- }
- }
- );
+ HttpsURLConnection
+ .setDefaultSSLSocketFactory(sc.getSocketFactory());
+ HttpsURLConnection
+ .setDefaultHostnameVerifier(new HostnameVerifier() {
+ public boolean verify(String urlHostname,
+ javax.net.ssl.SSLSession _session) {
+ return true;
+ }
+ });
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- char[] tmpArrayA = {
- (char)Integer.parseInt("EF", 16),
- (char)Integer.parseInt("BF", 16),
- (char)Integer.parseInt("BF", 16),
- };
- char[] tmpArrayB = { (char)Integer.parseInt("00", 16) };
- userdataSeparator = new String(tmpArrayA);
- responseSeparator = new String(tmpArrayB);
+ char[] tmpArrayA = { (char) Integer.parseInt("EF", 16),
+ (char) Integer.parseInt("BF", 16),
+ (char) Integer.parseInt("BF", 16), };
+ char[] tmpArrayB = { (char) Integer.parseInt("00", 16) };
+ userdataSeparator = new String(tmpArrayA);
+ responseSeparator = new String(tmpArrayB);
-
}
-
- public void createSession() {
+
+ public int createSession() throws Throwable {
URL url = null;
HttpsURLConnection createSession = null;
+ int responseCode = -1;
try {
- url = new URL(String.format("https://%s/SOA/sessions/Create?durable=false", this.headnode));
+ url = new URL(String.format(
+ "https://%s/SOA/sessions/Create?durable=false",
+ this.headnode));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
-
- createSession = (HttpsURLConnection)url.openConnection();
+
+ createSession = (HttpsURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
- }
-
- System.out.println(String.format("Create session...%s", url.toString()));
+ }
+
+ System.out
+ .println(String.format("Create session...%s", url.toString()));
try {
createSession.setRequestMethod("POST");
createSession.setDoOutput(true);
- createSession.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
- createSession.setRequestProperty("CONTENT-TYPE", "application/json; charset=utf-8");
+ createSession.setRequestProperty("Authorization", "Basic "
+ + this.basicauthinfo);
+ createSession.setRequestProperty("CONTENT-TYPE",
+ "application/json; charset=utf-8");
createSession.setRequestProperty("Accept", "application/json");
createSession.setRequestProperty("TransferEncoding", "chunked");
createSession.setReadTimeout(60000);
- createSession.connect();
- OutputStreamWriter wr = new OutputStreamWriter(createSession.getOutputStream());
- String sessionStartInfo = String.format("{\"ServiceName\":\"MyGenericService\",\"Username\":\"%s\",\"Password\":\"%s\",\"MaxMessageSize\":2147483647,\"Runtime\":-1}", this.username.replaceAll("\\\\", "\\\\\\\\"), this.password) ;
+ createSession.connect();
+ OutputStreamWriter wr = new OutputStreamWriter(
+ createSession.getOutputStream());
+ String sessionStartInfo = String
+ .format("{\"ServiceName\":\"%s\",\"Username\":\"%s\",\"Password\":\"%s\",\"MaxMessageSize\":2147483647,\"Runtime\":-1}",
+ this.servicename,
+ this.username.replaceAll("\\\\", "\\\\\\\\"),
+ this.password);
wr.write(sessionStartInfo);
wr.flush();
} catch (ProtocolException e) {
@@ -112,156 +122,370 @@ public void createSession() {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+
try {
char[] buffer = new char[0x10000];
StringBuilder sb = new StringBuilder();
- Reader reader = new InputStreamReader(createSession.getInputStream());
+ Reader reader = new InputStreamReader(
+ createSession.getInputStream());
int len;
do {
len = reader.read(buffer, 0, buffer.length);
- if(len > 0) {
+ if (len > 0) {
sb.append(buffer, 0, len);
- }
- } while( len >= 0);
- System.out.println(String.format("WebSessionInfo %s", sb.toString()));
- Pattern pattern = Pattern.compile("\"Id\":(\\d+),\"Secure\":(.*),\"ServiceOperationTimeout\":(\\d+),\"ServiceVersion\":(.*),\"TransportScheme\":(\\d+),\"UseInprocessBroker\":(.*),\"BrokerNode\":\"(.*)\",\"IsDurable\":(.*)");
+ }
+ } while (len >= 0);
+ System.out
+ .println(String.format("WebSessionInfo %s", sb.toString()));
+ Pattern pattern = Pattern
+ .compile("\"Id\":(\\d+),\"Secure\":(.*),\"ServiceOperationTimeout\":(\\d+),\"ServiceVersion\":(.*),\"TransportScheme\":(\\d+),\"UseInprocessBroker\":(.*),\"BrokerNode\":\"(.*)\",\"IsDurable\":(.*)");
Matcher matcher = pattern.matcher(sb.toString());
- if(matcher.find()) {
+ if (matcher.find()) {
this.sessionId = Integer.parseInt(matcher.group(1));
- this.brokernode = matcher.group(7);
- System.out.println(String.format("brokernode: %s", this.brokernode));
+ this.brokernode = matcher.group(7);
+ System.out.println(String.format("brokernode: %s",
+ this.brokernode));
}
- System.out.println(String.format("Session %d is created successfully!", this.sessionId));
-
+ responseCode = createSession.getResponseCode();
+ System.out.println(String.format(
+ "Session %d is created successfully!", this.sessionId));
+
} catch (IOException e) {
e.printStackTrace();
- }
+ }
+
+ throwIfError(responseCode, "CreateSession failed.");
+
+ return this.sessionId;
}
-
- public void closeSession() {
+
+ public void closeSession() throws Throwable {
URL url = null;
HttpsURLConnection closeSession = null;
+ int responseCode = -1;
try {
- url = new URL(String.format("https://%s/SOA/sessions/%d/Close", this.headnode, this.sessionId));
- closeSession = (HttpsURLConnection)url.openConnection();
+ url = new URL(String.format("https://%s/SOA/sessions/%d/Close",
+ this.headnode, this.sessionId));
+ closeSession = (HttpsURLConnection) url.openConnection();
closeSession.setRequestMethod("GET");
- closeSession.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
- closeSession.setRequestProperty("CONTENT-TYPE", "application/xml; charset=utf-8");
+ closeSession.setRequestProperty("Authorization", "Basic "
+ + this.basicauthinfo);
+ closeSession.setRequestProperty("CONTENT-TYPE",
+ "application/json; charset=utf-8");
closeSession.connect();
- int respCode = closeSession.getResponseCode();
- System.out.println(String.format("Session closed. %d", respCode));
- }catch (MalformedURLException e) {
+ responseCode = closeSession.getResponseCode();
+ System.out.println(String.format("Session closed. %d", responseCode));
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ throwIfError(responseCode, "CloseSession failed.");
+ }
+
+ public void attachSession(int attachSessionId) throws Throwable {
+ URL url = null;
+ HttpsURLConnection attachSession = null;
+ int responseCode = -1;
+ String websessionInfo = null;
+ System.out.println(String.format("Try to attach session %d...", attachSessionId));
+ try {
+ url = new URL(String.format(
+ "https://%s/SOA/sessions/%d/Attach?durable=false",
+ this.headnode, attachSessionId));
+ attachSession = (HttpsURLConnection) url.openConnection();
+ attachSession.setRequestMethod("GET");
+ attachSession.setRequestProperty("Authorization", "Basic "
+ + this.basicauthinfo);
+ attachSession.setRequestProperty("Accept", "application/json");
+ attachSession.setRequestProperty("CONTENT-TYPE",
+ "application/json; charset=utf-8");
+ attachSession.setDoInput(true);
+ attachSession.setReadTimeout(120000);
+
+ InputStreamReader reader = new InputStreamReader(
+ attachSession.getInputStream());
+ StringBuilder sb = new StringBuilder();
+ Reader in = new BufferedReader(reader);
+ int val;
+ while ((val = in.read()) > -1) {
+ sb.append((char) val);
+ }
+ in.close();
+ websessionInfo = sb.toString();
+ System.out.println(websessionInfo);
+ Pattern pattern = Pattern
+ .compile("\"Id\":(\\d+),\"Secure\":(.*),\"ServiceOperationTimeout\":(\\d+),\"ServiceVersion\":(.*),\"TransportScheme\":(\\d+),\"UseInprocessBroker\":(.*),\"BrokerNode\":\"(.*)\",\"IsDurable\":(.*)");
+ Matcher matcher = pattern.matcher(websessionInfo);
+ if (matcher.find()) {
+ this.sessionId = Integer.parseInt(matcher.group(1));
+ this.brokernode = matcher.group(7);
+ System.out.println(String.format("brokernode: %s",
+ this.brokernode));
+ }
+ responseCode = attachSession.getResponseCode();
+
+ System.out.println(String.format("Session %d attached successfully", this.sessionId));
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (ProtocolException e) {
// TODO Auto-generated catch block
- e.printStackTrace();
+ e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
+ throwIfError(responseCode, "AttachSession failed.");
+ }
+ public void sendRequest(String[] requests, String[] userdata) throws Throwable {
+ sendRequest(requests, userdata, this.defaultBatchId);
}
- public void sendRequest(String[] requests, String[] userdata) {
- sendRequest(requests, userdata, this.defaultBatchId);
+ public void sendRequest(String[] requests, String[] userdata, boolean commit) throws Throwable {
+ sendRequest(requests, userdata, commit, this.defaultBatchId);
}
- public void sendRequest(String[] requests, String[] userdata, String batchId) {
+ public void sendRequest(String[] requests, String[] userdata, String batchId) throws Throwable {
+ sendRequest(requests, userdata, true, batchId);
+ }
+
+ public void sendRequest(String[] requests, String[] userdata, boolean commit, String batchId) throws Throwable {
System.out.println("Sending request");
URL url = null;
HttpURLConnection sendRequest = null;
-
+ int responseCode = -1;
try {
- url = new URL(String.format("https://%s/SOA/sessions/%d/batches/%s?genericservice=true", this.brokernode, this.sessionId, batchId));
- System.out.println(url.toString());
+ url = new URL(
+ String.format(
+ "https://%s/SOA/sessions/%d/batches/%s?genericservice=true&commit=%b",
+ this.brokernode, this.sessionId, batchId, commit));
+ System.out.println(url.toString());
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
-
+
try {
- sendRequest = (HttpURLConnection)url.openConnection();
+ sendRequest = (HttpURLConnection) url.openConnection();
sendRequest.setRequestMethod("POST");
- sendRequest.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
- sendRequest.setRequestProperty("CONTENT-TYPE", "application/octet-stream");
+ sendRequest.setRequestProperty("Authorization", "Basic "
+ + this.basicauthinfo);
+ sendRequest.setRequestProperty("CONTENT-TYPE",
+ "application/octet-stream");
sendRequest.setRequestProperty("TransferEncoding", "chunked");
sendRequest.setDoOutput(true);
sendRequest.setReadTimeout(60000);
sendRequest.connect();
- OutputStreamWriter out = new OutputStreamWriter(sendRequest.getOutputStream());
- for(int i = 0; i < requests.length; i++) {
- out.write(requests[i]);
- out.write(userdataSeparator);
- out.write(userdata[i]);
- out.write(responseSeparator);
+ OutputStreamWriter out = new OutputStreamWriter(
+ sendRequest.getOutputStream());
+ for (int i = 0; i < requests.length; i++) {
+ out.write(requests[i]);
+ out.write(userdataSeparator);
+ out.write(userdata[i]);
+ out.write(responseSeparator);
}
out.flush();
-
+
InputStream is = sendRequest.getInputStream();
is.close();
int respCode = sendRequest.getResponseCode();
- System.out.println(String.format("SendRequest complete %d", respCode));
+ System.out.println(String.format("SendRequest complete %d",
+ respCode));
+ responseCode = sendRequest.getResponseCode();
} catch (IOException e1) {
e1.printStackTrace();
}
-
+ throwIfError(responseCode, "SendRequest failed");
}
- public String getResponse() {
+ public String getResponse() throws Throwable {
return getResponse(this.defaultBatchId);
}
+
+ public String getResponse(String batchId) throws Throwable {
+ return getResponse(batchId, "", "", -1, true);
+ }
+
+ public String getResponse(int count, boolean startFromBenginning) throws Throwable {
+ return getResponse(this.defaultBatchId, "", "", count, startFromBenginning);
+ }
- public String getResponse(String batchId) {
+ public String getResponse(String batchId, String action, String clientdata, int count, boolean startFromBenginning) throws Throwable {
URL url = null;
String response = null;
+ int responseCode = -1;
try {
- url = new URL(String.format("https://%s/SOA/sessions/%d/batches/%s?genericservice=true", this.brokernode, this.sessionId, batchId));
+ url = new URL(
+ String.format(
+ "https://%s/SOA/sessions/%d/batches/%s?genericservice=true&action=%s&clientdata=%s&count=%d&reset=%b",
+ this.brokernode, this.sessionId, batchId, action, clientdata, count, startFromBenginning));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
-
+
HttpURLConnection getResponse = null;
try {
- getResponse = (HttpURLConnection)url.openConnection();
+ getResponse = (HttpURLConnection) url.openConnection();
getResponse.setRequestMethod("GET");
- getResponse.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
+ getResponse.setRequestProperty("Authorization", "Basic "
+ + this.basicauthinfo);
getResponse.setDoInput(true);
- getResponse.setReadTimeout(60000);
-
- InputStreamReader reader = new InputStreamReader(getResponse.getInputStream());
+ getResponse.setReadTimeout(120000);
+
+ InputStreamReader reader = new InputStreamReader(
+ getResponse.getInputStream());
StringBuilder sb = new StringBuilder();
Reader in = new BufferedReader(reader);
int val;
- while((val = in.read()) > -1) {
- sb.append((char)val);
+ while ((val = in.read()) > -1) {
+ sb.append((char) val);
}
in.close();
response = sb.toString();
- String[] responses;
- String[] parts;
- responses = response.split(responseSeparator);
- for(int i = 0; i < responses.length - 1; i++) {
- parts = responses[i].split(userdataSeparator);
- System.out.println(String.format("Action: %s Response: %s UserData: %s", parts[0], parts[1], parts[2]));
- }
-
+ String[] responses;
+ String[] parts;
+ responses = response.split(responseSeparator);
+ for (int i = 0; i < responses.length - 1; i++) {
+ parts = responses[i].split(userdataSeparator);
+ System.out.println(String.format(
+ "Action: %s Response: %s UserData: %s", parts[0],
+ parts[1], parts[2]));
+ }
+ responseCode = getResponse.getResponseCode();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
+ throwIfError(responseCode, "GetResponse failed");
return response;
}
+
+ public void endRequests() throws Throwable {
+ endRequests(this.sessionId, this.defaultBatchId);
+ }
+
+ public void endRequests(int sessionId, String batchId) throws Throwable {
+ URL url = null;
+ HttpsURLConnection endRequests = null;
+ int responseCode = -1;
+ System.out.println(String.format("Batch %s end requests...", batchId));
+
+ try {
+ url = new URL(String.format("https://%s/SOA/sessions/%d/batches/%s/Commit", this.headnode, sessionId, batchId));
+ endRequests = (HttpsURLConnection) url.openConnection();
+ endRequests.setRequestMethod("GET");
+ endRequests.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
+ endRequests.connect();
+ responseCode = endRequests.getResponseCode();
+ System.out.println(String.format("Batch %s commited. %d", batchId, responseCode));
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ throwIfError(responseCode, "EndRequest failed");
+ }
+
+ public void purgeBatch() throws Throwable {
+ purgeBatch(this.sessionId, this.defaultBatchId);
+ }
+
+ public void purgeBatch(String batchId) throws Throwable {
+ purgeBatch(this.sessionId, batchId);
+ }
+
+ public void purgeBatch(int sessionId, String batchId) throws Throwable {
+ URL url = null;
+ HttpsURLConnection purgeClient = null;
+ int responseCode = -1;
+ System.out.println(String.format("Purge batch %s ...", batchId));
+ try {
+ url = new URL(String.format("https://%s/SOA/sessions/%d/batches/%s/Purge", this.headnode, sessionId, batchId));
+ purgeClient = (HttpsURLConnection) url.openConnection();
+ purgeClient.setRequestMethod("GET");
+ purgeClient.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
+ purgeClient.connect();
+ responseCode = purgeClient.getResponseCode();
+ System.out.println(String.format("Batch %s purged. %d", batchId, responseCode));
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ throwIfError(responseCode, "GetResponse failed");
+ }
+
+ public String getBatchStatus() throws Throwable {
+ return getBatchStatus(this.sessionId, this.defaultBatchId);
+ }
+
+ public String getBatchStatus(int sessionId, String batchId) throws Throwable {
+ URL url = null;
+ HttpsURLConnection getBatchStatus = null;
+ String batchStatus = null;
+ int responseCode = -1;
+ try {
+ url = new URL(String.format("https://%s/SOA/sessions/%d/batches/%s/Status", this.headnode, sessionId, batchId));
+ getBatchStatus = (HttpsURLConnection) url.openConnection();
+ getBatchStatus.setRequestMethod("GET");
+ getBatchStatus.setRequestProperty("Authorization", "Basic " + this.basicauthinfo);
+ getBatchStatus.setRequestProperty("Accept", "application/json");
+ getBatchStatus.setDoInput(true);
+ getBatchStatus.setReadTimeout(120000);
- private TrustManager[] getTrustManager() {
- TrustManager[] certs = new TrustManager[ ] {
- new X509TrustManager() {
- public X509Certificate[ ] getAcceptedIssuers() { return null; }
- public void checkClientTrusted(X509Certificate[] certs, String t) { }
- public void checkServerTrusted(X509Certificate[] certs, String t) { }
+ InputStreamReader reader = new InputStreamReader(
+ getBatchStatus.getInputStream());
+ StringBuilder sb = new StringBuilder();
+ Reader in = new BufferedReader(reader);
+ int val;
+ while ((val = in.read()) > -1) {
+ sb.append((char) val);
}
- };
- return certs;
- }
-}
+ in.close();
+ batchStatus = sb.toString();
+ responseCode = getBatchStatus.getResponseCode();
+ System.out.println(String.format("Batch %s status: %s", batchId, batchStatus));
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ throwIfError(responseCode, "GetBatchStatus failed");
+ return batchStatus;
+ }
+ public int getSessionId() {
+ return this.sessionId;
+ }
+
+ private TrustManager[] getTrustManager() {
+ TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+ public void checkClientTrusted(X509Certificate[] certs, String t) {
+ }
+
+ public void checkServerTrusted(X509Certificate[] certs, String t) {
+ }
+ } };
+ return certs;
+ }
+
+ private void throwIfError(int responseCode, String errorMsg) throws Throwable {
+ if(responseCode != 200 && responseCode != 201) {
+ throw new Exception(errorMsg);
+ }
+ }
+}
View
160 src/java/SoaWebApiClient/src/SoaWebApiClientTest.java
@@ -1,16 +1,166 @@
+import java.util.ArrayList;
+import java.util.List;
+
public class SoaWebApiClientTest {
+ private static String headnode = "newang1.cloudapp.net";
+ private static String username = "admin";
+ private static String password = "!!123abc";
+ private static String servicename = "CcpEchoSvc";
+
public static void main(String[] args) {
+// runBasicTest();
+ runMultiBatchTest();
+// runPartialSendRequestTest();
+// runPartialSendRequestTest2();
+// runPartialGetResponseTest();
+// runAttachSessionTest();
+ }
+
+ private static void runBasicTest() {
+ String[] requests = { "Request1", "Request2", "Request3" };
+ String[] userdata = { "UserData1", "UserData2", "UserData3" };
+ String response = null;
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ client.createSession();
+ client.sendRequest(requests, userdata);
+ client.getBatchStatus();
+ response = client.getResponse();
+ client.getBatchStatus();
+ client.closeSession();
+ } catch (Throwable e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.out.println("runBasicTest failed");
+ }
+ System.out.println("runBasicTest passed");
+ }
+
+ private static void runPartialSendRequestTest() {
+ String response = null;
+ List<String> requests = new ArrayList<String>();
+ List<String> userdata = new ArrayList<String>();
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ client.createSession();
+ for(int i = 0; i < 10; i++) {
+ requests.clear();
+ userdata.clear();
+ for(int j = 0; j < 10; j++) {
+ requests.add(String.format("Request%d_%d", i, j));
+ userdata.add(String.format("UserData%d_%d", i, j));
+ }
+ client.sendRequest(requests.toArray(new String[0]), userdata.toArray(new String[0]), false);
+ }
+ client.getBatchStatus();
+ client.endRequests();
+ response = client.getResponse();
+ client.getBatchStatus();
+ client.purgeBatch();
+ client.closeSession();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ System.out.println("runPartialSendRequestTest failed");
+ }
+ System.out.println("runPartialSendRequestTest passed");
+ }
+
+ private static void runPartialSendRequestTest2() {
+ String response = null;
+ int partialSendCount = 10;
+ List<String> requests = new ArrayList<String>();
+ List<String> userdata = new ArrayList<String>();
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ client.createSession();
+ for(int i = 0; i < partialSendCount; i++) {
+ requests.clear();
+ userdata.clear();
+ for(int j = 0; j < 10; j++) {
+ requests.add(String.format("Request%d_%d", i, j));
+ userdata.add(String.format("UserData%d_%d", i, j));
+ }
+ client.sendRequest(requests.toArray(new String[0]),
+ userdata.toArray(new String[0]),
+ i == (partialSendCount -1) ? true : false // commit on the last send request
+ );
+ }
+ response = client.getResponse();
+ client.purgeBatch();
+ client.closeSession();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ System.out.println("runPartialSendRequestTest2 failed");
+ }
+ System.out.println("runPartialSendRequestTest2 passed");
+ }
+
+ private static void runMultiBatchTest() {
String[] requests = { "Request1", "Request2", "Request3" };
String[] userdata = { "UserData1", "UserData2", "UserData3" };
String response = null;
- SoaWebApiClient client = new SoaWebApiClient("newang1.cloudapp.net", "MyGenericService", "admin", "!!123abc");
- client.createSession();
- client.sendRequest(requests, userdata);
- response = client.getResponse();
- client.closeSession();
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ client.createSession();
+ client.sendRequest(requests, userdata, "batch1");
+ response = client.getResponse("batch1");
+ client.purgeBatch("batch1");
+ client.sendRequest(requests, userdata, "batch2");
+ response = client.getResponse("batch2");
+ client.purgeBatch("batch2");
+ client.closeSession();
+ } catch (Throwable e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.out.println("runMultiBatchTest failed");
+ }
+ System.out.println("runMultiBatchTest passed");
+ }
+
+ private static void runPartialGetResponseTest() {
+ String response = null;
+ int partialGetResponseCount = 10;
+ int batchSize = 10;
+ List<String> requests = new ArrayList<String>();
+ List<String> userdata = new ArrayList<String>();
+ for(int i = 0; i < 100; i++) {
+ requests.add(String.format("Request%d", i));
+ userdata.add(String.format("UserData%d", i));
+ }
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ client.createSession();
+ client.sendRequest(requests.toArray(new String[0]),
+ userdata.toArray(new String[0]));
+ client.getResponse(batchSize, true);
+ for(int i = 0; i < partialGetResponseCount; i++) {
+ System.out.println(String.format("Get %d batch of response...", i));
+ response = client.getResponse(batchSize, false);
+ }
+ client.purgeBatch();
+ client.closeSession();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ System.out.println("runPartialGetResponseTest failed");
+ }
+ System.out.println("runPartialGetResponseTest passed");
+ }
+
+ private static void runAttachSessionTest() {
+ SoaWebApiClient client = new SoaWebApiClient(headnode, servicename, username, password);
+ SoaWebApiClient client2 = new SoaWebApiClient(headnode, servicename, username, password);
+ try {
+ int sessionId = client.createSession();
+ client2.attachSession(sessionId);
+ client2.closeSession();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ System.out.println("runAttachSessionTest failed");
+ }
+ System.out.println("runAttachSessionTest passed");
}
}

0 comments on commit a776221

Please sign in to comment.
Something went wrong with that request. Please try again.