Apache camel:为http4客户端设置cookie

时间:2013-06-13 10:32:39

标签: java scala apache-camel apache-httpclient-4.x

我正在玩Apache Camel,并且遇到一些问题,从“琐碎的例子”阶段过渡到'但是有这些烦人的细节'阶段。至关重要的是,如何明确地进入和修改端点。

在这种情况下,我需要为http客户端设置一个auth cookie。我有一个cookie存储对象,但不知道如何强制客户端使用它。我尝试使用HttpClientConfigurer,但HttpClient没有公开Cookie存储,但似乎也没有被调用。

我目前的代码:

class CookieClientConfigurer(cs: CookieStore) extends HttpClientConfigurer {
  /**
   * Not very nice, relying on a runtime type check. But what can you do?
   */
  def configureHttpClient(client: HttpClient) {
    client match {
      case client: AbstractHttpClient => client.setCookieStore(cs)
    }
  }
}

由以下人员调用:

val username = args(0)
val password = args(1)

val context = new DefaultCamelContext

locally {
  // First we need to grab some authentication cookie stuff!
  val httpClient = new DefaultHttpClient
  val post = new HttpPost("https://rt")
  val nvps = List(new BasicNameValuePair("username", username), new BasicNameValuePair("password", password))
  post.setEntity(new UrlEncodedFormEntity(asJavaList(nvps)))
  httpClient.execute(post)
  val ccc = new CookieClientConfigurer(httpClient.getCookieStore())
  val httpComp = context.getComponent("https4", classOf[HttpComponent])
  httpComp.setHttpClientConfigurer(ccc)
}

val routes = new RouteBuilder {
  def configure() = {
    from("timer://foo?fixedRate=true&delay=0&period=10000")
    .to("https4://rt/REST/1.0/ticket/335729/show")
    .to("file://test")

  }
}
context.addRoutes(routes)
context.start()
System.in.read()
context.stop()

知道如何设置"https4://rt/REST/1.0/ticket/335729/show"端点的cookie存储区吗?

1 个答案:

答案 0 :(得分:0)

因此,在进一步调查时,上述方法有效(它在其他地方失败),虽然我仍然不满意使用运行时类型检查来公开HttpClient的实现,所以关于如何做到这一点的答案更受欢迎!