I have a Sparql Endpoint(openrdf sesame) on my local machine. I am able to get the results of the queries(6) in json (using sparql-client node module). Now, I need to convert this json into json-ld so that I can easily display it in the jade file. How can I do this without using rdfstore-js in my node.js application? I did read about jsonld node module. The json data is quite huge and so I cant specify the context for it.
Problem with rdfstore-js:
Since the endpoint is on my local machine, I am facing the cross domain issue. So, I tried to load data from remote sparql endpoints into the store but it does not work.
https://github.com/antoniogarrote/rdfstore-js/issues/20
Alternatively, I am open to suggestions wherein I can display data without json-ld.
Edit: After reading Gregg' suggestion, I tried to use CONSTRUCT query. However, the sesame server
returns response of the query as text/plain. I tried the content negotiation by giving accept header for json-ld. But it would fail - gives me 'No acceptable file format found'.
Thanks!
I am now using http nodejs module as below. I get the response as text/plain. Is there a way to convert plain text into json-ld ?
var queryString = syllPrefix+" CONSTRUCT { ?uri ?p ?o } where { ?uri a syll:Person . ?uri ?p ?o}";
var encodedquerystring = encodeURIComponent(queryString);
var options = {
host: 'localhost',
port: 8080,
path: '/openrdf-sesame/repositories/myrepo?query=' +encodedquerystring,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain',
},
};
var req = http.get(options, function(res) {
// console.log("Got response: " + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log (data);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
req.end();
Sesame does not support the JSON-LD format out of the box. You will need to install a Sesame-compatible JSON-LD writer library (such as jsonld-java) into your existing Sesame installation.
To do this, get the jsonld-java and jsonld-java-sesame jar files plus their dependencies (unfortunately this is a bit of a chore as you will need to include several third party dependencies, such as Apache libraries and Jackson, as well - but you can download everything from Maven Central) and drop them into your Sesame deployment directory inside Tomcat. Typically this directory should be something like [TOMCAT_DIR]/webapps/openrdf-sesame/WEB-INF/lib. Once you've done this, you'll need to restart Tomcat for Sesame to recognize the new writer.
Once this is done, supplying the correct Accept header (media type application/ld+json) should get you JSON-LD results on your graph queries.
Alternatively, you can try using the RDF 1.1 JSON Alternate Serialization format, which is not really a standard like JSON-LD is, but is supported by Sesame. Media type application/rdf+json.
To get JSON-LD from sparql-client (at least the Ruby version), use CONSTRUCT or DESCRIBE rather than SELECT or ASK. If you're using directly, you can then serialize the resulting graph as JSON-LD (on a platform supporting this, such as Python, Ruby or Java), or if it's a remote connection, an HTTP service should allow you to content negotiation for the results using application/ld+json.
Turning SELECT or ASK into JSON-LD is more problematic, and would require a result-set vocabulary. The application/sparql+json is not easily directly interpreted as
Related
Due to the deprecation of request, we're currently rewriting the request-service in our node app with superagent. So far all looks fine, however we're not quite sure how to request binary data/octet-stream and to process the actual response body as a Buffer. According to the docs (on the client side) one should use
superAgentRequest.responseType('blob');
which seems to work fine on NodeJS, but I've also found this github issue where they use
superAgentRequest.buffer(true);
which works just as well. So I'm wondering what the preferred method to request binary data in NodeJS is?
According to superagent's source-code, using the responseType() method internally sets the buffer flag to true, i.e. the same as setting it manually to true.
In case of dealing with binary-data/octet-streams, a binary data parser is used, which is in fact just a simple buffer:
module.exports = (res, fn) => {
const data = []; // Binary data needs binary storage
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
fn(null, Buffer.concat(data));
});
};
In both cases this parser is used, which explains the behaviour. So you can go with either of the mentioned methods to deal with binary data/octet-streams.
As per documentation https://visionmedia.github.io/superagent/
SuperAgent will parse known response-body data for you, currently supporting application/x-www-form-urlencoded, application/json, and multipart/form-data. You can setup automatic parsing for other response-body data as well:
You can set a custom parser (that takes precedence over built-in parsers) with the .buffer(true).parse(fn) method. If response buffering is not enabled (.buffer(false)) then the response event will be emitted without waiting for the body parser to finish, so response.body won't be available.
So to parse other response types, you will need to set .buffer(true).parse(fn). But if you do not want to parse response then no need to set buffer(true).
Hi and thank you in advance for any help,
I am trying to post a JSON-Object to an ASP.Net MVC-Server via JQuery/Ajax. The Controller method is supposed to take the JSON input and use it as a DataProvider for List&Label 22. The Report should then be generated and offered to the user as a PDF file for download.
Since I want the structure of the JSON Object to be generic, I don't want to create a specific model in ASP.Net for this request, but rather pass over the JSON object as a string (I know that I might run into some size restrictions, but I will worry about that later :) ).
Here is my POST request:
<script>
function getReport() {
//dummy data
data = { JsonVariable1: 1, JsonVariable2: "JsonVariable2" };
var dataSource = JSON.stringify(data);
$.ajax({
type: "POST",
dataType: "text",
url: "#Url.Action("/JsonTest")",
data: "aDataSource=" + dataSource,
async: false,
success: function (result) {
alert('Success!');
}
});
}
And this is the Controller method:
[HttpPost]
public ActionResult JsonTest(string aDataSource) {
combit.ListLabel22.ListLabel vLL = new combit.ListLabel22.ListLabel();
JsonDataProvider vJsonProvider = new JsonDataProvider(aDataSource);
vLL.FileRepository = GetCurrentRepository();
vLL.AutoProjectFile = mReportRepositoryId;
vLL.DataSource = vJsonProvider;
vLL.ExportOptions.Add(LlExportOption.ExportTarget, "PDF");
vLL.Print(); //This causes problem on published server
return Json("Success");
}
Locally, i.e. in my Visual Studio (2015) dev environment this works fine. However, when I publish the code to my IIS-Server, the POST-request doesn't terminate. I have found this line
vLL.Print();
to be the problem. If I comment out this line, the request terminates as expected. This line generates the report and exports it to a PDF which will in turn be offered to the user as a download.
I'm using IIS 8.5 and .NET-Framework 4.5 on a Machine running Windows Server 2012 R2. A Printer Driver is installed and the regular List&Label funcationality is working (e.g. starting the web designer, previewing reports via HTML, etc).
Does anyone have any idea what I am missing here? I am not a web developer, and I may also have forgotten to adjust some configurations on my IIS Server.
Thanks!
Just calling the Printmethod is not enough as you need to tell List & Label where to generate the report. I'd rather use the Export method (as it wires up a number of convenient things like muting the file dialogs) in this way:
string reportResult = "Report." + Guid.NewGuid() + ".pdf";
string outputFile = Server.MapPath("~/exports/") + reportResult;
ExportConfiguration exportConfiguration = new ExportConfiguration(LlExportTarget.Pdf, outputFile, mReportRepositoryId);
vLL.Export(exportConfiguration);
You should then find your PDF in the "exports" path of your web application on the server. To troubleshoot issues like this you can use the provided debugging tool Debwin4. It shows you all calls to the API and hints on missing options or input.
I need to post a form that has been set to use windows-1252 charset for urlencoding its data. for simple characters, default encoding (utf8) works but it is the special characters that have to be encoded with the required charset.
the npm "request" package i am using does not allow setting any specific charset and uses utf8 by default underneath. i tried another package "Restler", which allows encoding to be set but it throws exception saying invalid charset when i specify windows-1252 (Node only offers a handful of encoding charsets (Buffer class) and windows-1252 is not one of them).
please let me know whether what i am trying to achieve is even possible in node nor not? for verification purposes, i created a little client in java and used apache's http client library with windows-1252 encoding and my request was successfully accepted by the server. so far, i have not been able to figure this out in node.
Sending HTTP request data in a legacy encoding like Windows-1252 is not straightforward in node, as there is no native support for these encodings.
Support can be added in the form of an iconv library, so it's definitely doable, even if it does not work out of the box.
The following targets restler, because you are using it, but in principle this applies to any client HTTP library.
Notes:
Traditional HTTP POSTs are URL-encoded, we will use qs for this.
Support for encodings other than UTF-8 will be provided by qs-iconv, as documented in qs - Dealing with special character sets.
Restler usually encodes data as UTF-8 if you pass it as a string or plain object, but if you pass a Buffer, Restler will send it as it is.
Setting a proper Content-Type and Content-Length will ensure the data can be interpreted properly at the receiving end. Since we supply our own data here, we need to set those headers manually.
Be aware that any character that is not contained in the target charset (Windows-1252 in this case) will be encoded as ? by iconv (%3F in URL form) and therefore will be lost.
Code:
var rest = require('restler');
var qs = require('qs');
var win1252 = require('qs-iconv/encoder')('win1252');
var requestData = {
key1: "‘value1‘",
key2: "‘value2‘"
};
var requestBody = qs.stringify(requestData, { encoder: win1252 });
// => "key1=%91value1%91&key2=%91value2%91"
var requestBuf = new Buffer(requestBody);
rest.post('your/url', {
data: requestBuf,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=windows-1252',
'Content-Length': requestBuf.length
}
}).on('complete', function(data) {
console.log(data);
});
Here's the deal:
I already have node.js server which communicates with a database;
I need to create a webpage for people to access and check leader-boards
from the database (the server queries the database to show the users
with top 10 scores);
The example below shows a server responding with html;
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<!doctype html>\n<html lang="en">\n' +
'<head>\n<meta charset="utf-8">\n<title>Test web page on node.js</title>\n' +
'<style type="text/css">* {font-family:arial, sans-serif;}</style>\n' +
'</head>\n<body>\n<h1>Euro 2012 teams</h1>\n' +
'<div id="content"><p>The teams in Group D for Euro 2012 are:</p><ul><li>England</li><li>France</li><li>Sweden</li><li>Ukraine</li></ul></div>' +
'\n</body>\n</html>');
res.end();
}).listen(8888, 'address');
console.log('Server running at http://address:8888');
How would I go about to do something similar but instead the list would be the result of a query to the database?
Note:
I already know how to query, I only need to know how to have the results shows in the HTML code above
Thanks in advance
Most modern web applications solve this problem by using a template engine. You asked for something similar, and this is likely the better way to go about it.
A template allows you to have a file or simply a string with variable placeholders and then you can pass a model (in this case, your return value from the database) to the template, render it, and send that to the client.
Depending on the database you're using, you will have to likely start making your queries using Node.js's asynchronous patterns. Generally when you make a query in Node.js you will follow a pattern similar to
...
db.create({"name" : "rawr"},
function(err, data) {
if (err) {
// handle error
}
// build your template
// res.send the compiled HTML
});
...
Of course this all depends on the database that you're using, but generally it will look something like that. If you're comfortable with HTML I recommend looking into Handlebars.js but also loking at the link provided above for a few more and find one you like. Without a few more details such as constraints or database being used, this is about as much as I can say.
I am trying print the pdf file in my local using printer. This is a code, tried to print.
fs.readFile('documents/AccountStatement.pdf', function(err, data) {
if (err)
throw err;
var printer = ipp.Printer("http://hostname:631/ipp/printer");
var msg = {
"operation-attributes-tag": {
"requesting-user-name": "KUMA1936",
"job-name": "My Test Job",
"document-format": "application/pdf"
},
data: data
};
printer.execute("Print-Job", msg, function(err, res){
console.log(res);
console.log(err);
});
});
In the above code what does printer.execute() method and "Print-Job" parameter. And what does 631 here.When i print the res,its shows
{ version: '1.1',
statusCode: 'server-error-operation-not-supported',
id: 442076,
'operation-attributes-tag':
{ 'attributes-charset': 'utf-8',
'attributes-natural-language': 'en-us' } }
err is null.
You can check the API docs. The first parameter (string) is an operation defined by IPP.
Description about Print-Job operation is given here
3.2.1 Print-Job Operation
This REQUIRED operation allows a client to submit a print job with
only one document and supply the document data (rather than just a
reference to the data). See Section 15 for the suggested steps for
processing create operations and their Operation and Job Template
attributes.
You can see other IPP supported operations here. 631 is the accepted port used for IPP, which uses TCP.
You can check more about the error here, which shows :
13.1.5.2 server-error-operation-not-supported (0x0501)
The IPP object does not support the functionality required to fulfill
the request. This is the appropriate response when the IPP object
does not recognize an operation or is not capable of supporting it.
See sections 3.1.6.1 and 3.1.7.
This means that there is no error in your code. Most likely your printer is not configured or does not support IPP. Last but not the least, the IPP.Printer has to be given the printer IP. So check the IP you are giving is valid (your code shows you gave hostname). From the project page it is given :
To find out if your printer supports IPP:
- Google your printer's specs
- Try: telnet YOUR_PRINTER 631. If it connects, that's a good sign.
- Use the '/examples/findPrinters.js' script.
In my case i just share the printer : (like share with another)
So URL gonna be (you will be found by findPrinters.js)
http://My-Computer-Name.local.:631/printers/my_printer_name
This should be help if your printer donesnt connect (like USB) with your computer. But connect via lan.
Look's like you get an IPP-Version-1.1-Response while sending IPP-2.0.
Try downgrading to 1.1 by using the version-option