package com.cadrlife.devsearch.esplugin; import com.cadrlife.devsearch.esplugin.service.CodeSearchIndexService; import com.fasterxml.jackson.databind.ObjectMapper; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.*; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestStatus.OK; public class ProjectsRestHandler extends BaseRestHandler { private final CodeSearchIndexService indexService; @Inject public ProjectsRestHandler(Settings settings, Client client, RestController restController, CodeSearchIndexService indexService) { super(settings, client); this.indexService = indexService; restController.registerHandler(GET, "/_dev-search/projects", this); } @Override public void handleRequest(final RestRequest request, final RestChannel channel) { try { ObjectMapper mapper = new ObjectMapper(); channel.sendResponse(new BytesRestResponse(OK, mapper.writeValueAsString(indexService.findAllProjects(0, 10000)))); } catch (Exception e) { logger.error("Failed to get projects", e); channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage())); } } }