调用Facebook时scala netty编解码器

时间:2013-11-20 22:16:26

标签: facebook scala playframework netty playframework-2.2

我正在使用使用Scala 2.10.2(运行Java 1.7.0_45)构建的play 2.2.1。我有一个端点,可以调用Facebook来验证从我们的客户传递给我们的Facebook oauth令牌。

我们异步调用本地RESTful数据库服务和https facebook graph api。换句话说,我们同时呼叫两个系统。间歇性地,我们在下面得到了例外。

java.lang.IllegalArgumentException: invalid version format: ᅥBヌ쪠ᄌS
    at org.jboss.netty.handler.codec.http.HttpVersion.(HttpVersion.java:102) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpVersion.valueOf(HttpVersion.java:62) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpResponseDecoder.createMessage(HttpResponseDecoder.java:104) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpMessageDecoder.decode(HttpMessageDecoder.java:189) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpClientCodec$Decoder.decode(HttpClientCodec.java:143) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpClientCodec$Decoder.decode(HttpClientCodec.java:127) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:500) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.handler.codec.http.HttpClientCodec.handleUpstream(HttpClientCodec.java:92) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90) ~[netty-3.7.0.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) ~[netty-3.7.0.Final.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_45]
    at java.lang.Thread.run(Thread.java:744) ~[na:1.7.0_45]
2013-11-20 16:55:27,175: ERROR [lt-dispatcher-2]                    application - An unexpected error occurred

我知道这篇帖子到stackoverflow:Send multiple asynchonous requests on a Netty client。虽然他们得到了类似的错误,但是他们的代码执行起来要复杂得多,所以我认为对我来说解决方案会有所不同。这是我们的代码:

      def getUserInfo(token: String): Future[FacebookUserInfo] = {

        val futureResponse: Future[Response] = WS.url(fbUrl + mePath).withQueryString("access_token" -> token).get()
        futureResponse.map {
          response => parseFacebookUserInfoResponse(response)
        }
      }

      def parseFacebookUserInfoResponse(response: Response): FacebookUserInfo = {
        Logger.debug("Got response code:" + response.status)
        if (response.status == HttpURLConnection.HTTP_OK) {
          val json: JsValue = Json.parse(response.body)
          val jsonResult: JsResult[FacebookUserInfo] = json.validate[FacebookUserInfo]
          jsonResult.fold(
            valid = {
              success => success
            },
            invalid = {
              error => throw new FacebookResponseException("Invalid Response received.")
            }
          )
        }

        else if (response.status == HttpURLConnection.HTTP_BAD_REQUEST) {
          throw new FacebookTokenException(response.json.as[FacebookError].message)
        } else
          throw new MyException(response.body)
      }

解决此问题的最佳方法是什么?我可以切换默认的http客户端并使用Apache HttpClient吗?我需要更新编解码器吗?我是否需要升级/降级netty?

0 个答案:

没有答案