Steffen Steffen - 2 months ago 11
Java Question

java.util.Collections$UnmodifiableRandomAccessList to Collections.singletonList

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)

Answer

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