/**
   * Handles the HTTP request by generating a Message and sending it to the request channel. If this
   * gateway's 'expectReply' property is true, it will also generate a response from the reply
   * Message once received. That response will be written by the {@link HttpMessageConverter}s.
   */
  public final void handleRequest(
      HttpServletRequest servletRequest, HttpServletResponse servletResponse)
      throws ServletException, IOException {
    Object responseContent = null;
    Message<?> responseMessage;

    final ServletServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    final ServletServerHttpResponse response = new ServletServerHttpResponse(servletResponse);

    try {
      responseMessage = super.doHandleRequest(servletRequest, servletResponse);
      if (responseMessage != null) {
        responseContent = setupResponseAndConvertReply(response, responseMessage);
      }
    } catch (Exception e) {
      responseContent = handleExceptionInternal(e);
    }
    if (responseContent != null) {

      if (responseContent instanceof HttpStatus) {
        response.setStatusCode((HttpStatus) responseContent);
      } else {
        this.writeResponse(responseContent, response, request.getHeaders().getAccept());
      }
    }
  }
Пример #2
0
  public void handleReturnValue(
      Object returnValue,
      MethodParameter returnType,
      ModelAndViewContainer mavContainer,
      NativeWebRequest webRequest)
      throws Exception {

    mavContainer.setRequestHandled(true);

    if (returnValue == null) {
      return;
    }

    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);

    Assert.isInstanceOf(HttpEntity.class, returnValue);
    HttpEntity<?> responseEntity = (HttpEntity<?>) returnValue;
    if (responseEntity instanceof ResponseEntity) {
      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, returnType, inputMessage, outputMessage);
    } else {
      // flush headers to the HttpServletResponse
      outputMessage.getBody();
    }
  }