Whitespace

This commit is contained in:
David Hoover
2011-04-22 16:38:52 -07:00
parent 8c9c2a42b0
commit e308ab9bf3
3 changed files with 30 additions and 47 deletions
@@ -21,17 +21,14 @@ import com.samskivert.util.ServiceWaiter;
public class HttpPostUtil public class HttpPostUtil
{ {
/** /**
* Return the results of a form post. Note that the http request takes * Return the results of a form post. Note that the http request takes place on another
* place on another thread, but this thread blocks until the results * thread, but this thread blocks until the results are returned or it times out.
* are returned or it times out.
* *
* @param url from which to make the request. * @param url from which to make the request.
* @param submission the entire submission eg "foo=bar&baz=boo&futz=foo". * @param submission the entire submission eg "foo=bar&baz=boo&futz=foo".
* @param timeout time to wait for the response, in seconds, or -1 * @param timeout time to wait for the response, in seconds, or -1 for forever.
* for forever.
*/ */
public static String httpPost (final URL url, final String submission, public static String httpPost (final URL url, final String submission, int timeout)
int timeout)
throws IOException, ServiceWaiter.TimeoutException throws IOException, ServiceWaiter.TimeoutException
{ {
final ServiceWaiter<String> waiter = new ServiceWaiter<String>( final ServiceWaiter<String> waiter = new ServiceWaiter<String>(
@@ -39,16 +36,13 @@ public class HttpPostUtil
Thread tt = new Thread() { Thread tt = new Thread() {
@Override public void run () { @Override public void run () {
try { try {
HttpURLConnection conn = HttpURLConnection conn = (HttpURLConnection) url.openConnection();
(HttpURLConnection) url.openConnection();
conn.setDoInput(true); conn.setDoInput(true);
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setUseCaches(false); conn.setUseCaches(false);
conn.setRequestProperty( conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
"Content-Type", "application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream( DataOutputStream out = new DataOutputStream(conn.getOutputStream());
conn.getOutputStream());
out.writeBytes(submission); out.writeBytes(submission);
out.flush(); out.flush();
out.close(); out.close();
@@ -6,11 +6,10 @@
package com.samskivert.util; package com.samskivert.util;
/** /**
* A handy base class for issuing server-side service requests and * A handy base class for issuing server-side service requests and awaiting their responses from
* awaiting their responses from within a servlet. * within a servlet.
* *
* <em>Note:</em> You might think that it would be keen to use this * <em>Note:</em> You might think that it would be keen to use this anonymously like so:
* anonymously like so:
* *
* <pre> * <pre>
* ServiceWaiter waiter = new ServiceWaiter() { * ServiceWaiter waiter = new ServiceWaiter() {
@@ -21,24 +20,21 @@ package com.samskivert.util;
* }; * };
* </pre> * </pre>
* *
* But that won't work because public methods in anonymous inner classes * But that won't work because public methods in anonymous inner classes do not have public
* do not have public visibility and so the invocation manager will not be * visibility and so the invocation manager will not be able to reflect your response methods
* able to reflect your response methods when the time comes to deliver * when the time comes to deliver the response. Unfortunately, there appears to be no manner in
* the response. Unfortunately, there appears to be no manner in which to * which to instruct the Java compiler to give public scope to an anonymous inner class rather
* instruct the Java compiler to give public scope to an anonymous inner * than default scope. Sigh.
* class rather than default scope. Sigh.
*/ */
public class ServiceWaiter<T> public class ServiceWaiter<T>
implements ResultListener<T> implements ResultListener<T>
{ {
/** Timeout to specify when you don't want a timeout. Use at your own /** Timeout to specify when you don't want a timeout. Use at your own risk. */
* risk. */
public static final int NO_TIMEOUT = -1; public static final int NO_TIMEOUT = -1;
public static class TimeoutException extends Exception public static class TimeoutException extends Exception
{ {
public TimeoutException () public TimeoutException () {
{
super("Timeout! Pow!"); super("Timeout! Pow!");
} }
} }
@@ -62,8 +58,7 @@ public class ServiceWaiter<T>
} }
/** /**
* Change the timeout being used for this ServiceWaiter after it * Change the timeout being used for this ServiceWaiter after it has been constructed.
* has been constructed.
*/ */
public void setTimeout (int timeout) public void setTimeout (int timeout)
{ {
@@ -80,8 +75,8 @@ public class ServiceWaiter<T>
} }
/** /**
* Marks the request as successful and posts the supplied response * Marks the request as successful and posts the supplied response argument for perusal by the
* argument for perusal by the caller. * caller.
*/ */
public synchronized void postSuccess (T arg) public synchronized void postSuccess (T arg)
{ {
@@ -101,8 +96,7 @@ public class ServiceWaiter<T>
} }
/** /**
* Returns the argument posted by the waiter when the response * Returns the argument posted by the waiter when the response arrived.
* arrived.
*/ */
public T getArgument () public T getArgument ()
{ {
@@ -120,8 +114,7 @@ public class ServiceWaiter<T>
/** /**
* Blocks waiting for the response. * Blocks waiting for the response.
* *
* @return true if a success response was posted, false if a failure * @return true if a success response was posted, false if a failure repsonse was posted.
* repsonse was posted.
*/ */
public boolean waitForResponse () public boolean waitForResponse ()
throws TimeoutException throws TimeoutException
@@ -136,8 +129,7 @@ public class ServiceWaiter<T>
} else { } else {
wait(1000L * _timeout); wait(1000L * _timeout);
} }
// if we get here without some sort of response, then // if we get here without some sort of response, then we've timed out
// we've timed out
if (_success == 0) { if (_success == 0) {
throw new TimeoutException(); throw new TimeoutException();
} }
@@ -164,9 +156,8 @@ public class ServiceWaiter<T>
postFailure(cause); postFailure(cause);
} }
/** Whether or not the response succeeded; positive for success, /** Whether or not the response succeeded; positive for success, negative for failure, zero
* negative for failure, zero means we haven't received the response * means we haven't received the response yet. */
* yet. */
protected int _success = 0; protected int _success = 0;
/** The argument posted by the waiter upon receipt of the response. */ /** The argument posted by the waiter upon receipt of the response. */
@@ -178,8 +169,7 @@ public class ServiceWaiter<T>
/** How many seconds to wait before giving up the ghost. */ /** How many seconds to wait before giving up the ghost. */
protected int _timeout; protected int _timeout;
/** If a response is not received within the specified timeout, an /** If a response is not received within the specified timeout, an exception is thrown which
* exception is thrown which redirects the user to an internal error * redirects the user to an internal error page. */
* page. */
protected static final int DEFAULT_WAITER_TIMEOUT = 30; protected static final int DEFAULT_WAITER_TIMEOUT = 30;
} }
@@ -36,9 +36,8 @@ public class ValueMarshaller
// look up an argument parser for the field type // look up an argument parser for the field type
Parser parser = _parsers.get(type); Parser parser = _parsers.get(type);
if (parser == null) { if (parser == null) {
String errmsg = "Don't know how to convert strings into " + throw new Exception(
"values of type '" + type + "'."; "Don't know how to convert strings into values of type '" + type + "'.");
throw new Exception(errmsg);
} }
return parser.parse(source); return parser.parse(source);
} }
@@ -48,7 +47,7 @@ public class ValueMarshaller
public Object parse (String source) throws Exception; public Object parse (String source) throws Exception;
} }
protected static Map<Class<?>,Parser> _parsers = new HashMap<Class<?>,Parser>(); protected static Map<Class<?>, Parser> _parsers = new HashMap<Class<?>, Parser>();
static { static {
Parser p; Parser p;
// we can parse strings // we can parse strings