Package org.rendersnake.site.example

Source Code of org.rendersnake.site.example.LoginForm

package org.rendersnake.site.example;

import static org.rendersnake.HtmlAttributesFactory.class_;
import static org.rendersnake.HtmlAttributesFactory.for_;
import static org.rendersnake.HtmlAttributesFactory.id;
import static org.rendersnake.HtmlAttributesFactory.type;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.rendersnake.HtmlCanvas;
import org.rendersnake.Renderable;
import org.rendersnake.StringResource;
import org.rendersnake.servlet.FormHandler;
import org.rendersnake.servlet.FormResponseAction;
import org.rendersnake.servlet.FormValidator;
import org.rendersnake.servlet.ServletUtils;
import org.rendersnake.site.ExamplesPage;
import org.rendersnake.site.components.SourceLink;

public class LoginForm implements FormHandler, FormValidator, Renderable {
    private static final String ERROR_LOGIN_PASSWORD = "error.LoginForm.password";
    private static final String ERROR_LOGIN_USERNAME = "error.LoginForm.username";
    static final String ID_USERNAME = "username";
    static final String ID_PASSWORD = "password";
   
    static final String VAR_USERNAME = "username";
    static final String VAR_PASSWORD = "password";

    public String username;
    public String password;
   
   
    public void renderOn(HtmlCanvas html) throws IOException {
        html
            .h1().write("Login Form")._h1()
            .p().write("This component implements Renderable for displaying the Form, FormValidator to validate incoming data and FormHandler to process the validated data.")._p()
            .write(StringResource.get("content/Example-login.html"),false);
       
        Object user = ServletUtils.sessionFor(html).getAttribute("user");
        if (null != user) {
            html.h4().write("Welcome " + user)._h4();
            return;
        }               
        this.renderErrorsOn(html);
        html.style().write("#sample label,input { padding: 4px; margin:4px;}")._style();
        html
            .form(id("sample").action(ServletUtils.responseFor(html).encodeURL(this.getClass().getName())).method("post"))
                .label(for_(ID_USERNAME)).write("Username:")._label()
                .input(
                    id(ID_USERNAME)
                    .variable(VAR_USERNAME,username))
            .br()
                .label(for_(ID_PASSWORD)).write("Password:")._label()
                .input(
                    type("password")
                    .id(ID_PASSWORD)
                    .variable(VAR_PASSWORD,password))
            .br()
                .input(type("submit").value("Log me in"))
        ._form();
       
        html.render(SourceLink.example("LoginForm"));
    }

    private void renderErrorsOn(HtmlCanvas html) throws IOException {
        HttpSession session = ServletUtils.sessionFor(html);
        if (session == null)
            return;
        Object usernameMsg = session.getAttribute(ERROR_LOGIN_USERNAME);
        Object passwordMsg = session.getAttribute(ERROR_LOGIN_PASSWORD);
        if (usernameMsg == null && passwordMsg == null)
            return;
        html
            .div(class_("errors"))
                .write((String)usernameMsg)
                .br()
                .write((String)passwordMsg)
            ._div();
    }
   
   
    public FormResponseAction handle(HttpServletRequest request, HttpServletResponse response) {
        // how can i tell on what page we are right now?
       
        request.getSession(true).setAttribute("user", username);
       
        return new FormResponseAction().rendering(new ExamplesPage());
    }

   
    public boolean validate(HttpServletRequest request) {
       
        boolean isValid = true;
        if (StringUtils.isBlank(username)) {
            request.getSession().setAttribute(ERROR_LOGIN_USERNAME, "Username is missing");
            isValid = false;
        }
        if (StringUtils.isBlank(password)) {
            request.getSession().setAttribute(ERROR_LOGIN_PASSWORD, "Password is missing");
            isValid = false;
        }
        return isValid;
    }

}
TOP

Related Classes of org.rendersnake.site.example.LoginForm

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.