Package de.chris_soft.nanodoa.web

Source Code of de.chris_soft.nanodoa.web.WebsiteWithPassword

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa.web;

import de.chris_soft.utilities.AppProperties;
import de.chris_soft.utilities.web.HtmlTag;
import de.chris_soft.utilities.web.HtmlWebsite;

/**
* Parent website class that checks for valid password. If it is valid, the real website is shown.
* @author Christian Packenius.
*/
public abstract class WebsiteWithPassword extends HtmlWebsite implements WebsiteVariableConstants, DocumentServerKeys {
  /**
   * @see de.chris_soft.utilities.web.HtmlWebsite#createWebSite(de.chris_soft.utilities.web.HtmlTag)
   */
  @Override
  public void createWebSite(HtmlTag body) {
    if (!isValidPassword()) {
      createPasswordInputSite(body);
    }
    else {
      createValidatedPasswordWebSite(body);
      copyPassword();
    }
  }

  private boolean isValidPassword() {
    String clientPasswordMD5code = getValue(varnamePasswordMD5);
    String serverPasswordMD5code = AppProperties.getProperty(PROP_KEY_DOCUMENT_SERVER_PASSWORD);
    return serverPasswordMD5code.equals(clientPasswordMD5code);
  }

  private void createPasswordInputSite(HtmlTag body) {
    addJavascriptLibrary("js/md5.js");
    addJavascriptLibrary("js/password.js");
    copyAllValues();
    body.add("p").add("b").addText("Please enter password for document server access:");
    HtmlTag p = body.add("p");
    p.add("input", "type=\"password\"", varnamePassword);
    p.add("input", "type=\"hidden\"", varnamePasswordMD5);
    p.add("input", "onClick=\"createPwMD5Hash();\" type=\"submit\" value=\"OK\"");
  }

  /**
   * The password has been validated. Now create real website.
   * @param body Body tag object for website content.
   */
  public abstract void createValidatedPasswordWebSite(HtmlTag body);

  /**
   * @see de.chris_soft.utilities.web.HtmlWebsite#getFirstFocusElement()
   */
  @Override
  public String getFirstFocusElement() {
    if (!isValidPassword()) {
      return varnamePassword;
    }
    return getFirstFocusElementForValidatedPasswordWebsite();
  }

  /**
   * Returns the element name that should have focus after website loading.
   * @return Name of element in form.
   */
  public abstract String getFirstFocusElementForValidatedPasswordWebsite();

  /**
   * Set password in values.
   */
  private void copyPassword() {
    setValue(varnamePasswordMD5, getValue(varnamePasswordMD5));
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.web.WebsiteWithPassword

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.