Skip to content

Commit d2a8df7

Browse files
Merge pull request #881 from MCMicS/AXIS2-6083
AXIS2-6083: Cookie Handling not work with 2.0.0
2 parents 63d4acb + ce68ba8 commit d2a8df7

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

modules/transport/http/src/main/java/org/apache/axis2/transport/http/impl/httpclient5/RequestImpl.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020

2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.net.URISyntaxException;
2423
import java.net.URI;
25-
import java.net.URL;
26-
import java.time.LocalDateTime;
2724
import java.util.ArrayList;
2825
import java.util.HashMap;
29-
import java.util.Iterator;
3026
import java.util.List;
3127
import java.util.Map;
3228

@@ -56,9 +52,11 @@
5652
import org.apache.hc.core5.http.HttpHost;
5753
import org.apache.hc.core5.http.HttpVersion;
5854
import org.apache.hc.core5.http.ProtocolVersion;
59-
import org.apache.hc.core5.http.ProtocolException;
55+
import org.apache.hc.core5.http.HeaderElement;
56+
import org.apache.hc.core5.http.message.BasicHeaderValueParser;
6057
import org.apache.hc.core5.http.message.HeaderGroup;
6158
import org.apache.hc.core5.http.io.entity.EntityUtils;
59+
import org.apache.hc.core5.http.message.ParserCursor;
6260
import org.apache.hc.core5.net.URIAuthority;
6361
import org.apache.hc.core5.util.Timeout;
6462

@@ -195,8 +193,17 @@ public Header[] getResponseHeaders() {
195193
public Map<String,String> getCookies() {
196194
Map<String,String> cookies = new HashMap<>();
197195
for (String name : COOKIE_HEADER_NAMES) {
198-
for (final org.apache.hc.core5.http.Header header : response.getHeaders()) {
199-
cookies.put(header.getName(), header.getValue());
196+
for (final org.apache.hc.core5.http.Header header : response.getHeaders(name)) {
197+
final String headerValue = header.getValue();
198+
if (headerValue == null) {
199+
continue;
200+
}
201+
final ParserCursor cursor = new ParserCursor(0, headerValue.length());
202+
final HeaderElement[] headerElements = BasicHeaderValueParser.INSTANCE.parseElements(headerValue,
203+
cursor);
204+
for (final HeaderElement headerElement : headerElements) {
205+
cookies.put(headerElement.getName(), headerElement.getValue());
206+
}
200207
}
201208
}
202209
return cookies;

modules/transport/http/src/test/java/org/apache/axis2/transport/http/HTTPSenderTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.axis2.context.ConfigurationContextFactory;
2525
import org.apache.axis2.context.MessageContext;
2626
import org.apache.axis2.context.OperationContext;
27+
import org.apache.axis2.context.ServiceContext;
28+
import org.apache.axis2.kernel.http.HTTPConstants;
2729
import org.apache.axis2.transport.http.mock.server.AbstractHTTPServerTest;
2830
import org.apache.axis2.transport.http.mock.server.BasicHttpServer;
2931

@@ -58,13 +60,16 @@ public abstract class HTTPSenderTest extends AbstractHTTPServerTest {
5860
* @throws IOException
5961
* Signals that an I/O exception has occurred.
6062
*/
61-
protected void sendViaHTTP(String httpMethod, String soapAction, String address, boolean rest)
63+
protected MessageContext sendViaHTTP(String httpMethod, String soapAction, String address, boolean rest)
6264
throws IOException {
6365
httpSender = getHTTPSender();
66+
ServiceContext serviceContext = new ServiceContext();
6467
MessageContext msgContext = new MessageContext();
68+
msgContext.setServiceContext(serviceContext);
6569
ConfigurationContext configContext = ConfigurationContextFactory
6670
.createEmptyConfigurationContext();
6771
OperationContext opContext = new OperationContext();
72+
opContext.setParent(serviceContext);
6873

6974
msgContext.setConfigurationContext(configContext);
7075
msgContext.setEnvelope(getEnvelope());
@@ -73,7 +78,7 @@ protected void sendViaHTTP(String httpMethod, String soapAction, String address,
7378
msgContext.setOperationContext(opContext);
7479
URL url = new URL(address);
7580
httpSender.send(msgContext, url, soapAction);
76-
81+
return msgContext;
7782
}
7883

7984
/**
@@ -303,6 +308,17 @@ public void testHandleResponseHTTPStatusCode500() throws Exception {
303308
sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService",
304309
"http://localhost:" + port + "/postService", true);
305310
}
311+
312+
public void testCookiesAreObtainedAfterRequest() throws Exception {
313+
httpSender = getHTTPSender();
314+
int port = getBasicHttpServer().getPort();
315+
getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_COOKIE);
316+
final MessageContext mc = sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService",
317+
"http://localhost:" + port + "/postService", true);
318+
319+
assertEquals("Cookie was not set", "JSESSIONID=abcde12345",
320+
mc.getProperty(HTTPConstants.COOKIE_STRING));
321+
}
306322

307323

308324
}

modules/transport/http/src/test/java/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public interface BasicHttpServer {
153153
public static final String RESPONSE_HTTP_202 = "response.http.202";
154154
public static final String RESPONSE_HTTP_400 = "response.http.400";
155155
public static final String RESPONSE_HTTP_500 = "response.http.500";
156+
public static final String RESPONSE_HTTP_COOKIE = "response.http.cookie";
156157

157158

158159
}

modules/transport/http/src/test/java/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Map;
3535
import java.util.concurrent.TimeUnit;
3636

37+
import org.apache.axis2.kernel.http.HTTPConstants;
3738
import org.apache.hc.core5.http.Header;
3839
import org.apache.hc.core5.http.HttpEntity;
3940
import org.apache.hc.core5.http.HttpException;
@@ -263,6 +264,10 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
263264
response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
264265
body = HttpEntities.create(outStream -> outStream.write((" Server Error").getBytes(StandardCharsets.UTF_8)), ContentType.TEXT_HTML.withCharset(StandardCharsets.UTF_8));
265266

267+
} else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_COOKIE)) {
268+
response.setCode(HttpStatus.SC_OK);
269+
response.addHeader(HTTPConstants.HEADER_SET_COOKIE, "JSESSIONID=abcde12345; Path=/; HttpOnly");
270+
body = HttpEntities.create(outStream -> outStream.write(("<Response>Cookie should be set<Response>").getBytes(StandardCharsets.UTF_8)), ContentType.TEXT_HTML.withCharset(StandardCharsets.UTF_8));
266271
}
267272

268273
if (body != null) {

0 commit comments

Comments
 (0)