From ae56f3a36122e28c3214c4662bbbe71f69362752 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 18 May 2010 11:12:54 +0000 Subject: [PATCH] SPR-7211 - Extend HttpMessage interface to expose requestURL --- .../http/client/ClientHttpRequest.java | 9 ++++++++- .../http/client/CommonsClientHttpRequest.java | 13 ++++++++++++- .../http/client/SimpleClientHttpRequest.java | 13 ++++++++++++- .../http/server/ServerHttpRequest.java | 11 ++++++++++- .../http/server/ServletServerHttpRequest.java | 17 +++++++++++++++-- .../AbstractHttpRequestFactoryTestCase.java | 4 +++- .../http/server/ServletHttpRequestTests.java | 17 ++++++++++++++--- 7 files changed, 74 insertions(+), 10 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java index 7b4521c61aa..ec3549ff8ca 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -17,6 +17,7 @@ package org.springframework.http.client; import java.io.IOException; +import java.net.URI; import org.springframework.http.HttpMethod; import org.springframework.http.HttpOutputMessage; @@ -39,6 +40,12 @@ public interface ClientHttpRequest extends HttpOutputMessage { */ HttpMethod getMethod(); + /** + * Return the URI of the request. + * @return the URI of the request + */ + URI getURI(); + /** * Execute this request, resulting in a {@link ClientHttpResponse} that can be read. * @return the response result of the execution diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java index b10d663929e..22080b99cad 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -17,11 +17,13 @@ package org.springframework.http.client; import java.io.IOException; +import java.net.URI; import java.util.List; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; import org.apache.commons.httpclient.methods.RequestEntity; @@ -55,6 +57,15 @@ final class CommonsClientHttpRequest extends AbstractClientHttpRequest { return HttpMethod.valueOf(httpMethod.getName()); } + public URI getURI() { + try { + return URI.create(httpMethod.getURI().getEscapedURI()); + } + catch (URIException ex) { + throw new IllegalStateException("Could not get HttpMethod URI: " + ex.getMessage(), ex); + } + } + @Override public ClientHttpResponse executeInternal(HttpHeaders headers, byte[] output) throws IOException { for (Map.Entry> entry : headers.entrySet()) { diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java index 9b7a92a37c0..5efc1cc6e23 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -18,6 +18,8 @@ package org.springframework.http.client; import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; import java.util.List; import java.util.Map; @@ -47,6 +49,15 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest { return HttpMethod.valueOf(this.connection.getRequestMethod()); } + public URI getURI() { + try { + return this.connection.getURL().toURI(); + } + catch (URISyntaxException ex) { + throw new IllegalStateException("Could not get HttpURLConnection URI: " + ex.getMessage(), ex); + } + } + @Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { for (Map.Entry> entry : headers.entrySet()) { diff --git a/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java index 339c4d445bf..8896aa833ef 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -16,6 +16,8 @@ package org.springframework.http.server; +import java.net.URI; + import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpMethod; @@ -33,4 +35,11 @@ public interface ServerHttpRequest extends HttpInputMessage { */ HttpMethod getMethod(); + /** + * Return the URI of the request. + * @return the URI of the request + */ + URI getURI(); + + } diff --git a/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 00ab9b5de04..a3c67885940 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -18,12 +18,14 @@ package org.springframework.http.server; import java.io.IOException; import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; -import org.springframework.util.Assert; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.util.Assert; /** * {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}. @@ -52,6 +54,17 @@ public class ServletServerHttpRequest implements ServerHttpRequest { return HttpMethod.valueOf(this.servletRequest.getMethod()); } + public URI getURI() { + try { + return new URI(servletRequest.getScheme(), null, servletRequest.getServerName(), + servletRequest.getServerPort(), servletRequest.getRequestURI(), + servletRequest.getQueryString(), null); + } + catch (URISyntaxException ex) { + throw new IllegalStateException("Could not get HttpServletRequest URI: " + ex.getMessage(), ex); + } + } + public HttpHeaders getHeaders() { if (this.headers == null) { this.headers = new HttpHeaders(); diff --git a/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java b/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java index c7fa64c632b..9f53c5d2336 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java +++ b/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java @@ -83,9 +83,11 @@ public abstract class AbstractHttpRequestFactoryTestCase { @Test public void status() throws Exception { + URI uri = new URI(BASE_URL + "/status/notfound"); ClientHttpRequest request = - factory.createRequest(new URI(BASE_URL + "/status/notfound"), HttpMethod.GET); + factory.createRequest(uri, HttpMethod.GET); assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod()); + assertEquals("Invalid HTTP URI", uri, request.getURI()); ClientHttpResponse response = request.execute(); assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode()); } diff --git a/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java index e090b899396..1b43f80cbca 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -16,16 +16,17 @@ package org.springframework.http.server; +import java.net.URI; import java.util.List; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.util.FileCopyUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.util.FileCopyUtils; /** * @author Arjen Poutsma @@ -48,6 +49,16 @@ public class ServletHttpRequestTests { assertEquals("Invalid method", HttpMethod.POST, request.getMethod()); } + @Test + public void getURI() throws Exception { + URI uri = new URI("http://example.com/path?query"); + mockRequest.setServerName(uri.getHost()); + mockRequest.setServerPort(uri.getPort()); + mockRequest.setRequestURI(uri.getPath()); + mockRequest.setQueryString(uri.getQuery()); + assertEquals("Invalid uri", uri, request.getURI()); + } + @Test public void getHeaders() throws Exception { String headerName = "MyHeader";