3

How to I convert a java.util.Collections$UnmodifiableRandomAccessList to a Collections.singletonList? In a attempt to store session between two services, i found this, but I cant figure out the step in between. First get the cookie info that i need to set:

Map<String, Collections> headerInfo = (Map<String, Collections>)
 ((BindingProvider) port).getResponseContext()
                         .get(MessageContext.HTTP_RESPONSE_HEADERS);

Now I can get the cookie info i need; If I do a

System.out.println(headerInfo.get("Set-Cookie"));

I get something like this

Set-Cookie=[PHPSESSID=rpsnc2g7o4ltbr6l9qus177p14; path=/];

Now I just need to do this:

((BindingProvider) port2).getRequestContext()
   .put(MessageContext.HTTP_REQUEST_HEADERS, 
      Collections.singletonMap("Cookie", Collections.singletonList(cookieValue)));

But I can not figure out how to get from headerInfo.get("Set-Cookie") to: cookieValue

This is the question I found the first part of my problems solution in Q:
JAX-WS client: maintain session/cookies across multiple services
(It might explain my problem a bit too)

Community
  • 1
  • 1
Steffen
  • 132
  • 1
  • 1
  • 10
  • 1
    Why do you need to do the conversion at all, instead of just keeping the original list? – Louis Wasserman Dec 09 '13 at 04:32
  • If i attemt using the original list: ((BindingProvider)customer).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,Collections.singletonMap("Cookie", headerInfo.get("Set-Cookie"))); This Exception is thrown: java.util.Collections$UnmodifiableRandomAccessList cannot be cast to java.util.Collections – Steffen Dec 09 '13 at 08:28
  • Update: You are right, I did not need the conversion, just the right typecast. Thank you! – Steffen Dec 09 '13 at 09:51

1 Answers1

5

The solution was to use the original list by casting to the correct class/interface:

List<String>

instead of:

Collections

worked.

Map<String, List<String>> headers = (Map<String, List<String>>)((BindingProvider) authPort).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
List<String> setCookie = (List<String>) headers.get("Set-Cookie");
((BindingProvider) servicePort).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,Collections.singletonMap("Cookie", setCookie ));
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Steffen
  • 132
  • 1
  • 1
  • 10