/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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.elasticsearch.hadoop.rest;

import org.elasticsearch.hadoop.EsHadoopIllegalArgumentException;
import org.elasticsearch.hadoop.cfg.ConfigurationOptions;
import org.elasticsearch.hadoop.cfg.Settings;
import org.elasticsearch.hadoop.util.Assert;
import org.elasticsearch.hadoop.util.StringUtils;


/**
 * ElasticSearch Rest Resource - index and type.
 */
public class Resource {

    private final String indexAndType;
    private final String type;
    private final String index;
    private final String bulk;
    private final String refresh;

    public Resource(Settings settings, boolean read) {
        String resource = (read ? settings.getResourceRead() : settings.getResourceWrite());

        String errorMessage = "invalid resource given; expecting [index]/[type] - received ";
        Assert.hasText(resource, errorMessage + resource);

        // add compatibility for now
        if (resource.contains("?") || resource.contains("&")) {
            if (!StringUtils.hasText(settings.getQuery())) {
                throw new EsHadoopIllegalArgumentException(String.format(
                        "Cannot specify a query in the target index and through %s", ConfigurationOptions.ES_QUERY));
            }

            // extract query
            int index = resource.indexOf("?");
            if (index > 0) {
                String query = resource.substring(index);

                // clean resource
                resource = resource.substring(0, index);
                index = resource.lastIndexOf("/");
                resource = (index > 0 ? resource.substring(0, index) : resource);

                settings.setProperty(ConfigurationOptions.ES_RESOURCE, resource);
                settings.setQuery(query);
            }
        }

        String res = StringUtils.sanitizeResource(resource);

        int slash = res.indexOf("/");
        if (slash < 0) {
            index = res;
            type = StringUtils.EMPTY;
        }
        else {
            index = res.substring(0, slash);
            type = res.substring(slash + 1);

            Assert.hasText(type, "No type found; expecting [index]/[type]");
        }
        Assert.hasText(index, "No index found; expecting [index]/[type]");

        StringBuilder fixedIndex = new StringBuilder();
        if (index.contains("{") && index.contains("}")) {
            int startPattern = index.indexOf("{");
            int endPattern = index.indexOf("}");
            if (endPattern > startPattern) {
                fixedIndex.append(index.substring(0, startPattern));
                fixedIndex.append(index.substring(endPattern + 1, index.length()));
            }
            else {
                fixedIndex.append(index);
            }
        }
        else {
            fixedIndex.append(index);
        }

        indexAndType = index + "/" + type;

        // check bulk
        bulk = (indexAndType.contains("{") ? "/_bulk" : indexAndType + "/_bulk");
        refresh = (index.contains("{") ? "/_refresh" : index + "/_refresh");
    }

    String bulk() {
        return bulk;
    }

    String mapping() {
        return indexAndType + "/_mapping";
    }

    String aliases() {
        return index + "/_aliases";
    }

    String indexAndType() {
        return indexAndType;
    }

    String type() {
        return type;
    }

    String index() {
        return index;
    }

    @Override
    public String toString() {
        return indexAndType;
    }

    public String refresh() {
        return refresh;
    }
}
@@ -3020,583 +3020,8 @@
 );%0A%0A
-        StringBuilder fixedIndex = new StringBuilder();%0A        if (index.contains(%22%7B%22) && index.contains(%22%7D%22)) %7B%0A            int startPattern = index.indexOf(%22%7B%22);%0A            int endPattern = index.indexOf(%22%7D%22);%0A            if (endPattern %3E startPattern) %7B%0A                fixedIndex.append(index.substring(0, startPattern));%0A                fixedIndex.append(index.substring(endPattern + 1, index.length()));%0A            %7D%0A            else %7B%0A                fixedIndex.append(index);%0A            %7D%0A        %7D%0A        else %7B%0A            fixedIndex.append(index);%0A        %7D%0A%0A
     

Remove unused code