I'm building an Android application which allows people scan QR code and open its content if that is a URL. But before opening, I want to check it's a safe URL. I check Google Safe Browsing API, and it turns out I need to download and save malware and infected URL list, which's impossible for Android client.
There's another way with http://www.google.com/safebrowsing/diagnostic?site= but this have two disadvantages:
1.It doesn't work with full URL or URL paramater:
Work: http://www.google.com/safebrowsing/diagnostic?site=http://example.com
Don't work: http://www.google.com/safebrowsing/diagnostic?site=http://example.com/abc/def?ghi=opq
2.It opens a web page which tell us URL is safe or not, but I'd rather want it return a HTTP status code: like 201 -> safe, 202 -> isn't safe (which is easier to process).
The first disadvantage's not much a problem, but the second's really annoying. Can anyone show me another way around? Really thank.
There is an API that maybe solves all of your problems: http://code.google.com/apis/safebrowsing/
And here is an Java client implementation for it: http://code.google.com/p/jgooglesafebrowsing/
One thing I would suggest is to have a server side implementation and have an Rest api exposed. Your android app can act as a client for the same
You need to urlencode your URL as you cannot have two ? characters in the URL at the same time.
So your URL should looks like:
https://transparencyreport.google.com/safe-browsing/search?url=http:%2F%2Fexample.com%2Fabc%2Fdef%3Fghi%3Dopq
In Java, you can use URLEncoder.encode().
See: Java URL encoding of query string parameters
Related
I have RESTful app which reacts on GET request and I need to store the state.
For example
localhost/subs?search=John
Then I need to add other parameter division by clicking button.
localhost/subs?search=John&division=develop
Meanwhile the data output will be distributed on pages. And appear new 2 parameters size and page.
localhost/subs?search=John&division=develop&size=5&page=0
In this situation when I click on next page button my url is resetting.
I’m really confused. How to save state and if parameter is already has in url then it should be changed for new value, if parameter doesn’t exist then append it.
If there good options?
At least I can use JavaScript by taking it and parsing url.
But I think it’s not good at all.
As I know, the RestAPI doesn't save any request parameters (Maybe you can do by adding them into a session attribute, but it's just made the logic code become more complex).
At least I can use JavaScript by taking it and parsing URL
Yes, I think you should.
Happy coding!
Play's flash is a "temporary storage mechanism that is kept for the duration of the next request", using cookies.
Is it possible to read the flash from javascript?
I want to implement a generic client-side notification mechanism, and also access that mechanism from the server. What I imagine is for any controller to do:
flash.put("notification", "You have been notified");
And then for my main.js to check if the "notification" flash exists, and if it does display it above (similar to Stack Overflow's notification mechanism).
However, I suspect that flash might be encrypted in a way that the client side cannot read it. I haven't found any code samples that show how to read it from the client side JS (not in a template). Is this possible directly?
As I'm writing this question, I'm actually coming up with a way to implement this: I can put a hidden div in the main template that all my views extend, and access that div from JS. Does it make sense? Is there a simpler approach?
I don't think it's possible to use JavaScript to read flash, even via the cookie. That cookie is actually encrypted with a security seeds (application.secret) configured in your application.conf.
However it is possible to implement what you want.
Create a tag to export flash variables into Javascript object.
Call that tag in your view.
Your main.js use that exposed variable to do whatever it want.
For me you have two solutions
In your view call a javascript function with the value as an argument, then in javascript you can store it in an object for later use
The flash cookie isn't encrypted, it is sign, so you can read it but you can't modify. Use a tool like firebug to see the content of your flash cookie
The Same Origin Policy is preventing me from fetching the JSON data I need from another web site (with permission). I saw one person who was working around this with JsonpRequestBuilder, but I'm not sure if that's going to be the best solution for me. The only other option that comes to my mind would be to have an intermediary servlet on my server.
What's my best bet here? I have concerns with both methods. With an intermediary servlet, I worry about the delay that would introduce. With the JsonpRequestBuilder, it looks like I have to create a complete JavascriptObject for each method I'll call from the other site, even though I only need to pull out a single value from some of those methods.
I don't use Java, but JSONP is what I usually implement when I need cross-domain chatter, and I'm sure someone will have made a Java library that unwraps it. It requires a change on the third-part's site, but it is a very simple change.
EDIT: Sounds like that is what that library does, sorry... but still... it's the way to go :)
Check out the CORS Specification. We are using this to successfully circumvent the SOP using our own server with GWT's devmode Jetty.
You don't have to "create a complete JavaScriptObject", a JavaScriptObject is actually just a mean to call to JavaScript from the Java world, so you only need the one getter for the value you need, and it can even return a "nested" value:
public native String getFoo() /*-{
return this.nested[0].obj.foo;
}-*/;
Whether you'll use JSONP (and JsonpRequestBuilder) or a "proxy servlet" actually only depends by the capabilities of the "service" you need to call: JSONP is JavaScript, not JSON, so the server has to return a "JSONP response script" or you won't be able to use JsonpRequestBuilder (and similarly, you won't be able to (safely) use CORS or a proxy-servlet if the server returns a "JSONP script" rather than application/json).
I would like to send a request to a Java Servlet from PHP and receive the response from the same and show it on the PHP page. How should this be done?
Thanks and Regards
Abishek R Srikaanth
If all you want is to print the response of a GET request to an external resource plain vanilla into the PHP response, then you can use file_get_contents() for that.
<?php echo file_get_contents("http://example.com/someservlet"); ?>
The servlet's doGet() method will be invoked and whatever response it returns (which can even be a forwarded JSP) will be printed as string to the PHP response.
If you want a little more fine grained control, e.g. using POST or something, then head to curl() instead. The linked PHP manual contains several examples.
Regardless of the way, please note that whenever it returns HTML, that you should ensure that you end up with valid HTML. For example, nesting <html> tags is illegal. Pass the PHP page through the w3 validator if you're unsure. Otherwise you'd better have to parse the HTML to extract the <body> pieces of interest or to use an <iframe> instead.
<iframe src="http://example.com/someservlet"></iframe>
If I'm understanding you correctly you want to read the response of a servlet in php and then output it from php?
You can use file_get_contents to the url (Probaly not the best way, but for simplicities sake it the easiest) and then just echo the output.
For example:
$content = file_get_content('http://www.google.com');
echo $content;
But if you want to be able to login or use the session at the servlet side you will need think of something else. As each request to the server from php will be a new one, it does not store cookies etc.. like browsers do.
Hope that helps
This is a situation which might do well to be rethought, but, if there are no other options...
If there is a way to actually update the portlet, then I would recommend creating some form of service call -- SOAP, custom RPC, etc -- on the Java side. Technically this is the most correct way to do things.
Failing that, if this is a simple GET request, then use file_get_contents.
If it has to be a POST/PUT/DELETE, then you can use cURL. cURL also has the benefit of being able to handle simulated sessions, which means that you are then able to simulate a log in and actions following that (though not without some difficulty).
If you don't have cURL and you need to POST/PUT/DELETE, then the streams library might be able to give you what you need.
If you don't have the streams library or cURL and you need to POST/PUT/DELETE, then there are other means of accomplishing that, but maybe you should really re-rethink that situation.
If all of the above don't work, then you will need to tame the Spectral Wolf. The Spectral Wolf fears only fire. I can no longer help you, but if you master the Spectral Wolf, he will guide you. Godspeed.
If you really want to do that, you can create a Java app that takes parameters to populate request and response objects, instantiates a servlet, runs the correct method, gets the result and displays it, then call this Java app from PHP.
Or you can use the experimental and unrecommended PHP / Java Integration module.
Is there a way of getting the websites absolute URL (http://www.domain.com/) using Java? because I've googled a bit but I only come across with having to make 2 or 3 classes to create that function =/
Update:
The thing is I am trying to create a crawler that will give me some information and among that I'd like to get the URL of the webpage it's getting the information from. I'm developing this in JAVA and what I meant to say was that I was wondering if there was some getUrl(); or any method like that to get me the Url, because I know it can be done but I've only done it writing a whole other class to retrieve the url and then inherit it and use it further...hope it made it clearer
I'm assuming you just want the domain from a JSP, however you may find you need the entire URL including the prefix, domain, path and parameters. The easiest way to get this quickly is to use the Request object and build it. Have a look here for more info:
http://www.exforsys.com/tutorials/jsp/jsp-request-object.html
Here is Sun's API on the HttpServletRequest interface:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html
The question is not really clear, but I'll make the assumption that you are trying to get the path from within a Servlet.
String realPath = getServletConfig().getServletContext().getRealPath(relativePath);
Could you be more specific? Your question states:
Is there a way of getting the websites
absolute URL (http://www.domain.com/)
using Java?
By "the website" which website are you asking for? I can see multiple ways of interpreting your question:
Given a URL, if there a way to get the hostname portion of it?
Given a relative path, how do you get the full path?
Within the context of a Servlet, is there a way to get the name of the deployed server?
etc...