// Path: src/main/java/org/jboss/elasticsearch/river/jira/mgm/JRMgmBaseActionListener.java
// public abstract class JRMgmBaseActionListener<Request extends JRMgmBaseRequest<?>, Response extends JRMgmBaseResponse<NodeResponse>, NodeResponse extends NodeJRMgmBaseResponse>
// 		implements ActionListener<Response> {
// 
// 	protected final ESLogger logger;
// 
// 	protected final RestRequest restRequest;
// 	protected final RestChannel restChannel;
// 	protected final Request actionRequest;
// 
// 	/**
// 	 * Create new action listener.
// 	 * 
// 	 * @param actionRequest this listener process result for
// 	 * @param restRequest we handle
// 	 * @param restChannel to write rest response into
// 	 */
// 	public JRMgmBaseActionListener(Request actionRequest, RestRequest restRequest, RestChannel restChannel) {
// 		super();
// 		this.restRequest = restRequest;
// 		this.restChannel = restChannel;
// 		this.actionRequest = actionRequest;
// 		logger = Loggers.getLogger(getClass());
// 	}
// 
// 	@Override
// 	public void onResponse(Response response) {
// 		try {
// 			NodeResponse nodeInfo = response.getSuccessNodeResponse();
// 			if (nodeInfo == null) {
// 				restChannel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, buildMessageDocument(restRequest,
// 						"No JiraRiver found for name: " + actionRequest.getRiverName())));
// 			} else {
// 				handleJiraRiverResponse(nodeInfo);
// 			}
// 		} catch (Exception e) {
// 			onFailure(e);
// 		}
// 	}
// 
// 	/**
// 	 * Implement this in subclasses to handle response from jira river and return REST response based on it.
// 	 * 
// 	 * @param nodeInfo operation response from node with jira river, never null
// 	 * @throws Exception if something is wrong
// 	 */
// 	protected abstract void handleJiraRiverResponse(NodeResponse nodeInfo) throws Exception;
// 
// 	@Override
// 	public void onFailure(Throwable e) {
// 		try {
// 			restChannel.sendResponse(new BytesRestResponse(restChannel, e));
// 		} catch (IOException e1) {
// 			logger.error("Failed to send failure response", e1);
// 		}
// 	}
// 
// 	/**
// 	 * Build response document with only one field called <code>message</code>. You can use this in
// 	 * {@link #handleJiraRiverResponse(NodeJRMgmBaseResponse)} implementation if you only need to return simple message.
// 	 * 
// 	 * @param restRequest to build response document for
// 	 * @param message to be placed in document
// 	 * @return document with message
// 	 * @throws IOException
// 	 */
// 	public static XContentBuilder buildMessageDocument(RestRequest restRequest, String message) throws IOException {
// 		XContentBuilder builder = RestXContentBuilder.restContentBuilder(restRequest);
// 		builder.startObject();
// 		builder.field("message", message);
// 		builder.endObject();
// 		return builder;
// 	}
// 
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/river/jira/mgm/RestJRMgmBaseAction.java
// public abstract class RestJRMgmBaseAction extends BaseRestHandler {
// 
// 	protected RestJRMgmBaseAction(Settings settings, RestController controller, Client client) {
// 		super(settings, controller, client);
// 	}
// 
// 	/**
// 	 * Prepare base REST URL for JIRA river management operations. <code>riverName</code> request parameter is defined
// 	 * here.
// 	 * 
// 	 * @return base REST management url ending by <code>/</code>
// 	 */
// 	protected String baseRestMgmUrl() {
// 		return "/" + RiverIndexName.Conf.indexName(settings) + "/{riverName}/_mgm_jr/";
// 	}
// 
// }


import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.jboss.elasticsearch.river.jira.mgm.JRMgmBaseActionListener;
import org.jboss.elasticsearch.river.jira.mgm.RestJRMgmBaseAction;
import static org.elasticsearch.rest.RestStatus.OK;
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.jboss.elasticsearch.river.jira.mgm.lifecycle;



/**
 * REST action handler for Jira river get state operation.
 * 
 * @author Vlastimil Elias (velias at redhat dot com)
 */
public class RestJRLifecycleAction extends RestJRMgmBaseAction {

	@Inject
	protected RestJRLifecycleAction(Settings settings, Client client, RestController controller) {
		super(settings, controller, client);
		String baseUrl = baseRestMgmUrl();
		controller.registerHandler(org.elasticsearch.rest.RestRequest.Method.POST, baseUrl + "stop", this);
		controller.registerHandler(org.elasticsearch.rest.RestRequest.Method.POST, baseUrl + "restart", this);
	}

	@Override
	public void handleRequest(final RestRequest restRequest, final RestChannel restChannel, Client client) {

		JRLifecycleCommand command = JRLifecycleCommand.RESTART;
		if (restRequest.path().endsWith("stop"))
			command = JRLifecycleCommand.STOP;

		JRLifecycleRequest actionRequest = new JRLifecycleRequest(restRequest.param("riverName"), command);

		client
				.admin()
				.cluster()
				.execute(
						JRLifecycleAction.INSTANCE,
						actionRequest,
// Path: src/main/java/org/jboss/elasticsearch/river/jira/mgm/JRMgmBaseActionListener.java
// public abstract class JRMgmBaseActionListener<Request extends JRMgmBaseRequest<?>, Response extends JRMgmBaseResponse<NodeResponse>, NodeResponse extends NodeJRMgmBaseResponse>
// 		implements ActionListener<Response> {
// 
// 	protected final ESLogger logger;
// 
// 	protected final RestRequest restRequest;
// 	protected final RestChannel restChannel;
// 	protected final Request actionRequest;
// 
// 	/**
// 	 * Create new action listener.
// 	 * 
// 	 * @param actionRequest this listener process result for
// 	 * @param restRequest we handle
// 	 * @param restChannel to write rest response into
// 	 */
// 	public JRMgmBaseActionListener(Request actionRequest, RestRequest restRequest, RestChannel restChannel) {
// 		super();
// 		this.restRequest = restRequest;
// 		this.restChannel = restChannel;
// 		this.actionRequest = actionRequest;
// 		logger = Loggers.getLogger(getClass());
// 	}
// 
// 	@Override
// 	public void onResponse(Response response) {
// 		try {
// 			NodeResponse nodeInfo = response.getSuccessNodeResponse();
// 			if (nodeInfo == null) {
// 				restChannel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, buildMessageDocument(restRequest,
// 						"No JiraRiver found for name: " + actionRequest.getRiverName())));
// 			} else {
// 				handleJiraRiverResponse(nodeInfo);
// 			}
// 		} catch (Exception e) {
// 			onFailure(e);
// 		}
// 	}
// 
// 	/**
// 	 * Implement this in subclasses to handle response from jira river and return REST response based on it.
// 	 * 
// 	 * @param nodeInfo operation response from node with jira river, never null
// 	 * @throws Exception if something is wrong
// 	 */
// 	protected abstract void handleJiraRiverResponse(NodeResponse nodeInfo) throws Exception;
// 
// 	@Override
// 	public void onFailure(Throwable e) {
// 		try {
// 			restChannel.sendResponse(new BytesRestResponse(restChannel, e));
// 		} catch (IOException e1) {
// 			logger.error("Failed to send failure response", e1);
// 		}
// 	}
// 
// 	/**
// 	 * Build response document with only one field called <code>message</code>. You can use this in
// 	 * {@link #handleJiraRiverResponse(NodeJRMgmBaseResponse)} implementation if you only need to return simple message.
// 	 * 
// 	 * @param restRequest to build response document for
// 	 * @param message to be placed in document
// 	 * @return document with message
// 	 * @throws IOException
// 	 */
// 	public static XContentBuilder buildMessageDocument(RestRequest restRequest, String message) throws IOException {
// 		XContentBuilder builder = RestXContentBuilder.restContentBuilder(restRequest);
// 		builder.startObject();
// 		builder.field("message", message);
// 		builder.endObject();
// 		return builder;
// 	}
// 
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/river/jira/mgm/RestJRMgmBaseAction.java
// public abstract class RestJRMgmBaseAction extends BaseRestHandler {
// 
// 	protected RestJRMgmBaseAction(Settings settings, RestController controller, Client client) {
// 		super(settings, controller, client);
// 	}
// 
// 	/**
// 	 * Prepare base REST URL for JIRA river management operations. <code>riverName</code> request parameter is defined
// 	 * here.
// 	 * 
// 	 * @return base REST management url ending by <code>/</code>
// 	 */
// 	protected String baseRestMgmUrl() {
// 		return "/" + RiverIndexName.Conf.indexName(settings) + "/{riverName}/_mgm_jr/";
// 	}
// 
// }


// Path: src/main/java/org/jboss/elasticsearch/river/jira/mgm/lifecycle/RestJRLifecycleAction.java
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.jboss.elasticsearch.river.jira.mgm.JRMgmBaseActionListener;
import org.jboss.elasticsearch.river.jira.mgm.RestJRMgmBaseAction;
import static org.elasticsearch.rest.RestStatus.OK;

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.jboss.elasticsearch.river.jira.mgm.lifecycle;



/**
 * REST action handler for Jira river get state operation.
 * 
 * @author Vlastimil Elias (velias at redhat dot com)
 */
public class RestJRLifecycleAction extends RestJRMgmBaseAction {

	@Inject
	protected RestJRLifecycleAction(Settings settings, Client client, RestController controller) {
		super(settings, controller, client);
		String baseUrl = baseRestMgmUrl();
		controller.registerHandler(org.elasticsearch.rest.RestRequest.Method.POST, baseUrl + "stop", this);
		controller.registerHandler(org.elasticsearch.rest.RestRequest.Method.POST, baseUrl + "restart", this);
	}

	@Override
	public void handleRequest(final RestRequest restRequest, final RestChannel restChannel, Client client) {

		JRLifecycleCommand command = JRLifecycleCommand.RESTART;
		if (restRequest.path().endsWith("stop"))
			command = JRLifecycleCommand.STOP;

		JRLifecycleRequest actionRequest = new JRLifecycleRequest(restRequest.param("riverName"), command);

		client
				.admin()
				.cluster()
				.execute(
						JRLifecycleAction.INSTANCE,
						actionRequest,
						new JRMgmBaseActionListener<JRLifecycleRequest, JRLifecycleResponse, NodeJRLifecycleResponse>(