Jena:如何显式指定RDFDataMgr.loadModel(url)发送的Accept Header?

时间:2016-05-19 07:29:48

标签: java http-headers rdf jena

我们需要查询带有错误的Http内容协商的服务器(它不理解Accept Header中的q参数)。所以我们需要用

发送查询

Accept:application/rdf+xml

而不是Jena的默认

Accept: text/turtle,application/n-triples;q=0.9,application/rdf+xml;q=0.7,application/trig,application/n-quads;q=0.9,text/x-nquads;q=0.8,application/x-trig;q=0.7,application/ld+json;q=0.6,*/*;q=0.5

这是我们目前用于设置Jena的Http处理的Java代码:

static {
    HttpOp.setDefaultHttpClient(createHttpClient(1000, 4000));
}

protected static HttpClient createHttpClient(final int connectTimeout, final int readTimeout) {
        final RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(connectTimeout)
                .setSocketTimeout(readTimeout)
                .setContentCompressionEnabled(true)
                .setRedirectsEnabled(true)
                .build();

        final HttpHost gndHost = new HttpHost("host.net", 80);
        final PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
        connMgr.setDefaultMaxPerRoute(10);
        connMgr.setMaxTotal(50);
        connMgr.setMaxPerRoute(new HttpRoute(gndHost), 2);

        return HttpClientBuilder.create()
                .setConnectionManager(connMgr)
                .setDefaultRequestConfig(requestConfig)
                .build();
    }

是否可以更改此常规设置,以便HttpQuery.setAccept(“application / rdf + xml”)用于所有请求?

2 个答案:

答案 0 :(得分:2)

函数QueryExecutionFactory.sparqlService返回一个QueryExecution对象,实际上是QueryEngineHTTP,并且有set*ContentType个方法。

如果您想进行非常详细的控制,可以直接使用HttpOp将{1}}传递给HttpResponseHandler

答案 1 :(得分:2)

此代码完成工作:

private Model fetchRemoteData(final String uri, final Boolean ignoreErrors) throws HttpException, TitleDataInvalidException {
        try {
            final Model rdfModel;
            if (uri.startsWith("http://d-nb.info")) {
                final Model tmpModel = ModelFactory.createDefaultModel();
                final TypedInputStream is = HttpOp.execHttpGet(uri, "application/rdf+xml");
                tmpModel.read(is, null);
                rdfModel = tmpModel;
            } else {
                rdfModel = RDFDataMgr.loadModel(uri);
            }
            return rdfModel;
        } catch (RiotException ex) {
    ...