Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Dec 5, 2011
0 parents commit 2837f6a
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/data
/work
/logs
/.idea
/target
.DS_Store
*.iml
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Servlet Transport for ElasticSearch
==================================

The wares transport plugin allows to use the REST interface over servlets.

In order to install the plugin, simply run: `bin/plugin -install elasticsearch/elasticsearch-transport-wares/1.0.0`. (though, it is a bit meaningless as it is aimed to be embedded within a servlet container)

---------------------------------------
| memcached Plugin | ElasticSearch |
---------------------------------------
| master | 0.18 -> master |
---------------------------------------
| 1.0.0 | 0.18 -> master |
---------------------------------------

133 changes: 133 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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">
<name>elasticsearch-transport-wares</name>
<modelVersion>4.0.0</modelVersion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-transport-wares</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<description>Servlet transport for ElasticSearch</description>
<inceptionYear>2009</inceptionYear>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:[email protected]:elasticsearch/elasticsearch-transport-wares.git</connection>
<developerConnection>scm:git:[email protected]:elasticsearch/elasticsearch-transport-wares.git
</developerConnection>
<url>http://github.com/elasticsearch/elasticsearch-transport-wares</url>
</scm>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>

<properties>
<elasticsearch.version>0.18.5</elasticsearch.version>
</properties>

<repositories>
<repository>
<id>spy</id>
<name>Spy Repository</name>
<url>http://files.couchbase.com/maven2/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
18 changes: 18 additions & 0 deletions src/main/assemblies/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<assembly>
<id></id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<excludes>
<exclude>org.elasticsearch:elasticsearch</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
167 changes: 167 additions & 0 deletions src/main/java/org/elasticsearch/wares/NodeServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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 org.elasticsearch.wares;

import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.support.RestUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;

/**
* A servlet that can be used to dispatch requests to elasticsearch. A {@link Node} will be started, reading
* config from either <tt>/WEB-INF/elasticsearch.json</tt> or <tt>/WEB-INF/elasticsearch.yml</tt> but, by defualt,
* with its internal HTTP interface disabled.
*
* <p>The node is registered as a servlet context attribute under <tt>elasticsearchNode</tt> so its easily
* accessible from other web resources if needed.
*
* <p>The servlet can be registered under a prefix URI, and it will automatically adjust to handle it.
*/
public class NodeServlet extends HttpServlet {

public static String NODE_KEY = "elasticsearchNode";

private Node node;

private RestController restController;

@Override public void init() throws ServletException {
getServletContext().log("Initializing elasticsearch Node '" + getServletName() + "'");
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();

InputStream resourceAsStream = getServletContext().getResourceAsStream("/WEB-INF/elasticsearch.json");
if (resourceAsStream != null) {
settings.loadFromStream("/WEB-INF/elasticsearch.json", resourceAsStream);
try {
resourceAsStream.close();
} catch (IOException e) {
// ignore
}
}

resourceAsStream = getServletContext().getResourceAsStream("/WEB-INF/elasticsearch.yml");
if (resourceAsStream != null) {
settings.loadFromStream("/WEB-INF/elasticsearch.yml", resourceAsStream);
try {
resourceAsStream.close();
} catch (IOException e) {
// ignore
}
}
if (settings.get("http.enabled") == null) {
settings.put("http.enabled", false);
}

node = NodeBuilder.nodeBuilder().settings(settings).node();
restController = ((InternalNode) node).injector().getInstance(RestController.class);
getServletContext().setAttribute(NODE_KEY, node);
}

@Override public void destroy() {
if (node != null) {
getServletContext().removeAttribute(NODE_KEY);
node.close();
}
}

@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletRestRequest request = new ServletRestRequest(req);
ServletRestChannel channel = new ServletRestChannel(request, resp);
try {
if (!restController.dispatchRequest(request, channel)) {
throw new ServletException("No mapping found for [" + request.uri() + "]");
}
channel.latch.await();
} catch (ServletException e) {
throw e;
} catch (Exception e) {
throw new IOException("failed to dispatch request", e);
}
if (channel.sendFailure != null) {
throw channel.sendFailure;
}
}

static class ServletRestChannel implements RestChannel {

final RestRequest restRequest;

final HttpServletResponse resp;

final CountDownLatch latch;

IOException sendFailure;

ServletRestChannel(RestRequest restRequest, HttpServletResponse resp) {
this.restRequest = restRequest;
this.resp = resp;
this.latch = new CountDownLatch(1);
}

@Override public void sendResponse(RestResponse response) {
resp.setContentType(response.contentType());
if (RestUtils.isBrowser(restRequest.header("User-Agent"))) {
resp.addHeader("Access-Control-Allow-Origin", "*");
if (restRequest.method() == RestRequest.Method.OPTIONS) {
// also add more access control parameters
resp.addHeader("Access-Control-Max-Age", "1728000");
resp.addHeader("Access-Control-Allow-Methods", "PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
}
}
String opaque = restRequest.header("X-Opaque-Id");
if (opaque != null) {
resp.addHeader("X-Opaque-Id", opaque);
}
try {
int contentLength = response.contentLength() + response.prefixContentLength() + response.suffixContentLength();
resp.setContentLength(contentLength);

ServletOutputStream out = resp.getOutputStream();
if (response.prefixContent() != null) {
out.write(response.prefixContent(), 0, response.prefixContentLength());
}
out.write(response.content(), 0, response.contentLength());
if (response.suffixContent() != null) {
out.write(response.suffixContent(), 0, response.suffixContentLength());
}
out.close();
} catch (IOException e) {
sendFailure = e;
} finally {
latch.countDown();
}
}
}
}
Loading

0 comments on commit 2837f6a

Please sign in to comment.