001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.context.request;
018:
019: import java.security.Principal;
020: import java.util.Locale;
021: import java.util.Map;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpSession;
025:
026: import org.springframework.util.StringUtils;
027:
028: /**
029: * {@link WebRequest} adapter for an {@link javax.servlet.http.HttpServletRequest}.
030: *
031: * @author Juergen Hoeller
032: * @since 2.0
033: */
034: public class ServletWebRequest extends ServletRequestAttributes
035: implements WebRequest {
036:
037: /**
038: * Create a new ServletWebRequest instance for the given request.
039: * @param request current HTTP request
040: */
041: public ServletWebRequest(HttpServletRequest request) {
042: super (request);
043: }
044:
045: public String getParameter(String paramName) {
046: return getRequest().getParameter(paramName);
047: }
048:
049: public String[] getParameterValues(String paramName) {
050: return getRequest().getParameterValues(paramName);
051: }
052:
053: public Map getParameterMap() {
054: return getRequest().getParameterMap();
055: }
056:
057: public Locale getLocale() {
058: return getRequest().getLocale();
059: }
060:
061: public String getContextPath() {
062: return getRequest().getContextPath();
063: }
064:
065: public String getRemoteUser() {
066: return getRequest().getRemoteUser();
067: }
068:
069: public Principal getUserPrincipal() {
070: return getRequest().getUserPrincipal();
071: }
072:
073: public boolean isUserInRole(String role) {
074: return getRequest().isUserInRole(role);
075: }
076:
077: public boolean isSecure() {
078: return getRequest().isSecure();
079: }
080:
081: public String getDescription(boolean includeClientInfo) {
082: HttpServletRequest request = getRequest();
083: StringBuffer buffer = new StringBuffer();
084: buffer.append("uri=").append(request.getRequestURI());
085: if (includeClientInfo) {
086: String client = request.getRemoteAddr();
087: if (StringUtils.hasLength(client)) {
088: buffer.append(";client=").append(client);
089: }
090: HttpSession session = request.getSession(false);
091: if (session != null) {
092: buffer.append(";session=").append(session.getId());
093: }
094: String user = request.getRemoteUser();
095: if (StringUtils.hasLength(user)) {
096: buffer.append(";user=").append(user);
097: }
098: }
099: return buffer.toString();
100: }
101:
102: public String toString() {
103: return "ServletWebRequest: " + getDescription(true);
104: }
105:
106: }
|