Support for new instruction format, dependency on Apache commons

This commit is contained in:
Michael Jumper
2011-09-10 19:08:42 -07:00
parent e3260ca3e9
commit 12717e038c
3 changed files with 48 additions and 32 deletions

View File

@@ -43,6 +43,13 @@
<version>1.6.1</version> <version>1.6.1</version>
</dependency> </dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -21,9 +21,13 @@ package net.sourceforge.guacamole.io;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.protocol.GuacamoleInstruction; import net.sourceforge.guacamole.protocol.GuacamoleInstruction;
import net.sourceforge.guacamole.protocol.GuacamoleInstruction.Operation; import net.sourceforge.guacamole.protocol.GuacamoleInstruction.Operation;
import org.apache.commons.lang3.ArrayUtils;
/** /**
* A GuacamoleReader which wraps a standard Java Reader, using that Reader as * A GuacamoleReader which wraps a standard Java Reader, using that Reader as
@@ -128,53 +132,58 @@ public class ReaderGuacamoleReader implements GuacamoleReader {
if (instructionBuffer == null) if (instructionBuffer == null)
return null; return null;
// Locate end-of-opcode and end-of-instruction // Build list of elements
int opcodeEnd = -1; LinkedList<String> elements = new LinkedList<String>();
int instructionEnd = -1; while (instructionStart < instructionBuffer.length) {
for (int i=instructionStart; i<instructionBuffer.length; i++) { // Find end of length
int lengthEnd = ArrayUtils.indexOf(instructionBuffer, '.', instructionStart);
char c = instructionBuffer[i]; // Parse length
int length = Integer.parseInt(new String(
instructionBuffer,
instructionStart,
lengthEnd - instructionStart
));
if (c == ':') // Parse element from just after period
opcodeEnd = i; instructionStart = lengthEnd + 1;
String element = new String(
instructionBuffer,
instructionStart,
length
);
else if (c == ';') { // Append element to list of elements
instructionEnd = i; elements.addLast(element);
// Read terminator after element
instructionStart += length;
char terminator = instructionBuffer[instructionStart];
// Continue reading instructions after terminator
instructionStart++;
// If we've reached the end of the instruction
if (terminator == ';')
break; break;
}
} }
// If no end-of-instruction marker, malformed. // Pull opcode off elements list
if (instructionEnd == -1) String opcode = elements.removeFirst();
throw new GuacamoleException("Malformed instruction.");
// If no end-of-opcode marker, end is end-of-instruction
if (opcodeEnd == -1)
opcodeEnd = instructionEnd;
// Parse opcode
String opcode = new String(instructionBuffer, instructionStart, opcodeEnd - instructionStart);
// Parse args
String[] args;
if (instructionEnd > opcodeEnd)
args = new String(instructionBuffer, opcodeEnd+1, instructionEnd - opcodeEnd - 1).split(",");
else
args = new String[0];
// Create instruction // Create instruction
GuacamoleInstruction instruction = new GuacamoleInstruction( GuacamoleInstruction instruction = new GuacamoleInstruction(
Operation.fromOpcode(opcode), Operation.fromOpcode(opcode),
args elements.toArray(new String[elements.size()])
); );
// Advance instructionBuffer // Detect end of buffer
instructionStart = instructionEnd + 1;
if (instructionStart >= instructionBuffer.length) if (instructionStart >= instructionBuffer.length)
instructionBuffer = null; instructionBuffer = null;
// Return parsed instruction
return instruction; return instruction;
} }

View File

@@ -221,7 +221,7 @@ public abstract class GuacamoleTunnelServlet extends HttpServlet {
tunnel.close(); tunnel.close();
// End-of-instructions marker // End-of-instructions marker
out.write(';'); out.write("0.;");
out.flush(); out.flush();
response.flushBuffer(); response.flushBuffer();