PageRenderTime 18ms CodeModel.GetById 13ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/webportal/src/test/java/au/org/emii/portal/test/authentication/MockGeoNetworkXmlUserLoginService.java

http://alageospatialportal.googlecode.com/
Java | 77 lines | 48 code | 12 blank | 17 comment | 6 complexity | 93f66f14c758a98b71678e90c69b3e2f MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5
 6package au.org.emii.portal.test.authentication;
 7
 8import java.io.IOException;
 9import java.io.PrintStream;
10import org.apache.commons.io.IOUtils;
11import org.apache.log4j.Logger;
12import org.simpleframework.http.Request;
13import org.simpleframework.http.Response;
14import org.simpleframework.http.core.Container;
15
16/**
17 * Use simple framework embedded http server to create simulated http responses
18 * @author geoff
19 */
20public class MockGeoNetworkXmlUserLoginService implements Container {
21
22    private final static String SUCCEED_OUTPUT = "test_data/xml.user.login-succeed";
23    private final static String FAIL_OUTPUT = "test_data/xml.user.login-fail";
24    public static final String GOOD_USER = "admin";
25    public static final String GOOD_PASSWORD = "admin";
26    public static final String BAD_USER = "nothere";
27    public static final String BAD_PASSWORD = "nothere";
28    private Logger logger = Logger.getLogger(getClass());
29    /**
30     * True if login requests will succeed otherwise false
31     */
32    private boolean succeed = true;
33
34    public boolean isSucceed() {
35        return succeed;
36    }
37
38    public void setSucceed(boolean succeed) {
39        this.succeed = succeed;
40    }
41
42
43    /**
44     * Fake long function
45     * @param username
46     * @param password
47     * @return
48     */
49    private boolean login(String username, String password) {
50        return username != null && username.equals(GOOD_USER) &&
51               password != null && password.equals(GOOD_PASSWORD);
52    }
53
54    @Override
55    public void handle(Request request, Response response) {
56        try {
57            String username = request.getParameter("username");
58            String password = request.getParameter("password");
59            PrintStream out = response.getPrintStream();
60            boolean loggedIn = login(username, password);
61            logger.debug("fake sever says logged in = " + loggedIn);
62            if (loggedIn) {
63                response.set("Content-Type", "application/xml");
64                out.print(IOUtils.toString(getClass().getClassLoader().getResourceAsStream(SUCCEED_OUTPUT)));
65            } else {
66                response.set("Content-Type", "text/html");
67                out.print(IOUtils.toString(getClass().getClassLoader().getResourceAsStream(FAIL_OUTPUT)));
68            }
69            out.close();
70        } catch (IOException e) {
71            logger.error("IO exception - should never happen", e);
72        }
73
74    }
75
76    
77}