Revision: 399
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=399&view=rev
Author: trevorolio
Date: 2007-09-13 14:02:37 -0700 (Thu, 13 Sep 2007)
Log Message:
-----------
Added bodyless (disembodied?) POST to the scripting API.
Modified Paths:
--------------
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/ScriptHTTPRequest.java
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/ScriptHTTPRequest.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/ScriptHTTPRequest.java 2007-09-12 17:43:30 UTC (rev 398)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/ScriptHTTPRequest.java 2007-09-13 21:02:37 UTC (rev 399)
@@ -23,6 +23,7 @@
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context;
@@ -36,7 +37,7 @@
public class ScriptHTTPRequest extends ScriptableObject {
public static final String USER_AGENT = "Ogoglio Script Request v0.1";
-
+
private Callable callback = null; //An event handler for an event that fires at every state change.
private int stateCode = 0; //Returns the state of the object as follows: 0 = uninitialized, 1 = initialized, 2 = sent, 3 = receiving and 4 = completed
@@ -118,6 +119,9 @@
}
public void jsFunction_send(String content) { //Sends the request.
+ if(content != null && "undefined".equals(content)){
+ content = null;
+ }
startRequest(method, url, content);
}
@@ -149,8 +153,8 @@
try {
callback.call(context, this.getParentScope(), (Scriptable) callback, args);
} catch (EcmaError e) {
- if(scriptSpace != null) {
- scriptSpace.jsFunction_log("Error: " + e.getLocalizedMessage());
+ if (scriptSpace != null) {
+ scriptSpace.jsFunction_log("Error: " + e.getLocalizedMessage());
} else {
Log.error("UpdateState error: " + e.getLocalizedMessage());
}
@@ -205,13 +209,23 @@
public void run() {
if ("GET".equals(methodType)) {
method = new GetMethod(url);
- method.setFollowRedirects(false);
+ } else if ("POST".equals(methodType)) {
+ if (content != null && content.length() != 0) {
+ if (scriptSpace != null) {
+ scriptSpace.jsFunction_log("Error: haven't implemented POST with a body (" + content + ") yet. Sorry");
+ }
+ throw new IllegalStateException("Haven't implemented POST with a body (" + content + ") yet.");
+ }
+ method = new PostMethod(url);
} else {
//TODO handle method types other than GET
- Log.warn("Bad method: " + method);
- throw new IllegalStateException("Haven't implemented anything but GET yet. Sorry.");
+ if (scriptSpace != null) {
+ scriptSpace.jsFunction_log("Error: haven't implemented HTTP method " + methodType + " yet. Sorry");
+ }
+ throw new IllegalStateException("Unimplemented ScriptHTTPRequest method: " + method);
}
+ method.setFollowRedirects(false);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
method.getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
try {
@@ -223,10 +237,10 @@
}
} catch (HttpException e) {
httpCode = 404;
- Log.error("Fatal protocol violation (HttpException) ",e);
+ Log.error("Fatal protocol violation (HttpException) ", e);
} catch (IOException e) {
httpCode = 404;
- Log.error("Fatal transport error (IOException) ",e);
+ Log.error("Fatal transport error (IOException) ", e);
} finally {
method.releaseConnection();
stateCode = 4;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|