Conundrum with POST and GET on Android app - java

I am trying to gain some information from my localhost database via my android app. I am using the HttpUrlConnection class to create objects that read/write from/to the database.
I am having this problem now, because the HttpUrlConnection object am trying to create needs to read and write at the same time to a PHP file hosted on the localhost database. I see that the HttpUrlConnection class has a method setRequestMethod, which if I want to write to the database i will use setRequestMethod("POST") and if I want to read from the database i will use
setRequestMethod("GET"). I need to do both at the same time. I need to initialize a variable in a PHP file with the POST method, and then search and read row values on the database based on the initialized POSTED variable.
Any advice will be appreciated.
Thank you

The response from server is not depend on GET or POST method. So you can receive the response in both cases, just design your server like this:
$value1 = $_REQUEST["name"];
$value2 = $_REQUEST["name2"];
...
do_some_thing();
...
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
//don't care it's POST or GET request.
See different between GET and POST method: http://www.w3schools.com/tags/ref_httpmethods.asp

Related

Unable to create endpoint with JDBC mssql in java

I am trying to create endpoint in jsp, i have been able to connect to the database using the following code but i need to understand the structure of it.
<%
String refNumb = request.getParameter("refNumb");
// String display = request.getParameter("display");
//String msg = request.getParameter("message");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://IPAddress;databaseName=FIRS",
"user", "pass");
%>
You have successfully done the first step which is to connect to your database via jdbc. You can follow these steps after you successful fetched records from the database.
Convert your records into json string in java, you can use json library and follow the steps on how to convert your records to json
Create a jsp file and use out.print to print the records on your browser.
If you can get to step two then final step is to give a use the url of your jsp which is the end point. As long as anyone can display the json data from your db then that data can easily be consume via js or any other programming language.
You can also research via google on how to implement and consume webservices in java(Soap or rest) and follow the tutorial step by step.

Amazon S3 AWS SDK [Java] - MultiPart Upload how to get a custom header in the http response?

how can I access a custom header from a server response when using TransferManager ?
we have a custom header added in the response from our server, from the client side we use multi part upload with default transfer manager
any suggestion how in how i could hook up it ?
so basically i want to pass over the response from the return response.getAwsResponse(); found in the class: AmazonS3Client on the method
private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request,
HttpResponseHandler<AmazonWebServiceResponse<X>> responseHandler,
String bucket, String key, boolean isAdditionalHeadRequestToFindRegion) {
that response will have the HTTP response from the server containing the custom heather which I'm after, basically is a unique Id send back when the file was 100% completed so than i can manipulate it.
I need to pass over this custom header from the response to the very beginning where I use the transfer manager and the upload.waitForCompletion(),
also i don't want to edit the amazon's,
so does anyone know if there is an interface or some other object which provides me access to it ?
After some debug into the framework I strongly believe that there is no way to have access to the http response when using the TransferManager
for what we are trying to do we need to send an unique id from the server to the client when the file upload is completed and assembled
** therefore if you don't mind in do not use the beauty of the TransferManager you could write "your own TransferMananger" than you will have full control, but again on the client side we don't really want to add custom code but have a standard and simple approach (but that is for my scenario), if you decide to do it manually it can be done I have already tried and works !
So as a alternative we though in send from the server via the eTag, which is not great but will do the job and will keep the client side simple and clean
Any suggestion in how to send this value back in a better way ?
Upload up = tm.upload(bucketName, file.getName(), file);
UploadResult result = (UploadResult) ((UploadImpl) up).getMonitor().getFuture().get();
String uniqueIdFromServer = result.getETag();

Accessing kerberos secured WebHDFS without SPnego

I have a working application for managing HDFS using WebHDFS.
I need to be able to do this on a Kerberos secured cluster.
The problem is, that there is no library or extension to negotiate the ticket for my app, I only have a basic HTTP client.
Would it be possible to create a Java service which would handle the ticket exchange and once it gets the Service ticket to just pass it to the app for use in a HTTP request?
In other words, my app would ask the Java service to negotiate the tickets and it would return the Service ticket back to my app in a string or raw string and the app would just attach it to the HTTP request?
EDIT: Is there a similar elegant solution like #SamsonScharfrichter described for HTTPfs? (To my knowledge, it does not support delegation tokens)
EDIT2: Hi guys, I am still completly lost. Im trying to figure out the Hadoop-auth client without any luck. Could you please help me out again? I already spent hours reading upon it without luck.
The examples say to do this:
* // establishing an initial connection
*
* URL url = new URL("http://foo:8080/bar");
* AuthenticatedURL.Token token = new AuthenticatedURL.Token();
* AuthenticatedURL aUrl = new AuthenticatedURL();
* HttpURLConnection conn = new AuthenticatedURL(url, token).openConnection();
* ....
* // use the 'conn' instance
* ....
Im lost already here. What initial connection do I need? How can
new AuthenticatedURL(url, token).openConnection();
take two parameters? there is no constructor for such a case. (im getting error because of this). Shouldnt a principal be somewhere specified? It is probably not going to be this easy.
URL url = new URL("http://<host>:14000/webhdfs/v1/?op=liststatus");
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
HttpURLConnection conn = new AuthenticatedURL(url, token).openConnection(url, token);
Using Java code plus the Hadoop Java API to open a Kerberized session, get the Delegation Token for the session, and pass that Token to the other app -- as suggested by #tellisnz -- has a drawback: the Java API requires quite a lot of dependencies (i.e. a lot of JARs, plus Hadoop native libraries). If you run you app on Windows, in particular, it will be a tough ride.
Another option is to use Java code plus WebHDFS to run a single SPNEGOed query and GET the Delegation Token, then pass it to the other app -- that option requires absolutely no Hadoop library on your server. The barebones version would be sthg like
URL urlGetToken = new URL("http://<host>:<port>/webhdfs/v1/?op=GETDELEGATIONTOKEN") ;
HttpURLConnection cnxGetToken =(HttpURLConnection) urlGetToken.openConnection() ;
BufferedReader httpMessage = new BufferedReader( new InputStreamReader(cnxGetToken.getInputStream()), 1024) ;
Pattern regexHasToken =Pattern.compile("urlString[\": ]+(.[^\" ]+)") ;
String httpMessageLine ;
while ( (httpMessageLine =httpMessage.readLine()) != null)
{ Matcher regexToken =regexHasToken.matcher(httpMessageLine) ;
if (regexToken.find())
{ System.out.println("Use that template: http://<Host>:<Port>/webhdfs/v1%AbsPath%?delegation=" +regexToken.group(1) +"&op=...") ; }
}
httpMessage.close() ;
That's what I use to access HDFS from a Windows Powershell script (or even an Excel macro). Caveat: with Windows you have to create your Kerberos TGT on the fly, by passing to the JVM a JAAS config pointing to the appropriate keytab file. But that caveat also applies to the Java API, anyway.
You could take a look at the hadoop-auth client and create a service which does the first connection, then you might be able to grab the 'Authorization' and 'X-Hadoop-Delegation-Token' headers and cookie from it and add it to your basic client's requests.
First you'll need to have either used kinit to authenticate your user for application before running. Otherwise, you're going to have to do a JAAS login for your user, this tutorial provides a pretty good overview on how to do that.
Then, to do the login to WebHDFS/HttpFS, we'll need to do something like:
URL url = new URL("http://youhost:8080/your-kerberised-resource");
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
String authorizationTokenString = conn.getRequestProperty("Authorization");
String delegationToken = conn.getRequestProperty("X-Hadoop-Delegation-Token");
...
// do what you have to to get your basic client connection
...
myBasicClientConnection.setRequestProperty("Authorization", authorizationTokenString);
myBasicClientConnection.setRequestProperty("Cookie", "hadoop.auth=" + token.toString());
myBasicClientConnection.setRequestProperty("X-Hadoop-Delegation-Token", delegationToken);

Why is an exception generated in java when I use the following code

Document doc1;
String url="http://www.google.com";
url= url +" and 1=1";
doc1=Jsoup.connect(url).get();
Here there is no problem with the connection as the following code gives no exception. The exception is generated only when I try to get the HTML code with the above code.
Document doc1;
String url="http://www.google.com";
url= url +" and 1=1";
Jsoup.connect(url);
Thanks!
JSoup.connect doesn't actually try to connect to the website. If you look through the documentation, you'll see that it only creates a Connection object. You can chain method calls on the Connection to set cookies, user agent, and other stuff before calling get, execute, post, or one of the other methods that will actually send the request.
(Here's another documentation link that might be easier to browse. Unfortunately, Javadoc's use of frames makes linking awkward.)
According to Jsoup javadoc:
Jsoup.connect(String url):
Creates a new Connection to a URL. Use to fetch and parse a HTML page.
Connection.get():
Execute the request as a GET, and parse the result.
So in the first sample you are querying google and getting an IOException because of the invalid URL but not in the second sample (no query is made)
Jsoup.connect doesn't actually connect to anything. It just creates a Connection object. If you want to set any special properties of the connection, you can do it before you call get() which is what actually connects.
As for why you get an exception: probably because http://www.google.com and 1=1 is not a valid URL.

Retrieving data from URL fails

I have been trying different ways to get data from the following link:
http://www.ensembl.org/Danio_rerio/Export/Output/Location?db=core;flank3_display=300;flank5_display=300;output=fasta;r=18:19408965-19409049;strand=feature;coding=yes;cdna=yes;peptide=yes;utr3=yes;exon=yes;intron=yes;genomic=unmasked;utr5=yes;_format=Text
Copy paste the link to a web browser works for me but I cannot get to it programmatically in java.
It seems that it doesn't follow the get protocol as the separation of parameters is not as expected.
I tried to use URL but it separates the link above into server path and query and results in HTTP 500.
I tried to use sockets but again failed.
I believe that what I need is a way to simply send the complete string unaltered and then read the result.
Any ideas?
This code reads first line from that URL successfully:
URL u = new URL("http://www.ensembl.org/Danio_rerio/Export/Output/Location?db=core;flank3_display=300;flank5_display=300;output=fasta;r=18:19408965-19409049;strand=feature;coding=yes;cdna=yes;peptide=yes;utr3=yes;exon=yes;intron=yes;genomic=unmasked;utr5=yes;_format=Text");
DataInputStream ds = new DataInputStream(u.openStream());
String s = ds.readLine();
System.out.println(s);
It prints out: >18 dna:chromosome chromosome:Zv9:18:19408665:19409349:1

Categories