diff --git a/pom.xml b/pom.xml index 3503967..5236194 100644 --- a/pom.xml +++ b/pom.xml @@ -17,11 +17,11 @@ - 0.20.5 + 1.4.2 2.1.3.Final UTF-8 - + The Apache Software License, Version 2.0 @@ -59,7 +59,7 @@ com.github.tlrx elasticsearch-test - 0.0.7 + 1.2.1 test diff --git a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java index 73de55d..d0d09dd 100644 --- a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java @@ -18,9 +18,10 @@ */ package org.elasticsearch.action.view; -import org.elasticsearch.ElasticSearchException; -import org.elasticsearch.ElasticSearchIllegalArgumentException; -import org.elasticsearch.ElasticSearchParseException; +import org.elasticsearch.index.VersionType; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.TransportSearchAction; @@ -31,7 +32,6 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.routing.ShardIterator; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -52,6 +52,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.index.shard.ShardId; public class TransportViewAction extends TransportShardSingleOperationAction { @@ -66,13 +68,19 @@ public TransportViewAction(Settings settings, ThreadPool threadPool, TransportService transportService, IndicesService indicesService, ViewService viewService, - TransportSearchAction searchAction) { - super(settings, threadPool, clusterService, transportService); + TransportSearchAction searchAction, + ActionFilters actionFilters) { + super(settings, ViewAction.NAME, threadPool, clusterService, transportService, actionFilters); this.indicesService = indicesService; this.viewService = viewService; this.searchAction = searchAction; } + @Override + protected boolean resolveIndex() { + return true; + } + @Override protected String executor() { return ThreadPool.Names.GENERIC; @@ -88,37 +96,30 @@ protected ViewResponse newResponse() { return new ViewResponse(); } - @Override - protected String transportAction() { - return ViewAction.NAME; - } - - @Override protected ClusterBlockException checkGlobalBlock(ClusterState state, ViewRequest request) { return state.blocks().globalBlockedException(ClusterBlockLevel.READ); } - @Override protected ClusterBlockException checkRequestBlock(ClusterState state, ViewRequest request) { return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index()); } - + @Override - protected ShardIterator shards(ClusterState state, ViewRequest request) { + protected ShardIterator shards(ClusterState state, InternalRequest action) { return clusterService.operationRouting() - .getShards(clusterService.state(), request.index(), request.type(), request.id(), null, null); + .getShards(clusterService.state(), action.concreteIndex(), action.request().type(), action.request().id(), null, null); } - + @Override - protected ViewResponse shardOperation(ViewRequest request, int shardId) throws ElasticSearchException { + protected ViewResponse shardOperation(ViewRequest request, ShardId shardId) throws ElasticsearchException { // Get the doc first IndexService indexService = indicesService.indexService(request.index()); - IndexShard indexShard = indexService.shardSafe(shardId); - GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false); + IndexShard indexShard = indexService.shardSafe(shardId.id()); + GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false, 1, VersionType.INTERNAL, null, false); - if (!getResult.exists()) { - throw new ElasticSearchIllegalArgumentException("Document not found, cannot render view"); + if (!getResult.isExists()) { + throw new ElasticsearchIllegalArgumentException("Document not found, cannot render view"); } // Try to get a view stored at document level @@ -132,7 +133,7 @@ protected ViewResponse shardOperation(ViewRequest request, int shardId) throws E Map mapping = mappingMetaData.sourceAsMap(); viewContext = extract(mapping, request.format()); } catch (IOException e) { - throw new ElasticSearchParseException("Failed to parse mapping content to map", e); + throw new ElasticsearchParseException("Failed to parse mapping content to map", e); } } } @@ -142,10 +143,10 @@ protected ViewResponse shardOperation(ViewRequest request, int shardId) throws E } // Set some org.elasticsearch.test.integration.views.mappings.data required for view rendering - viewContext.index(getResult.index()) - .type(getResult.type()) - .id(getResult.id()) - .version(getResult.version()) + viewContext.index(getResult.getIndex()) + .type(getResult.getType()) + .id(getResult.getId()) + .version(getResult.getVersion()) .source(getResult.sourceAsMap()); // Ok, let's render it with a ViewEngineService @@ -283,7 +284,7 @@ private ViewContext extract(Map sourceAsMap, String format) { } SearchResponse searchResponse = searchAction.execute(searchRequest).get(); - viewContext.queriesAndHits(queryName, searchResponse.hits()); + viewContext.queriesAndHits(queryName, searchResponse.getHits()); } catch (Exception e) { viewContext.queriesAndHits(queryName, null); diff --git a/src/main/java/org/elasticsearch/action/view/ViewAction.java b/src/main/java/org/elasticsearch/action/view/ViewAction.java index 513bf05..db690f0 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/ViewAction.java @@ -18,10 +18,10 @@ */ package org.elasticsearch.action.view; -import org.elasticsearch.action.Action; +import org.elasticsearch.action.ClientAction; import org.elasticsearch.client.Client; -public class ViewAction extends Action { +public class ViewAction extends ClientAction { public static final ViewAction INSTANCE = new ViewAction(); public static final String NAME = "view"; diff --git a/src/main/java/org/elasticsearch/action/view/ViewRequest.java b/src/main/java/org/elasticsearch/action/view/ViewRequest.java index f96f065..87907da 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewRequest.java +++ b/src/main/java/org/elasticsearch/action/view/ViewRequest.java @@ -18,9 +18,15 @@ */ package org.elasticsearch.action.view; +import java.io.IOException; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Required; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; + public class ViewRequest extends SingleShardOperationRequest { @@ -30,12 +36,42 @@ public class ViewRequest extends SingleShardOperationRequest { public static final String DEFAULT_VIEW = "default"; ViewRequest() { + this.type = "_all"; + } + + /** + * Constructs a new view request against the specified index. The {@link #type(String)} and {@link #id(String)} + * must be set. + */ + public ViewRequest(String index) { + super(index); + this.type = "_all"; + } + + /* + * Copy constructor that creates a new view request that is a copy of the one provided as an argument. + * The new request will inherit though headers and context from the original request that caused it. + */ + public ViewRequest(ViewRequest viewRequest, ActionRequest originalRequest) { + + super(originalRequest); + this.index = viewRequest.index; + this.type = viewRequest.type; + this.id = viewRequest.id; + } + + /** + * Constructs a new view request starting from the provided request, meaning that it will + * inherit its headers and context, and against the specified index. + */ + public ViewRequest(ActionRequest request, String index) { + super(request, index); } /** * Constructs a new view request against the specified index with the type and id. * - * @param index The index to get the document from + * @param index The index to view the document from * @param type The type of the document * @param id The id of the document */ @@ -59,7 +95,6 @@ public ViewRequest type(@Nullable String type) { /** * Sets the id of the document to fetch. */ - @Required public ViewRequest id(String id) { this.id = id; return this; @@ -84,5 +119,36 @@ public String id() { public String format() { return format; } - + @Override + public ActionRequestValidationException validate() { + ActionRequestValidationException validationException = super.validate(); + if (type == null) { + validationException = ValidateActions.addValidationError("type is missing", validationException); + } + if (id == null) { + validationException = ValidateActions.addValidationError("id is missing", validationException); + } + return validationException; + } + + @Override + public void readFrom(StreamInput in) throws IOException + { + super.readFrom(in); + type = in.readSharedString(); + id = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeSharedString(type); + out.writeString(id); + } + + @Override + public String toString() { + return "view [" + index + "][" + type + "][" + id + "]"; + } + } diff --git a/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java b/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java index 9c67a9a..54a2775 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java @@ -21,16 +21,16 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequestBuilder; import org.elasticsearch.client.Client; -import org.elasticsearch.client.internal.InternalClient; + public class ViewRequestBuilder extends SingleShardOperationRequestBuilder { public ViewRequestBuilder(Client client) { - super((InternalClient) client, new ViewRequest()); + super(client, new ViewRequest()); } public ViewRequestBuilder(Client client, String index, String type, String id) { - super((InternalClient) client, new ViewRequest(index, type, id)); + super(client, new ViewRequest(index, type, id)); } /** diff --git a/src/main/java/org/elasticsearch/action/view/ViewResponse.java b/src/main/java/org/elasticsearch/action/view/ViewResponse.java index 5e8336b..51c45e8 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewResponse.java +++ b/src/main/java/org/elasticsearch/action/view/ViewResponse.java @@ -18,7 +18,11 @@ */ package org.elasticsearch.action.view; +import java.io.IOException; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.get.GetResult; public class ViewResponse extends ActionResponse { @@ -40,4 +44,22 @@ public byte[] content() { public String contentType() { return this.contentType; } + + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + contentType = in.readString(); + int contentlength = in.readInt(); + content = new byte[contentlength]; + in.readFully(content); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(contentType); + out.writeInt(content.length); + out.writeBytes(content); + } } diff --git a/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java b/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java index c177bd0..32bbebf 100644 --- a/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java +++ b/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java @@ -34,10 +34,12 @@ public class ViewPlugin extends AbstractPlugin { + @Override public String description() { return "Elasticsearch View Plugin"; } + @Override public String name() { return "view-plugin"; } diff --git a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java index 2aa66f5..dc0b6a1 100644 --- a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java +++ b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java @@ -37,12 +37,12 @@ public class RestViewAction extends BaseRestHandler { @Inject public RestViewAction(Settings settings, Client client, RestController controller) { - super(settings, client); + super(settings, controller, client); controller.registerHandler(GET, "/_view/{index}/{type}/{id}", this); controller.registerHandler(GET, "/_view/{index}/{type}/{id}/{format}", this); } - public void handleRequest(final RestRequest request, final RestChannel channel) { + public void handleRequest(final RestRequest request, final RestChannel channel, Client client) { ViewRequest viewRequest = new ViewRequest(request.param("index"), request.param("type"), request.param("id")); if (request.hasParam("format")) { viewRequest.format(request.param("format")); @@ -57,22 +57,19 @@ public void handleRequest(final RestRequest request, final RestChannel channel) public void onResponse(ViewResponse response) { try { - channel.sendResponse(new BytesRestResponse(response.content(), response.contentType())); + channel.sendResponse(new BytesRestResponse(RestStatus.OK, response.contentType(), response.content())); } catch (Exception e) { onFailure(e); } } public void onFailure(Throwable e) { - try { + if (e instanceof ElasticSearchViewNotFoundException) { - channel.sendResponse(new XContentThrowableRestResponse(request, NOT_FOUND, e)); + channel.sendResponse(new BytesRestResponse(NOT_FOUND, e.toString())); } else { - channel.sendResponse(new XContentThrowableRestResponse(request, e)); + channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, e.toString())); } - } catch (IOException e1) { - logger.error("Failed to send failure response", e1); - } } }); } diff --git a/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java b/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java index 8d844ae..4582f8b 100644 --- a/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java +++ b/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java @@ -19,9 +19,9 @@ package org.elasticsearch.view.exception; -import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.ElasticsearchException; -public class ElasticSearchViewNotFoundException extends ElasticSearchException { +public class ElasticSearchViewNotFoundException extends ElasticsearchException { public ElasticSearchViewNotFoundException(String msg) { super(msg); }