How do I make a gnip query using a REST request (Java) - java

I have a domain name enrichment task.
I want to make a make a GNIP query using REST. Here's what I want to do:
Make a query to gnip containing a shortened url eg/"http ow.ly4eyW50fy4eP
Then use a gnip rule (maybe 'url_contains') to get the expanded url.
I am new to gnip, but I understand I can use the search api.
I'm having trouble finding a Java simple tutorial/'hello world' example for making gnip queries using rest :(
I found the rule 'url_contains' here:: http://support.gnip.com/apis/search_api/rules.html
And example REST requests here:
https://github.com/gnip/support/tree/master/Search%20API/Java
But nothing specifically on how to apply a gnip rule to a rest request.
I'm guessing the query might look like this (curl).
curl -X POST -u <me>
"https://search.gnip.com/accounts/me/search/prod.json -d '{
"rules": [
{
"value": "url_contains:bla"
}
]
}'
So my first question is, does this request look ok?
And if not, could anybody help me with an example? Or perhaps signpost me to an example online?

Ah! The answer is in the PostRequest.java class here ::
https://github.com/gnip/support/tree/master/Search%20API/Java
and no rule is required, the shortened url simply needs to be present in the payload as shown here::
http://support.gnip.com/enrichments/expanded_urls.html

Related

How to fix "error code=mustpostparams" on mediawiki-api in Java

I'm working on a project which use the mediawiki API and I have this error :
error code=mustpostparams
info:'The following parameters were found in the query string, but must be in the POST body: lgpassword, lgtoken.'
This is my code :
Mediawiki wiki=new Mediawiki(site);
Login login=wiki.login(login,password); //error
wiki.edit(title, content , summary);
wiki.logout();
To login to my mediawiki I use login action but I think I can use query action.
In the documentation : https://www.mediawiki.org/wiki/API:Tokens I found :
PARAMS = {
'action':"query",
'meta':"tokens",
'type':"login",
'format':"json"
}
I think it's exactly what I need but it's in python and I don't know how to do this in Java. I know that there is a function getQueryResult() but I don't know how to use it.
How to do the query action in Java ?
Thanks
Like this post, both are related :
I just add
Mediawiki wiki=new Mediawiki(site);
wiki.setVersion("Mediawiki 1.28"); //New
Login login=wiki.login(login,password);
And don't forget to use a bot ! https://www.mediawiki.org/wiki/API:Login

Scala with Spray Routing - accessing GET parameters?

I'm currently working on an application built in Scala with Spray routing.
So for dealing with a JSON document sent over POST, it's pretty easy to access the variables within the body, as follows;
respondWithMediaType(`application/json`) {
entity(as[String]) { body =>
val msg = (parse(body) \ "msg").extract[String]
val url = (parse(body) \ "url").extractOpt[String]
However, I'm now trying to write an additional query with GET, and am having some issues accessing the parameters sent through with the query.
So, I'm opening with;
get {
respondWithMediaType(`application/json`) {
parameterSeq { params =>
var paramsList = params.toList
So, this works well enough in that I can access the GET params in a sequential order (just by accessing the index) - the problem is, unfortunately I don't think we can expect GET params to always be sent in the correct order.
The list itself prints out in the following format;
List((msg,this is a link to google), (url,http://google.com), (userid,13))
Is there any simple way to access these params? For example, something along the lines of;
var message = paramsList['msg']
println(message) //returns "this is a link to google"
Or am I going about this completely wrong?
Apologies if this is a stupid question - I've only switched over to Scala very recently, and am still getting both acquainted with that, and re-acquainted with Java.
What I usually do is use the parameters directive to parse the data out to a case class which contains all the relevant data:
case class MyParams(msg: String, url: String, userId: Int)
parameters(
"msg".as[String],
"url".as[String],
"userId".as[Int]
).as[MyParams] {
myParams =>
// Here you have the case class containing all the data, already parsed.
}
To build your routes you could use the parameters directives. I'm not sure if this is what you're looking for, anyway you could use them as:
get {
parameters('msg) { (msg) =>
complete(s"The message is '$msg'")
}
}
Spray directives can be easily composed so you can use combine them in any way you want.
I hope that helps you.

How to call solr analysis api in java?

Is there a way to call solrs analysis api in java using solr-core and get the analyzed tokens.
Analysis api takes fieldName or fieldType and values and give the analyzed tokens.
Is there a way to get those tokens from java?
I found the following link: FieldAnalysisRequestHandler, But I could not get any examples to use it.
In the Admin UI (for which the FieldAnalysisRequestHandler is meant) you can call it by selecting a core and then go to the "Analysis" entry.
See https://cwiki.apache.org/confluence/x/UYDxAQ or https://cwiki.apache.org/confluence/x/FoDxAQ for that.
From a client (which I guess you mean, as you tagged this question with solrj) you need to call the correct URL.
Typically the FieldAnalysisRequestHandler is bound to /analysis/field, see your solrconfig.xml.
From Solrj it should work like this:
SolrQuery query = new SolrQuery();
solrQuery.setRequestHandler("/analysis/field");
solrQuery.set("analysis.fieldtype", "mytype");
solrQuery.set("analysis.fieldvalue", "myval");
QueryResponse solrResponse = solrServer.query(solrQuery);
But it doesn't seem like there's a great support for this in Solrj, probably because it's meant to be called from the Solr Admin UI as mentioned.

How to code Java APIs with Playframework

I want to write Java API code using playframework! (I'm using the Eclipse IDE.)
What would a sample API look like for insertion and selection?
How do I assign URLs in a routes file?
This answer applies to version 3 of the Play framework.
Here is a sample route file configuration to show a page. In this example, you can visit http://yoursite.com/1 , which will call controllers.Application.show("1").
# Extract the page parameter from the path.
# i.e. http://myserver.com/index
GET /:page controllers.Application.show(page)
SELECTION
Here is the corresponding show method definition in the controllers.Application controller:
public Result show(String page) {
String content = Page.getContentOf(page);
response().setContentType("text/html");
return ok(content);
}
INSERTION
For insertion, check out the "Form submission and validation" and "Working with JSON" sections of the documentation. There are examples there; sorry for not adding them here, but these are more complicated for this short format.
Source, and more information: https://www.playframework.com/documentation/3.0.x/JavaRouting

Jersey 2 getLinks() returns empty set despite links being in the resource

EDIT shorter version
Ok so looking at my own post I realize this is perhaps not the shortest thing to read, so I thought I would write a more concise version with some more hands-on details.
I am using Jersey 2 client framework to code against the Documentum REST API, hosted for the time being on my local machine. If I execute the following code:
WebTarget target = client.target("http://localhost:8080/dctm-rest/repositories/repo");
Response response = t.request().get();
and the XML which is returned looks like this
<?xml version="1.0" encoding="UTF-8"?>
<repository xmlns="http://identifiers.emc.com/vocab/documentum" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<... metadata entries></>
<links>
<link rel="self" href="http://localhost:8080/dctm-rest/repositories/repo"/>
<link rel="http://identifiers.emc.com/linkrel/users" href="http://localhost:8080/dctm-rest/repositories/darwin_test/users"/>
<... more links />
</links>
</repository>
... then what do I have to do in order to get the links in the response in the following manner:
Link self = response.getLink("self");
The above command just ends up with self being null.
END EDIT
I've tried my best finding a solution to this but with no luck so far.
Basically I am consuming a REST webservice which by default returns JSON formatted resources, which is pretty nice (alot nicer than xml anyway). I am using Jersey 2.4.1 to consume the service, and while I am pretty new to coding against REST-services, I haven't had any real issues besides this (and this is really more Jersey-related than REST as far as I can tell).
Basically, I am having no luck whatsoever in utilizing the Jersey 2 API for retrieving and traversing links, which are returned by the service. Jersey 2 has the following (short) example in its documentation:
// server side - adding links to a response:
Response r = Response.ok().
link("http://oracle.com", "parent").
link(new URI("http://jersey.java.net"), "framework").
build();
Which imho is the equivalence of the following returned data:
<link rel="parent" href="http://oracle.com"/>
<link rel="framework" href="http://jersey.java.net"/>
Now to get the links for these relations, the clientside code in the documentation looks like this:
// client-side processing:
final Response response = target.request().get();
URI u = response.getLink("parent").getUri();
URI u = response.getLink("framework").getUri();
I think this is pretty straightforward. Now my service, in the URL that my WebTarget object is pointing at, has roughly 10 links in the following JSON format:
{
... <metadata before the links> ...
"links" :
[
{
"rel" : "some relation",
"href" : "some http url"
}
... about 10 more entries like this
]
}
The problem is that if I do the client-side equivalence of what was in the documentation:
WebTarget t = client.target("url");
t.request().get().getLinks();
..then I get back nothing but an empty Set<Link>.
I suspect it might have to do with enabling JSON-binding in the Client somehow, but I have tried adding the dependency for MOXy, which appearently should be self-enabled if it's in the classpath, and that didn't help.
I'm sure I am missing something extremely basic but I've been searching all day, testing different media-type requests and anything I can think of but with no luck. And while my application is working fine without this sort of link-processing, I still want to get this working.
Thanks for any help on this.
// Andreas
EDIT Response to user1888440
I agree, the links are indeed in the entity returned when the resource is queried. This also aligns with the way I've been processing resources, where I use:
JsonObject o = Json.createReader(target.request().get().readEntity(InputStream.class)).readObject();
.. and then query the object for links in the following manner: I need to first get the "links"-JsonArray from the JsonObject o, then iterate over all links found, compare the "rel"-description with what I'm after and return the correct "href"-value.
However I think there should be a smoother way to get to the links in an entity, especially if we compare this procedure with the following:
response = target.request().get();
URI u = response.getLink("parent").getUri();
URI u = response.getLink("framework").getUri();
And I get what you're saying, that the above getLink("rel") / getLinks() methods only apply to links in the header of the response. But surely there must be some equivalent way of processing links inside an entity, which isn't as verbose as what I've described above?
PS: The example you suggested, to read an resource entity into a Map:
Map entity = t.get( Map.class );
.. doesn't work and results in a null value for entity.
The vendor is sending you the links in the entity that is embedded in the response not links in the response itself. Try this:
Map entity = t.get( Map.class );
Map<String,String> links = (Map<String,String>)entity.get( "links );
URI uriSelf = URI.create( links.get( "self" ) );
You see the links are in the entity not in the header of the request. The response links are for header links. You can also make a more specific object, this is just using maps to get at the property data.

Categories