GWT calling java from JS doesn't work with Iframe - gwt

I can run Java from JavaScript normally in a GWT application but when I run it in an iframe it doesn't work.
HTML
<input type="button" onclick="doIT()" value="Do"
class="fbbotton" style="margin-left: 20px" />
Java
$wnd.doIT = #com.application.client.application::saad();
static void saad()
{
GWT.log("saad");
}
What could be the problem?

If both pages are returned from different servers you need to take a look at the same origin policy (in short it is not possible).
If both sites are served from the same server (hostname and port) this should work, but you need to call the function properly.
If your iframe is a child of the page that your javascript is in you need to call: `
parent.doIT();

Related

Response redirect using IP in dotnetnuke

I am hosting and developer on my DNN portal. I need to redirect users using client IP. I think may be two ideas for this work.
1- DNN Setting
Maybe DNN has settings for it that I can set specific URL for client IP addresses and automatically DNN redirects to specific URL.
I read many topic but I could not find setting to do it.
Is there a way to do this?
2- New Module
I have a ascx that onload method has this code:
var IP = Server.HtmlEncode(Request.UserHostAddress).ToString();
using (Entities db = new Entities())
{
var retVal = db.URLAddresses.Where(u => u.IPAdress == IP).FirstOrDefault();
if (retVal != null)
Response.Redirect(retVal.URL);
}
But I should add this code to any ascx for redirect using client IP. This is impossible because maybe I haven't source code modules.
I think I should create new module. So I can add it to page. Module changes onload page and redirect to URL using client IP.
In this scenario, I try to create new module but I don't know how I can change onload method each page that is added module to it?
You can use IHttpModule and make a new Module for Including your class then you should add your IHttpModule to web.config .
For e.g
<add name="YourModule" type="YourAssembly, YourNameSpace" preCondition="managedHandler" />
See this Sites:
HTTP Handlers and HTTP Modules Overview
and How To Create an ASP.NET HTTP Module
DNN does have a Host setting that will allow or deny access to users logging in based on their IP address. It's in Host Settings > Advanced Settings > Login IP Filters. I don't think that will give you the desired result.
I would not suggest creating a module. It can be difficult copying it to all pages and ensuring one instance is added to every page.
Rather, I would create a skin (theme) token. To do this, create a simple class library project. Create an .ascx and ascx.cs file. You can leave the .ascx empty because you don't have any html to add to the pages. In the .cs, put something like this:
namespace MyCompany.DNN.Skin
{
public partial class IpRedirect : SkinObjectBase
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Put your redirect logic here
}
}
}
Then, in your theme/skin ascx pages, include the following:
<%# Register TagPrefix="myco" TagName="IPREDIRECT" Src="~/DesktopModules/MyCompany/IpRedirect/IpRedirect.ascx" %>
<myco:IPREDIRECT ID="pageRedirect" runat="server" />
This will ensure that this functionality will execute on all pages in the site that use the skin/theme.

hello.js not in AngularJS web app not working on iPhones and Blackberry Z10s

I have a login system that works for most people (on Chrome, Android devices, IE8, Firefox, etc), but it seems not to work for people with Z10s or iPhone 5s. I don't have access to these devices so it's difficult to test, so I wanted to ask whether I was setting up everything properly.
It's an AngularJS app, using hello.js for OAuth, and bootstrap-social and font-awesome for the sign in buttons.
To insert hello into Angular, in app.js I include:
var app = angular
.module('myapp', [
'ui.bootstrap',
'ui.router',
'hello',
])
...
.run([..., 'hello', ..., function(..., hello, ...) {
...
hello.init(...);
...
}]);
...
var helloApp = angular.module('hello', []);
helloApp.factory('hello', function() {
return window.hello; // Assumes hello has been loaded
});
Then, in my loginCtrl, I inject it with
angular.module('myapp').controller('loginCtrl', [..., 'hello', ...,
function(..., hello, $location, ...) {
...
$scope.doLogin = function(network) {
console.log('Calling hello ' + network);
hello.login(network);
};
...
}]);
And in my view, I have
<button id="facebookLogin" class="btn btn-social btn-facebook" ng-click="doLogin('facebook')">
<span class="fa fa-facebook pull-left"></span> <span>Sign in with Facebook</span>
</button>
<button id="googleLogin" class="btn btn-social btn-google-plus" ng-click="doLogin('google')">
<span class="fa fa-google-plus pull-left"></span> <span>Sign in with Google</span>
</button>
Yesterday I was using onclick="hello.login('facebook')", and I suspected that was breaking on certain devices because I shouldn't be using onclick and hello wasn't in scope, so that's why I changed it to ngClick and calling a function in scope. The specific effect of onclick on the users who had errors was to redirect the user to the default/catch-all route without accessing the server at all (I listen for hello events and call the server, so this suggests it wasn't calling hello at all.)
But still, I ask the people who are having issues to re-try (after refreshing obviously), and now they say the button simply does nothing.
Other buttons on the site work. In fact, to get to this page, they use a <button> that uses ui-router to get to this page.
I'm going to continue to search, but I just wanted to ask if I seemed to be hooking hello.js into AngularJS properly, and not making any other beginner mistakes.
I believe the problem is that iPhones and Z10s were blocking hello.js's popup OAuth authentication, but I couldn't just switch it to use page.
When I tried, Facebook returns me to my redirect_url with the fragment
#access_token=....&expires_in=4264&state={%22client_id%22%3A%22164986300332415%22%2C%22network%22%3A%22facebook%22%2C%22display%22%3A%22page%22%2C%22callback%22%3A%22_hellojs_3ojn1yy8%22%2C%22state%22%3A%22%22%2C%22oauth_proxy%22%3A%22https%3A%2F%2Fauth-server.herokuapp.com%2Fproxy%22%2C%22scope%22%3A%22basic%22%2C%22oauth%22%3A{%22version%22%3A2%2C%22auth%22%3A%22https%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%2F%22}}
it's ugly, but the point is it starts with a hash tag. So, while hello.js can normally read this, it couldn't in this case because Angular would mangle the address immediately. I'm not sure if this is because I specify to use hash-bangs instead of hash's, but it was.
And I couldn't send this to a PHP script or anything because the fragment after the hashtag would never make it to the PHP script.
So, my solution was to point the redirect_url at an independent page that has hello.js on it, but no Angular. It saves stuff to window.sessionStorage and redirects the user back to the login page, where hello.js passes the user through.
I'm not very confident in this ugly approach yet, so I put a browser sniffer to only do it for iPhones (and use popup for everyone else), I may remove this check in the future (because it's sketchy)
#matt it uses localstorage, not sessionstorage.
Use popup for all, and define a redirect_uri page with just hello.js In it. I dont know why you might think thats sounds wrong. All the demos do it this way. It also gives you a chance to display a nifty loading screen.
Sorry about the ugly fragment. It communcates a lot of state parameters which is used for the oauth proxy... most endpoints like facebook dont need it, so I might refactor that to make it a little less daunting.

Java Web Architecture

I am interested in putting together a URL Auction research site. It will be structured as a standard [Presentation Layer <-->Business Layer <--->Data Layer]. This site will have an index.html that will allow users to enter a url they are interested in. That index.html will then call a doPost method via servlet which in turn spits out a results page of whether or not that URL is for sale like so:
index.html
<html>
<head>
<title>URL Auction Search Page</title>
</head>
<body>
<CENTER>
<FORM ACTION="/ResultServlet/Results" METHOD=GET>
<INPUT TYPE=TEXT NAME="st">
<INPUT TYPE=SUBMIT VALUE=Submit>
</FORM>
</CENTER>
</body>
</html>
Servlet:
#WebServlet("/Results")
public class Results extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String str="";
private String businessLogic(String q){
try {
str = new compute.URL.GetAvailURI( "https://www.registerdomains.com/auctionAPI/Key:a05u3***1F2r6Z&urlSearch="+q);
/*more boring number crunching */
return str;
}
/*
protected void doGet(HttpServletRequest request, HttpServletResponse response)
}
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Results r = new Results();
String st = request.getParameter("st");
response.sendRedirect("results/resultActionURL.html?st="+r.businessLogic(st)");
}
}
However, there will be 2 other links from the results page that I need to process. Namely, a link called WhoOwnsThis.html which takes whois data and geographically maps this information on Google maps. Also, AppraiseResult.html that gives a real time appraisal of the url in question. OK- these 2 pages will take time (very loosely we’ll say 2 sec. ea.) to crunch so I am presenting the user with the results page while I then go on in the background and create the data for each of the other two results pages.
Question-
How do I handle the statefulness of these three results pages? My first thought is to create a unique directory each time the two additional results pages are created(WhoOwnsThis and AppraiseResult). Then I can embed the unique dir name as an argument in the results page url so that when they click on the link to the other 2 results pages, the dir name will be pulled from the results page url as a .js var and inserted to get the right page. However, I am also reading up on REST and wondering if that is a better way of handling the state for this transaction. What would industry standard recommend in this scenario?
Do you really want to work with such a low level API as Servlets? If you are open-minded look at Play Framework 2.0 and get the same experience as everyone on other platforms enjoy. If not, Spring MVC is a good choice. And if you prefer to just have JavaScript frontend then JAX-RS is a good choice.
Note that Play Framework 2.0 includes it's own class reloading mechanism, but you get the same experience with JRebel in other frameworks as well.
State in general is good to keep in the database, so it will be easy to scale. Java EE 6 Web Profile also gives you stateful session beans. Spring MVC has session scoped beans. Each of them do it in their own way, but as it is such a basic concept, there is plenty of documentation on their site and I'm not going to paste it here.
Have fun and welcome to the era of simple Java :)
I think the more modern layering is four-tiered:
View->Controller->Service->Persistence.
You can choose between asking the Java EE app server to maintain state for you either in the web tier via sessions or as stateful services in the service layer.

Easiest way to consume text returned from a REST service

I need to display on my Web page a simple text string returned from a REST service. I am currently using an XMLHttpRequest:
<div id="returnedText"></div>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.status == 200 && xhr.readyState == 4) {
document.getElementById("returnedText").innerHTML=xhr.responseText;
}
};
xhr.open("GET",url,true);
xhr.send(null);
</script>
Isn't there a lighter way? I considered using a script tag but the Web service in question doesn't support JSONP. I also did a naive attempt with an iframe (putting the REST url as src) but it didn't work.
I did another attempt with iframes and actually this works fine:
<iframe src="url"></iframe>
Where url is the REST service call.
I must have done something wrong the first time (maybe an authentication issue).
Well the iframe route is clunky since you'd be loading the REST response into it and then reaching into it via JS to get the response. What's more, it would cause a visible load in the browser's address bar area. AJAX came along to do away with the iframe hack :)
JSON-P requires about as much setup as AJAX and if your server doesn't support the callback, that's a none starter.
AJAX needn't be thought heavy. Kick it into its own utility function or, even better, use a library, which makes requests like these do'able in one line. jQuery example:
$.get('some/path').done(function(response) { /* do something */ });

generate a dynamical html

How can i generate a dynamiccal html code in iphone like below:
NSString sHtml = #"<html><form name= \"PIOform\" >\
<input type=\"text\" name=\"txtUserName\" >\
<input type=\"text\" name=\"txtPassWord\" >\
</form>\
<input type=\"button\" onclick=\"goCallLogin(PIOform)\" >
</html>";
Yes, I want to create an UI iphone application with feature like a client HTML form to call a javascript method in any server:
When i click a button in iphone. It will call goCallLogin(PIOform) method in JS at address:
http;//www.myserver.com/login.js
How can i do?
Thank you
This seems like something better suited for an iPhone web app than a native app. Maybe you should look into doing that instead? There is no need to create HTML like this with a native app. If you want to send values to a remote server from a native app, take a look at writing a web service and accessing it from the iPhone. You could use REST or XML-RPC to activate the goCallLogin method that you were referring to.
Also, you cannot just call a javascript through a web browser and expect it to work. Javascript is a client-side technology. You need to look into writing your web service with a server-side language/framework such as PHP, Ruby on Rails, ASP.NET, etc.