2012-04-29 7 views
4

App Engine에서 Twilio의 Java 클라이언트 라이브러리를 사용하려고합니다. 있는 그대로 실행되지 않으며 클라이언트 lib에 약간의 변경이 필요할 것입니다. 나는 다음과 같은 변경을했지만 작동하지 않습니다App Engine의 Twilio 클라이언트 라이브러리

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); 

} 

이 컴파일 및 실행을 얻는다 그러나 나는이 예외를 얻을 : 계획 'HTTPS'하지 : 에 의한 : org.apache.http.HttpException를 등기. 요청이 이루어지면 요청한 내용은 다음과 같습니다 (변경되지 않음).

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); 
    } 
} 

twilio와 함께 Java 용 appengine을 사용 했습니까? 기본 인증을 사용하는 XML 또는 JSON과의 RESTful 요청 일뿐입니다. 클라이언트 라이브러리를 사용하고 나 자신을 코딩하는 요청을하는 데 시간을 낭비하지 않기를 바랬습니다 ... 어떤 아이디어?

답변

3

최신 Twilio Java 클라이언트 라이브러리는 appengine (Apache HttpClient)과 잘 작동하지 않는 외부 라이브러리에 의존합니다. Twilio의 원래 Java 클라이언트 코드는 java.net.HttpURLConnection을 사용했으며 외부 종속성이 없습니다.

이전 Twilio 자바 클라이언트 코드를 twilio4j에 추가했습니다 (몇 가지 버그 수정). 나는 그것을 생산에 사용하고있다.

TODO : 패치는 친숙한 공식 twilio-java 프로젝트에 제공되어야합니다.

1

기본 문제는 Twilio-Java가 Twilio의 서버에 연결하기 위해 Apache HttpClient을 사용하고 있으며, 기본적으로 Google AppEngine과 호환되지 않습니다.

Apache HttpClient on Google AppEngine을 사용할 수 있습니다. 당신은 get the source code해야하고 지침에 따라 Twilio-Java 클라이언트 라이브러리를 수정해야합니다.