Uploading files to Amazon S3 using SigV4 API - java

I am looking to upload images to amazon S3 using the rest api that they provided. I got to know how to calculate the signing key for SigV4 from this document. This documentation tells you how the request should be signed. But I find it highly confusing as to what should be signed and where should the cannonical request be placed? Should it be placed in a separate header in the request?
Is there a working example/sample to use SigV4 rest api using java?

If you have a very specific reason for not using the provided SDK, the quickest path to getting this working it to look at how the requests are performed in a library where this is already working. You can look at the Java SDK itself to figure this out, but that's a bit dense.
Here is my favorite, although I think it's on sig v3:
http://geek.co.il/2014/05/26/script-day-upload-files-to-amazon-s3-using-bash
You can find out similar examples for v4:
http://geek.co.il/2014/11/19/script-day-amazon-aws-signature-version-4#footnote_0_33255
You can see how everything is compute and what is to be passed in the headers in very few lines of code.
EDIT
Look at http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-examples-using-sdks.html#sig-v4-examples-using-sdk-java for exactly what you are looking for. It has the bare minimum to get this going in java.

aws-v4-signer-java is a lightweight, zero-dependency library that makes it easy to generate AWS V4 signatures.
String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
HttpRequest request = new HttpRequest("GET", new URI("https://examplebucket.s3.amazonaws.com?max-keys=2&prefix=J"));
String signature = Signer.builder()
.awsCredentials(new AwsCredentials(ACCESS_KEY, SECRET_KEY))
.header("Host", "examplebucket.s3.amazonaws.com")
.header("x-amz-date", "20130524T000000Z")
.header("x-amz-content-sha256", contentSha256)
.buildS3(request, contentSha256)
.getSignature();
If you want to implement it yourself take a look at https://github.com/lucasweb78/aws-v4-signer-java/blob/master/src/main/java/uk/co/lucasweb/aws/v4/signer/Signer.java for an example of how to do it.
Disclaimer: I'm the libraries author.

Related

AWS ElasticSearchService - Java SDK example?

I am using the ElasticsearchService from Amazon. I am a little overwhelmed by their documentation. I find it vast but ever so difficult to navigate. Anyway, I am looking for an example of using the ESService using their AWS Java SDK. Do you have a link - or some code to insert a document?
I am actually using it from Scala, and what I've got so far is:
val awsEsClient: AWSElasticsearchClient = new AWSElasticsearchClient()
awsEsClient.setRegion(Region.getRegion(Regions.EU_CENTRAL_1))
awsEsClient.setEndpoint("es.eu-central-1.amazon.aws.com")
val createD = new CreateElasticsearchDomainRequest()
Where should I specify my own instance ARN? The uri that looks like
arn:aws:es:eu-central-1:xxxxxxxxxxx:domain/yyyyyyyy
Also, when using their SDK, I guess I don't need to specify anywhere the endpoint they provide? The one that goes by
search-yyyyyy-xxxxxxxxxx.eu-central-1.es.amazonaws.com
Or maybe this is what I should specify instead of the
awsEsClient.setEndpoint("es.eu-central-1.amazon.aws.com")
Thank you for your help and sorry if all these questions sound obvious.
So, I got the whole thing wrong from the beginning. The SDK is useful only in order to manage the service, like spin up new nodes and similar -- not to access it. For that, the only solution that Amazon offers is an HTTP endpoint, using the common REST api offered by Elastic Search.
The problem that came next was to authenticate the requests. I have compiled a scala library to do that for every request, which is available here: https://github.com/ticofab/aws-request-signer.

Podio API, attaching files to items

I have a problem with attaching a file to a specific item using Java API. I know it should be possible as this functionality described here in the Podio documentation https://developers.podio.com/doc/files/attach-file-22518 and examples for PHP and Ruby are given. However I cannot find such method in the podio java library. I could find in FileAPI just methods that provide uploading files, but not attaching them to specific objects as described in documentation.
I use Podio APi version 0.7.1
Any ideas how it should be done in Java?
Podio uses a REST-Style API. You send standard http-request, and you get back json-formatted data. So you can do it all without a special library for your programming language.
If there is no predefined java class for you, you can just do the call yourself. In the end it is just a HTTP-call.
From the ruby implemention, I see that you attach the file as multipart/form-data,
so it is the same a browser would do it. There should be http-handling java classes to help you.
You also need to add the information from the API-Page, like the POST-Parameters and of course the url. The most difficult part is probably the authentication headers, but you need to solve this problem only once.

How do I use Google Maps API in a Java code to fetch coordinates of specified location?

In a BI project I'm currently working on, we are in need of geo-coordinates for a list of locations. With the address location (such as "New York, US") as input, the output should be the coordinates as a latitude-longitude pair (like {40.71435, -74.00597}). The behaviour is similar to what is seen on this page.
A similar question earlier on SO points to using the Google Maps API in JavaScript to achieve this, but I'm looking for a Java solution -- some function of the form getCoordinates(location), because this is a small requirement in a larger Java program already in existence.
Any pointers on how I may use the Google Maps API (or any other maps API) in Java to achieve this would be of great help!
You can use the Google Geocoding HTTP API (see here).
To connect to it and get the responses you can use a Java URLConnection (tutorial is here) and parse the response using your favourite Json library (I personally use Jackson)
So you'd like a way to perform Google Maps API Geocoding via Java - here's one that might work for you. The response might not be in the exact same format you need but should be pretty workable:
http://code.google.com/p/geocoder-java/
You can see the final format returned in LatLng.java - just trace the code through starting from GeocodeResponse.java and you'll see the final format - the classes are pretty simple.

Generating Java from WSDL for use on Android with ksoap2-android SOAP client?

I have to access a existing SOAP webservice from an Android application. I have been provided some WSDL files describing the webservice. Reading some other answers here on SO, it seems ksoap2-android is the way to go, with respect to which SOAP client to use.
The next issue is then how to generate the Java classes needed from the WSDL files, and this is where I am coming up short. As far as I can see there are the following options:
AXIS2 code generator
WSDL2ksoap
JAX-WS wsimport tool
I initially tried #1, with the AXIS2 eclipse plugin for wsdl2code generator. The wizard did successfully generate a lot of Java code, however it also changed my android project to some kind of webservice project, and I was never able to get anything that was generated to compile, let alone work with ksoap2-android. Has anybody has success with this?
I am not able to run wsdl2ksoap successfully, as it seems to require a running webservice, and all I have at the current point in time is WSDL files. Likewise from reading the webpage, it seems to be a project in its initial stages, and not really ready for prime time.
JAX-WS wsimport I have not had a chance to try yet. However I am unsure if what it generates will work with ksoap2-android?
Question: How can I generate Java files from WSDL files, for use on Android with ksoap2-android SOAP client library?
Thanks a lot in advance.
(PS: Yes, the choice is SOAP, it is suboptimal for Android use, but I cannot change that.)
I found this tool to auto generate wsdl to android code,
http://www.wsdl2code.com/example.aspx
Here is the code:
public void callWebService() {
SampleService srv1 = new SampleService();
Request req = new Request();
req.companyId = "1";
req.userName = "userName";
req.password = "pas";
Response response = srv1.ServiceSample(req);
}
I had similar situation (I had only wsdl file without working webservice). I've used
http://easywsdl.com/
to generate classes for android without any problem. This tool uses ksoap library. The great thing with this tool is that it supports WCF extensions and types like data contract with IsReference attribute or Guid.
My conclusion after quite a bit of researching is that there is no such (mature) tool available, unfortunately. Neither AXIS2 or JAX-WS will work on Android, and WSDL2ksoap is simply too immature for any real use.
However there is a proprietary tool called wsclient++ that will do the job really well. (Read update below, when put to real use, it does not stand the distance at all.) It does not use the ksoap2-android client library, it has it's own.
The client library is a bit crude as it has a hard dependency on the http transport, making (unit) testing a bit complicated. But it can be modified quite easily to allow DI, as the source is available in the distributed jar file.
The wsdl to java generator however works just perfect, and will save us tons of time.
Update
After working with wsclient++ for a while, it is clear that the generated classes are really crude, and does not handle error cases at all. (Every method declares throws Exception).
We are no longer using wsclient++, and I would not recommend anyone to use it!
We have not really found any working alternative, unfortunately. :/
In the end we converted our WSDL files using AXIS2, and then wrote a bunch of custom script to strip and transform the generated java files to something that will build on android using ksoap2-android library. Very hackish, and needs tons of manual labor to run. Unfortunately. If you find a better way, or one comes up, please provide a new answer.
I use Apache CXF tool just to create dto, and i wrote a class to perform a basic unmarshalling based on name of elements
A bit late on this, but there is a ksoap2 stub generator under development, and I successfully used it to create the stubs.
http://ksoap2-stub-gen.sourceforge.net/
Also someone made it availabe as an online service (i.e. you give your WSDL's URL and the service will return a zip file containing the stubs).
http://www.davidgouveia.net/2011/04/online-stub-generator-for-android-applications-using-ksoap2/
I have used for iPhone too some auto-generated classes I wanted to see here too.
wsdl2code is one of the similar what I have used at iPhone. Give an url with wsdl file you will get some classes to download. For me the hardest part it was to download the required parts. It took more than 2 minutes of searching :) ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar needed to download ad drag-and-drop to ADT ( Eclipse) . It is super easy, especially if you have used the counterpart at iPhone. - a similar tool I have used.
However in my case I am not happy at all with the solution, because I see I am using cannon, a set of cannons to shot a sparrow. In my case it should be used a HTTP Post and not including dependencies from other libraries.
To be honest I don't care to much, because once the server side believe we have unlimited battery power and unlimited data plan, than I close my eyes and I don't care about marshaling-unmarshaling overheads, which use the CPU ( battery ) increase the data transmitted over network.
In worse case it should be a JSON + HTTP POST not SOAP for mobiles...
I would suggest to talk at server side guys and explain for they why it will not good if they do 2 click on wizards and we do other click on forms to get the generated code. At least while the application is not a huge one, even than should be budget to optimise for mobile a few interfaces implementations.

Accessing website search box in Java

What I need to write is a code snippet that would go to a website e.g. www.google.com find the search box put in the phrase and retrieve HTML code of results page/pages. Is it possible to achieve this in Java?
e.g. www.google.com
For Google, use the JSON/Atom Custom Search API. It is the only (legal) way to access Google search.
Yes, use something like HttpClient, although there are other similar options.
Most probably you should be able to pass a parameter to the url (have a look at the google url after issuing a search, there are plenty of parameters) or use a post request (if the site supports it, check for an API description).
If you read the URL directly from Java (e.g. using the URL class) you'll get the returned HTMl as is.
The first tool I thought of was Selenium. It is primarily a web testing framework, but can be used to automate a browser for the kind of operation you're suggesting.
http://seleniumhq.org/docs/03_webdriver.html#getting-started-with-selenium-webdriver
HttpUnit can also be used. It's a well documented, open source and easy to use unit test framework.

Categories