Skip to content

Commit aca96cb

Browse files
* Removed MICHAEL from text to speech
* Added `BluemixUtils` as a way to get credentials from VCAP_SERVICES
1 parent d70976f commit aca96cb

7 files changed

Lines changed: 433 additions & 86 deletions

File tree

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 71 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343
import com.ibm.watson.developer_cloud.util.ResponseUtil;
4444

4545
/**
46-
* Watson service abstract common functionality of various Watson Services. It
47-
* handle authentication and default url
48-
*
46+
* Watson service abstract common functionality of various Watson Services. It handle
47+
* authentication and default url
48+
*
4949
* @author German Attanasio Ruiz ([email protected])
50-
* @see <a
51-
* href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/">
52-
* IBM Watson Developer Cloud</a>
50+
* @see <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/"> IBM
51+
* Watson Developer Cloud</a>
5352
*/
5453
public abstract class WatsonService {
5554

@@ -68,11 +67,10 @@ public abstract class WatsonService {
6867
private static final int CONNECTION_TIMEOUT = 120000;
6968

7069
/** The Constant log. */
71-
private static final Logger log = Logger.getLogger(WatsonService.class
72-
.getName());
70+
private static final Logger log = Logger.getLogger(WatsonService.class.getName());
7371

7472
/**
75-
* Field MAX_TOTAL_CONNECTIONS. (value is 1000)
73+
* Field MAX_CONNECTIONS_PER_ROUTE. (value is 1000)
7674
*/
7775
private static final int MAX_CONNECTIONS_PER_ROUTE = 1000;
7876

@@ -101,19 +99,18 @@ public abstract class WatsonService {
10199

102100
/**
103101
* Instantiates a new Watson service.
104-
*
102+
*
105103
*/
106-
public WatsonService() {
107-
}
104+
public WatsonService() {}
108105

109106
/**
110107
* Builds the request URI appending the service end point to the path.<br>
111108
* <b>From:</b> /v1/foo/bar <br>
112109
* <b>to:</b>https://host:port/api/v1/foo/bar
113-
*
110+
*
114111
* @param request
115112
* the http request
116-
*
113+
*
117114
* @return the URI including the service end point
118115
*/
119116
private URI buildRequestURI(HttpRequestBase request) {
@@ -129,11 +126,12 @@ private URI buildRequestURI(HttpRequestBase request) {
129126
}
130127

131128
/**
132-
* Execute the Http request and discard the response.
133-
* Use this when you don't want to get the response but you want to make sure
134-
* we read it so that the underline connection is released
135-
*
136-
* @param request the request
129+
* Execute the Http request and discard the response. Use this when you don't want to
130+
* get the response but you want to make sure we read it so that the underline
131+
* connection is released
132+
*
133+
* @param request
134+
* the request
137135
*/
138136
protected void executeWithoutResponse(HttpRequestBase request) {
139137
HttpResponse response = execute(request);
@@ -146,19 +144,18 @@ protected void executeWithoutResponse(HttpRequestBase request) {
146144

147145
/**
148146
* Execute the Http request.
149-
*
147+
*
150148
* @param request
151149
* the http request
152-
*
150+
*
153151
* @return the http response
154152
*/
155153
protected HttpResponse execute(HttpRequestBase request) {
156154

157155
setAuthentication(request);
158156

159157
if (getEndPoint() == null)
160-
throw new IllegalArgumentException(
161-
"service endpoint was not specified");
158+
throw new IllegalArgumentException("service endpoint was not specified");
162159

163160
if (!request.containsHeader(ACCEPT)) {
164161
request.addHeader(ACCEPT, getDefaultContentType());
@@ -189,48 +186,41 @@ protected HttpResponse execute(HttpRequestBase request) {
189186
// There was a Client Error 4xx or a Server Error 5xx
190187
// Get the error message and create the exception
191188
String error = getErrorMessage(response);
192-
log.log(Level.SEVERE, "HTTP Status: " + status + ", message: "+ error);
189+
log.log(Level.SEVERE, "HTTP Status: " + status + ", message: " + error);
193190

194191
switch (status) {
195192
case HttpStatus.SC_BAD_REQUEST: // HTTP 400
196193
throw new BadRequestException(error != null ? error : "Bad Request");
197194
case HttpStatus.SC_UNAUTHORIZED: // HTTP 401
198-
throw new UnauthorizedException(
199-
"Unauthorized: Access is denied due to invalid credentials");
195+
throw new UnauthorizedException("Unauthorized: Access is denied due to invalid credentials");
200196
case HttpStatus.SC_FORBIDDEN: // HTTP 403
201-
throw new ForbiddenException(error != null ? error
202-
: "Forbidden: Service refuse the request");
197+
throw new ForbiddenException(error != null ? error : "Forbidden: Service refuse the request");
203198
case HttpStatus.SC_NOT_FOUND: // HTTP 404
204199
throw new NotFoundException(error != null ? error : "Not found");
205200
case HttpStatus.SC_NOT_ACCEPTABLE: // HTTP 406
206-
throw new ForbiddenException(error != null ? error
207-
: "Forbidden: Service refuse the request");
201+
throw new ForbiddenException(error != null ? error : "Forbidden: Service refuse the request");
208202
case HttpStatus.SC_REQUEST_TOO_LONG: // HTTP 413
209-
throw new RequestTooLargeException(
210-
error != null ? error
211-
: "Request too large: The request entity is larger than the server is able to process");
203+
throw new RequestTooLargeException(error != null ? error
204+
: "Request too large: The request entity is larger than the server is able to process");
212205
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: // HTTP 415
213206
throw new UnsupportedException(
214207
error != null ? error
215208
: "Unsupported MIME type: The request entity has a media type which the server or resource does not support");
216209
case 429: // HTTP 429
217-
throw new TooManyRequestsException(error != null ? error
218-
: "Too many requests");
210+
throw new TooManyRequestsException(error != null ? error : "Too many requests");
219211
case HttpStatus.SC_INTERNAL_SERVER_ERROR: // HTTP 500
220-
throw new InternalServerErrorException(error != null ? error
221-
: "Internal Server Error");
212+
throw new InternalServerErrorException(error != null ? error : "Internal Server Error");
222213
case HttpStatus.SC_SERVICE_UNAVAILABLE: // HTTP 503
223-
throw new ServiceUnavailableException(error != null ? error
224-
: "Service Unavailable");
214+
throw new ServiceUnavailableException(error != null ? error : "Service Unavailable");
225215
default: // other errors
226216
throw new ServiceResponseException(status, error);
227217
}
228218
}
229219

230220
/**
231221
* Gets the API key.
232-
*
233-
*
222+
*
223+
*
234224
* @return the API key
235225
*/
236226
protected String getApiKey() {
@@ -239,8 +229,8 @@ protected String getApiKey() {
239229

240230
/**
241231
* Gets the default content type.
242-
*
243-
*
232+
*
233+
*
244234
* @return the default content type
245235
*/
246236
protected String getDefaultContentType() {
@@ -249,8 +239,8 @@ protected String getDefaultContentType() {
249239

250240
/**
251241
* Gets the default request.
252-
*
253-
*
242+
*
243+
*
254244
* @return the default request
255245
*/
256246
protected HttpParams getDefaultRequestParams() {
@@ -266,8 +256,8 @@ protected HttpParams getDefaultRequestParams() {
266256

267257
/**
268258
* Gets the API end point.
269-
*
270-
*
259+
*
260+
*
271261
* @return the API end point
272262
*/
273263
public String getEndPoint() {
@@ -276,16 +266,16 @@ public String getEndPoint() {
276266

277267
/**
278268
* Gets the error message from a JSON response
279-
*
269+
*
280270
* <pre>
281271
* {
282272
* code: 400
283273
* error: 'bad request'
284274
* }
285275
* </pre>
286-
*
276+
*
287277
* .
288-
*
278+
*
289279
* @param response
290280
* the HTTP response
291281
* @return the error message from the json object
@@ -308,8 +298,8 @@ private String getErrorMessage(HttpResponse response) {
308298

309299
/**
310300
* Gets the http client.
311-
*
312-
*
301+
*
302+
*
313303
* @return the http client
314304
*/
315305
public HttpClient getHttpClient() {
@@ -321,25 +311,24 @@ public HttpClient getHttpClient() {
321311

322312
/**
323313
* Gets the thread safe client.
324-
*
314+
*
325315
* @return the thread safe client
326316
*/
327317
private HttpClient getThreadSafeClient() {
328318

329-
DefaultHttpClient client = new DefaultHttpClient(getDefaultRequestParams());
330-
ClientConnectionManager mgr = client.getConnectionManager();
331-
HttpParams params = client.getParams();
319+
DefaultHttpClient client = new DefaultHttpClient(getDefaultRequestParams());
320+
ClientConnectionManager mgr = client.getConnectionManager();
321+
HttpParams params = client.getParams();
332322

333-
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
334-
mgr.getSchemeRegistry()), params);
323+
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
335324

336-
return client;
325+
return client;
337326
}
338327

339328
/**
340329
* Gets the user agent.
341-
*
342-
*
330+
*
331+
*
343332
* @return the user agent
344333
*/
345334
private final String getUserAgent() {
@@ -348,7 +337,7 @@ private final String getUserAgent() {
348337

349338
/**
350339
* Sets the API key.
351-
*
340+
*
352341
* @param apiKey
353342
* the new API key
354343
*/
@@ -358,7 +347,7 @@ public void setApiKey(String apiKey) {
358347

359348
/**
360349
* Sets the end point.
361-
*
350+
*
362351
* @param endPoint
363352
* the new end point
364353
*/
@@ -368,7 +357,7 @@ public void setEndPoint(String endPoint) {
368357

369358
/**
370359
* Sets the username and password.
371-
*
360+
*
372361
* @param username
373362
* the username
374363
* @param password
@@ -381,29 +370,31 @@ public void setUsernameAndPassword(String username, String password) {
381370

382371
/**
383372
* Sets the authentication.
384-
*
385-
* @param request the new authentication
373+
*
374+
* @param request
375+
* the new authentication
386376
*/
387-
protected void setAuthentication(HttpRequestBase request){
388-
if (getApiKey() == null)
389-
throw new IllegalArgumentException(
390-
"apiKey or username and password were not specified");
391-
else {
392-
request.addHeader(AUTHORIZATION,
393-
apiKey.startsWith("Basic ") ? apiKey : "Basic " + apiKey);
377+
protected void setAuthentication(HttpRequestBase request) {
378+
if (getApiKey() == null) {
379+
throw new IllegalArgumentException("apiKey or username and password were not specified");
380+
} else {
381+
request.addHeader(AUTHORIZATION, apiKey.startsWith("Basic ") ? apiKey : "Basic " + apiKey);
394382
}
395383

396384
}
397385

398386
/**
399387
* Execute the request and return the POJO that represent the response.
400-
*
401-
* @param <T> The POJO that represents the response object
402-
* @param request the request
403-
* @param returnType the POJO class to be parsed from the response
388+
*
389+
* @param <T>
390+
* The POJO that represents the response object
391+
* @param request
392+
* the request
393+
* @param returnType
394+
* the POJO class to be parsed from the response
404395
* @return the POJO object that represent the response
405396
*/
406-
protected <T> T executeRequest(Request request, Class<T> returnType) {
397+
protected <T> T executeRequest(Request request, Class<T> returnType) {
407398
HttpRequestBase requestBase = request.build();
408399
try {
409400
HttpResponse response = execute(requestBase);
@@ -412,9 +403,10 @@ protected <T> T executeRequest(Request request, Class<T> returnType) {
412403
throw new RuntimeException(e);
413404
}
414405
}
406+
415407
/*
416408
* (non-Javadoc)
417-
*
409+
*
418410
* @see java.lang.Object#toString()
419411
*/
420412
@Override

src/main/java/com/ibm/watson/developer_cloud/text_to_speech/v1/TextToSpeech.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public InputStream synthesize(final String text, final Voice voice) {
112112
* @return the input stream with the synthesized audio
113113
*/
114114
public InputStream synthesize(final String text, final String format) {
115-
return synthesize(text, Voice.EN_MICHAEL, format);
115+
return synthesize(text, Voice.EN_LISA, format);
116116
}
117117

118118
/**

src/main/java/com/ibm/watson/developer_cloud/text_to_speech/v1/model/Voice.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public class Voice extends GenericModel {
3333
/** The Constant ES_SOFIA. */
3434
public static final Voice ES_SOFIA = new Voice("es-US_SofiaVoice", "female", "es-US");
3535

36-
/** The Constant EN_MICHAEL. */
37-
public static final Voice EN_MICHAEL = new Voice("en-US_MichaelVoice", "male", "en-US");
38-
3936
/** The Constant EN_LISA. */
4037
public static final Voice EN_LISA = new Voice("en-US_LisaVoice", "female", "en-US");
4138

0 commit comments

Comments
 (0)