【问题标题】:How to get HttpServletRequest in apache Camel based spring-ws EIP flow如何在基于 apache Camel 的 spring-ws EIP 流中获取 HttpServletRequest
【发布时间】:2014-04-29 15:13:23
【问题描述】:

我有一个简单的 Apache Camel EIP 流程,其中入口点是 spring-ws Web 服务。如何访问服务请求的 HttpServletRequest 对象?

from("spring-ws:uri:http://localhost:8080/test-ws-1.0/ws/TestService?endpointMapping=#endpointMapping")
.log("body is:\n${body}")
.process(new HttpRequestParserProcessor())

我希望能够从 HttpRequestParserProcessor 中获取请求的用户主体名称和请求的远程 IP 地址。当我调试处理器时,Exchange 的 in 对象显示为 SpringWebserviceMessage 对象。

提前致谢, 下午。

【问题讨论】:

  • 对于 HTTP 组件,这是有效的:HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);。也许你可以试一试。
  • 嗨@Peter,在我的例子中,body 是一个 SpringWebserviceMessage 对象,如果我尝试按照你的建议进行调用,我会收到请求为空。
  • 对不起,我之前的评论有误。调试时exchange.getIn()是SpringWebserviceMessage对象,exchange.getIn().getBody()是DOMSource对象。
  • 使用 CXF 你可以试试org.apache.cxf.message.Message cxfMessage = in.getHeader(CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class);ServletRequest request = (ServletRequest)cxfMessage.get("HTTP.REQUEST");,见svn.apache.org/repos/asf/camel/trunk/components/camel-cxf/src/…
  • 我为您的问题添加了答案,包括我找到的 Spring Webservice 解决方案。

标签: apache-camel spring-ws


【解决方案1】:

来自http://docs.spring.io/spring-ws/sites/1.5/reference/html/common.html

TransportContext context = TransportContextHolder.getTransportContext();
HttpServletConnection connection = (HttpServletConnection )context.getConnection();
HttpServletRequest request = connection.getHttpServletRequest();
String ipAddress = request.getRemoteAddr();

或者使用 CXF 代替 Spring-WS。 CXF 与 Camel 来自同一个厨房,因此可以更好地集成。使用 CXF,您可以访问 ServletRequest,如下所示(请参阅 here):

org.apache.cxf.message.Message cxfMessage = in.getHeader(CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class);
ServletRequest request = (ServletRequest)cxfMessage.get("HTTP.REQUEST");

【讨论】:

  • 在这个类中声明了常量:org.apache.cxf.transport.http.AbstractHTTPDestination:public static final String HTTP_REQUEST = "HTTP.REQUEST";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
相关资源
最近更新 更多