void checkReports(final Map<String, ValidationReport> reports) throws RestLiServiceException { final StringBuffer errorMsgs = new StringBuffer(); for (final Map.Entry<String, ValidationReport> reportEntry : reports.entrySet()) { final ValidationReport report = reportEntry.getValue(); if (!report.getErrorMsgs().isEmpty()) { errorMsgs.append("Validator " + reportEntry.getKey() + " reports errors: "); for (final String msg : report.getErrorMsgs()) { errorMsgs.append(msg + System.getProperty("line.separator")); } } } if (errorMsgs.length() > 0) { throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, errorMsgs.toString()); } }
if (project == null) { final String errorMsg = "Project '" + projectName + "' not found."; throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, errorMsg); final String errorMsg = "Installation Failed. Project '" + projectName + "' was already removed."; throw new RestLiServiceException(HttpStatus.S_410_GONE, errorMsg); + " has no permission to write to project " + project.getName(); logger.error(errorMsg); throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, errorMsg); final String errorMsg = "URL " + packageUrl + " is malformed."; logger.error(errorMsg, e); throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, errorMsg); FileUtils.deleteDirectory(tempDir); throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, errorMsg, e);
URI leaderUri = this.leaderFinderOpt.get().getLeaderMetadata().getUri(); RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_301_MOVED_PERMANENTLY, String.format("New leader <a href=\"%s\">%s</a>", leaderUri, leaderUri)); exception.setErrorDetails(new DataMap(ImmutableMap.of(LOCATION_301, leaderUri.toString()))); throw new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, "No configuration for the requested resource."); } catch (IOException ioe) {
@Override public Policy get(String resourceId) { try { ThrottlingPolicy throttlingPolicy = (ThrottlingPolicy) this.broker.getSharedResource(new ThrottlingPolicyFactory(), new SharedLimiterKey(resourceId)); Policy restliPolicy = new Policy(); restliPolicy.setPolicyName(throttlingPolicy.getClass().getSimpleName()); restliPolicy.setResource(resourceId); restliPolicy.setParameters(new StringMap(throttlingPolicy.getParameters())); restliPolicy.setPolicyDetails(throttlingPolicy.getDescription()); MetricContext resourceContext = (MetricContext) broker.getSharedResource(new MetricContextFactory(), new SubTaggedMetricContextKey(resourceId, ImmutableMap.of(RESOURCE_ID_TAG, resourceId))); StringMap metrics = new StringMap(); for (Map.Entry<String, Meter> meter : resourceContext.getMeters().entrySet()) { metrics.put(meter.getKey(), Double.toString(meter.getValue().getOneMinuteRate())); } restliPolicy.setMetrics(metrics); return restliPolicy; } catch (NotConfiguredException nce) { throw new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Policy not found for resource " + resourceId); } } }
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { long permits = request.getPermits(); long allocated = 0; try { if (limiter.acquirePermits(permits) != null) { allocated = permits; } else { throw new RestLiServiceException(HttpStatus.S_403_FORBIDDEN, "Not enough permits."); } } catch (InterruptedException ie) { // return no permits } PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(allocated); allocation.setExpiration(Long.MAX_VALUE); if (allocated <= 0) { allocation.setMinRetryDelayMillis(60000); } return allocation; }
private void validate() { if (_flashbackRunner == null) { throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "FlashbackRunner is not started "); } } }
@Action(name = "changeMatchRule") public void changeMatchRule(@ActionParam("matchRule") String matchRule) { validate(); MatchRule namedMatchRule = NamedMatchRule.fromString(matchRule); if (namedMatchRule == null) { throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Unknown match rule" + matchRule); } _flashbackRunner.setMatchRule(namedMatchRule); }
@Action(name = "changeScene") public void changeScene(@ActionParam("sceneName") String sceneName) { validate(); SceneConfiguration sceneConfiguration = new SceneConfiguration(_scenePath, _currSceneMode, sceneName); try { _flashbackRunner.setScene(SceneFactory.create(sceneConfiguration)); } catch (IOException e) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, e); } }
private void check(boolean condition) { if (!condition) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR); } }
@Override public Greeting call() throws Exception { throw new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED); } });
void validateAuth(String auth) { if (auth==null || ! auth.equals("PLEASE")) { throw new RestLiServiceException(HttpStatus.S_401_UNAUTHORIZED, "Invalid auth token"); } }
_currSceneMode = SceneMode.fromString(sceneMode); if (_currSceneMode == null) { throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Unknown scene mode" + sceneMode); throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Unknown match rule" + matchRule); _flashbackRunner.start(); } catch (IOException | InterruptedException e) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, e);
@Override public void onError(Throwable e) { callback.onError(e instanceof RestLiServiceException ? e : new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, e)); }
/** * An example action throwing an exception. */ @Action(name = "exceptionTest") public void exceptionTest() { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Test Exception"); } }
@Override public void onDrainComplete() { _topLevelCallback.onStreamError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR)); }
@Override public BatchUpdateResult<CompoundKey, Message> batchUpdate(BatchPatchRequest<CompoundKey, Message> patches) { if (!patches.getData().keySet().equals(DB.keySet())) { throw new RestLiServiceException(HttpStatus.S_417_EXPECTATION_FAILED); } return buildUpdateResult(patches.getData().keySet()); }
private void validate(GroupMembership membership) { if (membership.hasGroupID() || membership.hasMemberID()) { throw new RestLiServiceException(S_400_BAD_REQUEST, "groupID and memberID fields cannot be set while saving/updating a GroupMembership. " + "They are derived from the resource identifier"); } }
@Override public BatchUpdateResult<CompoundKey, Message> batchUpdate(BatchUpdateRequest<CompoundKey, Message> entities) { if (!entities.getData().equals(DB)) { throw new RestLiServiceException(HttpStatus.S_417_EXPECTATION_FAILED); } return buildUpdateResult(entities.getData().keySet()); }
private void validateSingleResponse(RestLiDataValidator validator, RecordTemplate entity) { ValidationResult result = validator.validateOutput(entity); if (!result.isValid()) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, result.getMessages().toString()); } }
@Action(name = "modifyCustomContext") public void modifyCustomContext(BaseResource resource) { java.util.Optional<Object> foo = resource.getContext().getCustomContextData("foo"); if (foo.isPresent() && foo.get().equals("bar")) { resource.getContext().putCustomContextData("foo", "newbar"); return; } throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR); } }