我想禁止从外部完全访问Solr内核,并且只能用于查询。因此,我在端口上启动了Jetty servlet容器内的辅助服务器瓦特/连接器实例(除主Web应用程序外),该服务器无法从WWW访问。使用电梯作为代理
当有传入HTTP请求到liftweb
应用,我RestHelper
钩:
object Dispatcher extends RestHelper {
serve {
case List("api", a @ _*) JsonGet _ => JString("API is not implemented yet. rest: " + a)
}
}
瞄准我的浏览器http://localhost/api/solr/select?q=region
我得到回应"API is not implemented yet. rest: List(solr, select)"
,所以它似乎工作。现在,我想在内部端口(Solr所在的位置)上进行连接,以便使用URL的后api
部分(即http://localhost:8080/solr/select?q=region
)传递查询。我正在捕获URL的尾部REST部分(通过a @ _*
),但我如何访问URL参数?将原始字符串(在api
路径元素之后)传递给Solr实例是非常理想的,只是为了防止冗余分析/构建步骤。所以适用于Solr的回应:我想避免解析构建JsonResponse。
这似乎是做一些HTTP重定向的一个很好的例子,但我必须打开隐藏的Solr的端口,据我所知。
什么是最有效的方法来应对这项任务?
编辑:
好了,我错过了JsonGet
来Req
价值,它具有所有必要的信息后。但是,还有一种方法可以避免不需要的解析/组合URL到隐藏端口和JSON响应?
SOLUTION:
这是我有consdering戴夫的建议:
import net.liftweb.common.Full
import net.liftweb.http.{JsonResponse, InMemoryResponse}
import net.liftweb.http.provider.HTTPRequest
import net.liftweb.http.rest.RestHelper
import dispatch.{Http, url}
object ApiDispatcher extends RestHelper {
private val SOLR_PORT = 8080
serve { "api" :: Nil prefix {
case JsonGet(path @ List("solr", "select"), r) =>
val u = localApiUrl(SOLR_PORT, path, r.request)
Http(url(u) >> { is =>
val bytes = Stream.continually(is.read).takeWhile(-1 !=).map(_.toByte).toArray
val headers = ("Content-Length", bytes.length.toString) ::
("Content-Type", "application/json; charset=utf-8") :: JsonResponse.headers
Full(InMemoryResponse(bytes, headers, JsonResponse.cookies, 200))
})
}}
private def localApiUrl(port: Int, path: List[String], r: HTTPRequest) =
"%s://localhost:%d/%s%s".format(r.scheme, port, path mkString "/", r.queryString.map("?" + _).openOr(""))
}