|
From: <ian...@us...> - 2008-02-10 21:55:38
|
Revision: 732
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=732&view=rev
Author: iansmith
Date: 2008-02-10 13:55:34 -0800 (Sun, 10 Feb 2008)
Log Message:
-----------
Added support for getting the list of sims (SimDocuments) or a particular sim.
Added new fields "retired" and "reachable" that more properly indicate what state a sim can be in.
Added tests for most of the API, but not for deleteSim() b/c there is no way to recover the sim at this point and then the rest of the tests would break.
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-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/StatusServlet.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-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClient.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -476,8 +476,8 @@
return results;
}
- public SimDocument[] getSimDocuments() throws IOException {
- XMLElement elem=wire.fetchAuthenticatedXML(descriptor.getSpaceSimURI(), authenticator.getAuthCookie());
+ public SimDocument[] getSimDocuments(boolean serversOnly) throws IOException {
+ XMLElement elem=wire.fetchAuthenticatedXML(descriptor.getSpaceSimURI(serversOnly), authenticator.getAuthCookie());
if (!elem.getName().toLowerCase().equals("list")) {
throw new IOException("Unable to parse the list of sim documents!"+elem);
}
@@ -658,4 +658,8 @@
throw new IOException("Unable to understand result of PUT to "+descriptor.getSpaceSimURI(simID));
}
}
+
+ public boolean deleteSim(long simID) throws IOException {
+ return wire.sendDelete(descriptor.getSpaceSimURI(simID), authenticator.getAuthCookie());
+ }
}
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-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIClientWire.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -25,8 +25,17 @@
private static final String BAD_SUN_VERSION = "1.6"; //don't add a semicolon on the end of this, as we want to match all 1.6x jvms
+ private int connectTimeout=0;
+ private int readTimeout=0;
+
+ private HttpURLConnection openURIWithTimeouts(URI uri) throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ connection.setConnectTimeout(connectTimeout);
+ connection.setReadTimeout(readTimeout);
+ return connection;
+ }
public InputStream performPUT(URI uri, InputStream input, String type, int length, String authCookie) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("PUT");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -44,7 +53,7 @@
}
public InputStream performPOST(URI uri, InputStream body, String type, String authCookie) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -65,7 +74,7 @@
}
public InputStream performPOST(URI uri, String body, String type, String authCookie) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -88,7 +97,7 @@
}
public XMLElement sendAuthenticatedXML(URI uri, String body, String method, String authCookie) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod(method);
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -118,7 +127,7 @@
}
public boolean sendDelete(URI uri, String authCookie) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("DELETE");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -154,7 +163,7 @@
}
}
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -198,7 +207,7 @@
}
private HttpURLConnection prepPutConnection(URI uri, String type, long length, String authCookie) throws IOException, MalformedURLException, ProtocolException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("PUT");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -238,7 +247,7 @@
}
}
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -270,7 +279,7 @@
public long getHeadDate(URI uri, String authCookie, String headerName) {
try {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("HEAD");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -288,7 +297,7 @@
public String getHeadValue(URI uri, String authCookie, String headerName) {
try {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("HEAD");
connection.setAllowUserInteraction(false);
if (authCookie != null) {
@@ -323,7 +332,7 @@
}
public String getAuthCookieViaPost(URI uri, String body, String type) throws IOException {
- HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ HttpURLConnection connection = openURIWithTimeouts(uri);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
connection.setDoOutput(true);
@@ -340,4 +349,11 @@
data.parseFromReader(new InputStreamReader(connection.getInputStream()));
return cookie;
}
+
+ public void setConnectTimeout(int millis) {
+ connectTimeout=millis;
+ }
+ public void setReadTimeout(int millis) {
+ readTimeout=millis;
+ }
}
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-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/WebAPIDescriptor.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -3,8 +3,6 @@
import java.io.IOException;
import java.net.URI;
-import com.ogoglio.message.proto.AsyncProto;
-import com.ogoglio.message.proto.AsyncProtoFactory;
import com.ogoglio.util.ArgumentUtils;
import com.ogoglio.util.WebConstants;
@@ -21,8 +19,13 @@
return WebAPIUtil.appendToURI(serviceURI, "space/");
}
- public URI getSpaceSimURI() {
- return WebAPIUtil.appendToURI(getSpacesURI(), "sim/");
+ public URI getSpaceSimURI(boolean serversOnly) {
+ String suffix="sim/";
+ if (serversOnly) {
+ suffix=suffix+"?serversOnly=true";
+ }
+ return WebAPIUtil.appendToURI(getSpacesURI(), suffix);
+
}
public URI getSpaceSimURI(long id) {
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-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -419,7 +419,9 @@
public void testSimDocumentAndRetirement() throws AuthenticationFailedException, IOException {
WebAPIClient adminWebClient = createAdminWebClient();
- SimDocument[] sims=adminWebClient.getSimDocuments();
+ WebAPIClient stdWebClient = createStandardWebClient();
+
+ SimDocument[] sims=adminWebClient.getSimDocuments(false);
assertNotNull(sims);
assertEquals(1,sims.length);
@@ -433,12 +435,22 @@
simDoc=adminWebClient.getSimDocument(sims[0].getSimID());
assertEquals(simDoc.isReachable(),!origReachable);
+ //check perms and but don't actually do deletion
+ assertFalse(stdWebClient.deleteSim(simDoc.getSimID()));
+
//put back in right state
adminWebClient.setSimReachable(sims[0].getSimID(), true);
adminWebClient.setSimRetired(sims[0].getSimID(), false);
}
+ private WebAPIClient createStandardWebClient() throws IOException, AuthenticationFailedException {
+ WebAPIAuthenticator basicAuthenticator = new WebAPIAuthenticatorFactory().authenticate(wire1, descriptor1, USERNAME1, PASSWORD1);
+ assertNotNull("got null auth cookie", basicAuthenticator.getAuthCookie());
+ WebAPIClient basicWebClient = new WebAPIClient(descriptor1, basicAuthenticator, wire1);
+ return basicWebClient;
+ }
+
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-server/src/main/java/com/ogoglio/site/SpaceServlet.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2008-02-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -55,7 +55,7 @@
import com.ogoglio.xml.SpaceSimulatorDocument;
import com.ogoglio.xml.server.DocumentFactory;
-public class SpaceServlet extends WebappServletBase{
+public class SpaceServlet extends WebappServletBase {
public static final String MESSAGE_PROXY_KEY = "spaceMessageProxy";
@@ -68,7 +68,7 @@
try {
//don't bother running a proxy with no space resources!
if (servletNeeded) {
- messageProxy = new MessageProxy(getSessionFactory(),new WebAPIDescriptor(new URI(baseUrl)));
+ messageProxy = new MessageProxy(getSessionFactory(), new WebAPIDescriptor(new URI(baseUrl)));
config.getServletContext().setAttribute(MESSAGE_PROXY_KEY, messageProxy);
}
} catch (URISyntaxException e) {
@@ -109,9 +109,9 @@
}
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws PersistException, ServletException, IOException {
- String localAddr=request.getLocalAddr();
- if (preferredIPAddr!=null) {
- localAddr=preferredIPAddr;
+ String localAddr = request.getLocalAddr();
+ if (preferredIPAddr != null) {
+ localAddr = preferredIPAddr;
}
ServiceDocument serviceDocument = new ServiceDocument(messageProxy.getUserCount(), messageProxy.getSimCount(), localAddr);
sendStringResponse(serviceDocument.toString(), "text/xml", response);
@@ -188,7 +188,7 @@
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord accountRecord) throws PersistException, IOException {
long bodyDataID = Long.parseLong(pathElements[pathElements.length - 1]);
BodyDataRecord bodyDataRecord = BodyPersistTasks.findBodyDataByID(bodyDataID, getSessionFactory());
- if(bodyDataRecord == null){
+ if (bodyDataRecord == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
@@ -198,12 +198,12 @@
public void doDelete(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord accountRecord) throws PersistException, IOException {
long bodyDataID = Long.parseLong(pathElements[pathElements.length - 1]);
BodyDataRecord bodyDataRecord = BodyPersistTasks.findBodyDataByID(bodyDataID, getSessionFactory());
- if(bodyDataRecord == null){
+ if (bodyDataRecord == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
-
- if(accountRecord == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(accountRecord.getAccountlevel())){
+
+ if (accountRecord == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(accountRecord.getAccountlevel())) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
@@ -796,46 +796,66 @@
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());
+ SimRecord simRecord = SimPersistTasks.findSimByID(simID, getSessionFactory());
if (simRecord == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
- SimDocument doc=DocumentFactory.documentFromRecord(simRecord);
+ 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) {
+ if (simRec == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
-
+
if (!isUserAnAdmin(response, authedAccount)) {
return; //is SC_FORBIDDEN
}
- SimDocument simDoc= new SimDocument(parseXML(request.getInputStream()));
+ SimDocument simDoc = new SimDocument(parseXML(request.getInputStream()));
simRec = SimPersistTasks.updateSim(simID, simDoc, sessionFactory);
- if (simRec== null) {
+ if (simRec == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
sendStringResponse(DocumentFactory.documentFromRecord(simRec).toString(), "text/xml", response);
-
+
}
+
+ public void doDelete(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
+ }
+
+ SimPersistTasks.delete(simRec, sessionFactory);
+ response.setStatus(HttpServletResponse.SC_OK);
+ }
+
private boolean isUserAnAdmin(HttpServletResponse response, AccountRecord authedAccount) {
//check for null is superfluous?
if (authedAccount == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
@@ -845,7 +865,9 @@
return true;
}
}
+
private class BaseSimResource extends AuthenticatedSiteResource {
+ public static final String ONLY_SERVERS = "onlyServers";
public BaseSimResource() {
super("sim", true, getSessionFactory());
@@ -853,34 +875,51 @@
}
public void doGet(HttpServletRequest request, HttpServletResponse response, String[] pathElements, AccountRecord authedAccount) throws PersistException, ServletException, IOException {
+ boolean onlyServers = false;
+
//check for null is superfluous?
if (authedAccount == null || !AccountDocument.ACCOUNT_LEVEL_ADMIN.equals(authedAccount.getAccountlevel())) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return;
}
+ if ("true".equalsIgnoreCase(request.getParameter(ONLY_SERVERS))) {
+ onlyServers = true;
+ }
+
SimRecord[] simRecs = SimPersistTasks.findSims(getSessionFactory());
-
+ Log.debug("Found a list of sims in BaseSimResource, length is " + simRecs.length);
XMLElement list = new XMLElement("list");
for (int i = 0; i < simRecs.length; i++) {
SimDocument simDoc = DocumentFactory.documentFromRecord(simRecs[i]);
- try {
- URI uri = new URI(simDoc.getSimURI().toString() + "space/");
- XMLElement spaceList = new WebAPIClientWire().fetchAuthenticatedXML(uri, null);
- if (spaceList != null) {
- XMLElement[] spaceElements = spaceList.getChildren(SpaceSimulatorDocument.NAME);
- if (spaceElements != null) {
- for (int j = 0; j < spaceElements.length; j++) {
- simDoc.addSpaceSimulatorDocument(new SpaceSimulatorDocument(spaceElements[j]));
+ if ((simDoc.isReachable()) && (!onlyServers)) {
+ try {
+ URI uri = new URI(simDoc.getSimURI().toString() + "space/");
+ Log.debug("About to poll " + uri + " to see about it's space list...");
+ WebAPIClientWire wire = new WebAPIClientWire();
+ wire.setConnectTimeout(2000);
+ wire.setReadTimeout(2000);
+ XMLElement spaceList = wire.fetchAuthenticatedXML(uri, null);
+ if (spaceList != null) {
+ XMLElement[] spaceElements = spaceList.getChildren(SpaceSimulatorDocument.NAME);
+ if (spaceElements != null) {
+ for (int j = 0; j < spaceElements.length; j++) {
+ simDoc.addSpaceSimulatorDocument(new SpaceSimulatorDocument(spaceElements[j]));
+ }
+ } else {
+ Log.error("Got null spaceList: " + uri);
}
}
- } else {
- Log.error("Got null spaceList: " + uri);
+
+ } catch (IOException e) {
+ //probably due to a timeout
+ SimRecord rec = SimPersistTasks.findSimByID(simDoc.getSimID(), getSessionFactory());
+ rec.setReachable(false);
+ SimPersistTasks.update(rec, getSessionFactory());
+ } catch (Exception e) {
+ Log.error("Error trying to reach a sim to get it's list of spaces/users!", e);
}
- } catch (Exception e) {
- e.printStackTrace();
}
-
list.addChild(simDoc.toElement());
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/StatusServlet.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/StatusServlet.java 2008-02-09 15:31:27 UTC (rev 731)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/StatusServlet.java 2008-02-10 21:55:34 UTC (rev 732)
@@ -196,17 +196,17 @@
}
if (request.getParameter("brief")!=null) {
- StatusData sd=computeCurrentStatus();
- addSpaceInfo(sd);
+ StatusData sd=computeCurrentStatus(false);
+ //addSpaceInfo(sd);
StringBuffer buffer=new StringBuffer();
- buffer.append(""+sd.load_1min+","+sd.simCount+","+sd.userCount);
+ buffer.append(""+sd.load_1min);
sendStringResponse(buffer.toString(), "text/plain", response);
response.setStatus(HttpServletResponse.SC_OK);
return;
}
try {
- StatusData data = computeCurrentStatus();
+ StatusData data = computeCurrentStatus(true);
List copy = new ArrayList();
addCurrentStatusToListAndCopy(data, copy);
@@ -240,11 +240,14 @@
}
}
- private StatusData computeCurrentStatus() throws IOException {
+ private StatusData computeCurrentStatus(boolean includeAll) throws IOException {
StatusData data = new StatusData();
+ getLoadAvg(data);
+ if (!includeAll) {
+ return data;
+ }
data.timestamp = System.currentTimeMillis();
- getLoadAvg(data);
getMemoryInfo(data);
getDiskAvailable(data);
getNetworkInfo(data);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|