/*
* Copyright (C) 2014 Jörg Prante
*
* Licensed 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.xbib.elasticsearch.rest.action.knapsack.state;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.support.RestToXContentListener;
import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateAction;
import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateRequest;
import org.xbib.elasticsearch.action.knapsack.state.KnapsackStateResponse;
import org.xbib.elasticsearch.knapsack.KnapsackParameter;
import static org.elasticsearch.rest.RestRequest.Method.POST;
public class RestKnapsackStateAction extends BaseRestHandler implements KnapsackParameter {
private final static ESLogger logger = ESLoggerFactory.getLogger(RestKnapsackStateAction.class.getSimpleName());
@Inject
public RestKnapsackStateAction(Settings settings, Client client, RestController controller) {
super(settings, controller, client);
controller.registerHandler(POST, "/_export/state", this);
controller.registerHandler(POST, "/_import/state", this);
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
try {
KnapsackStateRequest stateRequest = new KnapsackStateRequest();
client.admin().indices().execute(KnapsackStateAction.INSTANCE, stateRequest,
new RestToXContentListener<KnapsackStateResponse>(channel));
} catch (Throwable ex) {
try {
logger.error(ex.getMessage(), ex);
channel.sendResponse(new BytesRestResponse(channel, ex));
} catch (Exception ex2) {
logger.error(ex2.getMessage(), ex2);
}
}
}
}