Package org.olat.course.nodes.tu

Source Code of org.olat.course.nodes.tu.TUConfigForm

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.course.nodes.tu;

import java.net.MalformedURLException;
import java.net.URL;

import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.PasswordElement;
import org.olat.core.gui.formelements.RadioButtonGroupElement;
import org.olat.core.gui.formelements.SpacerElement;
import org.olat.core.gui.formelements.StaticHTMLTextElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.formelements.VisibilityDependsOnSelectionRule;
import org.olat.core.gui.translator.Translator;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.util.StringHelper;
import org.olat.modules.ModuleConfiguration;

/**
* Description:<BR/>
* TODO: Class Description for TUConfigForm
* <P/>
* Initial Date:  Oct 12, 2004
*
* @author Felix Jost
* @author Lars Eberle (<a href="http://www.bps-system.de/">BPS Bildungsportal Sachsen GmbH</a>)
*/
public class TUConfigForm extends Form {
  /** config option: password */
  public static final String CONFIGKEY_PASS = "pass";
  /** config option: username */
  public static final String CONFIGKEY_USER = "user";
  /** config option: port */
  public static final String CONFIGKEY_PORT = "port";
  /** config option: uri */
  public static final String CONFIGKEY_URI = "uri";
  /** config option: query */
  public static final String CONFIGKEY_QUERY = "query";
  /** config option: hostname */
  public static final String CONFIGKEY_HOST = "host";
  /** config option: protocol */
  public static final String CONFIGKEY_PROTO = "proto";
  /** supported protocols */
  public static final String[] PROTOCOLS = new String[] {"http", "https"};
 
   /** Configuration key: use tunnel for iframe or display directly ("<iframe src='www.ethz.ch'></iframe>"). Values: true, false **/
  public static final String CONFIG_TUNNEL = "useframetunnel"; // don't change value, used in config
 
  /** Configuration key: display content in iframe: Values: true, false **/
  public static final String CONFIG_IFRAME = "iniframe";
 
  /** Configuration key: display content in new browser window: Values: true, false **/
  public static final String CONFIG_EXTERN = "extern";

 
  /*
   *  This are the options keys which are displayed to the user.
   *  They are only used inside this form and will not be saved
   *  anywhere, so feel free to change them...
   */
 
  private static final String OPTION_TUNNEL_THROUGH_OLAT_INLINE = "tunnelInline";
  private static final String OPTION_TUNNEL_THROUGH_OLAT_IFRAME = "tunnelIFrame";
  private static final String OPTION_SHOW_IN_OLAT_IN_AN_IFRAME  = "directIFrame";
  private static final String OPTION_SHOW_IN_NEW_BROWSER_WINDOW = "extern";
 
  /*
   *  StartPage Values are saved in config, so don't change them from true and false...
   */

  private static final String OPTION_SHOW_STARTPAGE = Boolean.TRUE.toString();
  private static final String OPTION_SKIP_STARTPAGE = Boolean.FALSE.toString();
 
  /*
   * NLS support:
   */

  private static final String NLS_OPTION_TUNNEL_INLINE_LABEL      = "option.tunnel.inline.label";
  private static final String NLS_OPTION_TUNNEL_IFRAME_LABEL      = "option.tunnel.iframe.label";
  private static final String NLS_OPTION_OLAT_IFRAME_LABEL        = "option.olat.iframe.label";
  private static final String NLS_OPTION_EXTERN_PAGE_LABEL        = "option.extern.page.label";
  private static final String NLS_DESCRIPTION_LABEL                = "description.label";
  private static final String NLS_DESCRIPTION_PREAMBLE            = "description.preamble";
  private static final String NLS_DISPLAY_CONFIG_STARTPAGE_TRUE    = "display.config.startPage.true";
  private static final String NLS_DISPLAY_CONFIG_STARTPAGE_FALSE  = "display.config.startPage.false";
  private static final String NLS_DISPLAY_CONFIG_STARTPAGE        = "display.config.startPage";
  private static final String NLS_DISPLAY_CONFIG_EXTERN           = "display.config.extern";

  private ModuleConfiguration config;
 
  private TextElement thost;
  private TextElement tuser;
  private PasswordElement tpass;
 
  private RadioButtonGroupElement selectables;
 
  /**
   * Constructor for the tunneling configuration form
   * @param name
   * @param config
   * @param withCancel
   */
  public TUConfigForm(String name, Translator translator, ModuleConfiguration config, boolean withCancel) {
    super(name, translator);
    this.config = config;
    int configVersion = config.getConfigurationVersion();
   
    String proto = (String)config.get(CONFIGKEY_PROTO);
    String host = (String)config.get(CONFIGKEY_HOST);
    String uri = (String)config.get(CONFIGKEY_URI);
    if (uri != null && uri.length() > 0 && uri.charAt(0) == '/')
      uri = uri.substring(1);
    String query = null;
    if (configVersion == 2) {
      //query string is available since config version 2
      query = (String) config.get(TUConfigForm.CONFIGKEY_QUERY);
    }
    Integer port = (Integer)config.get(CONFIGKEY_PORT);
    String user = (String)config.get(CONFIGKEY_USER);
    String pass = (String)config.get(CONFIGKEY_PASS);

    String fullURI = getFullURL(proto, host, port, uri, query).toString();
   
    thost = new TextElement("TUConfigForm.url", fullURI, true, 60, 255);
    thost.setExample("http://www.yourserver.com/page.html");
    addFormElement("host", thost);
    addFormElement("st", new SpacerElement(true, false));
   
    // new radio button element
    String[] selectableValues = new String[] {
        OPTION_TUNNEL_THROUGH_OLAT_INLINE,
        OPTION_TUNNEL_THROUGH_OLAT_IFRAME,
        OPTION_SHOW_IN_OLAT_IN_AN_IFRAME,
        OPTION_SHOW_IN_NEW_BROWSER_WINDOW
    };
    String[] selectableLabels = new String[] {
        translate(NLS_OPTION_TUNNEL_INLINE_LABEL),
        translate(NLS_OPTION_TUNNEL_IFRAME_LABEL),
        translate(NLS_OPTION_OLAT_IFRAME_LABEL),
        translate(NLS_OPTION_EXTERN_PAGE_LABEL)
    };
    StaticHTMLTextElement expl = new StaticHTMLTextElement(NLS_DESCRIPTION_LABEL, translate(NLS_DESCRIPTION_PREAMBLE), 32600);
    addFormElement("expl", expl);
   
    String loadedConfig = convertConfigToNewStyle(config);
    selectables = new RadioButtonGroupElement(true, NLS_DISPLAY_CONFIG_EXTERN, selectableValues, selectableLabels);
    selectables.select(loadedConfig, true);
    selectables.setNoLabel(true);
    selectables.setHTMLIsAllowed(true);
    addFormElement("selectables",  selectables);
    addFormElement("s3", new SpacerElement(false, true));

    tuser = new TextElement("TUConfigForm.user", (user == null) ? "" : user, 255);
    tpass = new PasswordElement("TUConfigForm.pass", 255);
    tpass.setValue((pass == null) ? "" : pass);

    addFormElement("user", tuser);
    addFormElement("pass", tpass);

    /*
     * Rules for:
     * - show username and password field if option 1 or 2 is chosen
     * - hide username and password field if option 3 or 4 is chosen
     * - hide startbutton field if option 4 is chosen
     */
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tuser, OPTION_TUNNEL_THROUGH_OLAT_INLINE, true,  tuser.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tuser, OPTION_TUNNEL_THROUGH_OLAT_IFRAME, true,  tuser.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tuser, OPTION_SHOW_IN_OLAT_IN_AN_IFRAME,  false, tuser.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tuser, OPTION_SHOW_IN_NEW_BROWSER_WINDOW, false, tuser.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tpass, OPTION_TUNNEL_THROUGH_OLAT_INLINE, true,  tpass.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tpass, OPTION_TUNNEL_THROUGH_OLAT_IFRAME, true,  tpass.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tpass, OPTION_SHOW_IN_OLAT_IN_AN_IFRAME,  false, tpass.getValue(), true, true));
    addVisibilityDependsOnSelectionRule(new VisibilityDependsOnSelectionRule(selectables, tpass, OPTION_SHOW_IN_NEW_BROWSER_WINDOW, false, tpass.getValue(), true, true));
   
    addSubmitKey("TUConfigForm.save");
    if (withCancel) setCancelKey("search.form.cancel");
  }

  public static StringBuilder getFullURL(String proto, String host, Integer port, String uri, String query) {
    StringBuilder fullURL = new StringBuilder();
    if (proto != null && host != null) {
      fullURL.append(proto).append("://");
      fullURL.append(host);
      if (port != null) {
        if (proto.equals("http") || proto.equals("https")) {
          if (proto.equals("http") && port.intValue() != 80) fullURL.append(":" + port);
          else if (proto.equals("https") && port.intValue() != 443) fullURL.append(":" + port);
        else fullURL.append(":" + port);
      }
      if (uri == null) {
        fullURL.append("/");
      } else {
        // append "/" if not already there, old configurations might have no "/"
        if (uri.indexOf("/") != 0) fullURL.append("/");
        fullURL.append(uri);
      }
      if (query != null) fullURL.append("?").append(query);
    }
    return fullURL;
  }

  /**
   * @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
   */
  public boolean validate() {
    try {
      new URL(thost.getValue());
    } catch (MalformedURLException e) {
      thost.setErrorKey("TUConfigForm.invalidurl");
      return false;
    }
    return true;
  }
 
  private String convertConfigToNewStyle(ModuleConfiguration cfg) {
    Boolean tunnel = cfg.getBooleanEntry(CONFIG_TUNNEL);
    Boolean iframe = cfg.getBooleanEntry(CONFIG_IFRAME);
    Boolean extern = cfg.getBooleanEntry(CONFIG_EXTERN);
    if (tunnel == null && iframe == null && extern == null) {        // nothing saved yet
      return OPTION_TUNNEL_THROUGH_OLAT_INLINE;
    } else {                                                        // something is saved ...
      if (extern != null && extern.booleanValue()) {                // ... it was extern...
        return OPTION_SHOW_IN_NEW_BROWSER_WINDOW;
      } else if (tunnel != null && tunnel.booleanValue()) {          // ... it was tunneled
        if (iframe != null && iframe.booleanValue()) {              // ... and in a iframe
          return OPTION_TUNNEL_THROUGH_OLAT_IFRAME;
        } else {                                                    // ... no iframe
          return OPTION_TUNNEL_THROUGH_OLAT_INLINE;
        }
      } else {                                                      // ... no tunnel means inline
        return OPTION_SHOW_IN_OLAT_IN_AN_IFRAME;
      }
    }
  }
 
  /**
   * @return the updated module configuration using the form data
   */
  public ModuleConfiguration getUpdatedConfig() {
    URL url = null;
    try {
      url = new URL(thost.getValue());
    } catch (MalformedURLException e) {
      throw new OLATRuntimeException("MalformedURL in TUConfigForm which should not happen, since we've validated before. URL: " + thost.getValue(), e);
    }
    config.setConfigurationVersion(2);
    config.set(CONFIGKEY_PROTO, url.getProtocol());
    config.set(CONFIGKEY_HOST, url.getHost());
    config.set(CONFIGKEY_URI, url.getPath());
    config.set(CONFIGKEY_QUERY, url.getQuery());
    int port = url.getPort();
    config.set(CONFIGKEY_PORT, new Integer(port != -1 ? port : url.getDefaultPort()));
    config.set(CONFIGKEY_USER, getFormUser());
    config.set(CONFIGKEY_PASS, getFormPass());
   
    // now save new mapped config:
    String selected = selectables.getSelectedKey();
   
    // if content should be show in extern window
    config.setBooleanEntry(CONFIG_EXTERN, selected.equals(OPTION_SHOW_IN_NEW_BROWSER_WINDOW));
    // if content should be tunneled
    config.setBooleanEntry(CONFIG_TUNNEL, (selected.equals(OPTION_TUNNEL_THROUGH_OLAT_INLINE) || selected.equals(OPTION_TUNNEL_THROUGH_OLAT_IFRAME)));
    // if content should be displayed in iframe
    config.setBooleanEntry(CONFIG_IFRAME, (selected.equals(OPTION_TUNNEL_THROUGH_OLAT_IFRAME) || selected.equals(OPTION_SHOW_IN_OLAT_IN_AN_IFRAME)));     
    return config;
  }

  private String getFormUser() {
    if (StringHelper.containsNonWhitespace(tuser.getValue()))
      return tuser.getValue();
    else
      return null;
  }

  private String getFormPass() {
    return tpass.getValue();
  }
}
TOP

Related Classes of org.olat.course.nodes.tu.TUConfigForm

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.