Search in sources :

Example 86 with HttpStatus

use of org.springframework.http.HttpStatus in project cuba by cuba-platform.

the class ServicesController method invokeServiceMethodGet.

@GetMapping("/{serviceName}/{methodName}")
public ResponseEntity<String> invokeServiceMethodGet(@PathVariable String serviceName, @PathVariable String methodName, @RequestParam(required = false) String modelVersion, @RequestParam Map<String, String> paramsMap) {
    ServicesControllerManager.ServiceCallResult result = servicesControllerManager.invokeServiceMethodGet(serviceName, methodName, paramsMap, modelVersion);
    HttpStatus status;
    if (result == null) {
        status = HttpStatus.NO_CONTENT;
        result = new ServicesControllerManager.ServiceCallResult("", false);
    } else {
        status = HttpStatus.OK;
    }
    String contentType = result.isValidJson() ? "application/json;charset=UTF-8" : "text/plain;charset=UTF-8";
    return ResponseEntity.status(status).header("Content-Type", contentType).body(result.getStringValue());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ServicesControllerManager(com.haulmont.restapi.service.ServicesControllerManager)

Example 87 with HttpStatus

use of org.springframework.http.HttpStatus in project hub-alert by blackducksoftware.

the class LoginHandlerTest method authenticateUserWithIntegrationRestExceptionTest.

@Test
public void authenticateUserWithIntegrationRestExceptionTest() throws IntegrationException {
    final LoginActions loginActions = Mockito.mock(LoginActions.class);
    final LoginHandler loginHandler = new LoginHandler(objectTransformer, loginActions, csrfTokenRepository);
    final HttpStatus responseCode = HttpStatus.BAD_GATEWAY;
    Mockito.when(loginActions.authenticateUser(Mockito.any(), Mockito.any())).thenThrow(new IntegrationRestException(responseCode.value(), "", ""));
    final ResponseEntity<String> response = loginHandler.authenticateUser(null, null, null);
    assertEquals(responseCode, response.getStatusCode());
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) HttpStatus(org.springframework.http.HttpStatus) LoginActions(com.blackducksoftware.integration.hub.alert.web.actions.LoginActions) Test(org.junit.Test)

Example 88 with HttpStatus

use of org.springframework.http.HttpStatus in project webofneeds by researchstudio-sat.

the class LinkedDataRestBridge method createRestTemplateForReadingLinkedData.

private RestTemplate createRestTemplateForReadingLinkedData(String webID) throws Exception {
    RestTemplate template = CryptographyUtils.createSslRestTemplate(this.keyStoreService.getUnderlyingKeyStore(), this.keyStoreService.getPassword(), new PredefinedAliasPrivateKeyStrategy(keyPairAliasDerivationStrategy.getAliasForNeedUri(webID)), this.trustStoreService.getUnderlyingKeyStore(), this.trustStrategy, readTimeout, connectionTimeout, true);
    // prevent the RestTemplate from throwing an exception when the server responds with 4xx or 5xx status
    // because we want to hand the orginal response back to the original caller in BridgeForLinkedDataController
    template.setErrorHandler(new DefaultResponseErrorHandler() {

        @Override
        protected boolean hasError(final HttpStatus statusCode) {
            return false;
        }
    });
    return template;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) HttpStatus(org.springframework.http.HttpStatus) RestTemplate(org.springframework.web.client.RestTemplate) PredefinedAliasPrivateKeyStrategy(won.cryptography.ssl.PredefinedAliasPrivateKeyStrategy)

Example 89 with HttpStatus

use of org.springframework.http.HttpStatus in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Example 90 with HttpStatus

use of org.springframework.http.HttpStatus in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)165 ResponseEntity (org.springframework.http.ResponseEntity)43 HttpHeaders (org.springframework.http.HttpHeaders)38 Test (org.junit.jupiter.api.Test)26 MediaType (org.springframework.http.MediaType)17 Mono (reactor.core.publisher.Mono)17 IOException (java.io.IOException)16 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)15 Collections (java.util.Collections)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 URI (java.net.URI)12 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 Map (java.util.Map)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Resource (org.springframework.core.io.Resource)9 HashMap (java.util.HashMap)8