Search in sources :

Example 6 with GemfireRestException

use of org.apache.geode.rest.internal.web.exception.GemfireRestException in project geode by apache.

the class QueryAccessController method runAdhocQuery.

/**
   * Run an adhoc Query specified in a query string
   * 
   * @param oql OQL query string to be executed
   * @return query result as a JSON document
   */
@RequestMapping(method = RequestMethod.GET, value = "/adhoc", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@ApiOperation(value = "run an adhoc query", notes = "Run an unnamed (unidentified), ad-hoc query passed as a URL parameter", 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 = "GemFire throws an error or exception") })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("@securityService.authorize('DATA', 'READ')")
public ResponseEntity<String> runAdhocQuery(@RequestParam("q") String oql) {
    logger.debug("Running an adhoc Query ({})...", oql);
    oql = decode(oql);
    final Query query = getQueryService().newQuery(oql);
    // and handle the Exceptions appropriately (500 Server Error)!
    try {
        Object queryResult = query.execute();
        return processQueryResponse(query, null, queryResult);
    } catch (FunctionDomainException fde) {
        throw new GemfireRestException("A function was applied to a parameter that is improper for that function!", fde);
    } catch (TypeMismatchException tme) {
        throw new GemfireRestException("Bind parameter is not of the expected type!", tme);
    } catch (NameResolutionException nre) {
        throw new GemfireRestException("Name in the query cannot be resolved!", nre);
    } catch (IllegalArgumentException iae) {
        throw new GemfireRestException(" The number of bound parameters does not match the number of placeholders!", iae);
    } catch (IllegalStateException ise) {
        throw new GemfireRestException("Query is not permitted on this type of region!", ise);
    } catch (QueryExecutionTimeoutException qete) {
        throw new GemfireRestException("Query execution time is exceeded max query execution time (gemfire.Cache.MAX_QUERY_EXECUTION_TIME) configured! ", qete);
    } catch (QueryInvocationTargetException qite) {
        throw new GemfireRestException("Data referenced in from clause is not available for querying!", qite);
    } catch (QueryExecutionLowMemoryException qelme) {
        throw new GemfireRestException("Query execution gets canceled due to low memory conditions and the resource manager critical heap percentage has been set!", qelme);
    } catch (Exception e) {
        throw new GemfireRestException("Server has encountered while executing Adhoc query!", e);
    }
}
Also used : GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) ResourceNotFoundException(org.apache.geode.rest.internal.web.exception.ResourceNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with GemfireRestException

use of org.apache.geode.rest.internal.web.exception.GemfireRestException in project geode by apache.

the class AbstractBaseController method getMembers.

protected Set<DistributedMember> getMembers(final String... memberIdNames) {
    ValidationUtils.returnValueThrowOnNull(memberIdNames, new GemfireRestException("No member found to run function"));
    final Set<DistributedMember> targetedMembers = new HashSet<>(ArrayUtils.length(memberIdNames));
    final List<String> memberIdNameList = Arrays.asList(memberIdNames);
    InternalCache cache = getCache();
    Set<DistributedMember> distMembers = cache.getDistributedSystem().getAllOtherMembers();
    // Add the local node to list
    distMembers.add(cache.getDistributedSystem().getDistributedMember());
    for (DistributedMember member : distMembers) {
        if (memberIdNameList.contains(member.getId()) || memberIdNameList.contains(member.getName())) {
            targetedMembers.add(member);
        }
    }
    return targetedMembers;
}
Also used : GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) HashSet(java.util.HashSet)

Example 8 with GemfireRestException

use of org.apache.geode.rest.internal.web.exception.GemfireRestException in project geode by apache.

the class AbstractBaseController method getValue.

protected <T> T getValue(final String regionNamePath, final Object key, boolean postProcess) {
    Assert.notNull(key, "The Cache Region key to read the value for cannot be null!");
    Region r = getRegion(regionNamePath);
    try {
        Object value = r.get(key);
        if (postProcess) {
            return (T) securityService.postProcess(regionNamePath, key, value, false);
        } else {
            return (T) value;
        }
    } catch (SerializationException se) {
        throw new DataTypeNotSupportedException("The resource identified could not convert into the supported content characteristics (JSON)!", se);
    } catch (NullPointerException npe) {
        throw new GemfireRestException(String.format("Resource (%1$s) configuration does not allow null keys!", regionNamePath), npe);
    } catch (IllegalArgumentException iae) {
        throw new GemfireRestException(String.format("Resource (%1$s) configuration does not allow requested operation on specified key!", regionNamePath), iae);
    } catch (LeaseExpiredException lee) {
        throw new GemfireRestException("Server has encountered error while processing this request!", lee);
    } catch (TimeoutException te) {
        throw new GemfireRestException("Server has encountered timeout error while processing this request!", te);
    } catch (CacheLoaderException cle) {
        throw new GemfireRestException("Server has encountered CacheLoader error while processing this request!", cle);
    } catch (PartitionedRegionStorageException prse) {
        throw new GemfireRestException("CacheLoader could not be invoked on partitioned region!", prse);
    }
}
Also used : GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) SerializationException(org.apache.geode.SerializationException) LeaseExpiredException(org.apache.geode.distributed.LeaseExpiredException) DataTypeNotSupportedException(org.apache.geode.rest.internal.web.exception.DataTypeNotSupportedException) PartitionedRegionStorageException(org.apache.geode.cache.PartitionedRegionStorageException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Region(org.apache.geode.cache.Region) JSONObject(org.json.JSONObject) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 9 with GemfireRestException

use of org.apache.geode.rest.internal.web.exception.GemfireRestException in project geode by apache.

the class AbstractBaseController method processQueryResponse.

ResponseEntity<String> processQueryResponse(Query query, Object[] args, Object queryResult) throws JSONException {
    if (queryResult instanceof Collection<?>) {
        Collection processedResults = new ArrayList(((Collection) queryResult).size());
        for (Object result : (Collection) queryResult) {
            processedResults.add(securityService.postProcess(null, null, result, false));
        }
        String queryResultAsJson = JSONUtils.convertCollectionToJson(processedResults);
        final HttpHeaders headers = new HttpHeaders();
        headers.setLocation(toUri("queries", query.getQueryString()));
        return new ResponseEntity<>(queryResultAsJson, headers, HttpStatus.OK);
    } else {
        throw new GemfireRestException("Server has encountered error while generating query result into restful format(JSON)!");
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) JSONObject(org.json.JSONObject)

Example 10 with GemfireRestException

use of org.apache.geode.rest.internal.web.exception.GemfireRestException in project geode by apache.

the class AbstractBaseControllerJUnitTest method testConvertErrorAsJsonT.

/**
   * Method: convertErrorAsJson(Throwable t)
   */
@Test
public void testConvertErrorAsJsonT() throws Exception {
    String message = "This is an error message";
    Exception e = new GemfireRestException(message, new NullPointerException());
    String json = abstractBaseController.convertErrorAsJson(e);
    ErrorMessage errorMessage = mapper.readValue(json, ErrorMessage.class);
    assertEquals(message, errorMessage.message);
}
Also used : GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

GemfireRestException (org.apache.geode.rest.internal.web.exception.GemfireRestException)10 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 JSONObject (org.json.JSONObject)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ArrayList (java.util.ArrayList)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 HashMap (java.util.HashMap)2 List (java.util.List)2 LowMemoryException (org.apache.geode.cache.LowMemoryException)2 Execution (org.apache.geode.cache.execute.Execution)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)2 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)2 Query (org.apache.geode.cache.query.Query)2 QueryExecutionLowMemoryException (org.apache.geode.cache.query.QueryExecutionLowMemoryException)2