Dispatcher.forward and response.flush() in AJAX - ajax

I'm working on a Java EE based application and we are heavily using AJAX there.
In there we use two type of techniques to write ajax response.
1) Pass some data throgh ajax
res.setContentType("text/xml");
res.setHeader("Cache-Control", "no-cache");
PrintWriter writer = null;
writer = res.getWriter();
StringBuffer xmlDoc = new StringBuffer("<?xml version='1.0'?>");
xmlDoc.append("<selectChoice>");
xmlDoc.append("<selectElement>");
writer.print(xmlDoc.toString());
writer.flush();
writer.close();
2)send complete jsp fragment via ajax
firstly ajax call servlet,
then servlet set some variable sessions,
and then forward into jsp page rendered the variable stored previously
RequestDispatcher disp = context.getRequestDispatcher(APP_pagePath + page);
disp.forward((HttpServletRequest)req, res);
in the first one as a standard practise we use flush() method. Then my question is how it is handling on second scenario (dispatcher.forward). In there we do not explicitly flush(). can you guys please explain the difference between these two and how the flushing handles in second scenario.

Related

Using HtmlUnit, is there a way to pause execution of Javascript, then resume?

In HtmlUnit for testing, I'm coming across a case where, on page load, it'd be useful to NOT execute the Javascript automatically, and instead wait for me to initiate and tell the Javascript to start executing?
My specific use-case is testing something which the Javascript does some tests, and then does a location replace to send the user on to another page. I want to check some headers which I'm returning for testing/validation, and then let the JS execute as usual.
My current thought is to have a flag I pass to the page when testing which will cause the JS to not automatically run, and wait until I call a JS function from within the Java code via webClient.getJavaScriptEngine().execute().
While not specifically being able to pause JavaScript before invoking, it may be worthwhile to use the WebConnectionWrapper class to inspect/modify the response data or outgoing requests, effectively giving you a chance to execute your own code before the JavaScript is invoked.
An example usage of this is as follows:
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
// set more options
// create a WebConnectionWrapper with an (subclassed) getResponse() impl
new WebConnectionWrapper(webClient) {
public WebResponse getResponse(WebRequest request) throws IOException {
WebResponse response = super.getResponse(request);
if (request.getUrl().toExternalForm().contains("my_url")) {
String content = response.getContentAsString();
// intercept and/or change content
WebResponseData data = new WebResponseData(content.getBytes(),
response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders());
response = new WebResponse(data, request, response.getLoadTime());
}
return response;
}
};
// use the client as usual
HtmlPage page = webClient.getPage(uri);
}
The above code is from the official documentation here:
How to modify the outgoing request or incoming response?
The getResponse() method that you would override is called before each request is made and also allows you to modify the WebResponse object that is passed back to WebClient for its continued processing.
Sorry but at the moment (version 2.43.0) we have no such option. Feel free to open a issue on github for this.
I guess other test tools might also benefit from this function.

calling webapi from asp.net application as a client

I have a web api with a method to return string value. I am having this web api controller inside a web application. I am trying to call the method in the api using the following code:
Stream data = client.OpenRead(new Uri("http://localhost:40786/api/Getvalues/getstring"));
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();
I am calling this method on click event of a web-page say Default.aspx. The code runs fine however instead of calling web-api and returning it's value, it is returning HTML mark-up of the page on which i have the button. No idea what is happening. Can anyone suggest what I am missing here ?
Try this code:
WebClient client = new WebClient();
string resultStr = client.DownloadString(http://localhost:40786/api/Getvalues/getstring");

Capture current JSF page content

I want to capture the current page and send it to an application that converts it to pdf.
This is what I am using:
FacesContext facesContext=FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
// RequestPrinter.debugString();
response.reset();
// download a pdf file
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename="+new Date().toString()+".pdf");
prince.setVerbose(true);
prince.setLog(logFile);
try{
//getPath() to the page the user is currently on
URL pagePath=new URL(this.getPath());
URLConnection urlConnection = pagePath.openConnection();
urlConnection.setDoOutput(true);
int length = urlConnection.getContentLength();
//Lets use inputStream
BufferedInputStream bis=new BufferedInputStream(urlConnection.getInputStream());
response.setContentLength(length);
//this.getPageUsingJSoup().data().getBytes();
//call prince and pass params for inputstream outputStream
prince.convert(bis,response.getOutputStream());
urlConnection.getInputStream().close();
}catch(MalformedURLException mu){
mu.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
facesContext.responseComplete();
Since the website requires authentication, the pdf generated is the loging error page.
Is there a way to capture the page's content that uses the current user's session?
Thank you in advance.
Just request the page in the same HTTP session as the current request. If your webapp supports URL rewriting (as by default), then just append session ID as jsessionid path fragment:
String sessionId = ((HttpSession) externalContext.getSession()).getId();
InputStream input = new URL("http://localhost:8080/context/page.jsf;jsessionid=" + sessionId).openStream();
// ...
Or if your webapp doesn't accept URL rewriting, but accepts cookies only, then set it as a request cookie the usual way:
URLConnection connection = new URL("http://localhost:8080/context/page.jsf").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
InputStream input = connection.getInputStream();
// ...
Note that I removed setDoOutput() since you don't seem to be interested in performing a POST request.
I do not know how to capture the page's content using the current user's session, but I can suggest another way to do it - you could move the pdf conversion logic inside a Selenium test-case and use the test-case to navigate and login to the page requiring authentication. After the automated tc has logged in, you could call your pdf conversion logic...?
Yes of course there is. You are sending this content, so you have it. You should store the Content Object. If you dont have it, inspect your byte streams. The content should be there ;)
There of couple of websites which allow you to convert the entire page to pdf and save it as .pdf file. Try out the site http://pdfcrowd.com/ Hope this helps you.

WebApi Output Cache

I'm trying to implement an output cache in WebApi that can cache the responses already processed by filters and generate responses that are not processed by formatters.
From what I've seen ActionFilterAttribute's OnActionExecuting and OnActionExecuted are execute before the serialization formatters and so if you cache a response, at a cache hit you will respond the exact same content and that content will be serialized again to transmission.
As a possible solution in MVC I think that you can do this by implementing a IResultFilter that override OnResultExecuted by caching the serialized response. With this approach I don't know how to intercept the request handling to avoid the serialization formatters, I think that a possible solution to intercept is create a custom ActionResult to be handled directly by IResultFilter. Please note that this solution is not suitable for me because I'm implementing OutputCache in a WebApi application.
While writing the response, formatters in Web API come into action for HttpContents of type ObjectContent only.
In you OnActionExecuted method, you could force the serialization to happen by doing something like below and then set the response content as StreamContent (this way the formatters wouldn't be coming into picture):
An example below:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
HttpResponseMessage response = actionExecutedContext.Response;
if (response != null && response.IsSuccessStatusCode)
{
ObjectContent originalContent = response.Content as ObjectContent;
if (originalContent != null)
{
MemoryStream ms = new MemoryStream();
// NOTE:
// 1. We are forcing serialization to occur into a buffered stream here
// 2. This can cause exception. You can leave it as it is and Web API's exception handling mechanism should
// do the right thing.
originalContent.CopyToAsync(ms).Wait();
// reset the position
ms.Position = 0;
StreamContent newContent = new StreamContent(ms);
// Copy the headers
foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
{
newContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
//dispose the original content
originalContent.Dispose();
//TODO: cache this new httpcontent 'newContent' (so we are caching both serialized body + headers too)
//Set the response
//NOTE: This newContent will not hit the formatters
actionExecutedContext.ActionContext.Response.Content = newContent;
}
}
}

p:commandButton does not open window with update

I generate an exportList in my Bean:
public void exportExcel() throws WriteException {
try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"hours.xls\";");
OutputStream out = response.getOutputStream();
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("de", "DE"));
WritableWorkbook workbook = Workbook.createWorkbook(out, ws);
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
sheet.addCell(new Label(0, 0, "ID", bold));
int row = 1;
for (Hour hour : this.listHours) {
sheet.addCell(new Label(0, row, String.valueOf(hour.getId())));
row++;
}
SheetFormatter.setOptimalColumnWidth(sheet);
workbook.write();
workbook.close();
response.flushBuffer();
context.responseComplete();
context.addMessage(null, new FacesMessage("Liste Exportiert"));
}
catch (Exception e) {
}
}
In my page I call method in p:commandButton
<p:commandButton value="#{msg.export}" update="growl"
immediate="true" action="#{hoursView.exportExcel()}" />
My page will not open the excel-List... If if add the attribute ajax="false" it works but then update will not execute...
For information my Bean is SessionScoped if this makes some differences
Your first mistake is that you're trying to download a file using ajax. That just isn't possible. Ajax is executed by JavaScript code which has due to security reasons no facilities to force a "Save as" dialogue and/or write the retrieved response to the local disk file system. That would otherwise open doors to various nasty security breach possibilities.
So, using ajax="false" is absolutely necessary.
Your second mistake is that you're trying to mix different responses into a single response. That just isn't possible. You can only return either a file download, or an ajax update, not both. To retrieve two different responses, you basically need to let the client send two different requests. You could approach this as follows:
Let client send an ajax request to backing bean.
Let server create and save Excel file in server's temporary storage system and generate an unique URL so that it could be accessed by a servlet.
Let server send an ajax response containing the message and the URL.
Let client display the message and invoke a new GET request on the URL. You could use window.location=url; to let JavaScript invoke a new GET request on the URL.

Resources