Package org.apache.wicket.markup.html.pages

Source Code of org.apache.wicket.markup.html.pages.BrowserInfoPage$PostBackForm

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.wicket.markup.html.pages;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.IClusterable;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.ClientProperties;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.request.ClientInfo;
import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;
import org.apache.wicket.settings.IRequestCycleSettings;


/**
* <p>
* This page uses a form post right after the page has loaded in the browser,
* using JavaScript or alternative means to detect and pass on settings to the
* embedded form. The form submit method updates this session's
* {@link org.apache.wicket.request.ClientInfo} object and then redirects to the
* original location as was passed in as a URL argument in the constructor.
* </p>
* <p>
* This page is being used by the default implementation of
* {@link WebRequestCycle#newClientInfo}, which in turn uses
* {@link IRequestCycleSettings#getGatherExtendedBrowserInfo() a setting} to
* determine whether this page should be redirected to (it does when it is
* true).
* </p>
*
* @author Eelco Hillenius
*/
public class BrowserInfoPage extends WebPage
{
  /**
   * Holds properties of the client.
   */
  public static class ClientPropertiesBean implements IClusterable
  {
    private static final long serialVersionUID = 1L;

    private String navigatorAppCodeName;
    private String navigatorAppName;
    private String navigatorAppVersion;
    private Boolean navigatorCookieEnabled = Boolean.FALSE;
    private Boolean navigatorJavaEnabled = Boolean.FALSE;
    private String navigatorLanguage;
    private String navigatorPlatform;
    private String navigatorUserAgent;
    private String screenColorDepth;
    private String screenHeight;
    private String screenWidth;
    private String utcOffset;
    private String browserWidth;
    private String browserHeight;

    /**
     * Gets browserHeight.
     *
     * @return browserHeight
     */
    public String getBrowserHeight()
    {
      return browserHeight;
    }

    /**
     * Gets browserWidth.
     *
     * @return browserWidth
     */
    public String getBrowserWidth()
    {
      return browserWidth;
    }

    /**
     * Gets navigatorAppCodeName.
     *
     * @return navigatorAppCodeName
     */
    public String getNavigatorAppCodeName()
    {
      return navigatorAppCodeName;
    }

    /**
     * Gets navigatorAppName.
     *
     * @return navigatorAppName
     */
    public String getNavigatorAppName()
    {
      return navigatorAppName;
    }

    /**
     * Gets navigatorAppVersion.
     *
     * @return navigatorAppVersion
     */
    public String getNavigatorAppVersion()
    {
      return navigatorAppVersion;
    }

    /**
     * Gets navigatorCookieEnabled.
     *
     * @return navigatorCookieEnabled
     */
    public Boolean getNavigatorCookieEnabled()
    {
      return navigatorCookieEnabled;
    }

    /**
     * Gets navigatorJavaEnabled.
     *
     * @return navigatorJavaEnabled
     */
    public Boolean getNavigatorJavaEnabled()
    {
      return navigatorJavaEnabled;
    }

    /**
     * Gets navigatorLanguage.
     *
     * @return navigatorLanguage
     */
    public String getNavigatorLanguage()
    {
      return navigatorLanguage;
    }

    /**
     * Gets navigatorPlatform.
     *
     * @return navigatorPlatform
     */
    public String getNavigatorPlatform()
    {
      return navigatorPlatform;
    }

    /**
     * Gets navigatorUserAgent.
     *
     * @return navigatorUserAgent
     */
    public String getNavigatorUserAgent()
    {
      return navigatorUserAgent;
    }

    /**
     * Gets screenColorDepth.
     *
     * @return screenColorDepth
     */
    public String getScreenColorDepth()
    {
      return screenColorDepth;
    }

    /**
     * Gets screenHeight.
     *
     * @return screenHeight
     */
    public String getScreenHeight()
    {
      return screenHeight;
    }

    /**
     * Gets screenWidth.
     *
     * @return screenWidth
     */
    public String getScreenWidth()
    {
      return screenWidth;
    }

    /**
     * Gets utcOffset.
     *
     * @return utcOffset
     */
    public String getUtcOffset()
    {
      return utcOffset;
    }

    /**
     * Merge this with the given properties object.
     *
     * @param properties
     *            the properties object to merge with
     */
    public void merge(ClientProperties properties)
    {
      properties.setNavigatorAppName(navigatorAppName);
      properties.setNavigatorAppVersion(navigatorAppVersion);
      properties.setNavigatorAppCodeName(navigatorAppCodeName);
      properties.setCookiesEnabled((navigatorCookieEnabled != null) ? navigatorCookieEnabled
          .booleanValue() : false);
      properties.setJavaEnabled((navigatorJavaEnabled != null) ? navigatorJavaEnabled
          .booleanValue() : false);
      properties.setNavigatorLanguage(navigatorLanguage);
      properties.setNavigatorPlatform(navigatorPlatform);
      properties.setNavigatorUserAgent(navigatorUserAgent);
      properties.setScreenWidth(getInt(screenWidth));
      properties.setScreenHeight(getInt(screenHeight));
      properties.setBrowserWidth(getInt(browserWidth));
      properties.setBrowserHeight(getInt(browserHeight));
      properties.setScreenColorDepth(getInt(screenColorDepth));
      properties.setUtcOffset(utcOffset);
    }

    /**
     * Sets browserHeight.
     *
     * @param browserHeight
     *            browserHeight
     */
    public void setBrowserHeight(String browserHeight)
    {
      this.browserHeight = browserHeight;
    }

    /**
     * Sets browserWidth.
     *
     * @param browserWidth
     *            browserWidth
     */
    public void setBrowserWidth(String browserWidth)
    {
      this.browserWidth = browserWidth;
    }

    /**
     * Sets navigatorAppCodeName.
     *
     * @param navigatorAppCodeName
     *            navigatorAppCodeName
     */
    public void setNavigatorAppCodeName(String navigatorAppCodeName)
    {
      this.navigatorAppCodeName = navigatorAppCodeName;
    }

    /**
     * Sets navigatorAppName.
     *
     * @param navigatorAppName
     *            navigatorAppName
     */
    public void setNavigatorAppName(String navigatorAppName)
    {
      this.navigatorAppName = navigatorAppName;
    }

    /**
     * Sets navigatorAppVersion.
     *
     * @param navigatorAppVersion
     *            navigatorAppVersion
     */
    public void setNavigatorAppVersion(String navigatorAppVersion)
    {
      this.navigatorAppVersion = navigatorAppVersion;
    }

    /**
     * Sets navigatorCookieEnabled.
     *
     * @param navigatorCookieEnabled
     *            navigatorCookieEnabled
     */
    public void setNavigatorCookieEnabled(Boolean navigatorCookieEnabled)
    {
      this.navigatorCookieEnabled = navigatorCookieEnabled;
    }

    /**
     * Sets navigatorJavaEnabled.
     *
     * @param navigatorJavaEnabled
     *            navigatorJavaEnabled
     */
    public void setNavigatorJavaEnabled(Boolean navigatorJavaEnabled)
    {
      this.navigatorJavaEnabled = navigatorJavaEnabled;
    }

    /**
     * Sets navigatorLanguage.
     *
     * @param navigatorLanguage
     *            navigatorLanguage
     */
    public void setNavigatorLanguage(String navigatorLanguage)
    {
      this.navigatorLanguage = navigatorLanguage;
    }

    /**
     * Sets navigatorPlatform.
     *
     * @param navigatorPlatform
     *            navigatorPlatform
     */
    public void setNavigatorPlatform(String navigatorPlatform)
    {
      this.navigatorPlatform = navigatorPlatform;
    }

    /**
     * Sets navigatorUserAgent.
     *
     * @param navigatorUserAgent
     *            navigatorUserAgent
     */
    public void setNavigatorUserAgent(String navigatorUserAgent)
    {
      this.navigatorUserAgent = navigatorUserAgent;
    }

    /**
     * Sets screenColorDepth.
     *
     * @param screenColorDepth
     *            screenColorDepth
     */
    public void setScreenColorDepth(String screenColorDepth)
    {
      this.screenColorDepth = screenColorDepth;
    }

    /**
     * Sets screenHeight.
     *
     * @param screenHeight
     *            screenHeight
     */
    public void setScreenHeight(String screenHeight)
    {
      this.screenHeight = screenHeight;
    }

    /**
     * Sets screenWidth.
     *
     * @param screenWidth
     *            screenWidth
     */
    public void setScreenWidth(String screenWidth)
    {
      this.screenWidth = screenWidth;
    }

    /**
     * Sets utcOffset.
     *
     * @param utcOffset
     *            utcOffset
     */
    public void setUtcOffset(String utcOffset)
    {
      this.utcOffset = utcOffset;
    }

    private int getInt(String value)
    {
      int intValue = -1;
      try
      {
        intValue = Integer.parseInt(value);
      }
      catch (NumberFormatException e)
      {
        // Do nothing
      }
      return intValue;
    }
  }

  /**
   * Form for posting JavaScript properties.
   */
  private final class PostBackForm extends Form
  {
    private static final long serialVersionUID = 1L;

    /**
     * Construct.
     *
     * @param id
     *            component id
     */
    public PostBackForm(String id)
    {
      super(id, new CompoundPropertyModel(new ClientPropertiesBean()));

      add(new TextField("navigatorAppName"));
      add(new TextField("navigatorAppVersion"));
      add(new TextField("navigatorAppCodeName"));
      add(new TextField("navigatorCookieEnabled"));
      add(new TextField("navigatorJavaEnabled"));
      add(new TextField("navigatorLanguage"));
      add(new TextField("navigatorPlatform"));
      add(new TextField("navigatorUserAgent"));
      add(new TextField("screenWidth"));
      add(new TextField("screenHeight"));
      add(new TextField("screenColorDepth"));
      add(new TextField("utcOffset"));
      add(new TextField("browserWidth"));
      add(new TextField("browserHeight"));
    }

    /**
     * @see org.apache.wicket.markup.html.form.Form#onSubmit()
     */
    protected void onSubmit()
    {
      ClientPropertiesBean propertiesBean = (ClientPropertiesBean)getModelObject();

      WebRequestCycle requestCycle = (WebRequestCycle)getRequestCycle();
      WebSession session = (WebSession)getSession();
      ClientInfo clientInfo = session.getClientInfo();

      if (clientInfo == null)
      {
        clientInfo = new WebClientInfo(requestCycle);
        getSession().setClientInfo(clientInfo);
      }

      if (clientInfo instanceof WebClientInfo)
      {
        WebClientInfo info = (WebClientInfo)clientInfo;
        ClientProperties properties = info.getProperties();
        propertiesBean.merge(properties);
      }
      else
      {
        warnNotUsingWebClientInfo(clientInfo);
      }

      continueToPrevious();
    }
  }

  /** log. */
  private static final Log log = LogFactory.getLog(BrowserInfoPage.class);

  private static final long serialVersionUID = 1L;

  /** the url to continue to after this page. */
  private String continueTo;

  /**
   * Bookmarkable constructor. This is not for normal framework client use. It
   * will be called whenever Javascript is not supported, and the browser info
   * page's meta refresh fires to this page. Prior to this, the other
   * constructor should already have been called.
   *
   * @param parameters
   *            page parameters with the original url in it
   */
  public BrowserInfoPage(PageParameters parameters)
  {
    String to = (String)parameters.get("cto");
    if (to == null)
    {
      throw new IllegalArgumentException("parameter cto must be provided!");
    }
    setContinueTo(to);
    initComps();
    WebRequestCycle requestCycle = (WebRequestCycle)getRequestCycle();
    WebSession session = (WebSession)getSession();
    ClientInfo clientInfo = session.getClientInfo();
    if (clientInfo == null)
    {
      clientInfo = new WebClientInfo(requestCycle);
      getSession().setClientInfo(clientInfo);
    }
    else if (clientInfo instanceof WebClientInfo)
    {
      WebClientInfo info = (WebClientInfo)clientInfo;
      ClientProperties properties = info.getProperties();
      properties.setJavaEnabled(false);
    }
    else
    {
      warnNotUsingWebClientInfo(clientInfo);
    }
    continueToPrevious();
  }

  /**
   * Constructor. The page will redirect to the given url after waiting for
   * the given number of seconds.
   *
   * @param continueTo
   *            the url to redirect to when the browser info is handled
   */
  public BrowserInfoPage(final String continueTo)
  {
    if (continueTo == null)
    {
      throw new IllegalArgumentException("Argument continueTo must be not null");
    }
    setContinueTo(continueTo);
    initComps();
  }

  /**
   * @see org.apache.wicket.Component#isVersioned()
   */
  public boolean isVersioned()
  {
    return false;
  }

  /**
   * Adds components.
   */
  private final void initComps()
  {
    WebComponent meta = new WebComponent("meta");
    PageParameters parameters = new PageParameters();
    parameters.put("cto", continueTo);
    CharSequence url = urlFor(new BookmarkablePageRequestTarget(BrowserInfoPage.class,
        parameters));
    meta.add(new AttributeModifier("content", true, new Model("0; url=" + url)));
    add(meta);
    WebMarkupContainer link = new WebMarkupContainer("link");
    link.add(new AttributeModifier("href", true, new Model((Serializable)url)));
    add(link);
    add(new PostBackForm("postback"));
  }

  /**
   * Log a warning that for in order to use this page, you should really be
   * using {@link WebClientInfo}.
   *
   * @param clientInfo
   *            the actual client info object
   */
  private void warnNotUsingWebClientInfo(ClientInfo clientInfo)
  {
    log.warn("using " + getClass().getName() + " makes no sense if you are not using "
        + WebClientInfo.class.getName() + " (you are using "
        + clientInfo.getClass().getName() + " instead)");
  }

  /**
   * Continue to the location previous to this interception.
   */
  protected final void continueToPrevious()
  {
    // continue to original distination
    RequestCycle requestCycle = getRequestCycle();
    // Since we are explicitly redirecting to a page already, we do not
    // want a second redirect to occur automatically
    requestCycle.setRedirect(false);
    // Redirect there
    Response response = requestCycle.getResponse();
    response.reset();
    response.redirect(continueTo);
  }

  /**
   * Set the url to continue to after this page.
   *
   * @param continueTo
   *            the url
   */
  protected final void setContinueTo(String continueTo)
  {
    this.continueTo = continueTo;
  }
}
TOP

Related Classes of org.apache.wicket.markup.html.pages.BrowserInfoPage$PostBackForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.