Skip to content

Commit

Permalink
Adding REST routes from JavaMonitor into wotaskd
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Robert committed Aug 9, 2012
1 parent 5d910ba commit 2120612
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Applications/wotaskd/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<classpathentry kind="src" path="Sources"/>
<classpathentry exported="true" kind="con" path="WOFramework/ERExtensions"/>
<classpathentry exported="true" kind="con" path="WOFramework/JavaMonitorFramework"/>
<classpathentry exported="true" kind="con" path="WOFramework/JavaWOExtensions"/>
<classpathentry exported="true" kind="con" path="WOFramework/ERJars"/>
<classpathentry exported="true" kind="con" path="WOFramework/JavaEOAccess"/>
<classpathentry exported="true" kind="con" path="WOFramework/JavaEOControl"/>
Expand All @@ -13,5 +12,6 @@
<classpathentry exported="true" kind="con" path="WOFramework/JavaXML"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="WOFramework/ERRest"/>
<classpathentry kind="con" path="WOFramework/JavaWOExtensions"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@
import com.webobjects.monitor._private.MObject;
import com.webobjects.monitor._private.MSiteConfig;
import com.webobjects.monitor._private.String_Extensions;
import com.webobjects.monitor.wotaskd.rest.controllers.MApplicationController;
import com.webobjects.monitor.wotaskd.rest.controllers.MHostController;
import com.webobjects.monitor.wotaskd.rest.controllers.MSiteConfigController;

import er.extensions.appserver.ERXApplication;
import er.rest.routes.ERXRoute;
import er.rest.routes.ERXRouteRequestHandler;

public class Application extends ERXApplication {
private LocalMonitor _localMonitor;
Expand Down Expand Up @@ -195,6 +200,16 @@ public Application() {

// Set up multicast listen thread
createRequestListenerThread();

ERXRouteRequestHandler restHandler = new ERXRouteRequestHandler();
restHandler.addDefaultRoutes("MApplication", false, MApplicationController.class);
restHandler.insertRoute(new ERXRoute("MApplication","/mApplications/{name:MApplication}/addInstance", ERXRoute.Method.Get, MApplicationController.class, "addInstance"));
restHandler.insertRoute(new ERXRoute("MApplication","/mApplications/{name:MApplication}/deleteInstance", ERXRoute.Method.Get, MApplicationController.class, "deleteInstance"));
restHandler.addDefaultRoutes("MHost", false, MHostController.class);
restHandler.addDefaultRoutes("MSiteConfig", false, MSiteConfigController.class);
restHandler.insertRoute(new ERXRoute("MSiteConfig","/mSiteConfig", ERXRoute.Method.Put, MSiteConfigController.class, "update"));

ERXRouteRequestHandler.register(restHandler);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.webobjects.monitor.wotaskd.rest.controllers;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOApplication;
import com.webobjects.appserver.WORequest;
import com.webobjects.monitor._private.MSiteConfig;
import com.webobjects.monitor.wotaskd.Application;

import er.rest.routes.ERXDefaultRouteController;

public class JavaMonitorController extends ERXDefaultRouteController {

public JavaMonitorController(WORequest request) {
super(request);
}

protected MSiteConfig siteConfig() {
return application().siteConfig();
}

public Application application() {
return (Application )WOApplication.application();
}

public WOActionResults createAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

public WOActionResults destroyAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

public WOActionResults indexAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

public WOActionResults newAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

public WOActionResults showAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

public WOActionResults updateAction() throws Throwable {
// TODO Auto-generated method stub
return null;
}

protected void checkPassword() throws SecurityException {
String pw = context().request().stringFormValueForKey("pw");
if(!siteConfig().compareStringWithPassword(pw)) {
throw new SecurityException("Invalid password");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.webobjects.monitor.wotaskd.rest.controllers;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WORequest;
import com.webobjects.monitor._private.MApplication;
import com.webobjects.monitor._private.MHost;
import com.webobjects.monitor._private.MInstance;

import er.extensions.eof.ERXKeyFilter;

public class MApplicationController extends JavaMonitorController {

public MApplicationController(WORequest request) {
super(request);
}

public WOActionResults createAction() throws Throwable {
checkPassword();
ERXKeyFilter filter = ERXKeyFilter.filterWithAttributes();
MApplication application = create(filter);
siteConfig().addApplication_W(application);
return response(application, filter);
}

public WOActionResults destroyAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("mApplication");
deleteApplication(application);
return response(application, ERXKeyFilter.filterWithNone());
}

public WOActionResults indexAction() throws Throwable {
checkPassword();
return response(siteConfig().applicationArray(), ERXKeyFilter.filterWithAttributes());
}

public WOActionResults showAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("mApplication");
return response(application, ERXKeyFilter.filterWithAttributes());
}

public WOActionResults updateAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("mApplication");
update(application, ERXKeyFilter.filterWithAttributes());
return response(application, ERXKeyFilter.filterWithAttributes());
}

public WOActionResults addInstanceAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("name");
// Old code. The if statement replaces this code along with the addInstanceOnAllHostsAction() method. kib 20110622
// addInstance(application, (MHost)routeObjectForKey("host"), false);
if (request().stringFormValueForKey("host") != null) {
MHost mHost = siteConfig().hostWithName(request().stringFormValueForKey("host"));
addInstance(application, mHost, false);
} else
addInstance(application, null, true);
return response(application, ERXKeyFilter.filterWithNone());
}

public WOActionResults deleteInstanceAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("name");
deleteInstance(application, Integer.valueOf(request().stringFormValueForKey("id")));
return response(application, ERXKeyFilter.filterWithNone());
}

public WOActionResults addInstanceOnAllHostsAction() throws Throwable {
checkPassword();
MApplication application = (MApplication) routeObjectForKey("name");
addInstance(application, null, true);
return response(application, ERXKeyFilter.filterWithNone());
}

private void addInstance(MApplication application, MHost host, boolean addToAllHosts) {
try {
if (addToAllHosts) {
for (MHost aHost : siteConfig().hostArray()) {
siteConfig().addInstances_M(aHost, application, 1);
}
} else {
siteConfig().addInstances_M(host, application, 1);
}
} finally {
}
}

private void deleteInstance(MApplication application, Integer instanceId) {
final MInstance instance = application.instanceWithID(instanceId);
try {
siteConfig().removeInstance_M(instance);
} finally {
}
}

private void deleteApplication(MApplication application) {
try {
siteConfig().removeApplication_M(application);
} finally {
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.webobjects.monitor.wotaskd.rest.controllers;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WORequest;
import com.webobjects.monitor._private.MHost;

import er.extensions.eof.ERXKeyFilter;

public class MHostController extends JavaMonitorController {

public MHostController(WORequest request) {
super(request);
}

public WOActionResults createAction() throws Throwable {
checkPassword();
MHost host = create(ERXKeyFilter.filterWithAttributes());
siteConfig().addHost_M(host);
return response(host, ERXKeyFilter.filterWithAttributes());
}

public WOActionResults indexAction() throws Throwable {
checkPassword();
return response(siteConfig().hostArray(), ERXKeyFilter.filterWithAttributes());
}

public WOActionResults showAction() throws Throwable {
checkPassword();
MHost host = siteConfig().hostWithName((String) routeObjectForKey("mHost"));
return response(host, ERXKeyFilter.filterWithAttributes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.webobjects.monitor.wotaskd.rest.controllers;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WORequest;
import com.webobjects.foundation.NSMutableDictionary;
import com.webobjects.monitor._private.MSiteConfig;

import er.extensions.eof.ERXKeyFilter;
import er.extensions.foundation.ERXStringUtilities;

public class MSiteConfigController extends JavaMonitorController {

public MSiteConfigController(WORequest request) {
super(request);
}

public WOActionResults updateAction() throws Throwable {
checkPassword();
if (siteConfig().hostArray().count() == 0) {
throw new IllegalStateException("You cannot update the SiteConfig before adding a host.");
}
MSiteConfig siteConfig = (MSiteConfig) object(ERXKeyFilter.filterWithAttributes());
update(siteConfig, ERXKeyFilter.filterWithAttributes());
pushValues(siteConfig);
return response(siteConfig, ERXKeyFilter.filterWithAttributes());
}

private void pushValues(MSiteConfig newSiteConfig) {
// Grab the new and current hashed passwords. Any new password coming in has already been hashed
// and if we don't have a new password we need the old hashed one to put back into the SiteConfig
// once we've blatted the values with the new incoming values.
String newHashedPassword = newSiteConfig.password();
String currentHashedPassword = siteConfig().password();

if (!ERXStringUtilities.stringIsNullOrEmpty(newHashedPassword)) {
// This is needed to populate the passwordDictionary in the request posted to wotaskd.
siteConfig()._setOldPassword();
}

// Now we've cached the new value remove it from the newSiteConfig.
newSiteConfig.values().removeObjectForKey("password");

// Build a dictionary of new values. Because we might only be updating a few values (and not the whole
// SiteConfig) we'll start with all the current values, less the password which we've already cached.
NSMutableDictionary newValues = siteConfig().values();
newValues.removeObjectForKey("password");

// Overwrite and/or add the new incoming values.
newValues.addEntriesFromDictionary(newSiteConfig.values());

// Push the complete set of new values into the current SiteConfig object.
siteConfig().updateValues(newValues);

// OK, let's check what needs to be done with the password. If we've got a new one set that, otherwise
// if we've got an old one put that back into the SiteConfig.
if (!ERXStringUtilities.stringIsNullOrEmpty(newHashedPassword)) {
siteConfig().values().takeValueForKey(newHashedPassword, "password");
} else if (!ERXStringUtilities.stringIsNullOrEmpty(currentHashedPassword)) {
siteConfig().values().takeValueForKey(currentHashedPassword, "password");
}

if (!ERXStringUtilities.stringIsNullOrEmpty(newHashedPassword)) {
siteConfig()._resetOldPassword();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.webobjects.monitor.wotaskd.rest.delegates;

import com.webobjects.appserver.WOApplication;
import com.webobjects.monitor._private.MSiteConfig;
import com.webobjects.monitor.wotaskd.Application;

import er.rest.ERXAbstractRestDelegate;

public abstract class JavaMonitorRestDelegate extends ERXAbstractRestDelegate {

protected MSiteConfig siteConfig() {
return application().siteConfig();
}

public Application application() {
return (Application )WOApplication.application();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.webobjects.monitor.wotaskd.rest.delegates;

import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.foundation.NSArray;
import com.webobjects.monitor._private.MApplication;

import er.extensions.eof.ERXQ;
import er.rest.ERXRestContext;

public class MApplicationRestDelegate extends JavaMonitorRestDelegate {
public Object primaryKeyForObject(Object obj, ERXRestContext context) {
NSArray<MApplication> objects = ERXQ.filtered(siteConfig().applicationArray(), ERXQ.is("name", obj));
return objects.size() == 0 ? null : objects.objectAtIndex(0);
}

public Object createObjectOfEntityWithID(EOClassDescription entity, Object id, ERXRestContext context) {
return new MApplication((String)id, siteConfig());
}

public Object objectOfEntityWithID(EOClassDescription entity, Object id, ERXRestContext context) {
return (siteConfig().applicationWithName((String)id));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.webobjects.monitor.wotaskd.rest.delegates;

import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.foundation.NSArray;
import com.webobjects.monitor._private.MHost;

import er.extensions.eof.ERXQ;
import er.rest.ERXRestContext;

public class MHostRestDelegate extends JavaMonitorRestDelegate {
public Object createObjectOfEntityWithID(EOClassDescription entity, Object id, ERXRestContext context) {
return new MHost(siteConfig(), (String)id, MHost.MAC_HOST_TYPE);
}

public Object objectOfEntityWithID(EOClassDescription entity, Object id, ERXRestContext context) {
return (siteConfig().hostWithName((String)id));
}

public Object primaryKeyForObject(Object obj, ERXRestContext context) {
NSArray<MHost> objects = ERXQ.filtered(siteConfig().hostArray(), ERXQ.is("name", obj));
return objects.size() == 0 ? null : objects.objectAtIndex(0);
}

}
Loading

0 comments on commit 2120612

Please sign in to comment.