summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/utils/Kvm.java
blob: 896150a16455a754fca601aedcf6a2be818cc6e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package de.spline.kvm.utils;

import java.util.HashMap;
import java.util.Map;

import com.gistlabs.mechanize.MechanizeAgent;
import com.gistlabs.mechanize.Resource;
import com.gistlabs.mechanize.document.Document;
import com.gistlabs.mechanize.document.node.Node;
import com.gistlabs.mechanize.parameters.Parameters;

public class Kvm
{
    protected String host;
    protected MechanizeAgent agent;

    public Kvm(String host)
    {
        this.host = host;
        this.agent = new MechanizeAgent();
    }

    public void login(String username, String password)
    {
        Parameters param = new Parameters();
        param.add("login", username);
        param.add("password", password);
        param.add("action_login", "Login");

        Resource resource = agent.post(host + "/auth.asp", param);
        if (resource.asString().contains("Authentication failed.")) {
            System.err.println("Invalid login!");
            System.exit(1);
        }
    }

    public Map<String, String> getAppletParameters()
    {
        Document doc = agent.get(host + "/title_app.asp");
        if (!doc.asString().contains("<param name=\"APPLET_ID\"")) {
            logout();

            System.err.println("Error getting applet parameters.");
            System.exit(1);
        }

        Map<String, String> params = new HashMap<>();
        for (Node node : doc.getRoot().findAll("html body applet param")) {
            String name = node.getAttribute("name");
            String value = node.getAttribute("value");
            params.put(name.toUpperCase(), value);
        }

        return params;
    }

    public void logout()
    {
        agent.get(host + "/logout");
    }
}