Search in sources :

Example 1 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project cas by apereo.

the class JsonUtils method render.

/**
 * Render model and view.
 *
 * @param model    the model
 * @param response the response
 */
@SneakyThrows
public static void render(final Object model, final HttpServletResponse response) {
    final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setPrettyPrint(true);
    final MediaType jsonMimeType = MediaType.APPLICATION_JSON;
    jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) SneakyThrows(lombok.SneakyThrows)

Example 2 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project commons by craftercms.

the class HttpMessageConvertingResponseWriter method writeWithMessageConverters.

@SuppressWarnings("unchecked")
public <T> void writeWithMessageConverters(T returnValue, HttpServletRequest request, HttpServletResponse response) throws IOException, HttpMediaTypeNotAcceptableException {
    Class<?> returnValueClass = returnValue.getClass();
    List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(returnValueClass);
    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
    for (MediaType r : requestedMediaTypes) {
        for (MediaType p : producibleMediaTypes) {
            if (r.isCompatibleWith(p)) {
                compatibleMediaTypes.add(getMostSpecificMediaType(r, p));
            }
        }
    }
    if (compatibleMediaTypes.isEmpty()) {
        throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
    }
    List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(mediaTypes);
    MediaType selectedMediaType = null;
    for (MediaType mediaType : mediaTypes) {
        if (mediaType.isConcrete()) {
            selectedMediaType = mediaType;
            break;
        } else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) {
            selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
            break;
        }
    }
    if (selectedMediaType != null) {
        selectedMediaType = selectedMediaType.removeQualityValue();
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
                ((HttpMessageConverter<T>) messageConverter).write(returnValue, selectedMediaType, new ServletServerHttpResponse(response));
                logger.debug(LOG_KEY_WRITTEN_WITH_MESSAGE_CONVERTER, returnValue, selectedMediaType, messageConverter);
                return;
            }
        }
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ArrayList(java.util.ArrayList) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 3 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-boot-admin by codecentric.

the class InstancesProxyController method endpointProxy.

@ResponseBody
@RequestMapping(path = INSTANCE_MAPPED_PATH, method = { RequestMethod.GET, RequestMethod.HEAD, RequestMethod.POST, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.DELETE, RequestMethod.OPTIONS })
public void endpointProxy(@PathVariable("instanceId") String instanceId, HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    ServletServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    Flux<DataBuffer> requestBody = DataBufferUtils.readInputStream(request::getBody, this.bufferFactory, 4096);
    InstanceWebProxy.ForwardRequest fwdRequest = createForwardRequest(request, requestBody, this.adminContextPath + INSTANCE_MAPPED_PATH);
    this.instanceWebProxy.forward(this.registry.getInstance(InstanceId.of(instanceId)), fwdRequest, (clientResponse) -> {
        ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
        response.setStatusCode(clientResponse.statusCode());
        response.getHeaders().addAll(this.httpHeadersFilter.filterHeaders(clientResponse.headers().asHttpHeaders()));
        try {
            OutputStream responseBody = response.getBody();
            response.flush();
            return clientResponse.body(BodyExtractors.toDataBuffers()).window(1).concatMap((body) -> writeAndFlush(body, responseBody)).then();
        } catch (IOException ex) {
            return Mono.error(ex);
        }
    }).block();
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) AdminController(de.codecentric.boot.admin.server.web.AdminController) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaderFilter(de.codecentric.boot.admin.server.web.HttpHeaderFilter) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) InstanceRegistry(de.codecentric.boot.admin.server.services.InstanceRegistry) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) URI(java.net.URI) AntPathMatcher(org.springframework.util.AntPathMatcher) InstanceWebClient(de.codecentric.boot.admin.server.web.client.InstanceWebClient) OutputStream(java.io.OutputStream) PathMatcher(org.springframework.util.PathMatcher) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Flux(reactor.core.publisher.Flux) BodyExtractors(org.springframework.web.reactive.function.BodyExtractors) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HandlerMapping(org.springframework.web.servlet.HandlerMapping) InstanceWebProxy(de.codecentric.boot.admin.server.web.InstanceWebProxy) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) BodyInserters(org.springframework.web.reactive.function.BodyInserters) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) InstanceWebProxy(de.codecentric.boot.admin.server.web.InstanceWebProxy) OutputStream(java.io.OutputStream) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class ReactiveTypeHandlerTests method testEmitterContentType.

private void testEmitterContentType(String expected) throws Exception {
    ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse);
    ResponseBodyEmitter emitter = handleValue(Flux.empty(), Flux.class, forClass(String.class));
    emitter.extendResponse(message);
    assertThat(message.getHeaders().getContentType().toString()).isEqualTo(expected);
    resetRequest();
}
Also used : ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 5 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class ReactiveTypeHandlerTests method writeStreamJson.

@Test
public void writeStreamJson() throws Exception {
    this.servletRequest.addHeader("Accept", "application/x-ndjson");
    Sinks.Many<Bar> sink = Sinks.many().unicast().onBackpressureBuffer();
    ResponseBodyEmitter emitter = handleValue(sink.asFlux(), Flux.class, forClass(Bar.class));
    EmitterHandler emitterHandler = new EmitterHandler();
    emitter.initialize(emitterHandler);
    ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse);
    emitter.extendResponse(message);
    Bar bar1 = new Bar("foo");
    Bar bar2 = new Bar("bar");
    sink.tryEmitNext(bar1);
    sink.tryEmitNext(bar2);
    sink.tryEmitComplete();
    assertThat(message.getHeaders().getContentType().toString()).isEqualTo("application/x-ndjson");
    assertThat(emitterHandler.getValues()).isEqualTo(Arrays.asList(bar1, "\n", bar2, "\n"));
}
Also used : Sinks(reactor.core.publisher.Sinks) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)40 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)22 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7 Test (org.junit.jupiter.api.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)5 MediaType (org.springframework.http.MediaType)4 ResponseEntity (org.springframework.http.ResponseEntity)4 HashMap (java.util.HashMap)3 List (java.util.List)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 HttpHeaders (org.springframework.http.HttpHeaders)3 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)3 ServletException (jakarta.servlet.ServletException)2 ServletRequest (jakarta.servlet.ServletRequest)2