Reputation: 19446
I am trying to use Twilio's java client library on appengine. It does not appear to run as is and there will need to be some changes to the client lib. I have made the following changes, but does not work:
public TwilioRestClient(String accountSid, String authToken, String endpoint) {
validateAccountSid(accountSid);
validateAuthToken(authToken);
this.accountSid = accountSid;
this.authToken = authToken;
if ((endpoint != null) && (!endpoint.equals(""))) {
this.endpoint = endpoint;
}
/* origial code
* ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager();
* connMgr.setDefaultMaxPerRoute(10);
* this.httpclient = new DefaultHttpClient(connMgr);
*/
//my changes start HERE
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
BasicHttpParams params = new BasicHttpParams();
ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(params,schemeRegistry);
//my changes END HERE
this.httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("http.protocol.version",
HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.socket.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.connection.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.protocol.content-charset",
"UTF-8");
this.authAccount = new Account(this);
this.authAccount.setSid(this.accountSid);
this.authAccount.setAuthToken(this.authToken);
}
This gets it to compile and run, however I get this exception: Caused by: org.apache.http.HttpException: Scheme 'https' not registered. when the request is made. The request is in this (unaltered) code:
public TwilioRestResponse request(String path, String method,
Map<String, String> vars) throws TwilioRestException {
HttpUriRequest request = setupRequest(path, method, vars);
HttpResponse response;
try {
response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
String responseBody = "";
if (entity != null) {
responseBody = EntityUtils.toString(entity);
}
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();
TwilioRestResponse restResponse = new TwilioRestResponse(request
.getURI().toString(), responseBody, statusCode);
// For now we only set the first content type seen
for (Header h : contentTypeHeaders) {
restResponse.setContentType(h.getValue());
break;
}
return restResponse;
} catch (ClientProtocolException e1) {
throw new RuntimeException(e1);
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
Has any one used appengine for java with twilio? I know it is just a RESTful request with either XML or JSON using basic authentication. I was hoping to use the client library and not spend time doing the request coding myself...Any Ideas?
Upvotes: 4
Views: 1026
Reputation: 22741
The newest Twilio Java client library depends on external libs that don't play nice with appengine (Apache HttpClient). Twilio's original java client code just used java.net.HttpURLConnection and had no external dependencies.
I've added this older Twilio java client code to twilio4j (with a couple of bug fixes). I am using it in production.
TODO: A patch should be offered to the official twilio-java project that is appengine friendly.
Upvotes: 3
Reputation: 2474
The basic problem is that Twilio-Java is using the Apache HttpClient for connecting to Twilio's servers, and out of the box, it is not compatible with Google AppEngine.
It is possible to use the Apache HttpClient on Google AppEngine. You would have to get the source code and modify the Twilio-Java client library according to those instructions.
Upvotes: 1