2121import org .apache .http .message .BasicNameValuePair ;
2222
2323/**
24+ * Client for interacting with the Castlabs key ingestion API.
25+ *
2426 * @author Scott Kidder
2527 *
2628 */
2729public class CastlabsClient {
2830
29- private static final String CASTLABS_BASE_URL ="https://auth.drmtoday.com/" ;
30- private String baseUrl ;
31+ private static final String CASTLABS_AUTH_BASE_URL ="https://auth.drmtoday.com/" ;
32+ private static final String CASTLABS_INGESTION_BASE_URL ="https://fe.drmtoday.com/" ;
33+ private String authBaseUrl ;
34+ private String ingestionBaseUrl ;
3135private String username ;
3236private String password ;
3337private int connectionTimeoutSeconds = -1 ;
3438
3539public CastlabsClient (String username ,String password ) {
36- this (username ,password ,CASTLABS_BASE_URL , -1 );
37- }
38-
39- public CastlabsClient (String username ,String password ,String baseUrl ) {
40- this (username ,password ,baseUrl , -1 );
40+ this (username ,password ,CASTLABS_AUTH_BASE_URL ,CASTLABS_INGESTION_BASE_URL , -1 );
4141 }
4242
43- public CastlabsClient (String username ,String password ,String baseUrl ,
43+ public CastlabsClient (String username ,String password ,String authBaseUrl , String ingestionBaseUrl ,
4444int connectionTimeoutSeconds ) {
4545this .username =username ;
4646this .password =password ;
4747this .connectionTimeoutSeconds =connectionTimeoutSeconds ;
48- if (baseUrl .endsWith ("/" )) {
49- this .baseUrl =baseUrl ;
48+
49+ if (authBaseUrl .endsWith ("/" )) {
50+ this .authBaseUrl =authBaseUrl ;
51+
52+ }else {
53+ this .authBaseUrl =authBaseUrl +"/" ;
54+ }
55+
56+ if (ingestionBaseUrl .endsWith ("/" )) {
57+ this .ingestionBaseUrl =ingestionBaseUrl ;
5058
5159 }else {
52- this .baseUrl =baseUrl +"/" ;
60+ this .ingestionBaseUrl =ingestionBaseUrl +"/" ;
5361 }
5462 }
5563
5664/**
5765 * Login to the Castlabs API endpoint.
5866 *
59- * @return
60- * @throws CastlabsException
67+ * @return a ticket URL
68+ * @throws CastlabsException error reported by Castlabs
69+ * @throws IOException communication error when interacting with Castlabs API
6170 */
6271protected String login ()throws CastlabsException ,IOException {
63- final HttpPost loginRequest =new HttpPost (this .baseUrl +"cas/v1/tickets" );
72+ final HttpPost loginRequest =new HttpPost (this .authBaseUrl +"cas/v1/tickets" );
6473loginRequest .addHeader ("Content-Type" ,"application/x-www-form-urlencoded" );
6574loginRequest .setHeader ("Accept" ,"*/*" );
6675
@@ -108,14 +117,23 @@ protected String login() throws CastlabsException, IOException {
108117 }
109118 }
110119
111-
120+
121+ /**
122+ * Retrieve an authentication ticket with the given merchant ID.
123+ *
124+ * @param merchantId Castlabs-issued merchant ID
125+ * @return ticket that can be used to ingest encryption keys
126+ * @throws CastlabsException error reported by Castlabs
127+ * @throws IOException communication error when interacting with Castlabs API
128+ */
112129public String getTicket (String merchantId )throws CastlabsException ,IOException {
113130final HttpPost ticketRequest =new HttpPost (this .login ());
114131ticketRequest .addHeader ("Content-Type" ,"application/x-www-form-urlencoded" );
115132ticketRequest .setHeader ("Accept" ,"*/*" );
116133
117134final List <NameValuePair >entityParts =new ArrayList <NameValuePair >();
118- entityParts .add (new BasicNameValuePair ("service" ,"https://fe.drmtoday.com/frontend/rest/keys/v1/cenc/merchant/" +merchantId +"/key" ));
135+ entityParts .add (new BasicNameValuePair ("service" ,
136+ this .ingestionBaseUrl +"frontend/api/keys/v2/ingest/" +merchantId ));
119137
120138final CloseableHttpClient httpclient =HttpClients .createDefault ();
121139CloseableHttpResponse ticketResponse =null ;
@@ -134,8 +152,8 @@ public String getTicket(String merchantId) throws CastlabsException, IOException
134152final int statusCode =ticketResponse .getStatusLine ().getStatusCode ();
135153final String reason =ticketResponse .getStatusLine ().getReasonPhrase ();
136154if (200 !=statusCode ) {
137- throw new CastlabsException (
138- "Ticket retrieval failed: Response code=" +statusCode +", Reason=" +reason );
155+ throw new CastlabsException ("Ticket retrieval failed: Response code="
156+ +statusCode +", Reason=" +reason );
139157 }
140158 }else {
141159throw new CastlabsException ("No response when retrieving Castlabs ticket" );
@@ -148,6 +166,6 @@ public String getTicket(String merchantId) throws CastlabsException, IOException
148166 }catch (IOException e ) {
149167// ignore
150168 }
151- }
169+ }
152170 }
153171}