Search in sources :

Example 1 with ServerHttpResponse

use of org.springframework.http.server.ServerHttpResponse in project spring-security-oauth by spring-projects.

the class DefaultOAuth2ExceptionRenderer method handleHttpEntityResponse.

public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
    if (responseEntity == null) {
        return;
    }
    HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
    HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
    if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
        ((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
    }
    HttpHeaders entityHeaders = responseEntity.getHeaders();
    if (!entityHeaders.isEmpty()) {
        outputMessage.getHeaders().putAll(entityHeaders);
    }
    Object body = responseEntity.getBody();
    if (body != null) {
        writeWithMessageConverters(body, inputMessage, outputMessage);
    } else {
        // flush headers
        outputMessage.getBody();
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 2 with ServerHttpResponse

use of org.springframework.http.server.ServerHttpResponse in project cetc by DiscoverForever.

the class WebsocketConfiguration method httpSessionHandshakeInterceptor.

@Bean
public HandshakeInterceptor httpSessionHandshakeInterceptor() {
    return new HandshakeInterceptor() {

        @Override
        public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
            if (request instanceof ServletServerHttpRequest) {
                ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
                attributes.put(IP_ADDRESS, servletRequest.getRemoteAddress());
            }
            return true;
        }

        @Override
        public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        }
    };
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) Map(java.util.Map) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) Bean(org.springframework.context.annotation.Bean)

Example 3 with ServerHttpResponse

use of org.springframework.http.server.ServerHttpResponse in project ArTEMiS by ls1intum.

the class WebsocketConfiguration method httpSessionHandshakeInterceptor.

@Bean
public HandshakeInterceptor httpSessionHandshakeInterceptor() {
    return new HandshakeInterceptor() {

        @Override
        public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
            if (request instanceof ServletServerHttpRequest) {
                ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
                attributes.put(IP_ADDRESS, servletRequest.getRemoteAddress());
            }
            return true;
        }

        @Override
        public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        }
    };
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) Bean(org.springframework.context.annotation.Bean)

Example 4 with ServerHttpResponse

use of org.springframework.http.server.ServerHttpResponse 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 5 with ServerHttpResponse

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

the class ResponseBodyEmitterReturnValueHandler method handleReturnValue.

@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    Assert.state(response != null, "No HttpServletResponse");
    ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
    if (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        returnType = returnType.nested();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    Assert.state(request != null, "No ServletRequest");
    ResponseBodyEmitter emitter;
    if (returnValue instanceof ResponseBodyEmitter) {
        emitter = (ResponseBodyEmitter) returnValue;
    } else {
        emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest);
        if (emitter == null) {
            // Not streaming: write headers without committing response..
            outputMessage.getHeaders().forEach((headerName, headerValues) -> {
                for (String headerValue : headerValues) {
                    response.addHeader(headerName, headerValue);
                }
            });
            return;
        }
    }
    emitter.extendResponse(outputMessage);
    // At this point we know we're streaming..
    ShallowEtagHeaderFilter.disableContentCaching(request);
    // Wrap the response to ignore further header changes
    // Headers will be flushed at the first write
    outputMessage = new StreamingServletServerHttpResponse(outputMessage);
    HttpMessageConvertingHandler handler;
    try {
        DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
        WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
        handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
    } catch (Throwable ex) {
        emitter.initializeWithError(ex);
        throw ex;
    }
    emitter.initialize(handler);
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DelegatingServerHttpResponse(org.springframework.http.server.DelegatingServerHttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Aggregations

ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)14 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)9 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)9 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)8 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)4 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 ResponseEntity (org.springframework.http.ResponseEntity)3 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)3 ServletException (jakarta.servlet.ServletException)2 ServletRequest (jakarta.servlet.ServletRequest)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 Map (java.util.Map)2 Bean (org.springframework.context.annotation.Bean)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)1 InstanceRegistry (de.codecentric.boot.admin.server.services.InstanceRegistry)1 AdminController (de.codecentric.boot.admin.server.web.AdminController)1 HttpHeaderFilter (de.codecentric.boot.admin.server.web.HttpHeaderFilter)1 InstanceWebProxy (de.codecentric.boot.admin.server.web.InstanceWebProxy)1