2012-05-28 23 views
2

我使用DataBinder的調度以供製作工作得很好,只要Web服務器返回一個404DataBinder的調度:獲得403響應的壓縮內容

如果請求失敗,Web服務器返回一個HTTP請求403狀態碼,並在響應正文中提供詳細的錯誤消息,如XML。

如何讀取xml正文(不管403),例如我怎樣才能讓調度忽略所有的403錯誤?

我的代碼如下所示:

class HttpApiService(val apiAccount:ApiAccount) extends ApiService { 
    val http = new Http 

    override def baseUrl() = "http://ws.audioscrobbler.com/2.0" 

    def service(call:Call) : Response = { 
    val http = new Http 
    var req = url(baseUrl()) 
    var params = call.getParameterMap(apiAccount) 

    var response: NodeSeq = Text("") 

    var request: Request = constructRequest(call, req, params) 
    // Here a StatusCode exception is thrown. 
    // Cannot use StatusCode case matching because of GZIP compression 
    http(request <> {response = _}) 
    //returns the parsed xml response as NodeSeq 
    Response(response) 
    } 

    private def constructRequest(call: Call, req: Request, params: Map[String, String]): Request = { 
    val request: Request = call match { 
     case authCall: AuthenticatedCall => 
     if (authCall.isWriteRequest) req <<< params else req <<? params 
     case _ => req <<? params 
    } 
    //Enable gzip compression 
    request.gzip 
    } 
} 

回答

2

相信是這樣工作的:

val response: Either[String, xml.Elem] = 
    try { 
    Right(http(request <> { r => r })) 
    } catch { 
    case dispatch.StatusCode(403, contents) => 
     Left(contents) 
    } 

該錯誤會在左邊。成功將在右側。錯誤是一個字符串,應該包含您希望的XML響應。

如果你需要更多,我相信你可以看看HttpExecutor.x,它應該給你完全控制。不過,我已經使用派遣已經有一段時間了。

另外,我建議使用更多的val和更少的var。

+0

問題是內容是原始gzip內容。通常我會在所有可能的地方使用val。 – user3001

+0

有沒有辦法將內容傳遞給我之前定義的gzip和xml處理程序?我想要的是將錯誤內容解壓縮並解析爲XML – user3001

+0

在最新的分發版本(0.11.2)中找不到「StatusCode(代碼,內容)」,只存在「StatusCode(code)」 。 – null