I have to add an information in the cookie of a request that my application sends to another application, but it doesn't seem to be added correctly.
When I check the request with WireShark, I see two Cookie headers in the headers :
POST /service HTTP/1.1
Accept-Encoding: gzip
Cookie: iam=**************************
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: text/xml; charset=utf-8
Content-Length: 128393
Host: host-dev:9999
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_271)
Cookie: JSESSIONID=***********************
Authorization: Basic **************************
(I've changed some of the info)
In my code I have this :
@Service
public class ESignatureSoapConnector extends WebServiceGatewaySupport {
private ObjectFactory objectFactory;
@Autowired
public ESignatureSoapConnector(ESignatureMarshaller marshaller, ConfigurationProperties configurationProperties) throws Exception {
this.setMarshaller(marshaller);
this.setUnmarshaller(marshaller);
this.setDefaultUri(configurationProperties.getBaseUrl());
this.setMessageSender(buildMessageSender(configurationProperties.getUsername(), configurationProperties.getPassword()));
this.objectFactory = new ObjectFactory();
}
public ESignatureResponse signDocument(MTOMFile file, String iamCookieValue) {
ESignature request = new ESignature();
request.setInputDocument(file);
JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
.marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
@Override
public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpComponentsConnection connection = (HttpComponentsConnection) context.getConnection();
HttpPost post = connection.getHttpPost();
post.addHeader("Cookie", "iam=" + iamCookieValue);
}
});
return response.getValue();
}
private WebServiceMessageSender buildMessageSender(String username, String password) throws Exception {
...
}
}
I'm assuming the way I set the cookie isn't correct but I can't find the proper way to do it. The value for the cookie is different for each request, it's a soap request and I work in Spring
The solution we've found :
JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
.marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
@Override
public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
HttpClient httpClient = ((HttpComponentsMessageSender) getWebServiceTemplate().getMessageSenders()[0]).getHttpClient();
BasicClientCookie iamCookie = new BasicClientCookie(iamConfigurationProperties.getCookieName(), iamCookieValue);
iamCookie.setDomain(iamConfigurationProperties.getCookieDomain());
iamCookie.setPath(iamConfigurationProperties.getCookiePath());
((DefaultHttpClient) httpClient).getCookieStore().addCookie(iamCookie);
}
});