|
| 1 | +public with sharing class ExportReport { |
| 2 | + |
| 3 | + /* |
| 4 | + |
| 5 | + Credits: |
| 6 | + Progmatic Access to Salesforce.com Reports |
| 7 | + http://sfdc-heretic.warped-minds.com/2006/04/10/progmatic-access-to-salesforcecom-reports/ |
| 8 | + |
| 9 | + HTTP Callout to Salesforce |
| 10 | + http://boards.developerforce.com/t5/Best-Practices-Discussion/Http-Callout-to-Salesforce/td-p/179143 |
| 11 | + |
| 12 | + |
| 13 | + */ |
| 14 | + public class customException extends Exception{} |
| 15 | + |
| 16 | + String sfDomain = Properties.getSalesforceDomain; |
| 17 | + String un = Properties.getUsername; |
| 18 | + String pw = Properties.getPassword; |
| 19 | + |
| 20 | + //used to direct the flow and set values when unit testing |
| 21 | + public enum TestPath {REDIRECT, REDIRECT_ERROR, OK_CSV, OK_NONCSV, ERROR} |
| 22 | + |
| 23 | + private HTTPRequest buildWebService(String endPoint, String sid) { |
| 24 | + HttpRequest req = new HttpRequest(); |
| 25 | + req.setEndpoint(endPoint); |
| 26 | + req.setMethod('GET'); |
| 27 | + //if session id is provided add to the cookie so the webservice is authenticated |
| 28 | + if (sid!=null) req.setHeader('Cookie','sid='+sid); |
| 29 | + return req; |
| 30 | + } |
| 31 | + private HTTPResponse invokeWebService(HttpRequest req, String encoding, TestPath path) { |
| 32 | + Http http = new Http(); |
| 33 | + HTTPResponse res = new HTTPResponse(); |
| 34 | + if ( !Test.isRunningTest() ) { |
| 35 | + res = http.send(req); |
| 36 | + } else { |
| 37 | + //TEST EXECUTION PATH |
| 38 | + if (path == null) path = TestPath.OK_CSV; |
| 39 | + |
| 40 | + if (path == TestPath.Redirect) { |
| 41 | + res.setStatus('Found'); |
| 42 | + res.setStatusCode(302); |
| 43 | + res.setHeader('Location', 'https://na12.salesforce.com/secur/frontdoor.jsp?sid=123456789&apv=1'); |
| 44 | + } else if (path == TestPath.REDIRECT_ERROR) { |
| 45 | + res.setStatus('Found'); |
| 46 | + res.setStatusCode(302); |
| 47 | + //do not provide redirect location |
| 48 | + } else if (path == TestPath.OK_CSV) { |
| 49 | + res.setStatus('OK'); |
| 50 | + res.setStatusCode(200); |
| 51 | + res.setBody('IT WORKS!'); |
| 52 | + res.setHeader('Content-Type', encoding); |
| 53 | + } else if (path == TestPath.OK_NONCSV) { |
| 54 | + res.setStatus('OK'); |
| 55 | + res.setStatusCode(200); |
| 56 | + res.setBody(''); |
| 57 | + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); |
| 58 | + } else if (path == TestPath.ERROR) { |
| 59 | + res.setStatus('ERROR'); |
| 60 | + res.setStatusCode(404); |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + return res; |
| 65 | + } |
| 66 | + |
| 67 | + public String handleRedirectRequest(String redirectURI) { |
| 68 | + return handleRedirectRequest(redirectURI, TestPath.Redirect); |
| 69 | + } |
| 70 | + public String handleRedirectRequest(String redirectURI, TestPath path) { |
| 71 | + system.debug('Entering...handleRedirectRequest...' + redirectURI); |
| 72 | + |
| 73 | + try { |
| 74 | + HttpRequest req = buildWebService(redirectURI, null); |
| 75 | + HTTPResponse res = invokeWebService(req, '', path); |
| 76 | + |
| 77 | + if (res.getStatusCode() == 302) { |
| 78 | + //after first login salesforce retruns a redirect we need ot handle this |
| 79 | + redirectURI = res.getHeader('Location'); |
| 80 | + |
| 81 | + if (redirectURI != null) { |
| 82 | + System.debug('handleRedirectRequest...redirectURI ...' + redirectURI ); |
| 83 | + String sid = redirectURI.substring(redirectURI.indexOf('sid=')+4, redirectURI.indexOf('&', redirectURI.indexOf('sid='))); |
| 84 | + System.debug('handleRedirectRequest...sid...' + sid); |
| 85 | + sid = EncodingUtil.urlDecode(sid, 'UTF-8'); |
| 86 | + System.debug('handleRedirectRequest...sidDecoded...' + sid); |
| 87 | + return sid; |
| 88 | + } else { |
| 89 | + throw new customException('Redirect during login with no redirect location'); |
| 90 | + } |
| 91 | + } else { |
| 92 | + throw new customException('Unhandled response from web service, Web Service Response: ' + res); |
| 93 | + } |
| 94 | + } catch (System.CalloutException callOutEx) { |
| 95 | + throw new customException('Unable to login via web service. Error: ' + callOutEx.getMessage()); |
| 96 | + } catch (Exception ex) { |
| 97 | + throw new customException('Unkown Error: ' + ex.getMessage()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private String getSessionId() { |
| 102 | + //as these methods are called from scheduled classes and futures UserInfo.session is not available |
| 103 | + //as a workaround I do a login call using httprequest adns crape the session id |
| 104 | + |
| 105 | + if (sfDomain == null) { |
| 106 | + throw new customException('Salesoforce domain URL not defined in custom setting'); |
| 107 | + } |
| 108 | + if (un == null || pw == null) { |
| 109 | + throw new customException('Login credentials not defined in custom setting'); |
| 110 | + } |
| 111 | + //login to salesforce using web callout to scrape for the session id |
| 112 | + String authURL = sfDomain + '?un=' + un + '&pw=' + pw; |
| 113 | + String sid = handleRedirectRequest(authURL); |
| 114 | + |
| 115 | + return sid; |
| 116 | + } |
| 117 | + |
| 118 | + public blob getReportAsCSV(Id reportId, TestPath path) { |
| 119 | + String sid = getSessionId(); |
| 120 | + |
| 121 | + //url format to return an exported report in csv format |
| 122 | + String uri = sfDomain + reportId + '?export=1&enc=UTF-8&xf=csv'; |
| 123 | + return Blob.valueOf(getReport(uri, 'text/csv; charset=UTF-8', sid, path)); |
| 124 | + } |
| 125 | + public String getReportAsHTML(Id reportId, TestPath path) { |
| 126 | + String sid = getSessionId(); |
| 127 | + |
| 128 | + //url format to return report without headers |
| 129 | + String uri = sfDomain + reportId + '?isdtp=nv'; |
| 130 | + String body = getReport(uri, 'text/html; charset=UTF-8', sid, path); |
| 131 | + if (body.indexOf('<div class="bGeneratedReport') > 0) { |
| 132 | + //return just the body of the report output |
| 133 | + body = body.substring(body.indexOf('<div class="bGeneratedReport'), body.indexOf('</form>')); |
| 134 | + } else { |
| 135 | + //having trouble with more obscure reports e.g. Lead Status |
| 136 | + body = ''; |
| 137 | + } |
| 138 | + system.debug('runReport...body...' + body); |
| 139 | + return body; |
| 140 | + } |
| 141 | + private String getReport(String uri, String encoding, String sid, TestPath path) { |
| 142 | + try { |
| 143 | + system.debug('runReport...sid...' + sid); |
| 144 | + system.debug('runReport...uri...' + uri); |
| 145 | + |
| 146 | + HttpRequest req = buildWebService(uri, sid); |
| 147 | + HTTPResponse res = invokeWebService(req, encoding, path); |
| 148 | + |
| 149 | + if (res.getStatusCode() == 200) { |
| 150 | + String body = res.getBody(); |
| 151 | + system.debug('runReport...body...' + body); |
| 152 | + |
| 153 | + //check that the encoding is correct - during testing a response of text/html when csv is expected means the authentication failed somewhere |
| 154 | + if (res.getHeader('Content-Type').equals(encoding)) { |
| 155 | + return body; |
| 156 | + } else { |
| 157 | + throw new customException('Unexpected content type returned: ' + res.getHeader('Content-Type')); |
| 158 | + } |
| 159 | + } else { |
| 160 | + system.debug('runReport...res...' + res); |
| 161 | + throw new customException('Unhandled response from web service, Web Service Response: ' + res); |
| 162 | + } |
| 163 | + } catch (Exception ex) { |
| 164 | + throw new customException('Unkown Error: ' + ex.getMessage()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments