com.LSH.client.Error404.java Source code

Java tutorial

Introduction

Here is the source code for com.LSH.client.Error404.java

Source

package com.LSH.client;

import com.LSH.client.DataType.GetLinkData;
import com.LSH.client.DataType.ReturnLinkData;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.core.client.EntryPoint;

/**
 * Created by @author AlNat on 06.12.2016.
 * Licensed by Apache License, Version 2.0
 *
 * ?? UI  404  
 */
public class Error404 implements EntryPoint {

    private HTML label; // ?  

    public void onModuleLoad() {

        // ??  ? 
        label = new HTML();
        label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

        //    ?
        String url = Window.Location.getHref();// URL,    
        String browser = Window.Navigator.getUserAgent(); // user-agent ?
        String ip = getIP(); // IP ?? ?
        GetLinkData getLinkData = new GetLinkData(url, ip, browser); //    ?

        //   ?
        LSHServiceInterface.App.getInstance().getOriginal(getLinkData, new AsyncCallback<ReturnLinkData>() {
            @Override
            public void onFailure(Throwable caught) { // ?  ? ???
                Print404();
            }

            @Override
            public void onSuccess(ReturnLinkData link) { // ?  
                if (link.getErrorCode() != null) { // ?     
                    PrintError(link);
                } else {
                    if (link.getPassword() == null) { // ? ? ,  
                        Window.Location.assign(link.getOriginalLink());
                    } else { //  ? ? 
                        final PasswordDialog d = new PasswordDialog(link);

                        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() { //  ?
                            @Override
                            public void execute() {
                                d.textBox.setFocus(true);
                            }
                        });

                        d.show();
                        d.center();
                    }
                }
            }
        });

        RootPanel.get().add(label); //  label  ?
    }

    /**
     * ?  404 ?
     */
    private void Print404() {
        Window.setTitle("404 - Page Not Found");
        label.setHTML("<h1>404 Page!</h1><br>");
    }

    /**
     * ?  
     * @param link  ??
     */
    private void PrintError(ReturnLinkData link) {
        String result = link.getErrorCode();

        Window.setTitle("404 - Page Not Found");
        label.setHTML("<h1>404 Page!</h1><br>" + result + "<br><br> But, maybe, you want <br> to create a "
                + "<a href=\"LSH.html\">shortlink</a> ?");
    }

    /**
     * ?, ? MD5 ?  ?
     * @param in ? ?
     * @return ? ?  null ? 
     */
    private String getMD5(String in) {
        return LSH.getMD5(in);
    }

    /**
     *   ?  ?
     */
    @SuppressWarnings("Convert2Lambda")
    class PasswordDialog extends DialogBox {

        final PasswordTextBox textBox;

        PasswordDialog(final ReturnLinkData link) {

            setHTML("<h4>Please, input password</h4>");
            setAnimationEnabled(true);
            setGlassEnabled(true);

            // 
            HorizontalPanel panel = new HorizontalPanel();
            final Button button = new Button("OK");
            textBox = new PasswordTextBox();

            textBox.addKeyDownHandler(new KeyDownHandler() { // ? ? 
                @Override
                public void onKeyDown(KeyDownEvent event) {
                    if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                        button.click(); // ?      Enter
                    }
                }
            });

            //      
            panel.add(textBox);
            panel.add(button);

            button.addClickHandler(new ClickHandler() { // ?  ?  
                public void onClick(ClickEvent event) {

                    String t = getMD5(textBox.getText()); //  ? ? ?

                    if (t.equals(link.getPassword())) { // ?  ?
                        Window.Location.assign(link.getOriginalLink()); // 
                    } else { //  
                        label.setHTML("<h3>Wrong password!</h3><br>");
                    }

                }
            });

            setWidget(panel); // ?   
            textBox.setFocus(true);
        }

    }

    /**
     * ? ? ip ?
     * @return ip
     */
    private native String getIP() /*-{
                                  return $wnd.userip;
                                  }-*/;

}