Junit / HttpUnit poor support on Javascript - java

I have an application where everything starts from a single servlet and then based on some params to initial servlet complete behaviour of application changes, there are lots of things and forwarding which happens until you get response as a HTML.
I want to write test cases, where I only change those few initial parameters and get a response either as getText() or getXml() or getHTML().
I used HttpUnit, something like.
WebRequest request = new PostMethodWebRequest("http://localhost:9090/xyz/ETransServlet.fsc" );
request.setParameter( "window_id", "1" );
request.setParameter( "screen_id", "4" );
request.setParameter( "nav_to", "something.jsp" );
request.setParameter( "nav_from", "somethingfrom.jsp" );
HttpSession session = sr.getSession(true);
WebResponse response = client.getResponse(request);`
Now you see I have some parameter set window_id and all.
first thing, they are not reaching somehow.
secondly, even if I call some page with no params, still it gives error something like can't process script <script> blab bla </script>
It's unable to process javascript code that we are using. I have js.jar and nekohtml.jar in build path and beside I don't want it to validate javascript or html at all.
Can anyone tell what's wrong or how to stop javascript to validate or fix it even if it validates or not or some other solution entirely.

Related

Why doesn't forwardResolution redirect?

I'm new to Stripes and I'm trying to do like this: pass data from JSP1 using Ajax to the actionBean2 which belongs to JSP2 and then display the data on JSP2.
I do like this because JSP2 provides common interface for everything, and I want to redirect the user to JSP2 so that he/she could see the items that were selected in JSP1 using the standard interface of JSP2.
So, the problem occurs when I'm trying to pass the data from ActionBean2 to JSP2. Even though my return statement of actionBean2 looks like this:
return new ForwardResolution("/jsp2.jsp");
It actually doesn't redirect me from JSP1 to JSP2. RedirectResolution doesn't work as well.
I see the AJAX request from JSP1 being sent, the event handler in ActionBean2 receives it but doesn't redirect me from the JSP1. JSP1 even gets GET request for JSP2 url, but still the page doesn't reload. So, it looks like even though I'm in ActionBean2, it still uses JSP1.
My question is - should ForwardResolution redirect me to another page immediately (and reload the browser page)? And also - it my idea of passing data between these pages and actionBeans correct?
OK, I had to use a workaround, which I don't like at all. Still, it works. but if you have any suggestions, please, write.
I passed the parameters from JSP1 to JSP2 in URL using JavaScript redirect. Straight-forward method, but it solved the problem (JSPs didn't redirect when I tried to redirect using Resolution):
window.location.href ="jsp2.jsp?myparameters"+$.param(myparameters);
In JSP2 I used AJAX to call JSP2 actionBean.
if (window.location.search.indexOf("myparameters") != -1){
values = window.location.search.substr(window.location.search.indexOf("myparameters"), window.location.search.length);
values = decodeURIComponent( values );
//some parsing here, didn't include it
$.ajax({
url: "JSP2.action?from_redirect=",
data: {values: values},
type: "POST",
success: function(data) {
displayValues(data)
}
});
And in JSP2.action I made the from_redirect listener return:
return new StreamingResolution("application/javascript", data);
So, this method works, but I don't like 1 extra redirect.
if you have any suggestions, how to solve the problem with Resolutions, please write them. Also I would appreciate any help concerning removing extra redirects in this method.

RESTService returns HTML when writer does not contain data

I'm currently developing RESservices using the customService bean. One thing I've noticed is that for some reason when I dont use the responsewriter object but only set the response status using engine.getHttpResponse().setStatus(404) (for instance). The header is correctly set ( 404 ) but there is still some html generated.
I've already tried to set the rendered property on the view tag (of the xpage) to false but that doesn't seem to do the trick. Is there some documentation on how to use the CustomServiceBean and not returning any data?
Setting the Status is a good approach, so the API is easy to handle. However you want to consider:
set the content type to what would actually be rendered if you had a result. If you don't set response.contenttype it defaults to HTML
404 is meant for navigation/urls. Since your XPage renders a result, the request found a valid URL. That based on user and/or parameters there's no result isn't therefore a 4xx class of error, but rather a 5xx. Pick one of them.
when testing use wget or curl, so you can be sure the browser doesn't do the HTML
Let us know how it goes
I think returning a 404 response causes always HTML data as it normally gives you the error message etc.
If you don't want to response with any data just return an empty JSON object - as I assume you return JSON when you HAVE data, right?
dont set 404 status ,set null for this case,ant error status like 404 means creating html for that

Java Servlets, how to keep request attribute alive across several forwards?

I have a home-grown MVC implementation. A ControllerServlet like so:
/controller?cmd=EditUser&userid=55
From this URL, the controller creates a EditUserCommand.class instance and calls an execute() method which returns the result page (ex. user.jsp) to display.
The controller servlet then does a ...
getRequestDispatcher(resultPage).forward(request, response);
... and the resulting page is shown.
One of the things the controller does is set messages (error, info, and so on) as request attribtues. For example:
request.setAttribute("infoMessage", "User was edited successfully.");
And that message gets pulled out of the request in the user.jsp page and displayed.
Works fine.
Now here comes my problem.
Sometimes my commands don't return a page like user.jsp but return a URL like cmd=ShowUser&userid=55 for the result. This is because there might be things I want to check before displaying the final page, like permission to view the user and so on.
When I do this the "infoMessage" I placed in the request never appears because the result is a URL that makes a new call to the servlet, which is a new request. The new request doesn't maintain the request attributes from the first request; which makes sense, I just didn't forsee this happening.
How can I make my request variable "stay alive" until it's actually displayed on the final page that results from the original request?
Any suggestions or advice are appreciated. Just FYI, I can't re-write the entire app to go to something like Struts, Spring MVC, of JSF. It's not an option.
Thanks!
Rob
redirect generally looses request data because of brand new request from browser. One possible approach may be append your message to url string as attribute and read it when you need.
Based on your EDIT: After your edit also, my answer make sense. But, only one correction is, it is not brand new request because forward happens on server side.
Have you tried using a RequestDispatcher instead of a Redirect?
RequestDispatcher dispatcher = request.getRequestDispatcher("/myNextPage.jsp");
dispatcher.forward(request, response);
You may be giving a simple example, but your control flow causes the "Resend" Error, perhaps? Basically, after making any change to the data, the controller must immediately do a "Get" via a redirect and the screen should be displayed completely stateless.
Please take a look:
http://en.wikipedia.org/wiki/Post/Redirect/Get
As such, Attributes are not a great help. What you may want to consider is, maintaining a Bean/Object for every login user, and persist this object in a LRU cache (JCache or MemcacheD), and retrieve it on every entry to the application. Once you have that, you can maintain a pseudo-state such as previous results in that object.
In any case, using Attributes to retain state will severely constrain your options. You need to have a more generic flexible routing-independent mechanism.

Applet web app interaction

I have an applet that calls a URL, on a user action, and passes some parameters. These parameters are executed on a ROR server and then I update a partial, which is on the applet's page, based on the result. However, the partial is not updating.
The console says that the partial is rendered but it is not. Is it because I am calling the URL from the applet and the server is unaware that the applet is in the current session, hence it does not know which session partial to render?
Here is the code of the controller:
def add_point
#comments = Comment.find(:all)
render :update do |page|
page[:comment_text].value = ""
page.replace_html :comments_partial, :partial => 'comments', :collections=>#comments
end
end
EDIT: I use Apache commons http wrapper to send the request:
PostMethod post = new PostMethod("http://localhost:3001/vizs/add_point");
post.addParameter("upload[test]", dataImg);
post.addParameter("upload[user_id]", new Integer(user_id).toString());
post.addParameter("upload[viz_id]", new Integer(viz_id).toString());
I think I need to set additional request variables but I am unsure what to set....
Any suggestion on how to solve this?
Thanks,
Slotishtype
rails is not aware about the applet, and does not know how to work with it, you need to send xml or json or even html:
def add_point
#comments = Comment.find(:all)
render :partial => 'comments', :collections=>#comments, :layout => nil
end
parse it in applet and fill appropriate fields by yourself.

Jersey - Redirection Using Get Not Put, Causes Redirection Loop

I'm working on a web app that uses Jersey. I'm trying to implement a get-after-post sort of thing using a URIBuilder and a seeOther response. The aim is to redirect to the same URI the browser is already on, but to force a GET. It works a bit like this:
Request comes in via PUT
PUT request processed
SeeOther response returned
What should happen is that the browser picks up the 303 See Other and performs a GET on the URI it receives. Unfortunately, what's happening is that it performs a PUT on the URI instead (as far as I can tell) and the PUT sends it back to Step 1. above, causing a redirection loop.
Any ideas what's going wrong here?
private Response giveSeeOther(){
/*Get the base URI builder*/
final UriBuilder uriBuilder = m_uriInfo.getBaseUriBuilder();
/* Some stuff to create the URI */
final Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("uid", getUid());
final URI redirectUri = uriBuilder.path(SomeObject.class).
path(SomeObject.class, "get").
buildFromMap(parameterMap);
/* See Other (303) */
return Response.seeOther(redirectUri).build();}
That's the code for the see other method. I'm not sure what other code you might want to see, but let me know.
You need to use a 301 HTTP response code instead.
By using 303, your POST request is maintained, and redirected accordingly. By using 301, your request is "Moved permanently" via GET.
For other readers who might wonder why someone wants to do this, it's to prevent the user from submitting their POST data more than once by using the "Reload" function of their web browser (which users with "rotten communications" problems often do) to reload the "thank you" page that may not have loaded completely.
Hint: When you redirect in this manner, if you're not using cookies to ensure information gets to your "thank you" page, then you'll need to add one or more parameters to your request in the same way a regular GET form will. For example, if the order ID number is 82838, you can pass it along to your "thank you" page like this:
http://www.example.com/order/thank-you.pl?orderid=82838
There are obvious potential security issues with this which are easily resolved by having your "thank you" page code check that the order ID actually belongs to the currently logged in user before it displays the order status (I assume you wish to include order status information on that "thank you" page -- in this case, it's also nice to include a "Refresh" button {or link} for the user to check up on the order status if it's something that progresses in the short term over a number of steps).
I hope that's helpful to you.

Categories