PageRenderTime 25ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://alageospatialportal.googlecode.com/
Java | 81 lines | 54 code | 14 blank | 13 comment | 5 complexity | b48f4982a75d1e2d3a8a135b398b4c05 MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5package au.org.emii.portal.test.authentication;
 6
 7import java.io.IOException;
 8import java.io.PrintStream;
 9import org.apache.commons.io.IOUtils;
10import org.apache.log4j.Logger;
11import org.simpleframework.http.Request;
12import org.simpleframework.http.Response;
13import org.simpleframework.http.core.Container;
14
15/**
16 * Fake password change service used for changing passwords when the old
17 * password is known
18 * @author geoff
19 */
20public class MockGeoNetworkXmlPwUpdateService implements Container {
21
22    private Logger logger = Logger.getLogger(getClass());
23    public static final String BAD_PASSWORD = "aaaaaa";
24    public static final String GOOD_PASSWORD = "bbbbbb";
25    public static final String NEW_PASSWORD = "nnnnnn";
26    
27    private static final String SUCCEED_OUTPUT = "test_data/xml.user.pwupdate-succeed";
28    private static final String WRONG_PASSWORD_OUTPUT = "test_data/xml.user.pwupdate-fail_wrong_password";
29    private static final String UNAUTHORISED_OUTPUT = "test_data/xml.user.pwupdate-fail-unauthorised";
30    private boolean loggedIn = true;
31
32    @Override
33    public void handle(Request request, Response response) {
34        String outputFile = null;
35        try {
36            String password = request.getParameter("password");
37            String newPassword = request.getParameter("newPassword");
38            PrintStream out = response.getPrintStream();
39
40            if (loggedIn) {
41                if (password.equals(GOOD_PASSWORD)) {
42                    // ok change password
43                    response.set("Content-Type", "application/xml");
44                    outputFile = SUCCEED_OUTPUT;
45                    logger.info("password changed to " + newPassword);
46                } else {
47                    outputFile = WRONG_PASSWORD_OUTPUT;
48                    logger.error("password incorrect - not changing");
49                }
50            } else {
51                outputFile = UNAUTHORISED_OUTPUT;
52                logger.error("not logged in");
53            }
54
55
56            // XML mime type is only for a successful response - all other cases
57            // send html mime type back, even though output is valid xml.  This
58            // is to mimic the MEST server
59            if (!outputFile.equals(SUCCEED_OUTPUT)) {
60                response.set("Content-Type", "text/html");
61            }
62
63            String output = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(outputFile));
64            out.print(output);
65            out.close();
66
67        } catch (IOException ex) {
68            logger.error("mock server io error", ex);
69        }
70    }
71
72    public boolean isLoggedIn() {
73        return loggedIn;
74    }
75
76    public void setLoggedIn(boolean loggedIn) {
77        this.loggedIn = loggedIn;
78    }
79
80    
81}