-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aafc793
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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>hello-world</name> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.elasticsearch</groupId> | ||
<artifactId>hello-world</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<elasticsearch.version>0.17.6</elasticsearch.version> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>oss.sonatype.org</id> | ||
<name>OSS Sonatype</name> | ||
<releases> | ||
<enabled>true</enabled> | ||
</releases> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
<url>http://oss.sonatype.org/content/repositories/releases/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.elasticsearch</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
<version>${elasticsearch.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<!-- Create a zip file according to elasticsearch naming scheme --> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-antrun-plugin</artifactId> | ||
<version>1.6</version> | ||
<executions> | ||
<execution> | ||
<id>zip</id> | ||
<phase>package</phase> | ||
<configuration> | ||
<target> | ||
<zip basedir="${project.build.directory}" | ||
includes="${project.build.finalName}.jar" | ||
destfile="${project.build.directory}/elasticsearch-${project.name}-${elasticsearch.version}.zip" /> | ||
</target> | ||
</configuration> | ||
<goals> | ||
<goal>run</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
|
||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/elasticsearch/plugin/helloworld/HelloWorldPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.elasticsearch.plugin.helloworld; | ||
|
||
import org.elasticsearch.common.inject.Module; | ||
import org.elasticsearch.plugins.AbstractPlugin; | ||
import org.elasticsearch.rest.RestModule; | ||
import org.elasticsearch.rest.action.helloworld.HelloWorldAction; | ||
|
||
public class HelloWorldPlugin extends AbstractPlugin { | ||
|
||
public String name() { | ||
return "hello-world"; | ||
} | ||
|
||
public String description() { | ||
return "Hello World Plugin"; | ||
} | ||
|
||
@Override public void processModule(Module module) { | ||
if (module instanceof RestModule) { | ||
((RestModule) module).addRestAction(HelloWorldAction.class); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/org/elasticsearch/rest/action/helloworld/HelloWorldAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.elasticsearch.rest.action.helloworld; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.get.GetField; | ||
import org.elasticsearch.action.get.GetRequest; | ||
import org.elasticsearch.action.get.GetResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentBuilderString; | ||
import org.elasticsearch.rest.*; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.rest.RestRequest.Method.GET; | ||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND; | ||
import static org.elasticsearch.rest.RestStatus.OK; | ||
import static org.elasticsearch.rest.action.support.RestXContentBuilder.restContentBuilder; | ||
|
||
public class HelloWorldAction extends BaseRestHandler { | ||
|
||
public static String INDEX = "example"; | ||
public static String TYPE = "person"; | ||
|
||
@Inject public HelloWorldAction(Settings settings, Client client, RestController controller) { | ||
super(settings, client); | ||
|
||
// Define REST endpoints | ||
controller.registerHandler(GET, "/_hello/", this); | ||
controller.registerHandler(GET, "/_hello/{name}", this); | ||
} | ||
|
||
public void handleRequest(final RestRequest request, final RestChannel channel) { | ||
logger.debug("HelloWorldAction.handleRequest called"); | ||
|
||
final String name = request.hasParam("name") ? request.param("name") : "world"; | ||
|
||
final GetRequest getRequest = new GetRequest(INDEX, TYPE, name); | ||
getRequest.listenerThreaded(false); | ||
getRequest.operationThreaded(true); | ||
|
||
String[] fields = {"msg"}; | ||
getRequest.fields(fields); | ||
|
||
client.get(getRequest, new ActionListener<GetResponse>() { | ||
@Override public void onResponse(GetResponse response) { | ||
|
||
try { | ||
XContentBuilder builder = restContentBuilder(request); | ||
GetField field = response.field("msg"); | ||
String greeting = (field!=null) ? (String)field.values().get(0) : "Sorry, do I know you?"; | ||
builder | ||
.startObject() | ||
.field(new XContentBuilderString("hello"), name) | ||
.field(new XContentBuilderString("greeting"), greeting) | ||
.endObject(); | ||
|
||
if (!response.exists()) { | ||
channel.sendResponse(new XContentRestResponse(request, NOT_FOUND, builder)); | ||
} else { | ||
channel.sendResponse(new XContentRestResponse(request, OK, builder)); | ||
} | ||
} catch (Exception e) { | ||
onFailure(e); | ||
} | ||
} | ||
|
||
@Override public void onFailure(Throwable e) { | ||
try { | ||
channel.sendResponse(new XContentThrowableRestResponse(request, e)); | ||
} catch (IOException e1) { | ||
logger.error("Failed to send failure response", e1); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin |