示例#1
0
 /// <summary>
 /// Adds the Standard Custom Headers that dotNetRDF attaches to all responses from it's Handlers
 /// </summary>
 /// <param name="context">HTTP Context</param>
 /// <param name="config">Handler Configuration</param>
 public static void AddStandardHeaders(HttpContext context, BaseHandlerConfiguration config)
 {
     try
     {
         context.Response.Headers.Add("X-dotNetRDF-Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
     }
     catch (PlatformNotSupportedException)
     {
         context.Response.AddHeader("X-dotNetRDF-Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
     }
     if (config.IsCorsEnabled)
     {
         AddCorsHeaders(context);
     }
 }
示例#2
0
        /// <summary>
        /// Handles errors in processing SPARQL Query Requests
        /// </summary>
        /// <param name="context">Context of the HTTP Request</param>
        /// <param name="config">Handler Configuration</param>
        /// <param name="title">Error title</param>
        /// <param name="query">Sparql Query</param>
        /// <param name="ex">Error</param>
        /// <param name="statusCode">HTTP Status Code to return</param>
        public static void HandleQueryErrors(HttpContext context, BaseHandlerConfiguration config, String title, String query, Exception ex, int statusCode)
        {
            //Clear any existing Response and set our HTTP Status Code
            context.Response.Clear();
            context.Response.StatusCode = statusCode;

            if (config != null)
            {
                //If not showing errors then we won't return our custom error description
                if (!config.ShowErrors)
                {
                    return;
                }
            }

            //Set to Plain Text output and report the error
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType     = "text/plain";

            //Error Title
            context.Response.Write(title + "\n");
            context.Response.Write(new String('-', title.Length) + "\n\n");

            //Output Query with Line Numbers
            if (query != null && !query.Equals(String.Empty))
            {
                String[] lines = query.Split('\n');
                for (int l = 0; l < lines.Length; l++)
                {
                    context.Response.Write((l + 1) + ": " + lines[l] + "\n");
                }
                context.Response.Write("\n\n");
            }

            //Error Message
            context.Response.Write(ex.Message + "\n");

#if DEBUG
            //Stack Trace only when Debug build
            context.Response.Write(ex.StackTrace + "\n\n");
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
                context.Response.Write(ex.Message + "\n");
                context.Response.Write(ex.StackTrace + "\n\n");
            }
#endif
        }
        /// <summary>
        /// Applies the Writer Options from a Handler Configuration to a Writer
        /// </summary>
        /// <param name="writer">Writer</param>
        /// <param name="config">Handler Configuration</param>
        public static void ApplyWriterOptions(Object writer, BaseHandlerConfiguration config)
        {
            if (config != null)
            {
                // Apply Stylesheet to HTML writers
                var IHtmlWriterObjRef = writer as IHtmlWriter;
                if (IHtmlWriterObjRef != null)
                {
                    ((IHtmlWriter)writer).Stylesheet = config.Stylesheet;
                }

                // Apply Compression Options
                var ICompressingWriterObjRef = writer as ICompressingWriter;
                if (ICompressingWriterObjRef != null)
                {
                    ((ICompressingWriter)writer).CompressionLevel = config.WriterCompressionLevel;
                }
                var INamespaceWriterObjRef = writer as INamespaceWriter;
                if (INamespaceWriterObjRef != null)
                {
                    ((INamespaceWriter)writer).DefaultNamespaces = config.DefaultNamespaces;
                }
                var IDtdWriterObjRef = writer as IDtdWriter;
                if (IDtdWriterObjRef != null)
                {
                    ((IDtdWriter)writer).UseDtd = config.WriterUseDtds;
                }
                var IAttributeWriterObjRef = writer as IAttributeWriter;
                if (IAttributeWriterObjRef != null)
                {
                    ((IAttributeWriter)writer).UseAttributes = config.WriterUseAttributes;
                }
                var IHighSpeedWriterObjRef = writer as IHighSpeedWriter;
                if (IHighSpeedWriterObjRef != null)
                {
                    ((IHighSpeedWriter)writer).HighSpeedModePermitted = config.WriterHighSpeedMode;
                }
                var IPrettyPrintingWriterObjRef = writer as IHighSpeedWriter;
                if (IPrettyPrintingWriterObjRef != null)
                {
                    ((IPrettyPrintingWriter)writer).PrettyPrintMode = config.WriterPrettyPrinting;
                }
            }
        }
示例#4
0
 /// <summary>
 /// Handles errors in processing SPARQL Update Requests
 /// </summary>
 /// <param name="context">Context of the HTTP Request</param>
 /// <param name="config">Handler Configuration</param>
 /// <param name="title">Error title</param>
 /// <param name="update">SPARQL Update</param>
 /// <param name="ex">Error</param>
 public static void HandleUpdateErrors(HttpContext context, BaseHandlerConfiguration config, String title, String update, Exception ex)
 {
     HandleUpdateErrors(context, config, title, update, ex, (int)HttpStatusCode.InternalServerError);
 }
示例#5
0
        /// <summary>
        /// Helper function which returns the Results (Graph/Triple Store/SPARQL Results) back to the Client in one of their accepted formats
        /// </summary>
        /// <param name="context">Context of the HTTP Request</param>
        /// <param name="result">Results of the Sparql Query</param>
        /// <param name="config">Handler Configuration</param>
        public static void SendToClient(HttpContext context, Object result, BaseHandlerConfiguration config)
        {
            MimeTypeDefinition definition = null;
            String             ctype      = "text/plain";

            String[] acceptTypes = HandlerHelper.GetAcceptTypes(context);

            //Return the Results
            if (result is SparqlResultSet)
            {
                ISparqlResultsWriter sparqlWriter = null;

                //Try and get a MIME Type Definition using the HTTP Requests Accept Header
                if (acceptTypes != null)
                {
                    definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteSparqlResults);
                }
                //Try and get the registered Definition for SPARQL Results XML
                if (definition == null)
                {
                    definition = MimeTypesHelper.GetDefinitions(MimeTypesHelper.SparqlXml[0]).FirstOrDefault();
                }
                //If Definition is still null create a temporary definition
                if (definition == null)
                {
                    definition = new MimeTypeDefinition("SPARQL Results XML", MimeTypesHelper.SparqlXml, Enumerable.Empty <String>());
                    definition.SparqlResultsWriterType = typeof(VDS.RDF.Writing.SparqlXmlWriter);
                }

                //Set up the Writer appropriately
                sparqlWriter = definition.GetSparqlResultsWriter();
                context.Response.ContentType = definition.CanonicalMimeType;
                HandlerHelper.ApplyWriterOptions(sparqlWriter, config);

                //Clear any existing Response
                context.Response.Clear();

                //Send Result Set to Client
                context.Response.ContentEncoding = definition.Encoding;
                sparqlWriter.Save((SparqlResultSet)result, new StreamWriter(context.Response.OutputStream, definition.Encoding));
            }
            else if (result is IGraph)
            {
                IRdfWriter rdfWriter = null;

                //Try and get a MIME Type Definition using the HTTP Requests Accept Header
                if (acceptTypes != null)
                {
                    definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteRdf);
                }
                if (definition == null)
                {
                    //If no appropriate definition then use the GetWriter method instead
                    rdfWriter = MimeTypesHelper.GetWriter(acceptTypes, out ctype);
                }
                else
                {
                    rdfWriter = definition.GetRdfWriter();
                }

                //Setup the writer
                if (definition != null)
                {
                    ctype = definition.CanonicalMimeType;
                }
                context.Response.ContentType = ctype;
                HandlerHelper.ApplyWriterOptions(rdfWriter, config);

                //Clear any existing Response
                context.Response.Clear();

                //Send Graph to Client
                if (definition != null)
                {
                    context.Response.ContentEncoding = definition.Encoding;
                    rdfWriter.Save((IGraph)result, new StreamWriter(context.Response.OutputStream, definition.Encoding));
                }
                else
                {
                    rdfWriter.Save((IGraph)result, new StreamWriter(context.Response.OutputStream));
                }
            }
            else if (result is ITripleStore)
            {
                IStoreWriter storeWriter = null;

                //Try and get a MIME Type Definition using the HTTP Requests Accept Header
                if (acceptTypes != null)
                {
                    definition = MimeTypesHelper.GetDefinitions(acceptTypes).FirstOrDefault(d => d.CanWriteRdfDatasets);
                }
                if (definition == null)
                {
                    //If no appropriate definition then use the GetStoreWriter method instead
                    storeWriter = MimeTypesHelper.GetStoreWriter(acceptTypes, out ctype);
                }
                else
                {
                    storeWriter = definition.GetRdfDatasetWriter();
                }

                //Setup the writer
                if (definition != null)
                {
                    ctype = definition.CanonicalMimeType;
                }
                context.Response.ContentType = ctype;
                HandlerHelper.ApplyWriterOptions(storeWriter, config);

                //Clear any existing Response
                context.Response.Clear();

                //Send Triple Store to Client
                if (definition != null)
                {
                    context.Response.ContentEncoding = definition.Encoding;
                    storeWriter.Save((ITripleStore)result, new VDS.RDF.Storage.Params.StreamParams(context.Response.OutputStream, definition.Encoding));
                }
                else
                {
                    storeWriter.Save((ITripleStore)result, new VDS.RDF.Storage.Params.StreamParams(context.Response.OutputStream));
                }
            }
            else if (result is ISparqlDataset)
            {
                //Wrap in a Triple Store and then call self so the Triple Store writing branch of this if gets called instead
                TripleStore store = new TripleStore(new DatasetGraphCollection((ISparqlDataset)result));
                HandlerHelper.SendToClient(context, store, config);
            }
            else
            {
                throw new RdfOutputException("Unexpected Result Object of Type '" + result.GetType().ToString() + "' returned - unable to write Objects of this Type to the HTTP Response");
            }
        }
 /// <summary>
 /// Handles errors in processing SPARQL Query Requests
 /// </summary>
 /// <param name="context">Context of the HTTP Request</param>
 /// <param name="config">Handler Configuration</param>
 /// <param name="title">Error title</param>
 /// <param name="query">Sparql Query</param>
 /// <param name="ex">Error</param>
 public static void HandleQueryErrors(IHttpContext context, BaseHandlerConfiguration config, String title, String query, Exception ex)
 {
     HandleQueryErrors(context, config, title, query, ex, (int)HttpStatusCode.InternalServerError);
 }