I am pretty new to java and REST, in an application I am currently working on resources/tasks such as bug tracking are to be consumed from launchpad.net in a restful manner. There is an official python library (launchpadlib) that supports such interactions, but I want to do this using Java. I have looked around for samples to get me running to no avail. Does anyone have experience this in Java? Any insights is highly appreciated.
public class LaunchPad_Gatherer {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
ClientConfig config = new ClientConfig();
//WebTarget target = client.target("http://api.launchpad.net/1.0");
//WebTarget target = client.target("https://api.launchpad.net/1.0/bugs/cve/2015-5223/bugs");
WebTarget target = client.target("https://api.launchpad.net/1.0/bugs/cve");
System.out.println(target.request(MediaType.APPLICATION_JSON).get(String.class));
}
}
Related
Recently I had some task to integrate open source url short service.
I had choose firebase dynamic links by google , i did not managed to find some decent how to tutorial or quick start.
I have managed to execute the service and get the answer.
I would like to get some improvement suggestions for client initialization.
gradle dependency used
compile 'com.google.apis:google-api-services-firebasedynamiclinks:v1-rev860-1.25.0'
code
FirebaseDynamicLinks.Builder builder = new FirebaseDynamicLinks.Builder(GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(), null);
FirebaseDynamicLinksRequestInitializer firebaseDynamicLinksRequestInitializer = new FirebaseDynamicLinksRequestInitializer(
"***WEB_API_KEY***");
builder.setApplicationName("***Fire_Bass_Dummy_App***");
builder.setFirebaseDynamicLinksRequestInitializer(firebaseDynamicLinksRequestInitializer);
FirebaseDynamicLinks firebasedynamiclinks = builder.build();
CreateShortDynamicLinkRequest createShortLinkRequest = new CreateShortDynamicLinkRequest();
createShortLinkRequest.setLongDynamicLink("***Dynamic_Links_domain***?link=***long_link***");
Suffix suffix = new Suffix();
suffix.setOption("SHORT");
createShortLinkRequest.setSuffix(suffix);
FirebaseDynamicLinks.ShortLinks.Create request = firebasedynamiclinks.shortLinks().create(createShortLinkRequest);
CreateShortDynamicLinkResponse createShortDynamicLinkResponse = request.execute();
I created an AWS Lambda package (Java) with a function that reads some files from Amazon S3 and pushes the data to AWS ElasticSearch Service. Since I'm using AWS Elastic Search, I can't use the Transport client, in which case I'm working with the Jest Client to push via REST. The issue is with the Jest client.
Here's my Jest client instance:
public JestClient getClient() throws InterruptedException{
final Supplier<LocalDateTime> clock = () -> LocalDateTime.now(ZoneOffset.UTC);
DefaultAWSCredentialsProviderChain awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
final AWSSigner awsSigner = new AWSSigner(awsCredentialsProvider, REGION, SERVICE, clock);
JestClientFactory factory = new JestClientFactory() {
#Override
protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) {
builder.addInterceptorLast(new AWSSigningRequestInterceptor(awsSigner));
return builder;
}
#Override
protected HttpAsyncClientBuilder configureHttpClient(HttpAsyncClientBuilder builder) {
builder.addInterceptorLast(new AWSSigningRequestInterceptor(awsSigner));
return builder;
}
};
factory.setHttpClientConfig(
new HttpClientConfig.Builder(URL)
.discoveryEnabled(true)
.multiThreaded(true).build());
JestClient jestClient = factory.getObject();
return jestClient;
}
Since the AWS Elasticsearch domain is protected by an IAM access policy, I sign the requests for them to be authorized by AWS (example here). I use POJOs to index documents.
The problem I face is that I am not able to execute more than one action with the jest client instance. For example, if I created the index first :
client.execute(new CreateIndex.Builder(indexName).build());
and later on, I wanted to, for example do some bulk indexing:
for (Object object : listOfObjects) {
bulkIndexBuilder.addAction(new Index.Builder(object ).
index(INDEX_NAME).type(DOC_TYPE).build());
}
client.execute(bulkIndexBuilder.build());
only the first action will be executed and the second will fail. Why is that? Is it possible to execute more than one action?
Morover, using the provided code, I'm not able to execute more than 20 Bulk operations when I want to index the document. Basically, around 20 is fine, but anything more than that, the client.execute(bulkIndexBuilder.build()); just does not execute and the client shuts down.
Any help or suggestion would be appriciated.
UPDATE:
It seems that AWS ElasticSearch does not allow connecting to individual nodes. Simply turning off node discovery in the Jest client .discoveryEnabled(false) solved all the problems. This answer helped.
I'm testing PHP/Java Bridge connection. And I have a simple example yet.
The php file is:
require_once("http://localhost:8087/JavaBridge/java/Java.inc");
$world = new java("HelloWorld");
echo $world->hello(array("from PHP"));
And the java file:
import javax.swing.JOptionPane;
public class HelloWorld {
public static final String JAVABRIDGE_PORT="8087";
static final php.java.bridge.JavaBridgeRunner runner =
php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT);
public static void main(String args[]) throws Exception {
runner.waitFor();
System.exit(0);
}
public void hello(String args[]) throws Exception {
JOptionPane.showMessageDialog(null, "hello " + args[0]);
}
}
Everything works fine on one pc. But I have to implement connection from PHP server to java desktop application which is on the another server not on localhost, so "localhost:8087/JavaBridge/java/Java.inc" won't work. In future this java app will print on printer some data from php website.
So I need to call java function remotely. It should be a desktop App because I will write usb connection in future. Please help me, thanks.
You can't use require_once to include files from another host.
If this option is available, a lot of websites will be at risk.
Why don't you use it from another way, Here are some points that may help:
make java calls PHP.
write your result to some file in the destination server.
Make server reads that file.
If you don't like that, please read about web service, it may have what you need.
So I have been working on a web service which communicates between the client and server. It fully works on my computer and am able to send requests from client to server while not in the same package.
I am hosting my service like:
public class WebServiceServer {
public static void main(String[] args) {
String bindingURI = "http://localhost:4040/absolute/uploadService";
FileReceive service = new FileReceiveImpl();
Endpoint.publish(bindingURI, service);
System.out.println("Server started at: " + bindingURI);
}
}
And my client connects to my service like:
public static void main(String args[]) throws Exception {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(FileReceive.class);
factory.setAddress
("http://localhost:4040/absolute/uploadService");
FileReceive client =(FileReceive) factory.create();
From this point I would like to make the web-service public(able to connect from outside of the local-host). I installed ruby and ruby gems only to find out the local-tunnel is no longer available. I have then moved to using ngrok which forwarded a URL for me, but I cannot get any post requests to go through to it(as if it was my web-service) only get methods as I type the URL into my web-browser. Is anyone aware of any easier to use hosting services so that someone (outside of local-host) can post a file to my web-service? Thanks.
I'm working on Android game which is turn based, and I've chosen Nodejs for the server side. I've been exploring for about two weeks how to communicate from the Android client side to the Nodejs server. Is there any way to communicate between the two.
Kindly help me if any one have any experience with such a project.
There are lots of options for something like this depending on what your game requires for communicating between client and server. For instance looking up "TCP clients for android" here shows up answers like this. If fast updates are important between server and client then UDP is one option, if your game can cope with the loss of some packets in the middle.
Besides TCP/UDP you also have things like WebSockets for Android.
Combining Android & nodejs is no problem. First you have to define a middelware. You can use REST-Webservices or any other technology for the communication between the node server and the android client. There are many standard APIs and protocols. I would use Websocket for the communication. You can find Android / Node.js APIs with WebSocket support here:
http://cjihrig.com/blog/creating-your-own-node-js-websocket-echo-server/
https://github.com/Worlize/WebSocket-Node
http://jwebsocket.org/mobile/android/android_part1.htm
http://code.google.com/p/weberknecht/
You can use Volley in Android to make a json POST or GET request.
And for the NODE JS you can use node's built-in http module to create a simple HTTP server and then receive data from the req object.
const http=require('http');
const stringDecoder=require('string_decoder').StringDecoder;
var httpServer=http.createServer(function(req,res){
unifinedServer(req,res);
});
//Just another method.
var unifinedServer=function(req,res){
var decoder=new stringDecoder('utf-8');
var buffer='';
//reading the post data.
req.on('data',function(data){
buffer+=decoder.write(data);
});
//Reading of data is completed.
req.on('end',function(){
buffer+=decoder.end();
// Do what ever you want to do with the POST data.
});
}
//The Server is listening on a specific port.
httpServer.listen(7000,function(){
console.log("Server is now listening on Port..."+7000);
});
For the Android Code you can do this with volley:
String url = "http://example.com";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, url, postJsonObject, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
mTextView.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);