55import com .docusign .core .model .DoneExample ;
66import com .docusign .core .model .Session ;
77import com .docusign .core .model .User ;
8+ import com .docusign .monitor .api .DataSetApi ;
9+ import com .docusign .monitor .api .DataSetApi .GetStreamOptions ;
10+ import com .docusign .monitor .model .CursoredResult ;
811import org .json .JSONArray ;
912import org .json .JSONObject ;
1013import org .springframework .beans .factory .annotation .Autowired ;
11- import org .springframework .http .HttpHeaders ;
12- import org .springframework .http .HttpMethod ;
1314import org .springframework .stereotype .Controller ;
1415import org .springframework .ui .ModelMap ;
1516import org .springframework .web .bind .annotation .RequestMapping ;
1617
1718import javax .servlet .http .HttpServletResponse ;
18- import javax .ws .rs .core .MediaType ;
19- import java .io .BufferedReader ;
20- import java .io .IOException ;
21- import java .io .InputStreamReader ;
22- import java .net .HttpURLConnection ;
23- import java .net .URL ;
2419
2520
2621/**
@@ -33,11 +28,10 @@ public class M001GetMonitoringData extends AbstractMonitorController {
3328
3429 private final Session session ;
3530 private final User user ;
36- private static final String PROBLEMS_WITH_CONNECTION_ERROR_MESSAGE = "The connection string may be corrupt, please ensure that you are using the right URL." ;
3731
3832 @ Autowired
3933 public M001GetMonitoringData (DSConfiguration config , Session session , User user ) {
40- super (config , "m001" , "Get monitoring data" );
34+ super (config , "m001" , "Monitoring data result " );
4135 this .session = session ;
4236 this .user = user ;
4337 }
@@ -46,76 +40,48 @@ public M001GetMonitoringData(DSConfiguration config, Session session, User user)
4640 protected Object doWork (WorkArguments args , ModelMap model , HttpServletResponse response ) throws Exception {
4741 String accessToken = this .user .getAccessToken ();
4842
49- // Check if you are using the JWT authentication
43+ // Check, if you are using the JWT authentication
44+ // step 1 start
5045 accessToken = ensureUsageOfJWTToken (accessToken , this .session );
46+ // step 1 end
5147
52- String requestPath = session . getBasePath () + apiUrl ;
48+ JSONArray result = getMonitoringData ( accessToken ) ;
5349
54- JSONArray result = getMonitoringData (requestPath , accessToken , model );
50+ // Cleaning the data from wrong symbols
51+ String resultCleaned = result .toString ().replaceAll ("'" , "" );
5552
5653 // Process results
5754 DoneExample .createDefault (title )
58- .withMessage ("Results from the DataSet:GetStreamForDataset method:" )
59- .withJsonObject (result . toString () )
55+ .withMessage ("Results from DataSet:getStream method:" )
56+ .withJsonObject (resultCleaned )
6057 .addToModel (model );
6158
6259 return DONE_EXAMPLE_PAGE ;
6360 }
6461
65- protected JSONArray getMonitoringData (String requestPath , String accessToken , ModelMap model ) throws Exception {
62+ protected JSONArray getMonitoringData (String accessToken ) throws Exception {
6663 // Declare variables
6764 boolean complete = false ;
6865 String cursorValue = "" ;
69- Integer limit = 1 ; // Amount of records you want to read in one request
66+ // Integer limit = 1; // Amount of records you want to read in one request
7067 JSONArray result = new JSONArray ();
7168
72- // Get monitoring data
69+ DataSetApi datasetApi = this .createDataSetApi (accessToken , this .session );
70+ GetStreamOptions options = datasetApi .new GetStreamOptions ();
71+ // options.setLimit(limit);
72+
73+ // First call the endpoint with no cursor to get the first records.
74+ // After each call, save the cursor and use it to make the next
75+ // call from the point where the previous one left off when iterating through
76+ // the monitoring records
7377 do
7478 {
75- String cursorValueFormatted = (cursorValue .isEmpty ()) ? cursorValue : String .format ("=%s" , cursorValue );
76-
77- // Add cursor value and amount of records to read to the request
78- String requestParameters = String .format ("/stream?cursor%s&limit=%d" ,
79- cursorValueFormatted , limit );
80-
81- URL fullRequestPath = new URL (requestPath + requestParameters );
82- HttpURLConnection httpConnection = (HttpURLConnection ) fullRequestPath .openConnection ();
83- httpConnection .setRequestMethod (HttpMethod .GET .toString ());
84-
85- // Construct API headers
86- // step 2 start
87- httpConnection .setRequestProperty (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON );
88- httpConnection .setRequestProperty (HttpHeaders .AUTHORIZATION , BEARER_AUTHENTICATION + accessToken );
89- // step 2 end
90-
91- int responseCode = httpConnection .getResponseCode ();
92- if (responseCode < HttpURLConnection .HTTP_OK || responseCode >= HttpURLConnection .HTTP_BAD_REQUEST ) {
93- if (httpConnection .getResponseMessage () != PROBLEMS_WITH_CONNECTION_ERROR_MESSAGE ){
94- throw new Exception (httpConnection .getResponseMessage ());
95- }
96-
97- DoneExample .createDefault (this .title )
98- .withMessage (PROBLEMS_WITH_CONNECTION_ERROR_MESSAGE )
99- .addToModel (model );
100- }
101-
102- // step 3 start
103- BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (httpConnection .getInputStream ()));
104- String temp ;
105- StringBuilder stringBuilder = new StringBuilder ();
106- while ((temp = bufferedReader .readLine ()) != null ) {
107- stringBuilder .append (temp );
108- }
109- bufferedReader .close ();
110-
111-
79+ if (!cursorValue .isEmpty ())
80+ options .setCursor (cursorValue );
11281
113- httpConnection .disconnect ();
114- // Removing invalid symbols from the data
115- String responseData = stringBuilder .toString ().replaceAll ("'" , "" );
82+ CursoredResult cursoredResult = datasetApi .getStream ("2.0" , "monitor" , options );
11683
117- JSONObject object = new JSONObject (responseData );
118- String endCursor = object .getString ("endCursor" );
84+ String endCursor = cursoredResult .getEndCursor ();
11985
12086 // If the endCursor from the response is the same as the one that you already have,
12187 // it means that you have reached the end of the records
@@ -126,11 +92,10 @@ protected JSONArray getMonitoringData(String requestPath, String accessToken, Mo
12692 else
12793 {
12894 cursorValue = endCursor ;
129- result .put (new JSONObject (responseData ));
95+ result .put (new JSONObject (cursoredResult ));
13096 }
13197 }
13298 while (!complete );
133- //step 3 end
13499
135100 return result ;
136101 }
0 commit comments