001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.http.request;
018
019import java.util.Locale;
020
021import org.apache.wicket.core.request.ClientInfo;
022import org.apache.wicket.markup.html.pages.BrowserInfoPage;
023import org.apache.wicket.protocol.http.ClientProperties;
024import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
025import org.apache.wicket.request.cycle.RequestCycle;
026
027
028/**
029 * Default client info object for web applications.
030 * 
031 * @author Eelco Hillenius
032 */
033public class WebClientInfo extends ClientInfo
034{
035        private static final long serialVersionUID = 1L;
036
037        /**
038         * The user agent string from the User-Agent header, app. Theoretically, this might differ from
039         * {@link org.apache.wicket.protocol.http.ClientProperties#isNavigatorJavaEnabled()} property, which is
040         * not set until an actual reply from a browser (e.g. using {@link BrowserInfoPage} is set.
041         */
042        private final String userAgent;
043
044        /** Client properties object. */
045        private final ClientProperties properties;
046
047        /**
048         * Construct.
049         * 
050         * @param requestCycle
051         *            the request cycle
052         */
053        public WebClientInfo(RequestCycle requestCycle)
054        {
055                this(requestCycle, new ClientProperties());
056        }
057
058        /**
059         * Construct.
060         * 
061         * @param requestCycle
062         *            the request cycle
063         */
064        public WebClientInfo(RequestCycle requestCycle, ClientProperties properties)
065        {
066                this(requestCycle, ((ServletWebRequest)requestCycle.getRequest()).getContainerRequest()
067                        .getHeader("User-Agent"), properties);
068        }
069
070        /**
071         * Construct.
072         * 
073         * @param requestCycle
074         *            the request cycle
075         * @param userAgent
076         *            The User-Agent string
077         */
078        public WebClientInfo(final RequestCycle requestCycle, final String userAgent)
079        {
080                this(requestCycle, userAgent, new ClientProperties());
081        }
082
083        /**
084         * Construct.
085         * 
086         * @param requestCycle
087         *            the request cycle
088         * @param userAgent
089         *            The User-Agent string
090         * @param properties
091         *                        properties of client            
092         */
093        public WebClientInfo(final RequestCycle requestCycle, final String userAgent, final ClientProperties properties)
094        {
095                super();
096
097                this.userAgent = userAgent;
098
099                this.properties = properties;
100                properties.setRemoteAddress(getRemoteAddr(requestCycle));
101        }
102
103        /**
104         * Gets the client properties object.
105         * 
106         * @return the client properties object
107         */
108        public final ClientProperties getProperties()
109        {
110                return properties;
111        }
112
113        /**
114         * returns the user agent string.
115         * 
116         * @return the user agent string
117         */
118        public final String getUserAgent()
119        {
120                return userAgent;
121        }
122
123        /**
124         * returns the user agent string (lower case).
125         * 
126         * @return the user agent string
127         */
128        private String getUserAgentStringLc()
129        {
130                return (getUserAgent() != null) ? getUserAgent().toLowerCase(Locale.ROOT) : "";
131        }
132
133        /**
134         * Returns the IP address from {@code HttpServletRequest.getRemoteAddr()}.
135         *
136         * @param requestCycle
137         *            the request cycle
138         * @return remoteAddr IP address of the client, using
139         *         {@code getHttpServletRequest().getRemoteAddr()}
140         */
141        protected String getRemoteAddr(RequestCycle requestCycle)
142        {
143                ServletWebRequest request = (ServletWebRequest)requestCycle.getRequest();
144                return request.getContainerRequest().getRemoteAddr();
145        }
146}