Search in sources :

Example 1 with RestServersResultCollector

use of org.apache.geode.rest.internal.web.controllers.support.RestServersResultCollector in project geode by apache.

the class CommonCrudController method servers.

@RequestMapping(method = { RequestMethod.GET }, value = "/servers", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@ApiOperation(value = "fetch all REST enabled servers in the DS", notes = "Find all gemfire node where developer REST service is up and running!", response = void.class)
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 500, message = "if GemFire throws an error or exception") })
@PreAuthorize("@securityService.authorize('CLUSTER', 'READ')")
public ResponseEntity<?> servers() {
    logger.debug("Executing function to get REST enabled gemfire nodes in the DS!");
    Execution function;
    try {
        function = FunctionService.onMembers(getAllMembersInDS());
    } catch (FunctionException fe) {
        throw new GemfireRestException("Disributed system does not contain any valid data node that can host REST service!", fe);
    }
    try {
        final ResultCollector<?, ?> results = function.withCollector(new RestServersResultCollector()).execute(FindRestEnabledServersFunction.FIND_REST_ENABLED_SERVERS_FUNCTION_ID);
        Object functionResult = results.getResult();
        if (functionResult instanceof List<?>) {
            final HttpHeaders headers = new HttpHeaders();
            headers.setLocation(toUri("servers"));
            try {
                String functionResultAsJson = JSONUtils.convertCollectionToJson((ArrayList<Object>) functionResult);
                return new ResponseEntity<>(functionResultAsJson, headers, HttpStatus.OK);
            } catch (JSONException e) {
                throw new GemfireRestException("Could not convert function results into Restful (JSON) format!", e);
            }
        } else {
            throw new GemfireRestException("Function has returned results that could not be converted into Restful (JSON) format!");
        }
    } catch (ClassCastException cce) {
        throw new GemfireRestException("Key is of an inappropriate type for this region!", cce);
    } catch (NullPointerException npe) {
        throw new GemfireRestException("Specified key is null and this region does not permit null keys!", npe);
    } catch (LowMemoryException lme) {
        throw new GemfireRestException("Server has encountered low memory condition!", lme);
    } catch (IllegalArgumentException ie) {
        throw new GemfireRestException("Input parameter is null! ", ie);
    } catch (FunctionException fe) {
        throw new GemfireRestException("Server has encountered error while executing the function!", fe);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FunctionException(org.apache.geode.cache.execute.FunctionException) JSONException(org.json.JSONException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) ResponseEntity(org.springframework.http.ResponseEntity) Execution(org.apache.geode.cache.execute.Execution) RestServersResultCollector(org.apache.geode.rest.internal.web.controllers.support.RestServersResultCollector) ArrayList(java.util.ArrayList) List(java.util.List) LowMemoryException(org.apache.geode.cache.LowMemoryException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 LowMemoryException (org.apache.geode.cache.LowMemoryException)1 Execution (org.apache.geode.cache.execute.Execution)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 RestServersResultCollector (org.apache.geode.rest.internal.web.controllers.support.RestServersResultCollector)1 GemfireRestException (org.apache.geode.rest.internal.web.exception.GemfireRestException)1 JSONException (org.json.JSONException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1