Package org.jboss.elasticsearch.river.jira.mgm

Source Code of org.jboss.elasticsearch.river.jira.mgm.JRMgmBaseActionListener

/*
* 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;

import java.io.IOException;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;

/**
* Base action listener used for Jira River management action calls. Handles response in case if jira river is not
* found, or if there is some error.
*
* @author Vlastimil Elias (velias at redhat dot com)
*/
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;
  }

}
TOP

Related Classes of org.jboss.elasticsearch.river.jira.mgm.JRMgmBaseActionListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.