com.opensearchserver.hadse.index.IndexController.java Source code

Java tutorial

Introduction

Here is the source code for com.opensearchserver.hadse.index.IndexController.java

Source

/** Copyright 2014 OpenSearchServer, Inc.
 * http://www.opensearchserver.com
 *
 * 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 com.opensearchserver.hadse.index;

import java.io.IOException;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {

    @RequestMapping(method = RequestMethod.GET, value = "/{index_name}")
    @ResponseBody
    public HttpEntity<?> indexExists(@PathVariable String index_name) {
        if (!IndexCatalog.exists(index_name))
            return new ResponseEntity<String>("Index not found: " + index_name, HttpStatus.NOT_FOUND);
        return new ResponseEntity<String>("Index found", HttpStatus.FOUND);

    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{index_name}")
    @ResponseBody
    public HttpEntity<?> createIndex(@PathVariable String index_name,
            @RequestParam(value = "shards", required = false, defaultValue = "5") Integer shards,
            @RequestParam(value = "replicats", required = false, defaultValue = "1") Integer replicas) {
        try {
            return IndexCatalog.create(index_name, shards, replicas);
        } catch (Throwable e) {
            return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{index_name}")
    @ResponseBody
    public HttpEntity<?> deleteIndex(@PathVariable String index_name) {
        if (!IndexCatalog.exists(index_name))
            return new ResponseEntity<String>("Index not found: " + index_name, HttpStatus.NOT_FOUND);
        try {
            return IndexCatalog.delete(index_name);
        } catch (IOException e) {
            return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}