Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Experimental implementation of a Servlet that executes some server si…
…de commands via a simple http API.
  • Loading branch information
kdvolder committed Mar 6, 2012
1 parent 369294a commit 94bf4dc
Show file tree
Hide file tree
Showing 20 changed files with 1,301 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bundles/org.eclipse.orion.server.shell/.classpath
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.eclipse.orion.server.shell/.gitignore
@@ -0,0 +1 @@
bin
28 changes: 28 additions & 0 deletions bundles/org.eclipse.orion.server.shell/.project
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.orion.server.shell</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,8 @@
#Thu Feb 09 12:38:30 PST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
@@ -0,0 +1,4 @@
#Thu Feb 09 12:38:30 PST 2012
eclipse.preferences.version=1
pluginProject.extensions=false
resolve.requirebundle=false
24 changes: 24 additions & 0 deletions bundles/org.eclipse.orion.server.shell/META-INF/MANIFEST.MF
@@ -0,0 +1,24 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.orion.server.shell;singleton:=true
Bundle-Version: 0.0.1.qualifier
Bundle-Activator: org.eclipse.orion.server.shell.ShellActivator
Require-Bundle: org.eclipse.orion.server.servlets;bundle-version="0.1.0",
javax.servlet;bundle-version="2.5.0",
org.eclipse.osgi;bundle-version="3.7.0",
org.eclipse.orion.server.core;bundle-version="0.2.0",
org.json;bundle-version="1.0.0",
org.eclipse.core.filesystem;bundle-version="1.3.100",
org.eclipse.equinox.http.registry,
com.jcraft.jsch;bundle-version="0.1.44",
org.eclipse.orion.server.useradmin;bundle-version="0.2.0",
org.eclipse.equinox.common;bundle-version="3.6.0",
org.eclipse.equinox.preferences;bundle-version="3.4.0",
org.eclipse.core.jobs;bundle-version="3.5.100",
org.apache.commons.httpclient;bundle-version="3.1.0",
org.eclipse.equinox.http.servlet;bundle-version="1.1.300"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: %Bundle-Vendor
Bundle-Localization: bundle
7 changes: 7 additions & 0 deletions bundles/org.eclipse.orion.server.shell/build.properties
@@ -0,0 +1,7 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
plugin.xml,\
bundle.properties,\
.
src.includes = bundle.properties
2 changes: 2 additions & 0 deletions bundles/org.eclipse.orion.server.shell/bundle.properties
@@ -0,0 +1,2 @@
Bundle-Vendor = Eclipse.org - Orion
Bundle-Name = Orion Shell Servlet
32 changes: 32 additions & 0 deletions bundles/org.eclipse.orion.server.shell/plugin.xml
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/shellapi"
class="org.eclipse.orion.server.shell.ShellServlet">
</servlet>
</extension>

<!-- <extension
point="org.eclipse.equinox.http.registry.httpcontexts">
<httpcontext
id="org.eclipse.orion.server.git.httpcontext.plugins">
<resource-mapping
bundle="org.eclipse.orion.server.git"
path="/web/plugins">
</resource-mapping>
</httpcontext>
</extension>
<extension
point="org.eclipse.equinox.http.registry.resources">
<resource
alias="/plugins/git"
httpcontextId="org.eclipse.orion.server.git.httpcontext.plugins">
</resource>
<serviceSelector
filter="(other.info=org.eclipse.orion)">
</serviceSelector>
</extension> -->
</plugin>
@@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright (c) 2012 VMWare and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kris De Volder - initial API and implementation
*******************************************************************************/
package org.eclipse.orion.server.shell;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class ShellActivator implements BundleActivator {

@Override
public void start(BundleContext arg0) throws Exception {
}

@Override
public void stop(BundleContext arg0) throws Exception {
}

}
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2012 VMWare and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kris De Volder - initial API and implementation
*******************************************************************************/
package org.eclipse.orion.server.shell;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
//import org.eclipse.jetty.websocket.WebSocket;
//import org.eclipse.jetty.websocket.WebSocketServlet;
import org.eclipse.orion.server.servlets.OrionServlet;
import org.eclipse.orion.server.shell.mvn.ServletMvnHandler;
import org.eclipse.orion.server.shell.npm.ServletNPMHandler;
import org.eclipse.orion.server.shell.process.ServletExternalCommandHandler;
import org.eclipse.orion.server.shell.roo.ServletROOHandler;
import org.eclipse.orion.server.shell.vmc.ServletVMCHandler;

public class ShellServlet extends OrionServlet {

private static final long serialVersionUID = 1L;

public static final String SHELL_URI = "/shellapi"; //$NON-NLS-1$

private Map<String, ServletExternalCommandHandler> commandHandlers = new HashMap<String, ServletExternalCommandHandler>();

public ShellServlet() {
addServlet(new ServletNPMHandler());
addServlet(new ServletVMCHandler());
addServlet(new ServletROOHandler());
addServlet(new ServletMvnHandler());
}

private void addServlet(ServletExternalCommandHandler servletCommandHandler) {
commandHandlers.put(servletCommandHandler.getName(), servletCommandHandler);
}

// @Override
// public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
// return null;
// }

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathInfo = req.getPathInfo();
if (handleRequest(req, resp, pathInfo))
return;
// finally invoke super to return an error for requests we don't know how to handle
super.doGet(req, resp);
}

private boolean handleRequest(HttpServletRequest req, HttpServletResponse resp, String pathInfo) throws ServletException {
try {
IPath path = new Path(pathInfo);
String commandName = path.segment(0); // URI: /<commandName>/...
ServletExternalCommandHandler handler = commandHandlers.get(commandName);
if (handler!=null) {
return handler.handleRequest(req, resp.getOutputStream(), pathInfo);
}
} catch (IOException e) {
throw new ServletException(e);
}
return false;
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

}
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2012 VMWare and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kris De Volder - initial API and implementation
*******************************************************************************/
package org.eclipse.orion.server.shell.mvn;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.eclipse.orion.server.shell.process.ExternalCommand;
import org.eclipse.orion.server.shell.process.ExternalProcess;
import org.eclipse.orion.server.shell.process.ICommandContext;
import org.eclipse.orion.server.shell.process.ServletExternalCommandHandler;
import org.json.JSONException;
import org.json.JSONObject;

public class ServletMvnHandler extends ServletExternalCommandHandler {

protected static final String COMMAND = "mvn";

public static class SimpleMvnCommandHandler extends CommandHandler {
public SimpleMvnCommandHandler(String name, ServletExternalCommandHandler owner) {
super(name, owner);
}

@Override
protected boolean exec(HttpServletRequest request, JSONObject arguments, ICommandContext context, OutputStream out) throws ServletException {
try {
ExternalProcess process = new ExternalProcess(context, createCommand(arguments), out, out);
return true;
} catch (JSONException e) {
throw new ServletException(e);
} catch (IOException e) {
e.printStackTrace();
throw new ServletException(e);
} catch (InterruptedException e) {
throw new ServletException(e);
}
}

private ExternalCommand createCommand(JSONObject arguments) throws JSONException {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add(COMMAND);
cmdLine.add(getName());
return new ExternalCommand(cmdLine.toArray(new String[cmdLine.size()]));
}
}

private static final String[] SIMPLE_COMMANDS = {
"assemble", "build", "compile", "package", "test"
};

public ServletMvnHandler() {
super("mvn");
for (String name : SIMPLE_COMMANDS) {
new SimpleMvnCommandHandler(name, this);
}
}

}
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2012 VMWare and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kris De Volder - initial API and implementation
*******************************************************************************/
package org.eclipse.orion.server.shell.npm;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.eclipse.orion.server.shell.process.ExternalCommand;
import org.eclipse.orion.server.shell.process.ExternalProcess;
import org.eclipse.orion.server.shell.process.ICommandContext;
import org.eclipse.orion.server.shell.process.ServletExternalCommandHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ServletNPMHandler extends ServletExternalCommandHandler {

public ServletNPMHandler() {
super("npm");
new CommandHandler("install", this) {
@Override
protected boolean exec(HttpServletRequest request, JSONObject arguments, ICommandContext context, OutputStream out) throws ServletException {
try {
ExternalProcess process = new ExternalProcess(context, createCommand(arguments), out, out);
return true;
} catch (JSONException e) {
throw new ServletException(e);
} catch (IOException e) {
e.printStackTrace();
throw new ServletException(e);
} catch (InterruptedException e) {
throw new ServletException(e);
}
}

private ExternalCommand createCommand(JSONObject arguments) throws JSONException {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add("npm");
cmdLine.add("install");
if (arguments.getBoolean("force")) {
cmdLine.add("--force");
}
JSONArray packages = arguments.getJSONArray("packages");
int length = packages.length();
for (int i = 0; i < length; i++) {
cmdLine.add(packages.getString(i));
}
return new ExternalCommand(cmdLine.toArray(new String[cmdLine.size()]));
}
};
}

}

0 comments on commit 94bf4dc

Please sign in to comment.