[英]how to pass values through http headers dynamically using spring integration
I am working on an rest application, and my rest application is always called by passing some parameters through http headers.And I have a filter in my rest application which gets invoked for every request and retrieves the parameters from http headers as shown below. 我正在开发一个rest应用程序,我的rest应用程序总是通过在HTTP标头中传递一些参数来调用的,并且我的rest应用程序中有一个过滤器,该过滤器会针对每个请求被调用并从http标头中检索参数,如下所示。
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest=(HttpServletRequest) request;
String email = httpServletRequest.getHeader("user-email");
String userName = httpServletRequest.getHeader("user-name");
chain.doFilter(request, response);
}
my rest application in turn calls an soap service using spring integration.And the code for calling the soap service is as below. 我的其余应用程序依次使用spring集成调用soap服务。调用soap服务的代码如下。
@RequestMapping(value = "/projects", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Project> getProject(HttpServletRequest httpRequest) {
GetAuthorizedWebSendTransferProjects request = new GetAuthorizedWebSendTransferProjects();
GetAuthorizedWebSendTransferProjectsResponse response = gw.getResponse(request);
JAXBElement<ArrayOfProjectContainer> arr = response.getGetAuthorizedWebSendTransferProjectsResult();
ArrayOfProjectContainer arr1 = arr.getValue();
List<ProjectContainer> arr2 = arr1.getProjectContainer();
List<Project> projects = getPopulatedProjectList(arr2);
return projects;
}
application-context.xml 应用程序的context.xml
<int:chain input-channel="requestChannel" output-channel="outputChannel">
<int-ws:header-enricher>
<int-ws:soap-action
value="http://tempuri.org/IPermissionService/GetAuthorizedWebSendTransferProjects"/>
</int-ws:header-enricher>
<int-ws:outbound-gateway
uri="http://10.255.2.51/PermissionService.svc?wsdl" marshaller="marshaller"
unmarshaller="marshaller" interceptor="addHttpHeaderInterceptor"/>
</int:chain>
I also have an interceptor to add parameters to http headers which is an static data. 我也有一个拦截器,可将参数添加到http标头,这是一个静态数据。
@Override
public boolean handleRequest(MessageContext messageContext)
throws WebServiceClientException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
HttpURLConnection connection1= connection.getConnection();
connection1.addRequestProperty("user-email","ws_user1@biopacstest.domain");
connection1.addRequestProperty("user-name","ws_user1");
return true;
}
But I need to pass the "user-email" and "user-name" dynamically instead of the static one, ie, the data which I have received in the filter . 但是我需要动态传递“ user-email”和“ user-name”,而不是静态传递,即我在过滤器中收到的数据。 Can anybody help me out in solving this issue.
谁能帮助我解决这个问题。 Thanks in Advance.
提前致谢。
On solution would be to use a ThreadLocal
in your interceptor, then set the headers before invoking the gateway. 解决方案是在拦截器中使用
ThreadLocal
,然后在调用网关之前设置标头。
private final ThreadLocal<MyHolder> holder = new ThreadLocal<MyHolder>();
@Override
public boolean handleRequest(MessageContext messageContext)
throws WebServiceClientException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
HttpURLConnection connection1= connection.getConnection();
MyHolder holder = this.holder.remove();
connection1.addRequestProperty("user-email", holder.getEmail());
connection1.addRequestProperty("user-name", holder.getUser());
return true;
}
public Message<?> setUserInfo(Message<?> message, String user, String email) {
this.holder.set(new MyHolder(user, email));
return message;
}
Then, in your integration flow, add to the chain
... 然后,在集成流程中,添加到
chain
...
<int:service-activator expression=@interceptorBean.setUserInfo(#root, headers['user'], headers['email'])" />
(where MyHolder is a simple java bean). (其中MyHolder是一个简单的Java Bean)。 The expressions can be anything you want (not just header access).
表达式可以是您想要的任何内容(不仅仅是标头访问)。 Or, just pass in the
Message<?>
and determine the user/email internally in the code. 或者,只需传递
Message<?>
并在代码中内部确定用户/电子邮件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.