2082

how to pass values through http headers dynamically using spring integration

Question:

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.

Answer1:

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.

Recommend

  • POSTMAN - Schema validation is passed even for bad response data
  • Extract string between xml tags in android without parsing the xml
  • spring controller json receive json List
  • Spring Long Polling with DeferredResult
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • Spring MVC @RequestParam a list of objects
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • Django Haystack Rebuild Index
  • Does Apportable support to build library binary (.a/.so)?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Switching to Release Build causes runtime error in Web Reference
  • jquery mobile loadPage not working
  • QuartzCore.framework for Mono Develop
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • Add sale price programmatically to product variations
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?