<commit_before>package org.codelibs.elasticsearch.configsync.rest;

import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codelibs.elasticsearch.configsync.exception.IORuntimeException;
import org.codelibs.elasticsearch.configsync.exception.InvalidRequestException;
import org.codelibs.elasticsearch.configsync.service.ConfigSyncService;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
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.search.lookup.SourceLookup;
import org.elasticsearch.search.sort.SortOrder;

public class RestConfigSyncFileAction extends BaseRestHandler {

    private final ConfigSyncService configSyncService;

    @Inject
    public RestConfigSyncFileAction(final Settings settings, final Client client, final RestController controller,
            final ConfigSyncService configSyncService) {
        super(settings, controller, client);
        this.configSyncService = configSyncService;

        controller.registerHandler(RestRequest.Method.GET, "/_configsync/file", this);
        controller.registerHandler(RestRequest.Method.POST, "/_configsync/file", this);
        controller.registerHandler(RestRequest.Method.DELETE, "/_configsync/file", this);
    }

    @Override
    protected void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
        try {
            final BytesReference content = request.content();
            switch (request.method()) {
            case GET: {
                String path = request.param(ConfigSyncService.PATH);
                if (path == null && content != null && content.length() > 0) {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                }
                if (path == null) {
                    final String[] sortValues = request.param("sort", ConfigSyncService.PATH).split(":");
                    final String sortField;
                    final String sortOrder;
                    if (sortValues.length > 1) {
                        sortField = sortValues[0];
                        sortOrder = sortValues[1];
                    } else {
                        sortField = sortValues[0];
                        sortOrder = SortOrder.ASC.toString();
                    }

                    final String[] fields = request.paramAsStringArrayOrEmptyIfAll("fields");
                    configSyncService.getPaths(request.paramAsInt("from", 0), request.paramAsInt("size", 10), fields, sortField, sortOrder,
                            new ActionListener<List<Object>>() {

                                @Override
                                public void onResponse(final List<Object> response) {
                                    final Map<String, Object> params = new HashMap<>();
                                    params.put(fields.length == 0 ? "path" : "file", response);
                                    sendResponse(channel, params);
                                }

                                @Override
                                public void onFailure(final Throwable t) {
                                    sendErrorResponse(channel, t);
                                }
                            });
                } else {
                    configSyncService.getContent(path, new ActionListener<byte[]>() {

                        @Override
                        public void onResponse(final byte[] configContent) {
                            if (configContent != null) {
                                channel.sendResponse(new BytesRestResponse(OK, "application/octet-stream", configContent));
                            } else {
                                channel.sendResponse(new BytesRestResponse(NOT_FOUND));
                            }
                        }

                        @Override
                        public void onFailure(final Throwable t) {
                            sendErrorResponse(channel, t);
                        }
                    });
                }
            }
                break;
            case POST: {
                if (content == null) {
                    throw new InvalidRequestException("content is empty.");
                }
                String path = request.param(ConfigSyncService.PATH);
                byte[] contentArray;
                if (path != null) {
                    contentArray = content.array();
                } else {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                    final String fileContent = (String) sourceAsMap.get(ConfigSyncService.CONTENT);
                    contentArray = Base64.decode(fileContent);
                }
                configSyncService.store(path, contentArray, new ActionListener<IndexResponse>() {

                    @Override
                    public void onResponse(final IndexResponse arg0) {
                        sendResponse(channel, null);
                    }

                    @Override
                    public void onFailure(final Throwable t) {
                        sendErrorResponse(channel, t);
                    }
                });
            }
                break;
            case DELETE: {
                String path = request.param(ConfigSyncService.PATH);
                if (path == null && content != null && content.length() > 0) {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                }
                if (path == null) {
                    sendErrorResponse(channel, new InvalidRequestException(ConfigSyncService.PATH + " is empty."));
                    return;
                }<commit_msg>support CompsiteChannelBuffer<commit_after>package org.codelibs.elasticsearch.configsync.rest;

import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codelibs.elasticsearch.configsync.exception.IORuntimeException;
import org.codelibs.elasticsearch.configsync.exception.InvalidRequestException;
import org.codelibs.elasticsearch.configsync.service.ConfigSyncService;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
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.search.lookup.SourceLookup;
import org.elasticsearch.search.sort.SortOrder;

public class RestConfigSyncFileAction extends BaseRestHandler {

    private final ConfigSyncService configSyncService;

    @Inject
    public RestConfigSyncFileAction(final Settings settings, final Client client, final RestController controller,
            final ConfigSyncService configSyncService) {
        super(settings, controller, client);
        this.configSyncService = configSyncService;

        controller.registerHandler(RestRequest.Method.GET, "/_configsync/file", this);
        controller.registerHandler(RestRequest.Method.POST, "/_configsync/file", this);
        controller.registerHandler(RestRequest.Method.DELETE, "/_configsync/file", this);
    }

    @Override
    protected void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
        try {
            final BytesReference content = request.content();
            switch (request.method()) {
            case GET: {
                String path = request.param(ConfigSyncService.PATH);
                if (path == null && content != null && content.length() > 0) {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                }
                if (path == null) {
                    final String[] sortValues = request.param("sort", ConfigSyncService.PATH).split(":");
                    final String sortField;
                    final String sortOrder;
                    if (sortValues.length > 1) {
                        sortField = sortValues[0];
                        sortOrder = sortValues[1];
                    } else {
                        sortField = sortValues[0];
                        sortOrder = SortOrder.ASC.toString();
                    }

                    final String[] fields = request.paramAsStringArrayOrEmptyIfAll("fields");
                    configSyncService.getPaths(request.paramAsInt("from", 0), request.paramAsInt("size", 10), fields, sortField, sortOrder,
                            new ActionListener<List<Object>>() {

                                @Override
                                public void onResponse(final List<Object> response) {
                                    final Map<String, Object> params = new HashMap<>();
                                    params.put(fields.length == 0 ? "path" : "file", response);
                                    sendResponse(channel, params);
                                }

                                @Override
                                public void onFailure(final Throwable t) {
                                    sendErrorResponse(channel, t);
                                }
                            });
                } else {
                    configSyncService.getContent(path, new ActionListener<byte[]>() {

                        @Override
                        public void onResponse(final byte[] configContent) {
                            if (configContent != null) {
                                channel.sendResponse(new BytesRestResponse(OK, "application/octet-stream", configContent));
                            } else {
                                channel.sendResponse(new BytesRestResponse(NOT_FOUND));
                            }
                        }

                        @Override
                        public void onFailure(final Throwable t) {
                            sendErrorResponse(channel, t);
                        }
                    });
                }
            }
                break;
            case POST: {
                if (content == null) {
                    throw new InvalidRequestException("content is empty.");
                }
                String path = request.param(ConfigSyncService.PATH);
                byte[] contentArray;
                if (path != null) {
                    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                        content.writeTo(out);
                        contentArray = out.toByteArray();
                    }
                } else {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                    final String fileContent = (String) sourceAsMap.get(ConfigSyncService.CONTENT);
                    contentArray = Base64.decode(fileContent);
                }
                configSyncService.store(path, contentArray, new ActionListener<IndexResponse>() {

                    @Override
                    public void onResponse(final IndexResponse arg0) {
                        sendResponse(channel, null);
                    }

                    @Override
                    public void onFailure(final Throwable t) {
                        sendErrorResponse(channel, t);
                    }
                });
            }
                break;
            case DELETE: {
                String path = request.param(ConfigSyncService.PATH);
                if (path == null && content != null && content.length() > 0) {
                    final Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(content);
                    path = (String) sourceAsMap.get(ConfigSyncService.PATH);
                }
                if (path == null) {
                    sendErrorResponse(channel, new InvalidRequestException(ConfigSyncService.PATH + " is empty."));
                    return;
                }
{'fraction_non_alphanumeric': 0.08572671888598782, 'fraction_numerical': 0.002175805047867711, 'mean_word_length': 1.697378716744914, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7154847', 'n_tokens_mistral': 3178, 'n_tokens_neox': 3031, 'n_words': 635}