use of org.springframework.http.HttpStatus in project Flare-event-calendar by PollubCafe.
the class GlobalExceptionHandler method handleMethodArgumentNotValid.
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
BindingResult bindingResult = ex.getBindingResult();
List<ApiFieldError> apiFieldErrors = bindingResult.getFieldErrors().stream().map(fieldError -> new ApiFieldError(fieldError.getField(), fieldError.getDefaultMessage())).collect(toList());
ApiErrorsView apiErrorsView = new ApiErrorsView(apiFieldErrors);
return new ResponseEntity<>(apiErrorsView, HttpStatus.UNPROCESSABLE_ENTITY);
}
use of org.springframework.http.HttpStatus in project molgenis by molgenis.
the class ExceptionHandlerUtilsTest method testHandleExceptionNonHtmlRequest.
@Test
public void testHandleExceptionNonHtmlRequest() {
HandlerMethod handlerMethod = mock(HandlerMethod.class);
when(handlerMethod.hasMethodAnnotation(ResponseBody.class)).thenReturn(true);
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
assertEquals(handleException(new Exception("message"), handlerMethod, httpStatus, null, PRODUCTION), new ResponseEntity<>(ErrorMessageResponse.create("message"), httpStatus));
}
use of org.springframework.http.HttpStatus in project molgenis by molgenis.
the class ExceptionHandlerUtilsTest method testHandleExceptionHtmlRequest.
@SuppressWarnings("unchecked")
@Test
public void testHandleExceptionHtmlRequest() {
HandlerMethod handlerMethod = mock(HandlerMethod.class);
Class beanType = ExceptionHandlerUtils.class;
when(handlerMethod.getBeanType()).thenReturn(beanType);
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
Map<String, Object> expectedModel = new HashMap<>();
expectedModel.put("errorMessageResponse", ErrorMessageResponse.create("message"));
expectedModel.put("httpStatusCode", 500);
ModelAndView expectedModelAndView = new ModelAndView("view-exception", expectedModel, httpStatus);
Object modelAndView = handleException(new Exception("message"), handlerMethod, httpStatus, null, PRODUCTION);
assertTrue(EqualsBuilder.reflectionEquals(modelAndView, expectedModelAndView));
}
use of org.springframework.http.HttpStatus in project seldon-core by SeldonIO.
the class RestClientController method prediction.
@RequestMapping(value = "/api/v0.1/predictions", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> prediction(RequestEntity<String> requestEntity, Principal principal) {
String clientId = principal.getName();
String json = requestEntity.getBody();
logger.info(String.format("[%s] [%s] [%s] [%s]", "POST", requestEntity.getUrl().getPath(), clientId, json));
SeldonMessage request;
try {
SeldonMessage.Builder builder = SeldonMessage.newBuilder();
ProtoBufUtils.updateMessageBuilderFromJson(builder, requestEntity.getBody());
request = builder.build();
} catch (InvalidProtocolBufferException e) {
logger.error("Bad request", e);
throw new SeldonAPIException(ApiExceptionType.APIFE_INVALID_JSON, requestEntity.getBody());
}
HttpStatus httpStatus = HttpStatus.OK;
// At present passes JSON string. Could use gRPC?
String ret = predictionService.predict(json, clientId);
SeldonMessage response;
try {
SeldonMessage.Builder builder = SeldonMessage.newBuilder();
ProtoBufUtils.updateMessageBuilderFromJson(builder, ret);
response = builder.build();
} catch (InvalidProtocolBufferException e) {
logger.error("Bad response", e);
throw new SeldonAPIException(ApiExceptionType.APIFE_INVALID_RESPONSE_JSON, requestEntity.getBody());
}
kafkaProducer.send(clientId, RequestResponse.newBuilder().setRequest(request).setResponse(response).build());
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON);
ResponseEntity<String> responseEntity = new ResponseEntity<String>(ret, responseHeaders, httpStatus);
return responseEntity;
}
use of org.springframework.http.HttpStatus in project account-identity by cryptofiat.
the class AccountMapperController method authenticateIdCard.
@ApiOperation(value = "Associate created address with ID card")
@RequestMapping(method = POST, value = "/authorisations/idCards", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<AccountActivationResponse> authenticateIdCard(@Valid @RequestBody AuthenticateCommand authenticateCommand, Principal principal) throws JSONException, IOException {
// if (!principal) throw SOME CUSTOM EXCEPTION OF CARD NOT PRESENT
String ownerId = principal.getName();
HttpStatus status = HttpStatus.OK;
String txHash = new String();
EthereumAccount account = new EthereumAccount();
List<EthereumAccount> existingAccounts = accountManagementService.getAccountsByAccountAddress(Hex.toHexString(authenticateCommand.getAccountAddress()));
if (existingAccounts.size() == 0) {
account = accountManagementService.storeNewAccount(Hex.toHexString(authenticateCommand.getAccountAddress()), ownerId, AuthorisationType.ID_CARD);
if (accountActivationEnabled) {
try {
txHash = ethereumService.activateEthereumAccount(account.getAddress());
accountManagementService.markActivated(account, txHash);
} catch (IOException e) {
log.error("failed to activate account " + account.getAddress() + " on Ethereum", e);
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
}
} else {
log.error("Refusing to activate account: {0} binding(s) found already", existingAccounts.size());
status = HttpStatus.BAD_REQUEST;
}
return new ResponseEntity<>(AccountActivationResponse.builder().authenticationStatus(AuthenticationStatus.LOGIN_SUCCESS.name()).ownerId(ownerId).transactionHash(txHash).escrowTransfers(clearEscrow(account)).build(), status);
}
Aggregations