how to pass values through http headers dynamically using spring integration

你说的曾经没有我的故事 提交于 2019-12-11 12:03:43

问题


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.

 @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.

@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

<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.

 @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, i.e, the data which I have received in the filter . Can anybody help me out in solving this issue. Thanks in Advance.


回答1:


On solution would be to use a ThreadLocal in your interceptor, then set the headers before invoking the gateway.

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...

<int:service-activator expression=@interceptorBean.setUserInfo(#root, headers['user'], headers['email'])" />

(where MyHolder is a simple 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.



来源:https://stackoverflow.com/questions/28434487/how-to-pass-values-through-http-headers-dynamically-using-spring-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!