Search in sources :

Example 91 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-integration-samples by spring-projects.

the class MultipartClientForHttpOutboundClient method main.

public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/http-outbound-config.xml");
    Resource s2logo = new ClassPathResource(resourcePath);
    Map<String, Object> multipartMap = new HashMap<String, Object>();
    multipartMap.put("company", new String[] { "SpringSource", "VMWare" });
    multipartMap.put("company-logo", s2logo);
    logger.info("Created multipart request: " + multipartMap);
    MultipartRequestGateway requestGateway = context.getBean("requestGateway", MultipartRequestGateway.class);
    HttpStatus reply = requestGateway.postMultipartRequest(multipartMap);
    System.out.println("Replied with HttpStatus code: " + reply);
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HashMap(java.util.HashMap) HttpStatus(org.springframework.http.HttpStatus) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 92 with HttpStatus

use of org.springframework.http.HttpStatus in project molgenis by molgenis.

the class ExceptionHandlerUtilsTest method testHandleExceptionHtmlRequestDev.

@SuppressWarnings("unchecked")
@Test
public void testHandleExceptionHtmlRequestDev() {
    HandlerMethod handlerMethod = mock(HandlerMethod.class);
    Class beanType = ExceptionHandlerUtils.class;
    when(handlerMethod.getBeanType()).thenReturn(beanType);
    HttpStatus httpStatus = HttpStatus.NOT_FOUND;
    Exception ex = mock(Exception.class);
    StackTraceElement[] stack = new StackTraceElement[] { new StackTraceElement("class", "method", "file", 1) };
    when(ex.getStackTrace()).thenReturn(stack);
    when(ex.getLocalizedMessage()).thenReturn("message");
    Map<String, Object> expectedModel = new HashMap<>();
    expectedModel.put("errorMessageResponse", ErrorMessageResponse.create("message"));
    expectedModel.put("httpStatusCode", 404);
    expectedModel.put("stackTrace", stack);
    ModelAndView expectedModelAndView = new ModelAndView("view-exception", expectedModel, httpStatus);
    Object modelAndView = handleException(ex, handlerMethod, httpStatus, null, DEVELOPMENT);
    assertTrue(EqualsBuilder.reflectionEquals(modelAndView, expectedModelAndView));
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HashMap(java.util.HashMap) ExceptionHandlerUtils(org.molgenis.web.exception.ExceptionHandlerUtils) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.testng.annotations.Test)

Example 93 with HttpStatus

use of org.springframework.http.HttpStatus in project data-prep by Talend.

the class CommandHelper method async.

public static ResponseEntity<Void> async(final GenericCommand<?> command) {
    final Observable<?> stream = command.toObservable();
    return stream.map(is -> {
        // copy all headers from the command response so that the mime-type is correctly forwarded. Command has
        // the correct headers due to call to toBlocking() below.
        final MultiValueMap<String, String> headers = new HttpHeaders();
        HttpStatus status = command.getStatus();
        for (Header header : command.getCommandResponseHeaders()) {
            headers.put(header.getName(), Collections.singletonList(header.getValue()));
        }
        return new ResponseEntity<Void>(null, headers, status);
    }).toBlocking().first();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Header(org.apache.http.Header) HttpStatus(org.springframework.http.HttpStatus)

Example 94 with HttpStatus

use of org.springframework.http.HttpStatus in project data-prep by Talend.

the class APIClientTest method getPrepMetadata.

public DataSetMetadata getPrepMetadata(String preparationId) throws IOException {
    DataSetMetadata metadata;
    // when
    Response transformedResponse = given().when().get("/api/preparations/{id}/metadata", preparationId);
    HttpStatus responseStatus = HttpStatus.valueOf(transformedResponse.getStatusCode());
    if (ACCEPTED.equals(responseStatus)) {
        // first time we have a 202 with a Location to see asynchronous method status
        final String asyncMethodStatusUrl = transformedResponse.getHeader("Location");
        waitForAsyncMethodTofinish(asyncMethodStatusUrl);
        Response response = // 
        given().when().expect().statusCode(200).log().ifError().get("/api/preparations/{id}/metadata", preparationId);
        metadata = mapper.readValue(response.asInputStream(), DataSetMetadata.class);
    } else if (OK.equals(responseStatus)) {
        metadata = mapper.readValue(transformedResponse.asInputStream(), DataSetMetadata.class);
    } else {
        throw new RuntimeException("Could not get preparation metadata. Response was: " + transformedResponse.print());
    }
    return metadata;
}
Also used : Response(com.jayway.restassured.response.Response) HttpStatus(org.springframework.http.HttpStatus) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) UserDataSetMetadata(org.talend.dataprep.dataset.service.UserDataSetMetadata)

Example 95 with HttpStatus

use of org.springframework.http.HttpStatus in project incubator-servicecomb-java-chassis by apache.

the class SpringmvcConsumerResponseMapper method mapResponse.

@Override
public Object mapResponse(Response response) {
    HttpStatus status = HttpStatus.valueOf(response.getStatusCode());
    HttpHeaders httpHeaders = null;
    Map<String, List<Object>> headers = response.getHeaders().getHeaderMap();
    if (headers != null) {
        httpHeaders = new HttpHeaders();
        for (Entry<String, List<Object>> entry : headers.entrySet()) {
            for (Object value : entry.getValue()) {
                httpHeaders.add(entry.getKey(), String.valueOf(value));
            }
        }
    }
    Object realResult = realMapper.mapResponse(response);
    return new ResponseEntity<>(realResult, httpHeaders, status);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List)

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