1

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

  • What isn't added? Also you should be adding a cookie (which ultimately is a header, but still). – M. Deinum Oct 28 '20 at 15:16
  • The cookie I add (with the value "iam=****") creates a second cookie when there should be only one Cookie header, with the different values separeted by ; . There is no post.addCookie(), the only thing I can do is .addHeader() – Minemosynne Oct 28 '20 at 15:22
  • Then get the current value (`getHeaders`), and append it and then `setHeader`, instead of `addHeader`. Or setup Apache HttpClient with a Cookie Manager, and set a value in there so it will be added to the outgoing request. – M. Deinum Oct 28 '20 at 15:35
  • I tried getting the headers first, but it doesn't change anything. Since the other cookie (JSESSIONID) is in the cookie store, when I do a getHeaders() it isn't there and it adds my cookie as a header. The best would be to access the cookie store and add the cookie, but I can't find how to access it – Minemosynne Oct 30 '20 at 08:38

1 Answers1

1

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);
                    }
                });
  • in my case I got class cast exception in line with httpClient = (cannot cast to HttpUrlConnectionMessageSender so, my solution is: `new ObjectFactory().createCall(call), webServiceMessage -> { TransportContext transportContext = TransportContextHolder.getTransportContext(); HttpUrlConnection connection = (HttpUrlConnection) transportContext.getConnection(); connection.addRequestHeader("name", "value"); }` – Kirill Mikhailov Dec 21 '22 at 11:38