public int doEndTag() throws JspException {
try {
// if composite decorator, remember last page
Page oldPage = (Page) pageContext.getRequest().getAttribute(PAGE);
// parse bodycontent into Page object
PageParser parser = Factory.getInstance(config).getPageParser(contentType != null ? contentType : "text/html");
Page pageObj = null;
if (page == null) {
// inline content
pageObj = parser.parse(bodyContent.getString().toCharArray());
}
else if (page.startsWith("http://") || page.startsWith("https://")) {
try {
URL url = new URL(page);
URLConnection urlConn = url.openConnection();
urlConn.setUseCaches(true);
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConn.getInputStream()));
StringBuffer sbuf = new StringBuffer();
char[] buf = new char[1000];
for (; ;) {
int moved = in.read(buf);
if (moved < 0) break;
sbuf.append(buf, 0, moved);
}
in.close();
pageObj = parser.parse(sbuf.toString().toCharArray());
}
catch (MalformedURLException e) {
trace(e);
throw new JspException(e.toString());
}
catch (IOException e) {
trace(e);
throw new JspException(e.toString());
}
}
else {
// external content
String fullPath = page;
if (fullPath.length() > 0 && fullPath.charAt(0) != '/") {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
// find absolute path if relative supplied
String thisPath = request.getServletPath();
// check if it did not return null (could occur when the servlet container
// does not use a servlet to serve the requested resouce)
if (thisPath == null) {
String requestURI = request.getRequestURI();
if (request.getPathInfo() != null) {
// strip the pathInfo from the requestURI
thisPath = requestURI.substring(0, requestURI.indexOf(request.getPathInfo()));
}
else {
thisPath = requestURI;
}
}
fullPath = thisPath.substring(0, thisPath.lastIndexOf('/") + 1) + fullPath;
int dotdot;
while ((dotdot = fullPath.indexOf("..")) > -1) {
int prevSlash = fullPath.lastIndexOf('/", dotdot - 2);
fullPath = fullPath.substring(0, prevSlash) + fullPath.substring(dotdot + 2);
}
}
// include page using filter response
RequestDispatcher rd = pageContext.getServletContext().getRequestDispatcher(fullPath);
PageRequestWrapper pageRequest = new PageRequestWrapper((HttpServletRequest) pageContext.getRequest());
PageResponseWrapper pageResponse = new PageResponseWrapper((HttpServletResponse) pageContext.getResponse(), factory);
StringBuffer sb = new StringBuffer(contentType != null ? contentType : "text/html");
if (encoding != null) {
sb.append(";charset=").append(encoding);
}
pageResponse.setContentType(sb.toString());
// if rd == null, then the panel was not found, but this isn't correct, so we need to spit out
// something appropriate. What this is, well...I don't know yet.
if (rd == null) {
throw new ApplyDecoratorException("The specified resource in applyDecorator tag (" + fullPath + ") was not found.");
}
rd.include(pageRequest, pageResponse);
pageObj = pageResponse.getPage();
}
// If pageObj == null, then the panel source had some weird error in
// it. Stop writing bugs like this. They're ugly and they make you smell funny.
if (pageObj == null) {
throw new ApplyDecoratorException(page + " did not create a valid page to decorate.");
}
// add extra params to Page
Iterator paramKeys = params.keySet().iterator();
while (paramKeys.hasNext()) {
String k = (String) paramKeys.next();
String v = (String) params.get(k);
pageObj.addProperty(k, v);
}
// get decorator
if (decorator == null) decorator = "";
pageObj.setRequest((HttpServletRequest) pageContext.getRequest());
pageContext.getRequest().setAttribute(DECORATOR, decorator);
Decorator d = decoratorMapper.getDecorator(((HttpServletRequest) pageContext.getRequest()), pageObj);
pageContext.getRequest().removeAttribute(DECORATOR);
// apply decorator
if (d != null && d.getPage() != null) {
pageContext.getRequest().setAttribute(PAGE, pageObj);
pageContext.include(d.getPage());
}
else {
throw new JspException("Cannot locate inline Decorator: " + decorator);
}
// clean up
pageContext.getRequest().setAttribute(PAGE, oldPage);
}
catch (IOException e) {
trace(e);
throw new JspException(e.toString());
}
catch (ServletException e) {
trace(e);
throw new JspException(e.toString());
}
catch (ApplyDecoratorException e) {
try {
pageContext.getOut().println(e.getMessage());
}
catch (IOException ioe) {
System.err.println("IOException thrown in applyDecorator tag: " + e.toString());
}
}
return EVAL_PAGE;
}
Standard taglib method: apply decorator to page. |