From 6b7e7d9b5cb83d5bd5e9cb62473b4d1bdd67f1a6 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 7 Sep 2011 09:07:09 +0000 Subject: [PATCH] SPR-5973: UriBuilder --- .../servlet/support/ServletUriBuilder.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriBuilder.java diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriBuilder.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriBuilder.java new file mode 100644 index 00000000000..536c23efbf7 --- /dev/null +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriBuilder.java @@ -0,0 +1,54 @@ +/* + * 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.web.servlet.support; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.Assert; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.util.UriBuilder; + +/** @author Arjen Poutsma */ +public class ServletUriBuilder extends UriBuilder { + + + public static ServletUriBuilder fromCurrentServletRequest() { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + Assert.state(requestAttributes != null, "Could not find current RequestAttributes in RequestContextHolder"); + Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes); + + HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest(); + Assert.state(servletRequest != null, "Could not find current HttpServletRequest in ServletRequestAttributes"); + return fromServletRequest(servletRequest); + } + + public static ServletUriBuilder fromServletRequest(HttpServletRequest request) { + Assert.notNull(request, "'request' must not be null"); + + ServletUriBuilder builder = new ServletUriBuilder(); + builder.scheme(request.getScheme()); + builder.host(request.getServerName()); + builder.port(request.getServerPort()); + builder.path(request.getRequestURI()); +// builder.query(request.getQueryString()); + + return builder; + } + +}