public void ProcessRequest(HttpContext context, Uri serviceBaseUri)
        {
            var sparqlGenerator = new SparqlGenerator(_config.Map, _config.DefaultLanguageCode, _config.MaxPageSize);

            var messageWriterSettings = new ODataMessageWriterSettings(_config.BaseODataWriterSettings)
                {
                    BaseUri = serviceBaseUri
                };
            if (context.Request.AcceptTypes != null)
            {
                messageWriterSettings.SetContentType(String.Join(",", context.Request.AcceptTypes),
                                                     context.Request.Headers["Accept-Charset"]);
            }

            var requestMessage = new HttpDataRequestMessage(context.Request);
            ODataVersion effectiveVersion = GetEffectiveODataVersion(requestMessage);
            messageWriterSettings.Version = effectiveVersion;

            var metadataUri = new Uri(serviceBaseUri, "./$metadata");
            if (serviceBaseUri.Equals(context.Request.Url))
            {
                // We need to respond with the service document
                var responseMessage = new HttpDataResponseMessage(context.Response);
                context.Response.ContentType = "application/xml";
                context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                var feedGenerator = new ODataFeedGenerator(requestMessage, responseMessage,
                                                           _config.Map, serviceBaseUri.ToString(),
                                                           messageWriterSettings);
                feedGenerator.WriteServiceDocument();
            }
            else if (serviceBaseUri.ToString().TrimEnd('/').Equals(context.Request.Url.ToString().TrimEnd('/')))
            {
                // Trimming of trailing slash to normalize - the serviceBaseUri should always have a trailing slash, 
                // but we will redirect requests that do not to the proper service document url
                context.Response.RedirectPermanent(serviceBaseUri.ToString(), true);
            }
            else if (metadataUri.Equals(context.Request.Url))
            {
                context.Response.ContentType = "application/xml";
                context.Response.WriteFile(_config.MetadataPath);
            }
            else
            {
                var parsedQuery = QueryDescriptorQueryNode.ParseUri(
                    context.Request.Url,
                    serviceBaseUri,
                    _config.Model);
                sparqlGenerator.ProcessQuery(parsedQuery);
                var responseMessage = new HttpDataResponseMessage(context.Response);
                var feedGenerator = new ODataFeedGenerator(
                    requestMessage,
                    responseMessage,
                    _config.Map,
                    serviceBaseUri.ToString(),
                    messageWriterSettings);
                sparqlGenerator.SparqlQueryModel.Execute(_config.SparqlEndpoint, feedGenerator);
                context.Response.ContentType = "application/atom+xml";
            }
        }
Ejemplo n.º 2
0
 public void Execute(SparqlRemoteEndpoint endpoint, ODataFeedGenerator handler)
 {
     if (IsDescribe)
     {
         string query        = GetSparqlRepresentation();
         IGraph resultsGraph = endpoint.QueryWithResultGraph(query);
         if (!String.IsNullOrEmpty(DescribeResource))
         {
             // Create ODATA entry payload for single resource
             handler.CreateEntryFromGraph(resultsGraph, DescribeResource, GetEntityType(DescribeResource));
         }
         else
         {
             if (IsDescribe)
             {
                 if (SelectVariables.Count > 1)
                 {
                     throw new Exception(
                               "Cannot create an entity feed from a SPARQL query with multiple DESCRIBE bindings");
                 }
                 string countQuery   = GetSparqlCountQuery();
                 var    countResults = endpoint.QueryWithResultSet(countQuery);
                 var    firstResult  = countResults.Results.FirstOrDefault();
                 int    resultsCount = 0;
                 if (firstResult != null)
                 {
                     resultsCount = Int32.Parse((firstResult[0] as ILiteralNode).Value);
                 }
                 var selectVarInfo = VariableType[SelectVariables[0]];
                 if (selectVarInfo.IsCollection)
                 {
                     handler.CreateFeedFromGraph(resultsGraph, selectVarInfo.EntityType, resultsCount, this);
                 }
                 else
                 {
                     handler.CreateEntryFromGraphWithVariable(resultsGraph, SelectVariables[0], selectVarInfo.EntityType);
                 }
             }
         }
     }
     else
     {
         SparqlResultSet resultSet = endpoint.QueryWithResultSet(GetSparqlRepresentation());
         handler.CreateFeedFromResultSet(resultSet);
     }
 }
Ejemplo n.º 3
0
        public void ProcessRequest(HttpContext context, Uri serviceBaseUri)
        {
            var sparqlGenerator = new SparqlGenerator(_config.Map, _config.DefaultLanguageCode, _config.MaxPageSize);

            var messageWriterSettings = new ODataMessageWriterSettings(_config.BaseODataWriterSettings)
            {
                BaseUri = serviceBaseUri
            };

            if (context.Request.AcceptTypes != null)
            {
                messageWriterSettings.SetContentType(String.Join(",", context.Request.AcceptTypes),
                                                     context.Request.Headers["Accept-Charset"]);
            }

            var          requestMessage   = new HttpDataRequestMessage(context.Request);
            ODataVersion effectiveVersion = GetEffectiveODataVersion(requestMessage);

            messageWriterSettings.Version = effectiveVersion;

            var metadataUri = new Uri(serviceBaseUri, "./$metadata");

            if (serviceBaseUri.Equals(context.Request.Url))
            {
                // We need to respond with the service document
                var responseMessage = new HttpDataResponseMessage(context.Response);
                context.Response.ContentType     = "application/xml";
                context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                var feedGenerator = new ODataFeedGenerator(requestMessage, responseMessage,
                                                           _config.Map, serviceBaseUri.ToString(),
                                                           messageWriterSettings);
                feedGenerator.WriteServiceDocument();
            }
            else if (serviceBaseUri.ToString().TrimEnd('/').Equals(context.Request.Url.ToString().TrimEnd('/')))
            {
                // Trimming of trailing slash to normalize - the serviceBaseUri should always have a trailing slash,
                // but we will redirect requests that do not to the proper service document url
                context.Response.RedirectPermanent(serviceBaseUri.ToString(), true);
            }
            else if (metadataUri.Equals(context.Request.Url))
            {
                context.Response.ContentType = "application/xml";
                context.Response.WriteFile(_config.MetadataPath);
            }
            else
            {
                var parsedQuery = QueryDescriptorQueryNode.ParseUri(
                    context.Request.Url,
                    serviceBaseUri,
                    _config.Model);
                sparqlGenerator.ProcessQuery(parsedQuery);
                var responseMessage = new HttpDataResponseMessage(context.Response);
                var feedGenerator   = new ODataFeedGenerator(
                    requestMessage,
                    responseMessage,
                    _config.Map,
                    serviceBaseUri.ToString(),
                    messageWriterSettings);
                sparqlGenerator.SparqlQueryModel.Execute(_config.SparqlEndpoint, feedGenerator);
                context.Response.ContentType = "application/atom+xml";
            }
        }
Ejemplo n.º 4
0
 public void Execute(SparqlRemoteEndpoint endpoint, ODataFeedGenerator handler)
 {
     if (IsDescribe)
     {
         string query = GetSparqlRepresentation();
         IGraph resultsGraph = endpoint.QueryWithResultGraph(query);
         if (!String.IsNullOrEmpty(DescribeResource))
         {
             // Create ODATA entry payload for single resource
             handler.CreateEntryFromGraph(resultsGraph, DescribeResource, GetEntityType(DescribeResource));
         }
         else
         {
             if (IsDescribe)
             {
                 if (SelectVariables.Count > 1)
                 {
                     throw new Exception(
                         "Cannot create an entity feed from a SPARQL query with multiple DESCRIBE bindings");
                 }
                 string countQuery = GetSparqlCountQuery();
                 var countResults = endpoint.QueryWithResultSet(countQuery);
                 var firstResult = countResults.Results.FirstOrDefault();
                 int resultsCount = 0;
                 if (firstResult != null)
                 {
                     resultsCount = Int32.Parse((firstResult[0] as ILiteralNode).Value);
                 }
                 var selectVarInfo = VariableType[SelectVariables[0]];
                 if (selectVarInfo.IsCollection)
                 {
                     handler.CreateFeedFromGraph(resultsGraph, selectVarInfo.EntityType, resultsCount, this);
                 }
                 else
                 {
                     handler.CreateEntryFromGraphWithVariable(resultsGraph, SelectVariables[0], selectVarInfo.EntityType);
                 }
             }
         }
     }
     else
     {
         SparqlResultSet resultSet = endpoint.QueryWithResultSet(GetSparqlRepresentation());
         handler.CreateFeedFromResultSet(resultSet);
     }
 }