Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit114f9d9

Browse files
author
Jesse Michael
committed
add castlabs client functionality
updateAuthorizationSettingsaddSharedSecretsetFairplayConfiguration
1 parentd3ed0f1 commit114f9d9

File tree

4 files changed

+304
-3
lines changed

4 files changed

+304
-3
lines changed

‎src/main/java/com/brightcove/castlabs/client/CastlabsClient.java‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
importjava.io.UnsupportedEncodingException;
88
importjava.util.List;
99

10-
importcom.brightcove.castlabs.client.request.LinkAccountToSubMerchantRequest;
10+
importcom.brightcove.castlabs.client.request.*;
1111
importcom.fasterxml.jackson.core.JsonProcessingException;
1212
importcom.google.common.collect.Lists;
1313
importorg.apache.commons.io.IOUtils;
@@ -25,9 +25,7 @@
2525
importorg.apache.http.impl.client.HttpClients;
2626
importorg.apache.http.message.BasicNameValuePair;
2727

28-
importcom.brightcove.castlabs.client.request.IngestKeysRequest;
2928
importcom.brightcove.castlabs.client.response.IngestAssetsResponse;
30-
importcom.brightcove.castlabs.client.request.AddSubMerchantAccountRequest;
3129
importcom.brightcove.castlabs.client.response.AddSubMerchantAccountResponse;
3230
importcom.fasterxml.jackson.databind.DeserializationFeature;
3331
importcom.fasterxml.jackson.databind.ObjectMapper;
@@ -267,6 +265,99 @@ public void linkAccountToSubMerchant(final LinkAccountToSubMerchantRequest reque
267265
}
268266
}
269267

268+
/**
269+
* Update Account Authorization Setting
270+
*
271+
* @param request Request parameters to pass to Castlabs
272+
* @param merchantUuid UUID for the merchant
273+
* @throws CastlabsException error reported by Castlabs
274+
* @throws IOException network error while communicating with Castlabs REST API
275+
*/
276+
publicvoidupdateAuthorizationSettings(finalUpdateAuthorizationSettingsRequestrequest,finalStringmerchantUuid)
277+
throwsIOException,CastlabsException {
278+
279+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/config/v1/" +merchantUuid +"/auth/settings");
280+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
281+
282+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
283+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
284+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
285+
286+
if (statusCode !=HttpStatus.SC_OK) {
287+
finalHttpEntityresponseEntity =httpResponse.getEntity();
288+
289+
StringresponseBody ="";
290+
if (responseEntity !=null) {
291+
responseBody =IOUtils.toString(responseEntity.getContent());
292+
}
293+
294+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
295+
}
296+
}
297+
}
298+
299+
/**
300+
* Add a Shared Secret to the Castlabs Account
301+
*
302+
* @param request Request parameters to pass to Castlabs
303+
* @param merchantUuid UUID for the merchant
304+
* @throws CastlabsException error reported by Castlabs
305+
* @throws IOException network error while communicating with Castlabs REST API
306+
*/
307+
publicvoidaddSharedSecret(finalSharedSecretRequestrequest,finalStringmerchantUuid)
308+
throwsIOException,CastlabsException {
309+
310+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/config/v1/" +merchantUuid +"/upfront/secret/add");
311+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
312+
313+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
314+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
315+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
316+
317+
if (statusCode !=HttpStatus.SC_OK) {
318+
finalHttpEntityresponseEntity =httpResponse.getEntity();
319+
320+
StringresponseBody ="";
321+
if (responseEntity !=null) {
322+
responseBody =IOUtils.toString(responseEntity.getContent());
323+
}
324+
325+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
326+
}
327+
}
328+
}
329+
330+
/**
331+
* Add Fairplay configuration to the Castlabs Account
332+
*
333+
* @param request Request parameters to pass to Castlabs
334+
* @param merchantUuid UUID for the merchant
335+
* @throws CastlabsException error reported by Castlabs
336+
* @throws IOException network error while communicating with Castlabs REST API
337+
*/
338+
publicvoidsetFairplayConfiguration(finalFairplayRequestrequest,finalStringmerchantUuid)
339+
throwsIOException,CastlabsException {
340+
341+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/config/v1/" +merchantUuid +"/drm/fairplay");
342+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
343+
344+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
345+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
346+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
347+
348+
if (statusCode !=HttpStatus.SC_OK) {
349+
finalHttpEntityresponseEntity =httpResponse.getEntity();
350+
351+
StringresponseBody ="";
352+
if (responseEntity !=null) {
353+
responseBody =IOUtils.toString(responseEntity.getContent());
354+
}
355+
356+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
357+
}
358+
}
359+
}
360+
270361
privateHttpPostcreateHttpPostRequest(finalStringuri,finalObjectbody)throwsJsonProcessingException,UnsupportedEncodingException {
271362
finalHttpPostrequest =newHttpPost(uri);
272363
request.addHeader("Content-Type","application/json");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.brightcove.castlabs.client.request;
2+
3+
publicclassFairplayRequest {
4+
5+
privateStringproviderCertificate;
6+
privateStringproviderPrivateKey;
7+
privateStringapplicationSecretKey;
8+
9+
publicStringgetProviderCertificate() {
10+
returnproviderCertificate;
11+
}
12+
13+
publicvoidsetProviderCertificate(StringproviderCertificate) {
14+
this.providerCertificate =providerCertificate;
15+
}
16+
17+
publicStringgetProviderPrivateKey() {returnproviderPrivateKey; }
18+
19+
publicvoidsetProviderPrivateKey(StringproviderPrivateKey) {
20+
this.providerPrivateKey =providerPrivateKey;
21+
}
22+
23+
publicStringgetApplicationSecretKey() {returnapplicationSecretKey; }
24+
25+
publicvoidsetApplicationSecretKey(StringapplicationSecretKey) {this.applicationSecretKey =applicationSecretKey; }
26+
27+
@Override
28+
publicStringtoString() {
29+
return"UpdateAuthorizationSettingsRequest{" +
30+
"providerCertificate='" +providerCertificate +'\'' +
31+
", providerPrivateKey='" +providerPrivateKey +'\'' +
32+
", applicationSecretKey='" +applicationSecretKey +'\'' +
33+
'}';
34+
}
35+
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecom.brightcove.castlabs.client.request;
2+
3+
publicclassSharedSecretRequest {
4+
5+
privateBooleanenabled;
6+
privateStringdescription;
7+
privateStringsecret;
8+
9+
publicBooleangetEnabled() {
10+
returnenabled;
11+
}
12+
13+
publicvoidsetEnabled(Booleanenabled) {
14+
this.enabled =enabled;
15+
}
16+
17+
publicStringgetDescription() {
18+
returndescription;
19+
}
20+
21+
publicvoidsetDescription(Stringdescription) {
22+
this.description =description;
23+
}
24+
25+
publicStringgetSecret() {returnsecret; }
26+
27+
publicvoidsetSecret(Stringsecret) {
28+
this.secret =secret;
29+
}
30+
31+
@Override
32+
publicStringtoString() {
33+
return"UpdateAuthorizationSettingsRequest{" +
34+
"enabled='" +enabled +'\'' +
35+
", description='" +description +'\'' +
36+
", secret='" +secret +'\'' +
37+
'}';
38+
}
39+
40+
}

‎src/test/java/com/brightcove/castlabs/client/CastlabsClientTest.java‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,140 @@ public void testItCanHandleCastlabsErrorsWhileLinkingAnAccountToASubMerchant() t
377377
mockServerClient.verify(expectedRequest,VerificationTimes.once());
378378
}
379379

380+
@Test
381+
publicvoidtestItCanUpdateAuthorizationSettings()throwsException {
382+
finalStringmerchantId ="merchX";
383+
finalHttpRequestexpectedRequest =
384+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/auth/settings")
385+
.withQueryStringParameter("ticket",exampleTicket)
386+
.withHeader("accept","application/json")
387+
.withHeader("content-type","application/json");
388+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(200));
389+
UpdateAuthorizationSettingsRequestrequest =newUpdateAuthorizationSettingsRequest();
390+
request.setMode("UPFRONT");
391+
392+
// Castlabs doesn't return any response body, so we're effectively asserting "No Exception" here
393+
castlabsClient.updateAuthorizationSettings(request,"merchX");
394+
395+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
396+
}
397+
398+
@Test
399+
publicvoidtestItCanHandleCastlabsErrorsWhileUpdatingAuthorizationSetting()throwsException {
400+
finalStringmerchantId ="merchX";
401+
finalHttpRequestexpectedRequest =
402+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/auth/settings")
403+
.withQueryStringParameter("ticket",exampleTicket)
404+
.withHeader("accept","application/json")
405+
.withHeader("content-type","application/json");
406+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(418).withBody("I'm a teapot"));
407+
UpdateAuthorizationSettingsRequestrequest =newUpdateAuthorizationSettingsRequest();
408+
request.setMode("UPFRONT");
409+
410+
try {
411+
castlabsClient.updateAuthorizationSettings(request,"merchX");
412+
fail("Expected a CastlabsException to be returned");
413+
}catch(CastlabsExceptione) {
414+
assertThat(e.getMessage(),StringContains.containsString("418"));
415+
assertThat(e.getMessage(),StringContains.containsString("I'm a teapot"));
416+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
417+
}
418+
419+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
420+
}
421+
422+
@Test
423+
publicvoidtestItCanAddSharedSecret()throwsException {
424+
finalStringmerchantId ="merchX";
425+
finalHttpRequestexpectedRequest =
426+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/upfront/secret/add")
427+
.withQueryStringParameter("ticket",exampleTicket)
428+
.withHeader("accept","application/json")
429+
.withHeader("content-type","application/json");
430+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(200));
431+
SharedSecretRequestrequest =newSharedSecretRequest();
432+
request.setEnabled(true);
433+
request.setDescription("Test Shared Secret");
434+
request.setSecret("[shared_secret_bytes]");
435+
436+
// Castlabs doesn't return any response body, so we're effectively asserting "No Exception" here
437+
castlabsClient.addSharedSecret(request,"merchX");
438+
439+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
440+
}
441+
442+
@Test
443+
publicvoidtestItCanHandleCastlabsErrorsWhileAddingSharedSecret()throwsException {
444+
finalStringmerchantId ="merchX";
445+
finalHttpRequestexpectedRequest =
446+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/upfront/secret/add")
447+
.withQueryStringParameter("ticket",exampleTicket)
448+
.withHeader("accept","application/json")
449+
.withHeader("content-type","application/json");
450+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(418).withBody("I'm a teapot"));
451+
SharedSecretRequestrequest =newSharedSecretRequest();
452+
request.setEnabled(true);
453+
request.setDescription("Test Shared Secret");
454+
request.setSecret("[shared_secret_bytes]");
455+
456+
try {
457+
castlabsClient.addSharedSecret(request,"merchX");
458+
fail("Expected a CastlabsException to be returned");
459+
}catch(CastlabsExceptione) {
460+
assertThat(e.getMessage(),StringContains.containsString("418"));
461+
assertThat(e.getMessage(),StringContains.containsString("I'm a teapot"));
462+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
463+
}
464+
465+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
466+
}
467+
468+
@Test
469+
publicvoidtestItCanSetFairplayConfiguration()throwsException {
470+
finalStringmerchantId ="merchX";
471+
finalHttpRequestexpectedRequest =
472+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/drm/fairplay")
473+
.withQueryStringParameter("ticket",exampleTicket)
474+
.withHeader("accept","application/json")
475+
.withHeader("content-type","application/json");
476+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(200));
477+
FairplayRequestrequest =newFairplayRequest();
478+
request.setProviderCertificate("[PEM_encoded_certificate]");
479+
request.setProviderPrivateKey("[PEM_encoded_private_key]");
480+
request.setApplicationSecretKey("[HEX_encoded_secret_key]");
481+
482+
// Castlabs doesn't return any response body, so we're effectively asserting "No Exception" here
483+
castlabsClient.setFairplayConfiguration(request,"merchX");
484+
485+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
486+
}
487+
488+
@Test
489+
publicvoidtestItCanHandleCastlabsErrorsWhileSettingFairplayConfiguration()throwsException {
490+
finalStringmerchantId ="merchX";
491+
finalHttpRequestexpectedRequest =
492+
request().withMethod("POST").withPath("/frontend/rest/config/v1/" +merchantId +"/drm/fairplay")
493+
.withQueryStringParameter("ticket",exampleTicket)
494+
.withHeader("accept","application/json")
495+
.withHeader("content-type","application/json");
496+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(418).withBody("I'm a teapot"));
497+
FairplayRequestrequest =newFairplayRequest();
498+
request.setProviderCertificate("[PEM_encoded_certificate]");
499+
request.setProviderPrivateKey("[PEM_encoded_private_key]");
500+
request.setApplicationSecretKey("[HEX_encoded_secret_key]");
501+
502+
try {
503+
castlabsClient.setFairplayConfiguration(request,"merchX");
504+
fail("Expected a CastlabsException to be returned");
505+
}catch(CastlabsExceptione) {
506+
assertThat(e.getMessage(),StringContains.containsString("418"));
507+
assertThat(e.getMessage(),StringContains.containsString("I'm a teapot"));
508+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
509+
}
510+
511+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
512+
}
513+
380514
privateStringgetTestResourceAsString(Stringfilename)throwsIOException {
381515
finalStringpath =this.getClass().getClassLoader().getResource(filename).getFile();
382516
returnIOUtils.toString(newFileInputStream(path));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp