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

Commit8fca52c

Browse files
authored
Merge pull request#2 from zencoder/sub-merchant
Added support for sub-merchant creation
2 parents16b4b2a +66a1baf commit8fca52c

File tree

8 files changed

+323
-158
lines changed

8 files changed

+323
-158
lines changed

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
1313
/target/
14+
15+
# IntelliJ
16+
.idea
17+
*.iml

‎pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@
121121
<artifactId>commons-io</artifactId>
122122
<version>2.4</version>
123123
</dependency>
124+
<dependency>
125+
<groupId>commons-lang</groupId>
126+
<artifactId>commons-lang</artifactId>
127+
<version>2.4</version>
128+
</dependency>
124129
</dependencies>
125130
<distributionManagement>
126131
<snapshotRepository>
Lines changed: 105 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/**
22
* Copyright 2015 Brightcove Inc. All rights reserved.
3-
*
4-
* @author Scott Kidder
53
*/
64
packagecom.brightcove.castlabs.client;
75

86
importjava.io.IOException;
9-
importjava.util.ArrayList;
107
importjava.util.List;
118

9+
importcom.google.common.collect.Lists;
1210
importorg.apache.commons.io.IOUtils;
11+
importorg.apache.commons.lang.StringUtils;
1312
importorg.apache.http.Header;
1413
importorg.apache.http.HttpEntity;
1514
importorg.apache.http.NameValuePair;
@@ -24,14 +23,13 @@
2423

2524
importcom.brightcove.castlabs.client.request.IngestKeysRequest;
2625
importcom.brightcove.castlabs.client.response.IngestAssetsResponse;
26+
importcom.brightcove.castlabs.client.request.AddSubMerchantAccountRequest;
27+
importcom.brightcove.castlabs.client.response.AddSubMerchantAccountResponse;
2728
importcom.fasterxml.jackson.databind.DeserializationFeature;
2829
importcom.fasterxml.jackson.databind.ObjectMapper;
2930

3031
/**
31-
* Client for interacting with the Castlabs key ingestion API.
32-
*
33-
* @author Scott Kidder
34-
*
32+
* Client for interacting with the Castlabs API.
3533
*/
3634
publicclassCastlabsClient {
3735

@@ -44,12 +42,11 @@ public class CastlabsClient {
4442
privateintconnectionTimeoutSeconds = -1;
4543
privateObjectMapperobjectMapper;
4644

47-
publicCastlabsClient(Stringusername,Stringpassword) {
45+
publicCastlabsClient(finalStringusername,finalStringpassword) {
4846
this(username,password,CASTLABS_AUTH_BASE_URL,CASTLABS_INGESTION_BASE_URL, -1);
4947
}
5048

51-
publicCastlabsClient(Stringusername,Stringpassword,StringauthBaseUrl,
52-
StringingestionBaseUrl,intconnectionTimeoutSeconds) {
49+
publicCastlabsClient(finalStringusername,finalStringpassword,finalStringauthBaseUrl,finalStringingestionBaseUrl,finalintconnectionTimeoutSeconds) {
5350
this.objectMapper =newObjectMapper()
5451
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
5552
this.username =username;
@@ -71,33 +68,32 @@ public CastlabsClient(String username, String password, String authBaseUrl,
7168

7269
/**
7370
* Login to the Castlabs API endpoint.
74-
*
71+
*
7572
* @return a ticket URL
7673
* @throws CastlabsException error reported by Castlabs
77-
* @throws IOException communication error when interacting with Castlabs API
74+
* @throws IOExceptioncommunication error when interacting with Castlabs API
7875
*/
7976
protectedStringlogin()throwsCastlabsException,IOException {
8077
finalHttpPostloginRequest =newHttpPost(this.authBaseUrl +"cas/v1/tickets");
8178
loginRequest.addHeader("Content-Type","application/x-www-form-urlencoded");
8279
loginRequest.setHeader("Accept","*/*");
8380

84-
finalList<NameValuePair>entityParts =newArrayList<NameValuePair>();
81+
finalList<NameValuePair>entityParts =Lists.newArrayList();
8582
entityParts.add(newBasicNameValuePair("username",this.username));
8683
entityParts.add(newBasicNameValuePair("password",this.password));
8784

85+
if (this.connectionTimeoutSeconds >0) {
86+
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
87+
finalRequestConfigrequestConfig =
88+
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
89+
.setConnectTimeout(connectionTimeout)
90+
.setSocketTimeout(connectionTimeout).build();
91+
loginRequest.setConfig(requestConfig);
92+
}
93+
loginRequest.setEntity(newUrlEncodedFormEntity(entityParts));
94+
8895
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
89-
CloseableHttpResponseloginResponse =null;
90-
try {
91-
if (this.connectionTimeoutSeconds >0) {
92-
intconnectionTimeout =connectionTimeoutSeconds *1000;
93-
RequestConfigrequestConfig =
94-
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
95-
.setConnectTimeout(connectionTimeout)
96-
.setSocketTimeout(connectionTimeout).build();
97-
loginRequest.setConfig(requestConfig);
98-
}
99-
loginRequest.setEntity(newUrlEncodedFormEntity(entityParts));
100-
loginResponse =httpclient.execute(loginRequest);
96+
try (finalCloseableHttpResponseloginResponse =httpclient.execute(loginRequest)) {
10197
if (loginResponse !=null) {
10298
finalintstatusCode =loginResponse.getStatusLine().getStatusCode();
10399
finalStringreason =loginResponse.getStatusLine().getReasonPhrase();
@@ -115,47 +111,38 @@ protected String login() throws CastlabsException, IOException {
115111
}else {
116112
thrownewCastlabsException("No location header provided in API response");
117113
}
118-
}finally {
119-
try {
120-
if (loginResponse !=null)
121-
loginResponse.close();
122-
}catch (IOExceptione) {
123-
// ignore
124-
}
125114
}
126115
}
127116

128117

129118
/**
130-
* Retrieve an authentication ticket with the givenmerchant ID.
131-
*
132-
* @parammerchantId Castlabs-issued merchant ID
119+
* Retrieve an authentication ticket with the givenURL and return the URL with token appended
120+
*
121+
* @paramurl URL to request the token for
133122
* @return ticket that can be used to ingest encryption keys
134123
* @throws CastlabsException error reported by Castlabs
135-
* @throws IOException communication error when interacting with Castlabs API
124+
* @throws IOExceptioncommunication error when interacting with Castlabs API
136125
*/
137-
protectedStringgetTicket(StringmerchantId)throwsCastlabsException,IOException {
126+
protectedStringgetUrlWithTicket(finalStringurl)throwsCastlabsException,IOException {
138127
finalHttpPostticketRequest =newHttpPost(this.login());
139128
ticketRequest.addHeader("Content-Type","application/x-www-form-urlencoded");
140129
ticketRequest.setHeader("Accept","*/*");
141130

142-
finalList<NameValuePair>entityParts =newArrayList<NameValuePair>();
143-
entityParts.add(newBasicNameValuePair("service",
144-
this.ingestionBaseUrl +"frontend/api/keys/v2/ingest/" +merchantId));
131+
finalList<NameValuePair>entityParts =Lists.newArrayList();
132+
entityParts.add(newBasicNameValuePair("service",url));
133+
134+
if (this.connectionTimeoutSeconds >0) {
135+
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
136+
finalRequestConfigrequestConfig =
137+
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
138+
.setConnectTimeout(connectionTimeout)
139+
.setSocketTimeout(connectionTimeout).build();
140+
ticketRequest.setConfig(requestConfig);
141+
}
142+
ticketRequest.setEntity(newUrlEncodedFormEntity(entityParts));
145143

146144
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
147-
CloseableHttpResponseticketResponse =null;
148-
try {
149-
if (this.connectionTimeoutSeconds >0) {
150-
intconnectionTimeout =connectionTimeoutSeconds *1000;
151-
RequestConfigrequestConfig =
152-
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
153-
.setConnectTimeout(connectionTimeout)
154-
.setSocketTimeout(connectionTimeout).build();
155-
ticketRequest.setConfig(requestConfig);
156-
}
157-
ticketRequest.setEntity(newUrlEncodedFormEntity(entityParts));
158-
ticketResponse =httpclient.execute(ticketRequest);
145+
try (finalCloseableHttpResponseticketResponse =httpclient.execute(ticketRequest)) {
159146
if (ticketResponse !=null) {
160147
finalintstatusCode =ticketResponse.getStatusLine().getStatusCode();
161148
if (200 !=statusCode) {
@@ -168,47 +155,39 @@ protected String getTicket(String merchantId) throws CastlabsException, IOExcept
168155
}else {
169156
thrownewCastlabsException("No response when retrieving Castlabs ticket");
170157
}
171-
returnIOUtils.toString(ticketResponse.getEntity().getContent());
172-
}finally {
173-
try {
174-
if (ticketResponse !=null)
175-
ticketResponse.close();
176-
}catch (IOExceptione) {
177-
// ignore
178-
}
158+
returnurl +"?ticket=" +IOUtils.toString(ticketResponse.getEntity().getContent());
179159
}
180160
}
181161

182162
/**
183163
* Ingest one or more keys into the Castlabs keystore.
184-
*
185-
* @param request
164+
*
165+
* @param request Request parameters to pass to Castlabs
186166
* @param merchantId
187167
* @return response from Castlabs
188168
* @throws CastlabsException error reported by Castlabs
189-
* @throws IOException network error while communicating with Castlabs REST API
169+
* @throws IOExceptionnetwork error while communicating with Castlabs REST API
190170
*/
191-
publicIngestAssetsResponseingestKeys(IngestKeysRequestrequest,StringmerchantId)
171+
publicIngestAssetsResponseingestKeys(finalIngestKeysRequestrequest,finalStringmerchantId)
192172
throwsCastlabsException,IOException {
193-
finalStringuri =this.ingestionBaseUrl +"frontend/api/keys/v2/ingest/" +merchantId
194-
+"?ticket=" +this.getTicket(merchantId);
173+
174+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl+"frontend/api/keys/v2/ingest/" +merchantId);
195175
finalHttpPostingestRequest =newHttpPost(uri);
196176
ingestRequest.addHeader("Content-Type","application/json");
197177
ingestRequest.setHeader("Accept","application/json");
198178

179+
if (this.connectionTimeoutSeconds >0) {
180+
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
181+
finalRequestConfigrequestConfig =
182+
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
183+
.setConnectTimeout(connectionTimeout)
184+
.setSocketTimeout(connectionTimeout).build();
185+
ingestRequest.setConfig(requestConfig);
186+
}
187+
ingestRequest.setEntity(newStringEntity(objectMapper.writeValueAsString(request)));
188+
199189
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
200-
CloseableHttpResponseingestResponse =null;
201-
try {
202-
if (this.connectionTimeoutSeconds >0) {
203-
intconnectionTimeout =connectionTimeoutSeconds *1000;
204-
RequestConfigrequestConfig =
205-
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
206-
.setConnectTimeout(connectionTimeout)
207-
.setSocketTimeout(connectionTimeout).build();
208-
ingestRequest.setConfig(requestConfig);
209-
}
210-
ingestRequest.setEntity(newStringEntity(objectMapper.writeValueAsString(request)));
211-
ingestResponse =httpclient.execute(ingestRequest);
190+
try (finalCloseableHttpResponseingestResponse =httpclient.execute(ingestRequest)) {
212191
if (ingestResponse !=null) {
213192
finalintstatusCode =ingestResponse.getStatusLine().getStatusCode();
214193
if (200 !=statusCode) {
@@ -220,22 +199,61 @@ public IngestAssetsResponse ingestKeys(IngestKeysRequest request, String merchan
220199
}
221200
finalHttpEntityresponseEntity =ingestResponse.getEntity();
222201
if (responseEntity !=null) {
223-
finalIngestAssetsResponsegetPadResponse =objectMapper
224-
.readValue(responseEntity.getContent(),IngestAssetsResponse.class);
225-
returngetPadResponse;
202+
returnobjectMapper.readValue(responseEntity.getContent(),IngestAssetsResponse.class);
226203
}else {
227204
thrownewCastlabsException("Empty response entity from Castlabs");
228205
}
229206
}else {
230207
thrownewCastlabsException("No response when ingesting keys into Castlabs");
231208
}
232-
}finally {
233-
try {
234-
if (ingestResponse !=null)
235-
ingestResponse.close();
236-
}catch (IOExceptione) {
237-
// ignore
209+
}
210+
}
211+
212+
/**
213+
* Add a sub merchant account to Castlabs.
214+
*
215+
* @param request Request parameters to pass to Castlabs
216+
* @param merchantUuid UUID for the merchant that the sub-merchant is being created off
217+
* @return response from Castlabs
218+
* @throws CastlabsException error reported by Castlabs
219+
* @throws IOException network error while communicating with Castlabs REST API
220+
*/
221+
publicAddSubMerchantAccountResponseaddSubMerchantAccount(finalAddSubMerchantAccountRequestrequest,finalStringmerchantUuid)
222+
throwsIOException,CastlabsException {
223+
224+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/reselling/v1/reseller/" +merchantUuid +"/submerchant/add");
225+
finalHttpPostaddResellerRequest =newHttpPost(uri);
226+
addResellerRequest.addHeader("Content-Type","application/json");
227+
addResellerRequest.setHeader("Accept","application/json");
228+
229+
if (this.connectionTimeoutSeconds >0) {
230+
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
231+
finalRequestConfigrequestConfig =
232+
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
233+
.setConnectTimeout(connectionTimeout)
234+
.setSocketTimeout(connectionTimeout).build();
235+
addResellerRequest.setConfig(requestConfig);
236+
}
237+
addResellerRequest.setEntity(newStringEntity(objectMapper.writeValueAsString(request)));
238+
239+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
240+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(addResellerRequest)){
241+
finalHttpEntityresponseEntity =httpResponse.getEntity();
242+
if (responseEntity ==null) {
243+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
244+
}
245+
246+
finalStringresponseBody =IOUtils.toString(responseEntity.getContent());
247+
if(StringUtils.isBlank(responseBody)) {
248+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
238249
}
250+
251+
finalAddSubMerchantAccountResponseresponse =objectMapper.readValue(responseBody,AddSubMerchantAccountResponse.class);
252+
if(response.getSubMerchantUuid() ==null) {
253+
thrownewCastlabsException("Unexpected response from Castlabs: " +responseBody);
254+
}
255+
returnresponse;
239256
}
240257
}
258+
241259
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.brightcove.castlabs.client.request;
2+
3+
publicclassAddSubMerchantAccountRequest {
4+
5+
privateStringname;
6+
privateStringapiNameSuffix;
7+
privateStringlinkedUserUuid;
8+
9+
publicStringgetName() {
10+
returnname;
11+
}
12+
13+
publicvoidsetName(Stringname) {
14+
this.name =name;
15+
}
16+
17+
publicStringgetApiNameSuffix() {
18+
returnapiNameSuffix;
19+
}
20+
21+
publicvoidsetApiNameSuffix(StringapiNameSuffix) {
22+
this.apiNameSuffix =apiNameSuffix;
23+
}
24+
25+
publicStringgetLinkedUserUuid() {
26+
returnlinkedUserUuid;
27+
}
28+
29+
publicvoidsetLinkedUserUuid(StringlinkedUserUuid) {
30+
this.linkedUserUuid =linkedUserUuid;
31+
}
32+
33+
@Override
34+
publicStringtoString() {
35+
return"AddSubMerchantAccountRequest{" +
36+
"name='" +name +'\'' +
37+
", apiNameSuffix='" +apiNameSuffix +'\'' +
38+
", linkedUserUuid='" +linkedUserUuid +'\'' +
39+
'}';
40+
}
41+
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagecom.brightcove.castlabs.client.response;
2+
3+
publicclassAddSubMerchantAccountResponse {
4+
5+
privateStringsubMerchantUuid;
6+
7+
publicStringgetSubMerchantUuid() {
8+
returnsubMerchantUuid;
9+
}
10+
11+
publicvoidsetSubMerchantUuid(StringsubMerchantUuid) {
12+
this.subMerchantUuid =subMerchantUuid;
13+
}
14+
15+
@Override
16+
publicStringtoString() {
17+
return"AddSubMerchantAccountResponse{" +
18+
"subMerchantUuid='" +subMerchantUuid +'\'' +
19+
'}';
20+
}
21+
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp