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