Guest User

Untitled

a guest
Nov 7th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. public class headerUtility {
  2.  
  3. /** This is a hack to determine is the page is using SSL or not. If CipherSuite is defined that means it is HTTPS **/
  4. public Boolean hasSSL { get { return ApexPages.currentPage().getHeaders().get('CipherSuite') != null; } }
  5.  
  6. /** This is used to determine the device type **/
  7. public String userAgent { get { return ApexPages.currentPage().getHeaders().get('USER-AGENT'); } }
  8.  
  9. /** This will return which Salesforce Server you are on, example: na8.salesforce.com **/
  10. public String currentServer { get {return ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To'); } }
  11.  
  12. /** Check the useragent string and assign the device **/
  13. public String deviceType { get; set; } {
  14. if(userAgent.contains('iPhone')) deviceType = 'iPhone';
  15. else if(userAgent.contains('iPad')) deviceType = 'iPad';
  16. else if(userAgent.contains('BlackBerry')) deviceType = 'BlackBerry';
  17. else deviceType = 'Other';
  18. }
  19.  
  20. /** You could query a custom object to determine your home page, check the device or simply hard code it here **/
  21. public String returnUrl {get; set; } {
  22.  
  23. if (deviceType == 'iPad') {
  24. returnUrl = '/apex/LbMain';
  25. } else {
  26. returnUrl = '/apex/LbMain';
  27.  
  28. }
  29. }
  30.  
  31. public String username {get; set;}
  32. public String password {get; set;}
  33.  
  34. public PageReference login(){
  35.  
  36. String startUrl = System.currentPageReference().getParameters().get('startURL');
  37. return Site.login( username, password, startURL);
  38. }
  39.  
  40. /** Check to see if the page is using HTTPS, if not redirect it back to itself with HTTPS **/
  41. public PageReference redirect() {
  42. if (!hasSSL) {
  43. string host = ApexPages.currentPage().getHeaders().get('host');
  44. string url = ApexPages.currentPage().getUrl();
  45. PageReference homePage = new PageReference('https://'+host+url);
  46. homePage.setRedirect(true);
  47. return homePage;
  48. } else {
  49. return null;
  50. }
  51. }
  52.  
  53. @istest
  54.  
  55. headerUtility h = new headerUtility();
  56. h.username = 'Test@hilton.com';
  57. h.password ='Test123!';
  58.  
  59. pagereference pageref = page.LbMain;
  60.  
  61. test.setCurrentPage(pageref);
  62.  
  63. h.login();
  64. h.redirect();
  65.  
  66.  
  67. }
Add Comment
Please, Sign In to add comment