Skip to content

Commit

Permalink
Merge pull request ios-driver#295 from ios-driver/largedatafix
Browse files Browse the repository at this point in the history
Handle large data responses
  • Loading branch information
mmmulani committed Jan 28, 2015
2 parents 58e8f51 + 3674c93 commit f9f728e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
31 changes: 26 additions & 5 deletions ...java/org/uiautomation/ios/instruments/commandExecutor/CURLIAutomationCommandExecutor.java 100644 → 100755
Expand Up @@ -23,18 +23,18 @@
import org.uiautomation.ios.drivers.RemoteIOSNativeDriver;
import org.uiautomation.ios.servlet.DriverBasedServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.StringWriter;
import java.text.Normalizer;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CURLIAutomationCommandExecutor extends BaseUIAutomationCommandExecutor {

private final BlockingQueue<UIAScriptRequest> requestQueue = new ArrayBlockingQueue<>(1);
Expand Down Expand Up @@ -68,6 +68,10 @@ public static class UIAScriptServlet extends DriverBasedServlet {

private static final Logger log = Logger.getLogger(UIAScriptServlet.class.getName());
private static final long serialVersionUID = 41227429706998662L;
private final int UUID_LENGTH = 36;
private final String PARTIAL_IDENTIFIER = "b94b2b80-4330-11e4-916c-0800200c9a66-PARTIAL";
private final String FINAL_PART_IDENTIFIER = "b94b2b80-4330-11e4-916c-0800200c9a66-FINAL";
private HashMap jsonMap = new HashMap();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
Expand Down Expand Up @@ -112,7 +116,24 @@ private void getResponse(HttpServletRequest request, HttpServletResponse respons
if (request.getInputStream() != null) {
StringWriter writer = new StringWriter();
IOUtils.copy(request.getInputStream(), writer, "UTF-8");
String json = writer.toString();
String json = null;
String sessionUUID = null;
String jsonConcatenated = "";
if (writer.toString().endsWith(PARTIAL_IDENTIFIER)){
sessionUUID = writer.toString().substring(0, UUID_LENGTH);
if (jsonMap.get(sessionUUID) != null){
jsonConcatenated = (String)jsonMap.get(sessionUUID);
}
jsonConcatenated = jsonConcatenated.concat(writer.toString().substring(UUID_LENGTH, writer.toString().length() - PARTIAL_IDENTIFIER.length()));
jsonMap.put(sessionUUID, jsonConcatenated);
return;
}else if (writer.toString().endsWith(FINAL_PART_IDENTIFIER)){
sessionUUID = writer.toString().substring(0, UUID_LENGTH);
json = ((String)jsonMap.get(sessionUUID)).concat(writer.toString().substring(UUID_LENGTH, writer.toString().length() - FINAL_PART_IDENTIFIER.length()));
jsonMap.remove(sessionUUID);
}else{
json = writer.toString();
}
json = Normalizer.normalize(json, LanguageDictionary.form);
UIAScriptResponse r = new UIAScriptResponse(json);
log.fine("content : " + r);
Expand Down
37 changes: 34 additions & 3 deletions server/src/main/resources/instruments-js/UIAutomation.js
Expand Up @@ -104,11 +104,42 @@ var UIAutomation = {
* using eval().
*/
postResponseWithCURLAndGetNextCommand: function (jsonResponse) {
log("posting response : " + jsonResponse);
var nextCommand = this.HOST.performTaskWithPathArgumentsTimeout(this.CURL, [this.COMMAND,
var nextCommand;
var maxPartLength = 200000;
if (jsonResponse.length > maxPartLength){
var sessionId = "sessionId";
var partialIdentifier = "b94b2b80-4330-11e4-916c-0800200c9a66-PARTIAL";
var endIdentifier = "b94b2b80-4330-11e4-916c-0800200c9a66-FINAL"
var uuidLength = 36;
var jsonPart;
var index = 0;
var jsonRemainder = jsonResponse;
var startPos = jsonResponse.indexOf(sessionId) + sessionId.length + 3;
var sessionUUID = jsonResponse.substring(startPos, startPos + uuidLength);

while (jsonRemainder.length > maxPartLength){
jsonPart = sessionUUID + jsonRemainder.substring(0,maxPartLength) + partialIdentifier;
jsonRemainder = jsonRemainder.substring(maxPartLength, Number.MAX_VALUE);
log("posting response : " + jsonPart);
nextCommand = this.HOST.performTaskWithPathArgumentsTimeout(this.CURL, [this.COMMAND,
"--data-binary",
jsonPart],
600);
}
jsonRemainder = sessionUUID + jsonRemainder + endIdentifier
log("posting response : " + jsonRemainder);
nextCommand = this.HOST.performTaskWithPathArgumentsTimeout(this.CURL, [this.COMMAND,
"--data-binary",
jsonRemainder],
600);
}else{
log("posting response : " + jsonResponse);
nextCommand = this.HOST.performTaskWithPathArgumentsTimeout(this.CURL, [this.COMMAND,
"--data-binary",
jsonResponse],
600);
600);
}

if (nextCommand.exitCode != 0) {
throw new UIAutomationException("error getting new command. exit code : " + result
.exitCode);
Expand Down

0 comments on commit f9f728e

Please sign in to comment.