2015-02-10 53 views
1

我正在處理其他應用程序,並且我的其他應用程序總是通過傳遞一些參數通過http headers來調用。而且我的rest應用程序中有一個過濾器被調用每個請求並從http頭中檢索參數,如下所示。如何通過HTTP集成動態使用spring集成傳遞值

@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); 
} 

我的其他應用程序反過來使用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; 
} 

應用的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> 

我也有一個攔截器添加參數以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","[email protected]"); 
    connection1.addRequestProperty("user-name","ws_user1"); 
    return true; 
} 

但我需要通過「用戶電子郵件」和「用戶名」,而不是動態靜態之一,即,這是我在過濾器中已經接收到的數據。任何人都可以幫助我解決這個問題。提前致謝。

回答

1

解決方法是在攔截器中使用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; 
} 

然後,在你的集成流程,添加到chain ...

<int:service-activator [email protected](#root, headers['user'], headers['email'])" /> 

(其中MyHolder是一個簡單的java bean)。表達式可以是任何你想要的(不只是頭部訪問)。或者,只需傳入Message<?>並在代碼中內部確定用戶/電子郵件。