001/* 002 * Copyright (C) 2014 Jörg Prante 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.xbib.elasticsearch.rest.action.river.jdbc; 017 018import org.elasticsearch.client.Client; 019import org.elasticsearch.common.inject.Inject; 020import org.elasticsearch.common.settings.Settings; 021import org.elasticsearch.common.xcontent.XContentBuilder; 022import org.elasticsearch.rest.BaseRestHandler; 023import org.elasticsearch.rest.BytesRestResponse; 024import org.elasticsearch.rest.RestChannel; 025import org.elasticsearch.rest.RestController; 026import org.elasticsearch.rest.RestRequest; 027import org.elasticsearch.rest.RestRequest.Method; 028import org.elasticsearch.rest.RestStatus; 029import org.elasticsearch.rest.action.support.RestToXContentListener; 030import org.xbib.elasticsearch.action.plugin.jdbc.run.RunRiverAction; 031import org.xbib.elasticsearch.action.plugin.jdbc.run.RunRiverRequest; 032import org.xbib.elasticsearch.action.plugin.jdbc.run.RunRiverResponse; 033 034import java.io.IOException; 035 036import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 037 038public class RestRunRiverAction extends BaseRestHandler { 039 040 @Inject 041 public RestRunRiverAction(Settings settings, RestController controller, Client client) { 042 super(settings, client); 043 044 controller.registerHandler(Method.POST, "/_river/jdbc/{rivername}/_run", this); 045 } 046 047 @Override 048 protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception { 049 try { 050 String riverName = request.param("rivername"); 051 String riverType = "jdbc"; 052 RunRiverRequest runRiverRequest = new RunRiverRequest(); 053 runRiverRequest.setRiverName(riverName).setRiverType(riverType); 054 client.admin().cluster().execute(RunRiverAction.INSTANCE, runRiverRequest, 055 new RestToXContentListener<RunRiverResponse>(channel)); 056 XContentBuilder builder = jsonBuilder().startObject().field("ok", true).endObject(); 057 channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); 058 } catch (Throwable t) { 059 try { 060 channel.sendResponse(new BytesRestResponse(channel, t)); 061 } catch (IOException e1) { 062 channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR)); 063 } 064 } 065 } 066}