I use the following with Velocity in order to return and render the
content of a page.
You can verify the length of the page content when doing this.
Tony Oslund
public String getPageContent(Context context, String path) {
StringBuffer content = new StringBuffer();
BufferedReader in = null;
try {
URL url = new URL(path);
HttpMessage msg = new HttpMessage(url);
in = new BufferedReader(new
InputStreamReader(msg.sendPostMessage()));
StringBuffer myBuff = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
myBuff.append(line + "\n");
}
StringReader strReader = new StringReader(temp);
StringWriter writer = new StringWriter();
Velocity.evaluate(context, writer, "INFO", strReader);
content.append(writer.toString());
}
catch (java.net.MalformedURLException e1) {
content.append("<p> </p><p>URL Exception: Unable to
retrieve page content: (" + path + ") " + e1.getMessage() + "</p>");
}
catch (java.io.IOException e2) {
content.append("<p> </p><p>IO Exception: Unable to
retrieve page content: (" + path + ") " + e2.getMessage() + "</p>");
}
catch (Exception e3) {
content.append("<p> </p><p>General Exception: Unable to
retrieve page content: (" + path + ") " + e3.getMessage() + "</p>");
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
content.append("<p>" + e.getMessage() + "</p>");
}
}
}
return content.toString();
}
If you were to create a wrapper for it within a "tool" you could access
it from within a VM.
public String includeContent(RunData data, String path) {
Context context = TurbineVelocity.getContext(data);
... (my own security checking code here)
return getPageContent(context, url to content);
}
Then within your VM you could do...
#set ($mycontent = $tool.includeContent($data, some url))
#set ($length = $mycontent.length())
$mycontent
-- OR --
$tool.includeContent($data, some url)
FYI, if the page being pointed at by the url also contains a call to
includeContent, then it is recursive
ALSO FYI, This is a handy way of implementing a single VM to display
lots of generic content (read this as plain old html files) without
having to create a VM for each... and still maintain a session.
In addition to checking the length, you could also implement a security
model for the plain old html files by imbedding some logic within
includeContent.
Also works great if your web page developers are geographically located
someplace other than your programmers, because you can pull the skins
for you pages from remote servers (during development).
-----Original Message-----
From: Jeffery Painter [mailto:[email protected]]
Sent: Friday, March 13, 2009 1:22 PM
To: Turbine Users List
Subject: Re: content length of a page
I would look first at some of the old examples of extending the
RawScreen
for sending binary data. This does not solve your problem of a finding
the
content length of a Turbine/velocity generated page however.
You may need some intermediate step to generate the page then calculate
it's length before you can then set the content length in the response
object.
If you search the archives for things related to RawScreen or sending
binary data, you may get some help there.
You would need to make use of the ByteArrayOutputStream to in essence
capture your screen before you send it to the http response.
You can then update RunData as such:
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = someMethodBuildsMyScreen();
data.getResponse().setContentLength(baos.size());
javax.servlet.ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
}
I'm not really sure if there is a way to get the content length
directly.
Maybe you can clarify the problem if this was no help :P
--
Jeffery Painter
>
> Hello,
>
> I'd like to be able to return the content length of a turbine
generated
> page (actually it's jetspeed 1), so that the browser can render
better.
> Is this possible somehow? Is there already an existing function for
this?
> If not, any tip on at which point this could be included (I guess just
> before returning the request stream, when all velocity has been
rendered).
>
> Cheers, Bo
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]