I have a GWT + Spring Security web app. I was trying to add:
<security:session-management invalid-session-url="/X.html"/>
However, when I try to test this. It seems I see a:
com.google.gwt.user.client.rpc.InvocationException
with message as the HTML content of X.html. Can someone please advise on how to fix this?
Because GWT communicates with a server via Ajax RPC requests, the browser will not be redirected to X.html. What you need to do in your service calls is throw an exception if they are not authorized and handle in in void onFailure(Throwable caught) method of your AsyncCallback.
If you want to redirect to /X.html try:
Window.Location.replace(GWT.getHostPageBaseURL()+"X.html");
However, if you want to send the request to the server use RequestBuilder:
String url = GWT.getHostPageBaseURL() + "/X.html";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// invalid request
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// success
} else {
// sth went wrong
}
}
});
} catch (RequestException e) {
// couldn't connect to server
}
Related
this is mycode. It seem only execute 1 request
public class RestFulService extends AbstractVerticle {
#Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.get("/test/hello/:input").handler(new Handler<RoutingContext>() {
#Override
public void handle(RoutingContext routingContext) {
WorkerExecutor executor = vertx.createSharedWorkerExecutor("my-worker-pool",10,120000);
executor.executeBlocking(future -> {
try {
Thread.sleep(5000);
future.complete();
} catch (InterruptedException e) {
e.printStackTrace();
}
},false, res -> {
System.out.println("The result is: " + res.result());
routingContext.response().end("routing1"+res.result());
executor.close();
});
}
});
}
When i call 10 request from browser in same time, it take 50000ms to done all request.
Please guide me fix it.
Try with curl, I suspect your browser is using the same connection for all requests (thus waiting for a response before sending the next request).
By the way, you don't need to call createSharedWorkerExecutor on each request. You can do it once when the verticle is started.
I am trying to make http request to server from my GWT Application
Below is the code i am using which works fine from Client Side ..
But I want to do this from Server side ..
If I can get some solution for that
String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// Process the response in response.getText()
} else {
// Handle the error. Can get the status text from response.getStatusText()
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}
Reference: www.gwtproject.org/doc/latest/DevGuideServerCommunication.html
com.google.gwt.http.client.RequestBuilder is meant to be used on the GWT client side.
For your server side code to make HTTP requests, I recommend using the Apache HttpClient library.
I have created a very simple servlet that uses HTTP Post method. I have tested it on my local Apache Tomcat server using a simple HTML form that works. I want to integrate it with my GWT app. I am able to call it using FormPanel - in that case it downloads content and there is a flicker in my browser window.
I know I need to use RequestBuilder to access it. But my response.getStatusCode() in my overloaded public void onResponseReceived(Request request, Response response) method always return status as 0 and response.getText() return null
String url = "http://localhost:8080/servlets/servlet/ShapeColor";
builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
try {
String json = getJSONString();
//builder.setTimeoutMillis(10000);
builder.setHeader("Access-Control-Allow-Origin", "*");
builder.setHeader("Content-type", "application/x-www-form-urlencoded");
builder.sendRequest(json, new RequestCallback() {
#Override
public void onError(Request request, Throwable exception) {
Window.alert("Couldn't retrieve JSON");
}
#Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
System.out.println("res:"+response.getText());
} else {
System.out.println("err: " + response.getStatusCode()+","+response.getText());
}
}
});
//Request response = builder.send();
} catch (RequestException e) {
// TODO Auto-generated catch block
}
I have tried many thing including changing my servlet following CORS reference ( https://code.google.com/p/gwtquery/wiki/Ajax#CORS_%28Cross_Origin_Resource_Sharing%29 )
It always works on browser using my test.html, but not from my App. Although, onResponseReceived method always gets called
Thanks
KKM
Have you checked if your call in the app violates the Same-origin policy (http://en.wikipedia.org/wiki/Same-origin_policy) in some way? The GWT RequestBuilder uses XMLHttpRequest internally, so it does fall under the SOP.
Does your GWT app run inside the same domain (server + port) as the servlet? Does it use the same protocol (https or http)?
i'have two web appli, tapestry appli and a simple web appli(servelt). in tapestry appli , i have a form, and when it'll be sent, i call a httpClient for sending some informations to author appli using apache's httpClient. like this
void onSubmitFromForm() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/appli2/recep");
post.setHeader("referer", "http://localhost:9090/app1/start");
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("_data", getData());
post.setEntity(new UrlEncodedFormEntity(param));
HttpResponse response = client.execute(post);
response ?????
} catch (Exception e) {
e.printStackTrace();
}
}
And in my servelt recep of the simple web appli(2) i do the same like below
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(request.getHeader("referer"));
post.setHeader("p",getP());
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}
}
So, my recep reviev data from my form but it'cannot response it, i'would that tapersty appli could recieve the param 'P' from the simple web appli ?
thanks
If I'm correct you want your tapestry application to POST some form data received from a form submit within Tapestry to a servlet running on another application.
If this is what you want then what is missing is the haneling of the request and constructing a response in your servlet. Because both your tapestry page and your servlet are POST'ing meaning neither constructs a response for your HttpClient to deal with.
In your servlet you could:
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(getP());
out.close();
}
And deal with the response in your tapesty form handler.
When my users try to do an action our website after their session has expired (for instance, if they left their browser open), the server responds with a HTTP Status 405 because the user is no longer logged in.
When this happens, I want to redirect the user to the login screen.
How can I recognize when a 405 error code is returned within GWT so I can then redirect the users?
Thanks
Edit:
Here's the filter I'm using:
public class AuthenticationFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
boolean isLoggedIn = CustomSecurity.login((HttpServletRequest)req);
if (isLoggedIn) {
// TODO: How to redirect the user here???
}
}
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml content:
<filter>
<filter-name>Authentication Filter</filter-name>
<filter-class>com.company.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
How can I make that redirect the user? Also, is there a way to force the whole browser to redirect? Since this goes into a widget, I think a Window.Location.assign('url') will only redirect the widget's HTML content.
I currently work on a GWT application that does something very similar to what you are asking. However, we handle these redirects in a standard javax.servlet.Filter subclass that we define in our web.xml as per usual and it is mapped to /* so it catches all requests (we use this filter for many other reasons as well).
I don't know if you are also using a Filter or perhaps even just some sort of catch-all Servlet in your GWT app, but if you are then the solution to your issue is quite easy. You will have a handle to the HttpServletRequest and you can see if the getSession(false) method returns null then you know that the user who sent the request no longer has a session. Then you can just do a standard response.sendRedirect(...) to your login page.
For GWT-RPC you will get a StatusCodeException whenever the server returns anything but a 200 response code. The status code can be retrieved from the exception and you can redirect via GWT by using Window.Location.assign(url). For RequestBuilder the status code cqan be accessed via response.getStatusCode().
EDIT: Here is code to redirect if GWT's window isn't that root window.
private native void redirect(String url)/*-{
var win = $wnd;
while (win.parent != null && win.parent != win)
win = win.parent;
win.location = url;
}-*/;
In your RequestCallback implementation, you can check response.getStatusCode(). We have Spring Security on the backend, which is wired up to force the user to a form based login page if they are not authenticated. Upon authentication, it redirects them back.
For example:
#Override
public void onResponseReceived(Request request, Response response) {
if (SC_UNAUTHORIZED == response.getStatusCode()) {
Window.Location.reload();
}
}
use a try / catch for coders sakes!
AsyncCallback<String> callback = new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
try {
throw caught;
} catch (StatusCodeException statusCodeException) {
Util.handleException(statusCodeException);
} catch (Throwable exception) {
Util.handleException(exception);
}
}
public void onSuccess(String result) {
Window.alert("Result : " + result);
}
};
then to process your results
final static public void handleException(StatusCodeException exception) {
String errorMessage = exception.toString() +
"\n CODE: " + exception.getStatusCode() +
"\n MESSAGE: " + exception.getEncodedResponse();
Window.alert("ERROR: " + errorMessage);
}
final static public void handleException(Throwable exception) {
String errorMessage = exception.toString() + "\n";
for (StackTraceElement trace : exception.getStackTrace()) {
errorMessage += trace.getClassName() + " :: " +
trace.getMethodName() + " :: " +
trace.getLineNumber() + "\n";
}
Window.alert("ERROR: " + errorMessage);
}
remember that overloading is your friend! Dont forgot that if you do not compile your code in detailed or pretty you will get garbage for the stack trace. Unless of course someone figured out how to hack the JSStackEmulation class
http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions