INT-2035 Support Simple Cookie Handling

Initial commit - TODO: namspace.

Saves off Set-Cookie header in http_transferredCookie MessageHeader.

Transferred cookies are converted to Cookie: headers on subsequent
outbound messages, thus handling 3 or more interactions.

INT-2035 Namespace, Docs

Change boolean name; add namspace and documentation.

INT-2035 Docs

Added docs for cookie handling.

INT-2035 Polishing

Simplify implementation to just transfer Set-Cookie to
Cookie in the inbound side.

INT-2035 XSD Docs

Updated to reflect new implementation.
This commit is contained in:
Gary Russell
2011-11-12 08:18:15 -05:00
committed by Mark Fisher
parent 497d07bdad
commit a61b3617f3
9 changed files with 262 additions and 9 deletions

View File

@@ -35,7 +35,8 @@
reply-channel="replies"
charset="UTF-8"
order="77"
auto-startup="false">
auto-startup="false"
transfer-cookies="true">
<uri-variable name="foo" expression="headers.bar"/>
</outbound-gateway>

View File

@@ -79,6 +79,7 @@ public class HttpOutboundGatewayParserTests {
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));
}
@Test
@@ -125,6 +126,7 @@ public class HttpOutboundGatewayParserTests {
assertTrue(ObjectUtils.containsElement(mappedRequestHeaders, "requestHeader1"));
assertTrue(ObjectUtils.containsElement(mappedRequestHeaders, "requestHeader2"));
assertEquals("responseHeader", mappedResponseHeaders[0]);
assertEquals(true, handlerAccessor.getPropertyValue("transferCookies"));
}

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:channel id="ch1" />
<int-http:outbound-gateway url="http://localhost:18080/it/" request-channel="ch1" reply-channel="ch2"
transfer-cookies="true"
request-factory="requestFactory"
/>
<bean id="requestFactory" class="org.springframework.integration.http.outbound.CookieTests$RequestFactory" />
<int:transformer input-channel="ch2" output-channel="ch3" expression="'Hello, again!'" />
<int-http:outbound-gateway url="http://localhost:18080/it/" request-channel="ch3" reply-channel="ch4"
transfer-cookies="true"
request-factory="requestFactory"
/>
<int:transformer input-channel="ch4" output-channel="ch5" expression="'Hello, once more!'" />
<int-http:outbound-gateway url="http://localhost:18080/it/" request-channel="ch5" reply-channel="ch6"
transfer-cookies="true"
request-factory="requestFactory"
/>
<int:channel id="ch6">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,148 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.http.outbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @since 2.1
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class CookieTests {
@Autowired
private MessageChannel ch1;
@Autowired
private QueueChannel ch6;
private static ByteArrayOutputStream bos = new ByteArrayOutputStream();
private static List<HttpHeaders> allHeaders = new ArrayList<HttpHeaders>();
@Test
public void testCookie() throws Exception {
ch1.send(new GenericMessage<String>("Hello, world!"));
Assert.assertNotNull(ch6.receive());
bos.close();
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bos.toByteArray())));
String line = br.readLine();
assertEquals("Hello, world!Hello, again!Hello, once more!", line);
assertNull(br.readLine());
br.close();
assertEquals(3, allHeaders.size());
assertFalse(allHeaders.get(0).containsKey("Cookie"));
assertTrue(allHeaders.get(1).containsKey("Cookie"));
assertEquals("JSESSIONID=X123", allHeaders.get(1).get("Cookie").get(0));
assertTrue(allHeaders.get(2).containsKey("Cookie"));
assertEquals("JSESSIONID=X124", allHeaders.get(2).get("Cookie").get(0));
}
public static class RequestFactory implements ClientHttpRequestFactory {
private int count = 123;
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return new ClientHttpRequest() {
private HttpHeaders headers = new HttpHeaders();
public HttpHeaders getHeaders() {
return headers;
}
public OutputStream getBody() throws IOException {
return bos;
}
public URI getURI() {
return null;
}
public HttpMethod getMethod() {
return null;
}
public ClientHttpResponse execute() throws IOException {
allHeaders.add(headers);
return new ClientHttpResponse() {
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Set-cookie", "JSESSIONID=X" + count++); // test case insensitivity
headers.set("Content-Length", "2");
headers.set("Content-Type", "text/plain");
return headers;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("OK".getBytes());
}
public String getStatusText() throws IOException {
return "OK";
}
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
public void close() {
}
};
}
};
}
}
}