Skip to content

Commit

Permalink
Kick off a hawtio plugin that wraps the karaf gogo terminal emulator …
Browse files Browse the repository at this point in the history
…for #17, doesn't quite work yet
  • Loading branch information
gashcrumb committed Sep 12, 2013
1 parent 4cdad58 commit 1e2b6cb
Show file tree
Hide file tree
Showing 11 changed files with 793 additions and 1 deletion.
155 changes: 155 additions & 0 deletions hawtio-karaf-terminal/pom.xml
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>io.hawt</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>io.hawt</groupId>
<artifactId>hawtio-karaf-terminal</artifactId>
<description>hawtio :: Karaf terminal plugin</description>
<name>${project.artifactId}</name>

<packaging>war</packaging>

<properties>
<!-- filtered plugin properties -->
<plugin-context>/hawtio/hawtio-karaf-terminal</plugin-context>
<plugin-name>${project.artifactId}</plugin-name>
<plugin-domain />
<plugin-scripts>app/js/gogoPlugin.js</plugin-scripts>

<fuse.osgi.import>
javax.servlet.*,
javax.servlet.http.*,
org.apache.karaf.webconsole.gogo,
*;resolution:=optional
</fuse.osgi.import>
<!--
<fuse.osgi.export>
io.hawt.web.plugin
</fuse.osgi.export>
-->
</properties>

<dependencies>

<dependency>
<groupId>io.hawt</groupId>
<artifactId>hawtio-plugin-mbean</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api-version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.karaf.webconsole</groupId>
<artifactId>org.apache.karaf.webconsole.gogo</artifactId>
<version>${karaf-version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.webconsole</artifactId>
<version>${felix-webconsole-version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

<plugins>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven-bundle-plugin-version}</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>${webapp-outdir}/META-INF</manifestLocation>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Webapp-Context>${plugin-context}</Webapp-Context>
<Web-ContextPath>${plugin-context}</Web-ContextPath>

<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>

<Import-Package>
javax.servlet,
javax.servlet.http,
org.apache.karaf.webconsole.gogo,
*;resolution:=optional
</Import-Package>
<DynamicImport-Package>*</DynamicImport-Package>

<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>

<Bundle-Name>${project.description}</Bundle-Name>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Implementation-Title>hawtio karaf terminal plugin</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
</instructions>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${war-plugin-version}</version>
<configuration>
<outputFileNameMapping>@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@</outputFileNameMapping>
<packagingExcludes>**/classes/OSGI-INF/**</packagingExcludes>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifestFile>${webapp-outdir}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

</plugins>
</build>

</project>
@@ -0,0 +1,133 @@
/*
* Copyright 2012 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.hawt.web.plugin.karaf.terminal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
* @author Stan Lewis
* Based on http://balusc.blogspot.com/2007/07/fileservlet.html
*/
public class ResourceServlet extends HttpServlet {

private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

private final static Logger LOG = LoggerFactory.getLogger(ResourceServlet.class);

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

String requested = req.getPathInfo();

if (requested.startsWith("/")) {
requested = requested.substring(1, requested.length());
}


LOG.debug("Requested file : " + requested);

if (requested == null) {
LOG.debug("Requested path is null");
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}

InputStream in = null;
String type = null;

/*
if (Config.getInstance() != null) {
String contentDir = Config.getInstance().getContentDirectory();
if (contentDir != null && !contentDir.equals("")) {
File file = new File(contentDir, URLDecoder.decode(requested, "UTF-8"));
if (!file.exists()) {
LOG.debug("File " + file.getPath() + " does not exist");
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOG.info("Serving file: " + file.getAbsolutePath() + " of type " + type);
in = new FileInputStream(file);
}
}
*/

if (in == null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
LOG.debug("Serving resource: " + requested);
in = classLoader.getResourceAsStream(requested);
}
if (in == null) {
LOG.debug("Resource " + requested + " does not exist");
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}


type = getServletContext().getMimeType(requested);
if (type == null) {
type = "application/octet-stream";
}


resp.reset();
resp.setBufferSize(DEFAULT_BUFFER_SIZE);

resp.setContentType(type);
resp.setHeader("Content-Length", String.valueOf(in.available()));

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
input = new BufferedInputStream(in, DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(resp.getOutputStream(), DEFAULT_BUFFER_SIZE);

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
close(output);
close(input);
close(in);
}
}

private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
LOG.warn("Error closing stream : " + e.getLocalizedMessage());
}
}
}


}
@@ -0,0 +1,40 @@
package io.hawt.web.plugin.karaf.terminal;

import org.apache.felix.service.command.CommandProcessor;
import org.apache.karaf.webconsole.gogo.GogoPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* @author Stan Lewis
*/
public class TerminalServlet extends GogoPlugin {

private final static Logger LOG = LoggerFactory.getLogger(TerminalServlet.class);

@Override
public void setCommandProcessor(CommandProcessor commandProcessor) {
LOG.info("Setting command processor: {}", commandProcessor);
super.setCommandProcessor(commandProcessor);
}

@Override
protected PrintWriter startResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding( "utf-8" ); //$NON-NLS-1$
response.setContentType( "text/html" ); //$NON-NLS-1$

final PrintWriter pw = response.getWriter();
return pw;
}

@Override
protected void renderTopNavigation(HttpServletRequest request, PrintWriter pw) {
return;
}

}
@@ -0,0 +1,34 @@
<!--
~ Copyright 2012 Red Hat, Inc.
~
~ Red Hat licenses this file to you under the Apache License, version
~ 2.0 (the "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
~ implied. See the License for the specific language governing
~ permissions and limitations under the License.
-->

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">

<reference id="commandProcessor" interface="org.apache.felix.service.command.CommandProcessor" />

<bean id="gogoPlugin" class="io.hawt.web.plugin.karaf.terminal.TerminalServlet" init-method="start" destroy-method="stop">
<property name="commandProcessor" ref="commandProcessor" />
<property name="bundleContext" ref="blueprintBundleContext" />
</bean>

<bean id="plugin" class="io.hawt.web.plugin.HawtioPlugin" init-method="init" destroy-method="destroy">
<property name="name" value="${plugin-name}"/>
<property name="context" value="${plugin-context}"/>
<property name="domain" value="${plugin-domain}"/>
<property name="scripts" value="${plugin-scripts}"/>
</bean>

</blueprint>

0 comments on commit 1e2b6cb

Please sign in to comment.