Neo4J transactional REST api string escape doesn't work - java

While sending off Cypher queries to Neo4J's transactional Cypher API, I am running into the following error:
Neo.ClientError.Request.InvalidFormat Unable to deserialize request:
Unrecognized character escape ''' (code 39)
My Cypher query looks like this
MATCH (n:Test {id:'test'}) SET n.`label` = 'John Doe\'s house';
While this query works just fine when executed in Neo4J's browser interface it fails when using the REST API. Is this a bug or am I doing something wrong? In case this is not a bug, how do I have to escape ' to get it working in both?
Edit:
I found this answer and tested the triple single and triple double quotes but they just caused another Neo.ClientError.Request.InvalidFormat error to be thrown.
Note: I am using Neo4J 2.2.2
Note 2: Just in case it's important, below is the JSON body I am sending to the endpoint.
{"statements":[
{"statement": "MATCH (n:Test {id:'test'}) SET n.`label` = 'John Doe\'s house';"}
]}

You'll have to escape the \ too:
{"statements":[
{"statement": "MATCH (n:Test {id:'test'}) SET n.`label` = 'John Doe\\'s house';"}
]}
But if you use parameters (recommended), you can do
{"statements":[
{"statement": "MATCH (n:Test {id:'test'}) SET n.`label` = {lbl}",
"parameters" : {"lbl" : "Jane Doe's house"}
}
]}

Related

Invalid logback pattern

I was using this working pattern (logback.groovy):
{'((?:password(=|:|>))|(?:secret(=|:))|(?:salt(=|:)))','\$1*******\$3'}
to mask sensitive data. One day I needed to surround it with double quotes, like
was: password=smth
became: "password"="smth"
So I turned regexp into this (just added \" before and after keywords, and also I've tried \\"):
{'(\"?(?:password\"?(=|:|>))|(?:secret\"?(=|:))|(?:salt\"?(=|:)))','\$1*******\$3'}
But I get this error on app startup:
Failed to parse pattern
Unexpected character ('?' (code 63)): was expecting comma to separate Object entries
Can someone please explain to me what am I doing wrong?
If someone wondering here is correct version:
{'(\\\"?(?:password\\\"?(=|:|>))|(?:secret\\\"?(=|:))|(?:salt\\\"?(=|:)))','\$1*******\$3'}

Jmeter POST API issue

There is a JAVA REST API (Put request) that I want to hit with values from a CSV file.
The CSV file is:
The JMETER CSV configuration is:
This is how I have set up the JMETER Configuration to hit the API:
The deserialisation on the Java side is not happening correctly. From POSTMAN, the following works:
{
"productId": "ABC",
"score": 4.42489
}
Why is the Jmeter POST configuration not working correctly?
Error: Received Unknown exception com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of double from String value '$score': not a valid Double value
Update: On doing this in Jmeter configuration:
{
"productId" : "${product}",
"score": "${score}"
}
I got the following error:
Received Unknown exception com.fasterxml.jackson.core.JsonParseException: Unexpected character ('T' (code 84)): was expecting comma to separate OBJECT entries at [Source: java.io.PushbackInputStream#4063ce7d; line: 2, column: 18] Similarly for M, M and R. So 4 errors in total.
Update 2:[Solved]The following CSV Data Set Configuration worked without any change in the actual CSV file!!
A Big Thank you to #user7294900 for the help!!
i do wonder if you might also have a problem with product_id being the parameter name versus productID that works directly in postman.
You forgot curly braces (this is not velocity/postman)
You need to send HTTP Request Body Data:
{
"productId": "${product}",
"score": "${score}"
}
Check the maunual for more details:
Variables are referenced as follows:
${VARIABLE}
Also your quotes in CSV file is redundant, either change Allow quoted data to true or better yet remove all quotes " from CSV file.
You don't need quotation marks around "${product}" and ${score} as:
Your CSV file already has quotations around ProductId I don't think you need duplicate ones
Your application expects a Double and you are basically sending a String
So change your payload to look like:
{
"productId" : ${product},
"score": ${score}
}
More information:
JSON Data Types
REST API Testing - How to Do it Right

SPARQL Query to escape emojis?

I'm using SPARQL query to extract instances which is valid.
But using this query, I can also get instances which name contains emoticon (e.g., http://ko.dbpedia.org/resource/😼), and it gives me an error while iterating over the query resultsets. How can I escape from emojis?
SELECT DISTINCT ?s WHERE {
?s ?p ?o
FILTER regex(str(?s), "^http://ko.dbpedia.org/resource")
}
ORDER BY DESC(?s)
limit 100
Error message is as follows
Exception in thread "main" com.hp.hpl.jena.shared.JenaException: Convert results are FAILED.:virtuoso.jdbc4.VirtuosoException: Virtuoso Communications Link Failure (timeout) : malformed input around byte 34
at virtuoso.jena.driver.VirtuosoQueryExecution$VResultSet.moveForward(VirtuosoQueryExecution.java:498)
at virtuoso.jena.driver.VirtuosoQueryExecution$VResultSet.hasNext(VirtuosoQueryExecution.java:441)
at kr.ac.kaist.dm.BBox.TypeInference.LoadTriple.processTriples(LoadTriple.java:92)
at kr.ac.kaist.dm.BBox.TypeInference.TypeInferenceMain.main(TypeInferenceMain.java:110)
Sample Code is as follows.
VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparql, set);
ResultSet results = vqe.execSelect();
int i = 0;
while(results.hasNext()){ // <----- LoadTriple.java:92 here.
I just posted the extended version of this question on virtuoso-opensource issue #543.
I just want to escape from emoji rather than including all possible characters like "FILTER regex(?s, \"[a-zA-Z가-힣~!##$%^&*()-_=+|'<>]+\") }"
ENCODE_FOR_URI() should work:
FILTER regex(ENCODE_FOR_URI(str(?s)), "^http://ko.dbpedia.org/resource")
...though you would also need to URI encode the regex match string:
http%3A%2F%2Fko.dbpedia.org%2Fresource

How to express advanced expressions between query parameters in a REST API?

The problem (or missing feature) is the lack of expression possibility between different query parameters. As I see it you can only specify and between parameters, but how do you solve it if you want to have not equal, or or xor?
I would like to be able to express things like:
All users with age 20 or the name Bosse
/users?age=22|name=Bosse
All users except David and Lennart
/users?name!=David&name!=Lennart
My first idea is to use a query parameter called _filter and take a String with my expression like this:
All users with with age 22 or a name that is not Bosse
/users?_filter=age eq 22 or name neq Bosse
What is the best solution for this problem?
I am writing my API with Java and Jersey, so if there is any special solution for Jersey, let me know.
I can see two solutions to achieve that:
Using a special query parameter containing the expression when executing a GET method. It's the way OData does with its $filter parameter (see this link: https://msdn.microsoft.com/fr-fr/library/gg309461.aspx#BKMK_filter). Here is a sample:
/AccountSet?$filter=AccountCategoryCode/Value eq 2 or AccountRatingCode/Value eq 1
Parse.com also uses such approach with its where parameter but the query is described using a JSON structure (see this link: https://parse.com/docs/rest/guide#queries). Here is a sample:
curl -X GET \
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-G \
--data-urlencode 'where={"score":{"$gte":1000,"$lte":3000}}' \
https://api.parse.com/1/classes/GameScore
If it's something too difficult to describe, you could also use a POST method and specify the query in the request payload. ElasticSearch uses such approach for its query support (see this link: https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html). Here is a sample:
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "some query string here"
}
},
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}
}
'
Hope it helps you,
Thierry
OK so here it is
You could add + or - to include or exclude , and an inclusive filter keyword for AND and OR
For excluding
GET /users?name=-David,-Lennart
For including
GET /users?name=+Bossee
For OR
GET /users?name=+Bossee&age=22&inclusive=false
For AND
GET /users?name=+Bossee&age=22&inclusive=true
In this way the APIs are very intuitive, very readable also does the work you want it to do.
EDIT - very very difficult question , however I would do it this way
GET /users?name=+Bossee&age=22&place=NewYork&inclusive=false,true
Which means the first relation is not inclusive - or in other words it is OR
second relation is inclusive - or in other words it is AND
The solution is with the consideration that evaluation is from left to right.
Hey it seems impossible if you go for queryparams...
If it is the case to have advanced expressions go for PathParams so you will be having regular expressions to filter.
But to allow only a particular name="Bosse" you need to write a stringent regex.
Instead of taking a REST end point only for condition sake, allow any name value and then you need to write the logic to check manually with in the program.

OpenRdf Exception when parsing data from DBPedia

I use OpenRdf with Sparql to gather data from DBPedia but I encounter some errors on the following query ran against the DBPedia Sparql endpoint:
CONSTRUCT{
?battle ?relation ?data .
}
WHERE{
?battle rdf:type yago:Battle100953559 ;
?relation ?data .
FILTER(?relation != owl:sameAs)
}
LIMIT 1
OFFSET 18177
I modified the LIMIT and OFFSET to point out the specific result that provokes the problem.
The response is this one :
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
#prefix ns1: <http://en.wikipedia.org/wiki/> .
<http://dbpedia.org/resource/Mongol%E2%80%93Jin_Dynasty_War> foaf:isPrimaryTopicOf ns1:Mongol–Jin_Dynasty_War .
The problem is that the ns1:Mongol–Jin_Dynasty_War entity contains a minus sign, therefore I get the following exception when running this query inside a Java application using OpenRdf :
org.openrdf.query.QueryEvaluationException: org.openrdf.rio.RDFParseException: Expected '.', found '–' [line 3]
Is there any way to circumvent this problem ?
Thanks !
To help other users who might encounter the same problem, I'll post here the way to set the preferred output format for Graph Queries using OpenRDF v2.7.x.
You need to creat a subclass of SPARQLRepository to access the HTTPClient (for some reason, the field is protected.
public class NtripleSPARQLRepository extends SPARQLRepository {
public NtripleSPARQLRepository(String endpointUrl) {
super(endpointUrl);
this.getHTTPClient().setPreferredRDFFormat(RDFFormat.NTRIPLES);
}
}
The you just need to create a new Instance of this class :
NtripleSPARQLRepository repository = new NtripleSPARQLRepository(service);
RepositoryConnection connection = new SPARQLConnection(repository);
Query query = connection.prepareQuery(QueryLanguage.SPARQL, "YOUR_QUERY");
If you are querying a Virtuoso server, then you are probably encountering sloppiness in the implementation of Virtuoso. I have seen this when getting XML results (vertical tab in output but only XML 1.0) and most recently in JSON results (\U escape for characters not in Basic Multilingual Plane).

Categories