Skip to content

Commit 0d45463

Browse files
2 parents 369ae66 + 2fb6e61 commit 0d45463

6 files changed

Lines changed: 81 additions & 92 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<docusign.version>3.13.0-RC1</docusign.version>
2424
<rooms.version>1.1.0-RC1</rooms.version>
2525
<click.version>1.0.0-BETA</click.version>
26+
<monitor.version>1.0.0-BETA</monitor.version>
2627
</properties>
2728

2829
<dependencies>
@@ -99,6 +100,12 @@
99100
<version>${click.version}</version>
100101
</dependency>
101102

103+
<dependency>
104+
<groupId>com.docusign</groupId>
105+
<artifactId>docusign-monitor-java</artifactId>
106+
<version>${monitor.version}</version>
107+
</dependency>
108+
102109
<dependency>
103110
<groupId>org.projectlombok</groupId>
104111
<artifactId>lombok</artifactId>

src/main/java/com/docusign/common/ApiIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public enum ApiIndex {
44
ESIGNATURE("/pages/esignature/index", "/restapi"),
55
ROOMS("/pages/rooms/index", "/restapi"),
66
CLICK("/pages/click/index", "/clickapi"),
7-
MONITOR("/pages/monitor/index", "/api");
7+
MONITOR("/pages/monitor/index", "");
88

99
private final String indexPath;
1010
private final String baseUrlSuffix;

src/main/java/com/docusign/controller/monitor/examples/AbstractMonitorController.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.docusign.core.controller.AbstractController;
55
import com.docusign.core.model.AuthType;
66
import com.docusign.core.model.Session;
7-
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.beans.factory.annotation.Qualifier;
9-
import org.springframework.security.oauth2.client.OAuth2ClientContext;
7+
import com.docusign.monitor.api.DataSetApi;
8+
import com.docusign.monitor.client.ApiClient;
9+
import org.springframework.http.HttpHeaders;
1010
import org.springframework.stereotype.Controller;
1111

1212
/**
@@ -17,12 +17,6 @@ public abstract class AbstractMonitorController extends AbstractController {
1717

1818
private static final String EXAMPLE_PAGES_PATH = "pages/monitor/examples/";
1919

20-
protected static String apiUrl = "/v2.0/datasets/monitor";
21-
22-
@Qualifier("oauth2ClientContext")
23-
@Autowired
24-
private OAuth2ClientContext oAuth2ClientContext;
25-
2620
public AbstractMonitorController(DSConfiguration config, String exampleName, String title) {
2721
super(config, exampleName, title);
2822
}
@@ -38,5 +32,30 @@ protected String ensureUsageOfJWTToken(String accessToken, Session session) {
3832
return accessToken;
3933
}
4034
}
35+
36+
/**
37+
* Creates new instance of the Monitor API client.
38+
* @param accessToken user's access token
39+
* @param session active session
40+
* @return an instance of the {@link com.docusign.monitor.client.ApiClient}
41+
*/
42+
protected static ApiClient createApiClient(String accessToken, Session session) {
43+
ApiClient apiClient = new ApiClient(session.getBasePath());
44+
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, BEARER_AUTHENTICATION + accessToken);
45+
46+
return apiClient;
47+
}
48+
49+
/**
50+
* Creates a new instance of the DataSetApi. This method
51+
* creates an instance of the ApiClient class silently.
52+
* @param accessToken user's access token
53+
* @param session active session
54+
* @return an instance of the {@link DataSetApi}
55+
*/
56+
protected static DataSetApi createDataSetApi(String accessToken, Session session) {
57+
ApiClient apiClient = createApiClient(accessToken, session);
58+
return new DataSetApi(apiClient);
59+
}
4160
}
4261

src/main/java/com/docusign/controller/monitor/examples/M001GetMonitoringData.java

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@
55
import com.docusign.core.model.DoneExample;
66
import com.docusign.core.model.Session;
77
import 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;
811
import org.json.JSONArray;
912
import org.json.JSONObject;
1013
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.http.HttpHeaders;
12-
import org.springframework.http.HttpMethod;
1314
import org.springframework.stereotype.Controller;
1415
import org.springframework.ui.ModelMap;
1516
import org.springframework.web.bind.annotation.RequestMapping;
1617

1718
import 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
}

src/main/webapp/WEB-INF/templates/views/pages/monitor/examples/m001.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<p>API method used:
1212
<a target='_blank' rel='noopener noreferrer'
13-
href='https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstreamfordataset/'>DataSet:GetStreamForDataset</a>.
13+
href='https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstream/'>DataSet:getStream</a>.
1414
</p>
1515
<p>
1616
View source file <a target='_blank' href='${sourceUrl}'>${sourceFile}</a> on GitHub.

src/main/webapp/WEB-INF/templates/views/pages/monitor/index.jsp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
22
<jsp:include page='../../partials/head.jsp'/>
33

4-
<c:if test="${locals.user == null}">
5-
<!-- IF not signed in -->
6-
<div>
7-
<div class='jumbotron jumbotron-fluid'> <table>
8-
<tbody>
9-
<tr>
10-
<td>
11-
<h1 class='display-4'>Java Launcher</h1>
12-
<p class='Xlead'>Welcome to the Java code examples for DocuSign Monitor API with JWT Grant authentication</p>
13-
</td>
14-
<td>
15-
<img src='/assets/banner-code.png' />
16-
</td>
17-
</tr>
18-
</tbody>
19-
</table>
20-
</div>
21-
</c:if>
4+
5+
<div>
6+
<div class='jumbotron jumbotron-fluid'> <table>
7+
<tbody>
8+
<tr>
9+
<td>
10+
<h1 class='display-4'>Java Launcher</h1>
11+
<p class='Xlead'>Welcome to the Java code examples for DocuSign Monitor API with JWT Grant authentication</p>
12+
</td>
13+
<td>
14+
<img src='/assets/banner-code.png' />
15+
</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
</div>
2220

2321
<div class='container' style='margin-top: 40px' id='index-page'>
2422
<c:if test="${showDoc == true}">
@@ -30,7 +28,7 @@
3028
<h4 id='example001'>1. <a href='m001'>Get monitoring data</a></h4>
3129
<p> Demonstrates how to get and display all of your organization's monitoring data.</p>
3230
<p>API method used:
33-
<a target ='_blank' rel='noopener noreferrer' href='https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstreamfordataset/'>DataSet:GetStreamForDataset</a>
31+
<a target ='_blank' rel='noopener noreferrer' href='https://developers.docusign.com/docs/monitor-api/reference/monitor/dataset/getstream/'>DataSet:getStream</a>
3432
</p>
3533
</div>
3634

0 commit comments

Comments
 (0)