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