/*
 * Copyright 2013 the original author or authors.
 *
 * 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.elasticsearch.hadoop.rest;

import org.apache.commons.logging.LogFactory;
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;

    public Resource(Settings settings) {
        String resource = settings.getTargetResource();

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

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

            LogFactory.getLog(Resource.class).warn(
                    String.format(
                            "queries should be specified through '%s' option and not the target index; this option will be _removed_ in the next release",
                            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 = resource.substring(0, index);

                settings.cleanResource();
                settings.setResource(resource);
                settings.setQuery(query);
                settings.save();
            }
        }

        String res = resource.trim();

        if (res.startsWith("/")) {
            res = res.substring(1);
        }
        if (res.endsWith("/")) {
            res = res.substring(0, res.length() - 1);
        }

        int slash = res.indexOf("/");
        Assert.isTrue(slash >= 0 && slash < res.length() - 1, errorMessage);
        index = res.substring(0, slash);
        type = res.substring(slash + 1);

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

        indexAndType = index + "/" + type;
    }

    String bulk() {
        return indexAndType + "/_bulk";
    }

    // https://github.com/elasticsearch/elasticsearch/issues/2726
    String targetShards() {
        return indexAndType + "/_search_shards";
    }

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

    String indexAndType() {
        return indexAndType;
    }

    String type() {
        return type;
    }

    String index() {
        return index;
    }

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

    public String refresh() {
        return index + "/_refresh";
    }
}
@@ -2272,32 +2272,122 @@
 stIndexOf(%22/%22);%0A
+                Assert.isTrue(index %3E= 0 && index %3C resource.length() - 1, errorMessage);%0A
                 

fix parsing of / in legacy mode