Expiring Authentication Redirect Causes GET instead of POST - asp.net-mvc-2

I have a page where users are editing some content. If the authentication on that page expires, and they press the "Save" button, since their session is timed out, they are redirected to the login page. However, when they re-authenticate, the redirect does a GET on the page when I'm expecting a POST, so it generates a HttException "A public action method 'Close' was not found on controller 'HLPUSD.HEAT.Web.Controllers.TicketController."
What do I need to do to resolve this?
EDIT
Exception:
System.Web.HttpException (0x80004005): A public action method 'SavePrecedence' was not found on controller 'HLPUSD.HEAT.Web.Controllers.QueueController'.
at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

If you respect the following conventions:
public ActionResult Foo()
{
...
}
[HttpPost]
public ActionResult Foo(SomeViewModel model)
{
...
}
where the first action renders a form and the second action is used to handle the submission of this form it will work as you have both GET and POST actions for this resource and in the described scenario the users will simply be redirected to GET /SomeController/Foo which will simply render the form once again.

Related

Twilio statuscallback not working

I have created a statuscallback url for Twilio SMS.
But am getting this exception
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: It is invalid to call isReady() when the response has not been put into non-blocking mode (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"]->org.apache.catalina.connector.CoyoteOutputStream["ready"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: It is invalid to call isReady() when the response has not been put into non-blocking mode (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"]->org.apache.catalina.connector.CoyoteOutputStream["ready"])
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:292)
org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method
My code is as below
#RequestMapping(value = "/getstatus", method = RequestMethod.POST)
public Object getTwilioStatusCallback( HttpServletRequest httpRequest, final HttpServletResponse httpServletResponse){
// System.out.println(httpRequest.getParameter("MessageSid"));
httpServletResponse.setStatus(httpServletResponse.SC_NO_CONTENT);
return httpServletResponse;
}
Two things. First it looks like you are doing a synchronous call, which is a nono in web development. Second, your parameter as MessageSid.. Twilio is notorious for not having the correct reference in their documents. Try messageSid as a parameter. I had the same problem in Node.
You can also look at the entire request body and see how it shows up and get the correct parameter from there, which is what I had to do.
Hopefully this helps.

Why is sendRedirect(String path) not throwing IllegalStateException?

I've following code in my servlet
public class RedirectingExceptionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello, response is being committed");
out.flush();
response.sendRedirect("redirectedExceptionServlet");
System.out.println("Tried redirecting after committing response");
}
}
Here, I'm trying to redirect after the response has been committed.
As per the specification Send Redirect java doc sendRedirect(String) should throw IllegalStateException if you try to call sendRedirect(path) after response is committed
Rather i see a page with message : Hello, response is being committed
Another interesting thing is, i'm unable to see statement "Tried redirecting after committing response" in server console
Can someone pls explain me this behavior?
Env details : Windows 7, Tomcat 7.0.47, J2EE-6
Once the container encounters out.flush(); it returns from there and doesn't execute remaining lines of code or simply ignores them and this is true only in case there is no redirection before out.flush();
I have tried a lot of combinations. If someone is interested to have a look at these combinations, I can share them

How can I determine why onFailure is triggered in GWT-RPC?

I have a project that does 2 RPC calls and then saves the data that the user provided in tha datastore. The first RPC call works ok, but from the second I always recieve the onFailure() message. How can I determine why the onFailure() is triggered? I tried caught.getCause() but it doesn't return anything.
feedbackService.saveFeedback(email,studentName,usedTemplates,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
caught.getCause();
Window.alert("Failure!");
}
public void onSuccess(String result) {
Window.alert("Saved!");
}
});
Throwable instance is instance of an Exception. You can check if it is a custom Exception like this:
if (caught instanceOf CustomException){
or if you want to show the message of exception you can use the getMessage():
Window.alert("Failure: " + caught.getMessage());
GWT-rpc is not not easy to ebug if an error occurs.
The easiest part is th check if the Exception is part of StatusCodeException.
A Statuscode of 404 means, you are pointing to a wrong endpoint
0 means, that
The searver is unreachable
You don't have permissions to check, if the server is available (X-domain-request)
You can use the Chrome-Web-Inspector to bedug GWT-RPC
You should be able to see all calls from the browser to you backend.
The most common failures are because of serialization of object. You have to ensure, that all dtransferred object implement java.io.Serializable
Most of the time it will just be a server side exception being raised which fires the onFailure() method.
Try putting breakpoints on your server side. That should help you pinpoint what's going wrong.

In GWT, when is the AsyncCallback:onFailure method called in a json-p request using JsonpRequestBuilder

I've been implementing a GWT application that calls a REST-service (which we're also developing). When the REST-service returns anything with a HTTP-status other than 200 I would expect the onFailure method of AsyncCallback to be called. However I can't get this to happen.
To test it further I created a test GWT app and a test servlet. The part of the GWT app that calls the service looks like this:
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setCallbackParam("_jsonp");
jsonp.setFailureCallbackParam("_jsonp_failure");
jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>()
{
#Override
public void onFailure(Throwable caught)
{
Window.alert("Failure: " + caught.getMessage());
}
#Override
public void onSuccess(JavaScriptObject result)
{
Window.alert("Success");
}
});
The servlet-code looks like this:
public class MyRestServlet extends HttpServlet
{
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
String padding = httpServletRequest.getParameter("_jsonp_failure");
httpServletResponse.setContentType("application/x+javascript");
httpServletResponse.setStatus(500);
PrintWriter out = httpServletResponse.getWriter();
out.println(padding + "({\"some\":\"json\"});");
out.close();
}
}
OnFailure eventually gets called when the request times out, but I would expect it to be called as soon as the http response arrives(if it's a failure). I guess there is something I haven't understood and I would really appreciate to get some help with this.
Thanks
According to HTML5, if there's an error loading the script, an error event should be dispatched, and GWT doesn't listen for it (because almost no browser actually fires it AFAICT).
For best browser compatibility, you'd better always send a 200 status, but then call the failure callback (or in other words, return an error state/condition, rather than throw an exception).
Also, the argument to the failure callback is expected to be a string (will be the message of the exception).
From the server code where you call the REST service, throw an exception yourself if the response is something other than 200 (by writing code to check the response yourself). This way it will persist to the client side as an error and onFailure will be called in client side.
In GWT's mind currently nothing went wrong. It sent a request, got some result did not matter what, the call was successful. It does call the onFailure on a timeout because something did go wrong with the request "physically", and GWT persisted the exception to the client side as a failure.

How to know weathere the page is started navigating or not?

I am facing this Problem Since last day .Here it is and need help of you .
I am working on a web application, and Sending and retrieving data from Server through an RPC .
My problem is that If I clicked on any action , i am sending an request and if I Clicked on any other links which are navigate the page to other view, without retrieving data of first RPC i am getting the Exception because of that navigation .
I need to avoid it by knowing the navigation .
here i written the code .Lets check once :
#Override
public void onFailure(Throwable exception) {
if (exception instanceof StatusCodeException) {
if (((StatusCodeException) exception).getStatusCode() == 403) {
MyExcclass.showMessage("session expired");
logout();
} else {
MyExcclass.showInformation("unable to perform the action");
}
}
exception.printStackTrace();
}
I am getting the unable to perform action .
If the page is unloaded when the RPC response comes back, you should see a StatusCodeException with getStatusCode() of 0.
If you want, you can preemptively cancel the requests within a Window.addCloseHandler. This means you'd have to provide your own RpcRequestBuilder for each remote service to keep the Request instances around (in a Set) until they're completed (you'd wrap the RequestCallback to remove the associated Request from the Set in onSuccess or onFailure); and in your CloseHandler you'd iterate on the Set to cancel() all pending Request. Overall, it's easier to handle the getStatusCode() == 0 case in your AsyncCallback.onFailure methods.