|
From: <ian...@us...> - 2008-02-08 20:27:03
|
Revision: 729
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=729&view=rev
Author: iansmith
Date: 2008-02-08 12:27:01 -0800 (Fri, 08 Feb 2008)
Log Message:
-----------
Added support for messing with SimRecords/SimDocuments via the API. You can now programmatically set sims to not reachable or retired.
Modified Paths:
--------------
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClient.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClientWire.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIDescriptor.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/xml/SimDocument.java
maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java
maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/WebAPITest.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/persist/SimPersistTasks.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClient.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClient.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClient.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -37,6 +37,7 @@
import com.ogoglio.xml.PossessionDocument;
import com.ogoglio.xml.ServiceStateDocument;
import com.ogoglio.xml.SettingDocument;
+import com.ogoglio.xml.SimDocument;
import com.ogoglio.xml.SpaceDocument;
import com.ogoglio.xml.SpaceMemberDocument;
import com.ogoglio.xml.TemplateDocument;
@@ -475,6 +476,19 @@
return results;
}
+ public SimDocument[] getSimDocuments() throws IOException {
+ XMLElement elem=wire.fetchAuthenticatedXML(descriptor.getSpaceSimURI(), authenticator.getAuthCookie());
+ if (!elem.getName().toLowerCase().equals("list")) {
+ throw new IOException("Unable to parse the list of sim documents!"+elem);
+ }
+ XMLElement[] children=elem.getChildren("sim");
+ SimDocument[] result=new SimDocument[children.length];
+ for (int i=0; i<result.length;++i) {
+ result[i]=new SimDocument(children[i]);
+ }
+ return result;
+ }
+
public String getSpaceSetting(long spaceID, String key) {
try {
InputStream stream = wire.performGET(descriptor.getSettingURI(spaceID, key), authenticator.getAuthCookie(), false);
@@ -607,4 +621,41 @@
public void deleteBodyTexture(String username, long bodyConfigurationID) throws IOException {
wire.sendDelete(descriptor.getBodyTextureURI(username, bodyConfigurationID), authenticator.getAuthCookie());
}
+
+ public SimDocument getSimDocument(long simID) throws IOException {
+ XMLElement elem=wire.fetchAuthenticatedXML(descriptor.getSpaceSimURI(simID), authenticator.getAuthCookie());
+ return new SimDocument(elem);
+ }
+
+ public void setSimRetired(long simID, boolean b) throws IOException {
+ SimDocument doc=getSimDocument(simID); //b/c we don't actually know what state it's in
+ if (doc.isRetired()==b) {
+ return;
+ }
+ doc.setRetired(b);
+ SimDocument after=putAndReturnSimDocResult(simID, doc);
+ if (after==null || (!after.isRetired()==b)) {
+ throw new IOException("Unable to understand result of PUT to "+descriptor.getSpaceSimURI(simID));
+ }
+ }
+
+ public SimDocument putAndReturnSimDocResult(long simID, SimDocument doc) throws IOException {
+ InputStream str=wire.performPUT(descriptor.getSpaceSimURI(simID), doc.toString(), "text/xml", authenticator.getAuthCookie());
+ String result=StreamUtils.readInput(str);
+ //so we can test that something sensible happened
+ XMLElement root=XMLElement.parseElementFromString(result);
+ return new SimDocument(root);
+ }
+
+ public void setSimReachable(long simID, boolean b) throws IOException {
+ SimDocument doc=getSimDocument(simID); //b/c we don't actually know what state it's in
+ if (doc.isReachable()==b) {
+ return;
+ }
+ doc.setReachable(b);
+ SimDocument after=putAndReturnSimDocResult(simID, doc);
+ if (after==null || (!after.isReachable()==b)) {
+ throw new IOException("Unable to understand result of PUT to "+descriptor.getSpaceSimURI(simID));
+ }
+ }
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClientWire.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClientWire.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClientWire.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -6,6 +6,8 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
@@ -38,11 +40,7 @@
}
StreamUtils.write(input, connection.getOutputStream());
- if (connection.getResponseCode() != 200) {
- throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
- }
-
- return connection.getInputStream();
+ return checkResponseCodeAndReturnStream(uri, connection);
}
public InputStream performPOST(URI uri, InputStream body, String type, String authCookie) throws IOException {
@@ -63,11 +61,7 @@
StreamUtils.write(body, rawOutStream);
}
- if (connection.getResponseCode() != 200) {
- throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
- }
-
- return connection.getInputStream();
+ return checkResponseCodeAndReturnStream(uri, connection);
}
public InputStream performPOST(URI uri, String body, String type, String authCookie) throws IOException {
@@ -90,11 +84,7 @@
pw.close();
}
- if (connection.getResponseCode() != 200) {
- throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
- }
-
- return connection.getInputStream();
+ return checkResponseCodeAndReturnStream(uri, connection);
}
public XMLElement sendAuthenticatedXML(URI uri, String body, String method, String authCookie) throws IOException {
@@ -189,6 +179,25 @@
}
public InputStream performPUT(URI uri, InputStream input, String type, long length, String authCookie) throws IOException {
+ HttpURLConnection connection = prepPutConnection(uri, type, length, authCookie);
+ StreamUtils.write(input, connection.getOutputStream());
+ return checkResponseCodeAndReturnStream(uri, connection);
+ }
+
+ private InputStream checkResponseCodeAndReturnStream(URI uri, HttpURLConnection connection) throws IOException {
+ if (connection.getResponseCode() != 200) {
+ throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
+ }
+
+ return connection.getInputStream();
+ }
+ public InputStream performPUT(URI uri, String data, String type, String authCookie) throws IOException {
+ HttpURLConnection connection=prepPutConnection(uri, type, data.length(), authCookie);
+ StreamUtils.write(data, connection.getOutputStream());
+ return checkResponseCodeAndReturnStream(uri, connection);
+ }
+
+ private HttpURLConnection prepPutConnection(URI uri, String type, long length, String authCookie) throws IOException, MalformedURLException, ProtocolException {
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.setRequestMethod("PUT");
connection.setAllowUserInteraction(false);
@@ -201,13 +210,7 @@
if (length >= 0) {
connection.setRequestProperty("Content-length", Long.toString(length));
}
- StreamUtils.write(input, connection.getOutputStream());
-
- if (connection.getResponseCode() != 200) {
- throw new IOException("Get status " + connection.getResponseCode() + " from " + uri);
- }
-
- return connection.getInputStream();
+ return connection;
}
public InputStream fetchAuthenticatedStream(URI uri, String authCookie) throws IOException {
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIDescriptor.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIDescriptor.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIDescriptor.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -21,6 +21,14 @@
return WebAPIUtil.appendToURI(serviceURI, "space/");
}
+ public URI getSpaceSimURI() {
+ return WebAPIUtil.appendToURI(getSpacesURI(), "sim/");
+ }
+
+ public URI getSpaceSimURI(long id) {
+ return WebAPIUtil.appendToURI(getSpacesURI(), "sim/"+id);
+ }
+
public URI getSpaceURI(long spaceID) {
return WebAPIUtil.appendToURI(serviceURI, "space/" + spaceID);
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/xml/SimDocument.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/xml/SimDocument.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/xml/SimDocument.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -80,10 +80,15 @@
public boolean isReachable() {
return data.getBooleanAttribute(REACHABLE, false);
}
-
+ public void setReachable(boolean b) {
+ data.setAttribute(REACHABLE, b);
+ }
public boolean isRetired() {
return data.getBooleanAttribute(RETIRED, false);
}
+ public void setRetired(boolean b) {
+ data.setAttribute(RETIRED, b);
+ }
public URI getSimURI() {
String uri = data.getStringAttribute(SIM_URI);
Modified: maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java
===================================================================
--- maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -72,6 +72,7 @@
import com.ogoglio.xml.PossessionDocument;
import com.ogoglio.xml.ServiceStateDocument;
import com.ogoglio.xml.ShapeDocument;
+import com.ogoglio.xml.SimDocument;
import com.ogoglio.xml.SpaceDocument;
import com.ogoglio.xml.SpaceMemberDocument;
import com.ogoglio.xml.TemplateDocument;
@@ -172,16 +173,7 @@
}
public void testWebAdmin() throws AuthenticationFailedException, IOException {
- PropStorage ps = new PropStorage();
- if (ps.loadPropertySet(PropStorage.BOOTSTRAP_PROPS) == false) {
- fail("unable to load properties bootstrap.properties!");
- }
-
- WebAPIAuthenticator adminAuthenticator = new WebAPIAuthenticatorFactory().authenticate(wire1, descriptor1, ps.getKeyFromSet(PropStorage.BOOTSTRAP_PROPS, "bootstrapUser"), ps.getKeyFromSet(PropStorage.BOOTSTRAP_PROPS, "bootstrapUserPW"));
-
- assertNotNull("got null auth cookie", adminAuthenticator.getAuthCookie());
-
- WebAPIClient adminWebClient = new WebAPIClient(descriptor1, adminAuthenticator, wire1);
+ WebAPIClient adminWebClient = createAdminWebClient();
adminWebClient.createAccount(USERNAME1, AccountDocument.ACCOUNT_LEVEL_ADVANCED, "Susan", "Advanced", "http://example.com/susan/", "su...@ex...", PASSWORD1);
adminWebClient.createAccount(USERNAME2, AccountDocument.ACCOUNT_LEVEL_BASIC, "Tina", "Basic", "http://example.com/tina/", "ti...@ex...", PASSWORD1);
@@ -295,6 +287,20 @@
assertNull(basicWebClient.getBodyTexture(basicAuthenticator.getUsername(), configDoc.getBodyConfigurationID()));
}
+ private WebAPIClient createAdminWebClient() throws AuthenticationFailedException, IOException {
+ PropStorage ps = new PropStorage();
+ if (ps.loadPropertySet(PropStorage.BOOTSTRAP_PROPS) == false) {
+ fail("unable to load properties bootstrap.properties!");
+ }
+
+ WebAPIAuthenticator adminAuthenticator = new WebAPIAuthenticatorFactory().authenticate(wire1, descriptor1, ps.getKeyFromSet(PropStorage.BOOTSTRAP_PROPS, "bootstrapUser"), ps.getKeyFromSet(PropStorage.BOOTSTRAP_PROPS, "bootstrapUserPW"));
+
+ assertNotNull("got null auth cookie", adminAuthenticator.getAuthCookie());
+
+ WebAPIClient adminWebClient = new WebAPIClient(descriptor1, adminAuthenticator, wire1);
+ return adminWebClient;
+ }
+
private String getLastEmailValidationURL(String email) throws IOException {
File[] mailFiles = mailDirectory.listFiles(new FilenameFilter() {
@@ -411,6 +417,28 @@
}
+ public void testSimDocumentAndRetirement() throws AuthenticationFailedException, IOException {
+ WebAPIClient adminWebClient = createAdminWebClient();
+ SimDocument[] sims=adminWebClient.getSimDocuments();
+ assertNotNull(sims);
+ assertEquals(1,sims.length);
+
+ SimDocument simDoc= adminWebClient.getSimDocument(sims[0].getSimID());
+ boolean origRetired=simDoc.isRetired();
+ boolean origReachable = simDoc.isReachable();
+ adminWebClient.setSimRetired(simDoc.getSimID(),!origRetired);
+ simDoc=adminWebClient.getSimDocument(sims[0].getSimID());
+ assertEquals(simDoc.isRetired(),!origRetired);
+ adminWebClient.setSimReachable(simDoc.getSimID(),!origReachable);
+ simDoc=adminWebClient.getSimDocument(sims[0].getSimID());
+ assertEquals(simDoc.isReachable(),!origReachable);
+
+ //put back in right state
+ adminWebClient.setSimReachable(sims[0].getSimID(), true);
+ adminWebClient.setSimRetired(sims[0].getSimID(), false);
+ }
+
+
private void checkAttachments(SpaceDocument spaceDocument, ThingDocument[] thingDocs, SpaceClient spaceClient, WebAPIClient advancedClient) throws IOException {
TemplateDocument templateDoc = advancedClient.createTemplate("Attachment Test");
assertNotNull(templateDoc);
Modified: maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/WebAPITest.java
===================================================================
--- maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/WebAPITest.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/WebAPITest.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -56,7 +56,6 @@
public void tearDown() {
}
-
public void testWebAPIGuestAuthenticator() {
mockAuthFactory.authenticate(descriptor, GUEST_COOKIE);
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/persist/SimPersistTasks.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/persist/SimPersistTasks.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/persist/SimPersistTasks.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -21,6 +21,7 @@
import com.ogoglio.appdev.persist.HibernateTask;
import com.ogoglio.appdev.persist.PersistException;
+import com.ogoglio.xml.SimDocument;
public class SimPersistTasks {
@@ -79,6 +80,39 @@
task.setSessionFactory(sessionFactory);
task.execute();
}
+
+ public static SimRecord updateSim(final long simID, final SimDocument doc, SessionFactory sessionFactory) throws PersistException {
+ HibernateTask task = new HibernateTask() {
+ public Object run(Session hibernateSession) {
+ Query query = hibernateSession.getNamedQuery(SIM_BY_ID);
+ query.setLong("simID", simID);
+ SimRecord record = (SimRecord) query.uniqueResult();
+ if (record == null) {
+ return null;
+ }
+ //you can't move sims around
+ if (doc.getSimURI()!= null && !doc.getSimURI().equals(record.getSimURI())) {
+ return null;
+ }
+ boolean dirty=false, reachable=record.isReachable(), retired=record.isRetired();
+ if (doc.isReachable()!=reachable) {
+ dirty=true;
+ record.setReachable(doc.isReachable());
+ }
+ if (doc.isRetired()!=retired) {
+ dirty=true;
+ record.setRetired(doc.isRetired());
+ }
+ if (dirty) {
+ hibernateSession.update(record);
+ }
+ return record;
+ }
+ };
+ task.setSessionFactory(sessionFactory);
+ return (SimRecord) task.execute();
+ }
+
public static void delete(final SimRecord simRecord, SessionFactory sessionFactory) throws PersistException {
HibernateTask task = new HibernateTask() {
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2008-02-08 18:28:09 UTC (rev 728)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2008-02-08 20:27:01 UTC (rev 729)
@@ -102,7 +102,7 @@
private class BaseSpaceResource extends AuthenticatedSiteResource {
public BaseSpaceResource() {
super("space", false, getSessionFactory());
- addSubResource(new SimResource());
+ addSubResource(new BaseSimResource());
addSubResource(new StateResource());
addSubResource(new SpaceResource());
addSubResource(new BodiesResource());
@@ -796,14 +796,64 @@
sendStringResponse(result.toString(), "text/xml", response);
}
}
-
private class SimResource extends AuthenticatedSiteResource {
+ public SimResource() {
+ super(SiteResource.LONG_ELEMENT, true, getSessionFactory());
+ }
+ public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws PersistException, ServletException, IOException {
+ if (!isUserAnAdmin(response, authedAccount)) {
+ return; //is SC_FORBIDDEN
+ }
+
+ long simID = Long.parseLong(pathElements[pathElements.length - 1]);
+ SimRecord simRecord= SimPersistTasks.findSimByID(simID, getSessionFactory());
+ if (simRecord == null) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+ SimDocument doc=DocumentFactory.documentFromRecord(simRecord);
+ sendStringResponse(doc.toString(), "text/xml", response);
+ return;
+ }
+ public void doPut(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws PersistException, ServletException, IOException {
+ long simID = Long.parseLong(pathElements[pathElements.length - 1]);
+ SimRecord simRec = SimPersistTasks.findSimByID(simID, getSessionFactory());
+ if (simRec== null) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ if (!isUserAnAdmin(response, authedAccount)) {
+ return; //is SC_FORBIDDEN
+ }
- public SimResource() {
+ SimDocument simDoc= new SimDocument(parseXML(request.getInputStream()));
+ simRec = SimPersistTasks.updateSim(simID, simDoc, sessionFactory);
+ if (simRec== null) {
+ response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ sendStringResponse(DocumentFactory.documentFromRecord(simRec).toString(), "text/xml", response);
+
+ }
+ private boolean isUserAnAdmin(HttpServletResponse response, AccountRecord authedAccount) {
+ //check for null is superfluous?
+ if (authedAccount == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
+ response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+ return false;
+ }
+ return true;
+ }
+ }
+ private class BaseSimResource extends AuthenticatedSiteResource {
+
+ public BaseSimResource() {
super("sim", true, getSessionFactory());
+ addSubResource(new SimResource());
}
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws PersistException, ServletException, IOException {
+ //check for null is superfluous?
if (authedAccount == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|