SampleBoundingSetProvider.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * IBM Cognos Products: CAMAAA
  7. *
  8. * (C) Copyright IBM Corp. 2011, 2012
  9. *
  10. * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been
  11. * deposited with the U.S. Copyright Office.
  12. */
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. import com.cognos.CAM_AAA.authentication.IAccount;
  18. import com.cognos.CAM_AAA.authentication.IBoundingSetProvider;
  19. import com.cognos.CAM_AAA.authentication.UnrecoverableException;
  20. /**
  21. * Sample implementation of the {@link IBoundingSetProvider} interface. The user to tenant mapping is defined in a properties file.
  22. */
  23. public class SampleBoundingSetProvider implements IBoundingSetProvider
  24. {
  25. private static final String USERS_TO_BOUNDING_SETS_FILE_PROPERTY_NAME = "usersToBoundingSetsFile";
  26. private static final String INITIALIZATION_EXCEPTION_CAPTION = "Error initializing bounding set provider.";
  27. private final Properties usersToBoundingSets;
  28. public SampleBoundingSetProvider()
  29. {
  30. this.usersToBoundingSets = new Properties();
  31. }
  32. public void destroy()
  33. {
  34. }
  35. @Override
  36. public String[] getBoundingSet(final IAccount account) throws UnrecoverableException
  37. {
  38. final String userName = account.getUserName();
  39. final String BoundingSet = this.usersToBoundingSets.getProperty(userName);
  40. if (BoundingSet == null)
  41. throw new UnrecoverableException("Unknown user", "User '" + userName + "' does not belong to any bounding set.");
  42. final String[] BoundingSetArray = BoundingSet.split(",");
  43. return BoundingSetArray;
  44. }
  45. public void init(final Map< String, String > advancedConfigurations, final String namespaceId) throws UnrecoverableException
  46. {
  47. final String fileLocation = advancedConfigurations.get(SampleBoundingSetProvider.USERS_TO_BOUNDING_SETS_FILE_PROPERTY_NAME);
  48. if (fileLocation == null)
  49. throw new UnrecoverableException(SampleBoundingSetProvider.INITIALIZATION_EXCEPTION_CAPTION, "The advanced property '"
  50. + SampleBoundingSetProvider.USERS_TO_BOUNDING_SETS_FILE_PROPERTY_NAME + "' must be set.");
  51. final InputStream inStream = this.getClass().getResourceAsStream(fileLocation);
  52. if (inStream == null)
  53. throw new UnrecoverableException(SampleBoundingSetProvider.INITIALIZATION_EXCEPTION_CAPTION,
  54. "Unable to locate user to bounding sets mapping file '" + fileLocation + "'");
  55. try
  56. {
  57. //
  58. // Java Properties files only support ISO 8859-1 character encoding, therefore user names or tenant IDs of certain
  59. // locales are not supported by the sample. They are supported in the product.
  60. //
  61. this.usersToBoundingSets.load(inStream);
  62. }
  63. catch (final IOException ioe)
  64. {
  65. throw new UnrecoverableException(SampleBoundingSetProvider.INITIALIZATION_EXCEPTION_CAPTION,
  66. "Unable to load user to bounding sets mapping.");
  67. }
  68. finally
  69. {
  70. try
  71. {
  72. inStream.close();
  73. }
  74. catch (final IOException ioe)
  75. {
  76. // ignore it
  77. }
  78. }
  79. }
  80. }