1

How do I tell dotNetRDF to request and accept data from a remote triplestore where the response is encoded using gzip?

Looking at the source code for the LoadGraph method of SparqlHttpProtocolConnector, it doesn't appear to me to have a mechanism for setting the Accept-Encoding header, nor am I seeing any logic that would process a Content-Encoding header.

I tried modifying LoadGraph to set Accept-Encoding, and the content then comes back with the right Content-Type and Content-Encoding, but the line of code that determines how to handle the response is

IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);

and GetParser doesn't have any logic that considers the Content-Encoding.

However, it seems like the pieces are present: there's certainly infrastructure in place to process a gzipped file.

Is there another way to do this that I'm missing, or would this be a new feature request?

Thanks.

MWood
  • 68
  • 10

1 Answers1

2

You can extend SPARQLHttpProtocolConnector and then override the ApplyCustomRequestOptions method to apply the Accept-Encoding header.

Although the MimeTypesHelper does not discriminate on the Content-Encoding header of the response, you can instead use the HttpWebRequest.AutomaticDecompression Property to enable automatic decompression of the response stream. Again, this can be set in the ApplyCustomRequestOptions method.

So your extension class would be something like this:

public class CompressedSparqlHttpProtocolConnector : SparqlHttpProtocolConnector
{
  // Define appropriate constructors with the parameters you need e.g.
  public CompressedSparqlHttpProtocolConnector(Uri serviceUri)
: base(serviceUri) { }     

  protected override ApplyCustomRequestOptions(HttpWebRequest request)
  {
     // Request GZip encoded response, allow fallback to identity encoding
     request.Headers[HttpRequestHeader.ContentEncoding] = "gzip;q=1.0, identity;q=0.5"

     // Enable automatic decompression of the response
     request.AutomaticDecompression = DecompressionMethods.GZip;
  } 
}
Kal
  • 1,887
  • 12
  • 16
  • Thanks for the response. But ApplyCustomRequestOptions is defined in abstract class BaseEndpoint which is not one of the base classes for SparqlHttpProtocolConnector. (SparqlHttpProtocolConnector derives from BaseHttpConnector, which includes ApplyRequestOptions, but that method can't be overridden.) – MWood Nov 03 '16 at 18:01
  • Also, did you mean HttpRequestHeader.AcceptEncoding? As a work-around, I created a derived class as you suggested, but lifted the entire LoadGraph functionality from SparqlHttpProtocolConnector, adding the above functionality. Thanks for pointing me in the right direction. – MWood Nov 03 '16 at 18:11