|
From: <ian...@us...> - 2007-07-09 01:57:46
|
Revision: 221
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=221&view=rev
Author: iansmith
Date: 2007-07-08 18:57:48 -0700 (Sun, 08 Jul 2007)
Log Message:
-----------
This time with the new file.
Added Paths:
-----------
spaces/trunk/src/com/ogoglio/client/WebAPIClientWire.java
Added: spaces/trunk/src/com/ogoglio/client/WebAPIClientWire.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPIClientWire.java (rev 0)
+++ spaces/trunk/src/com/ogoglio/client/WebAPIClientWire.java 2007-07-09 01:57:48 UTC (rev 221)
@@ -0,0 +1,58 @@
+package com.ogoglio.client;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.net.URI;
+
+import com.ogoglio.site.AuthServlet;
+
+import nanoxml.XMLElement;
+
+public class WebAPIClientWire {
+ public XMLElement postAuthenticatedXML(URI uri, String body, String authCookie) throws IOException {
+ return sendAuthenticatedXML(uri, body, "POST", authCookie);
+ }
+ private static XMLElement sendAuthenticatedXML(URI uri, String body, String method, String authCookie) throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ connection.setRequestMethod(method);
+ connection.setAllowUserInteraction(false);
+ if (authCookie != null) {
+ connection.setRequestProperty("Cookie", AuthServlet.AUTH_COOKIE + "=" + authCookie);
+ }
+ if ("POST".equals(method)) {
+ connection.setDoOutput(true);
+ connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
+ connection.setRequestProperty("Content-length", Integer.toString(body.length()));
+ OutputStream rawOutStream = connection.getOutputStream();
+ PrintWriter pw = new PrintWriter(rawOutStream);
+ pw.print(body);
+ pw.flush();
+ pw.close();
+ }
+
+ WebAPIUtil.parseSetCookieHeader(connection.getHeaderField("Set-Cookie"));
+
+ if (connection.getResponseCode() != 200) {
+ throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
+ }
+
+ XMLElement data = new XMLElement();
+ data.parseFromReader(new InputStreamReader(connection.getInputStream()));
+ return data;
+ }
+
+ public boolean sendDelete(URI uri, String authCookie) throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ connection.setRequestMethod("DELETE");
+ connection.setAllowUserInteraction(false);
+ if (authCookie != null) {
+ connection.setRequestProperty("Cookie", AuthServlet.AUTH_COOKIE + "=" + authCookie);
+ }
+ connection.connect();
+ return connection.getResponseCode() == 200;
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|