PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/test/java/au/org/emii/portal/test/user/MockGeoNetworkXmlUserListService.java

http://alageospatialportal.googlecode.com/
Java | 102 lines | 50 code | 22 blank | 30 comment | 6 complexity | 790d85e972d9ed81102a513c4b30717e 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.user;
  7
  8import java.io.IOException;
  9import java.io.PrintStream;
 10import java.util.logging.Level;
 11import java.util.logging.Logger;
 12import org.apache.commons.io.IOUtils;
 13import org.simpleframework.http.Request;
 14import org.simpleframework.http.Response;
 15import org.simpleframework.http.core.Container;
 16
 17/**
 18 *
 19 * @author geoff
 20 */
 21public class MockGeoNetworkXmlUserListService implements Container {
 22
 23        /**
 24         * File to output on success - regular user
 25         */
 26        private final static String SUCCEED_REGULAR_OUTPUT = "test_data/xml.user.get-succeed_regular";
 27
 28        /**
 29         * File to output on success - admin user
 30         */
 31        private final static String SUCCEED_ADMIN_OUTPUT = "test_data/xml.user.get-succeed_admin";
 32        
 33        /**
 34         * File to output on invalid user requested
 35         */
 36        private final static String FAIL_INVALID_OUTPUT = "test_data/xml.user.get-fail_invalid_user";
 37        
 38        /**
 39         * File to output when not already logged in
 40         */
 41        private final static String FAIL_UNAUTHORISED_OUTPUT = "test_data/xml.user.fail_unauthorised";
 42
 43        /**
 44         * Give the username that is associated with the xml response in SUCCEED_REGULAR_OUTPUT
 45         */
 46        private final static String REGULAR_USERNAME = "HopeJ";
 47
 48        /**
 49         * Give the username that is associated with the xml response in SUCCEED_ADMIN_OUPUT
 50         */
 51        private final static String ADMIN_USERNAME = "portal";
 52
 53        /**
 54         * Flag to indicate whether we have authenticated to mest or not
 55         */
 56        public boolean loggedIn = false;
 57
 58        public boolean isLoggedIn() {
 59                return loggedIn;
 60        }
 61
 62        public void setLoggedIn(boolean loggedIn) {
 63                this.loggedIn = loggedIn;
 64        }
 65
 66        
 67
 68
 69
 70        @Override
 71        public void handle(Request request, Response response) {
 72                PrintStream out = null;
 73                try {
 74                        String username = request.getParameter("username");
 75                        String responseFile;
 76
 77                        if (loggedIn) {
 78                                // once logged in, you always get XML back
 79                                response.set("Content-Type", "application/xml");
 80
 81                                if (username.equals(REGULAR_USERNAME)) {
 82                                        responseFile = SUCCEED_REGULAR_OUTPUT;
 83                                } else if (username.equals(ADMIN_USERNAME)) {
 84                                        responseFile = SUCCEED_ADMIN_OUTPUT;
 85                                } else {
 86                                        responseFile = FAIL_INVALID_OUTPUT;
 87                                }
 88
 89                        } else {
 90                                responseFile = FAIL_UNAUTHORISED_OUTPUT;
 91                        }
 92
 93                        out = response.getPrintStream();
 94                        out.print(IOUtils.toString(getClass().getClassLoader().getResourceAsStream(responseFile)));
 95                } catch (IOException ex) {
 96                        Logger.getLogger(MockGeoNetworkXmlUserListService.class.getName()).log(Level.SEVERE, null, ex);
 97                } finally {
 98                        out.close();
 99                }
100        }
101
102}