Skip to content

Commit

Permalink
Allow for plugins to register REST pre processor, closes #1658.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 2, 2012
1 parent 07ba397 commit 43af504
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/elasticsearch/http/HttpServer.java
Expand Up @@ -27,10 +27,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.service.NodeService;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.StringRestResponse;
import org.elasticsearch.rest.*;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -114,6 +111,14 @@ public HttpStats stats() {

public void internalDispatchRequest(final HttpRequest request, final HttpChannel channel) {
if (request.rawPath().startsWith("/_plugin/")) {
for (RestPreProcessor preProcessor : restController.preProcessors()) {
if (!preProcessor.handleExternal()) {
continue;
}
if (!preProcessor.process(request, channel)) {
return;
}
}
handlePluginSite(request, channel);
return;
}
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/org/elasticsearch/rest/RestController.java
Expand Up @@ -28,6 +28,8 @@
import org.elasticsearch.rest.support.RestUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

/**
*
Expand All @@ -41,6 +43,9 @@ public class RestController extends AbstractLifecycleComponent<RestController> {
private final PathTrie<RestHandler> headHandlers = new PathTrie<RestHandler>(RestUtils.REST_DECODER);
private final PathTrie<RestHandler> optionsHandlers = new PathTrie<RestHandler>(RestUtils.REST_DECODER);

// non volatile since the assumption is that pre processors are registered on startup
private RestPreProcessor[] preProcessors = new RestPreProcessor[0];

@Inject
public RestController(Settings settings) {
super(settings);
Expand All @@ -58,6 +63,25 @@ protected void doStop() throws ElasticSearchException {
protected void doClose() throws ElasticSearchException {
}

/**
* Registers a pre processor to be executed before the rest request is actually handled.
*/
public synchronized void registerPreProcessor(RestPreProcessor preProcessor) {
RestPreProcessor[] copy = new RestPreProcessor[preProcessors.length + 1];
System.arraycopy(preProcessors, 0, copy, 0, preProcessors.length);
copy[preProcessors.length] = preProcessor;
Arrays.sort(copy, new Comparator<RestPreProcessor>() {
@Override
public int compare(RestPreProcessor o1, RestPreProcessor o2) {
return o2.order() - o1.order();
}
});
preProcessors = copy;
}

/**
* Registers a rest handler to be execute when the provided method and path match the request.
*/
public void registerHandler(RestRequest.Method method, String path, RestHandler handler) {
switch (method) {
case GET:
Expand All @@ -83,12 +107,21 @@ public void registerHandler(RestRequest.Method method, String path, RestHandler
}
}

public RestPreProcessor[] preProcessors() {
return preProcessors;
}

public boolean dispatchRequest(final RestRequest request, final RestChannel channel) {
final RestHandler handler = getHandler(request);
if (handler == null) {
return false;
}
try {
for (RestPreProcessor preProcessor : preProcessors) {
if (!preProcessor.process(request, channel)) {
return true;
}
}
final RestHandler handler = getHandler(request);
if (handler == null) {
return false;
}
handler.handleRequest(request, channel);
} catch (Exception e) {
try {
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/elasticsearch/rest/RestPreProcessor.java
@@ -0,0 +1,45 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch 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.rest;

/**
* Rest pre processor allowing to pre process REST requests.
* <p/>
* Experimental interface.
*/
public interface RestPreProcessor {

/**
* Optionally, the order the processor will work on. Execution is done from lowest value to highest.
* It is a good practice to allow to configure this for the relevant processor.
*/
int order();

/**
* Should this processor also process external (non REST) requests, like plugin site requests.
*/
boolean handleExternal();

/**
* Process the request, returning <tt>false</tt> if no further processing should be done. Note,
* make sure to send a response if returning <tt>false</tt>, otherwise, no response will be sent.
*/
boolean process(RestRequest request, RestChannel channel);
}

0 comments on commit 43af504

Please sign in to comment.