// handle relative URLs ourselves
// URL is relative, so we must be an HTTP request
if (!(pageContext.getRequest() instanceof HttpServletRequest
&& pageContext.getResponse() instanceof HttpServletResponse))
throw new JspTagException(
Resources.getMessage("IMPORT_REL_WITHOUT_HTTP"));
// retrieve an appropriate ServletContext
ServletContext c = null;
String targetUrl = targetUrl();
if (context != null)
c = pageContext.getServletContext().getContext(context);
else {
c = pageContext.getServletContext();
// normalize the URL if we have an HttpServletRequest
if (!targetUrl.startsWith("/")) {
String sp = ((HttpServletRequest)
pageContext.getRequest()).getServletPath();
targetUrl = sp.substring(0, sp.lastIndexOf('/'))
+ '/' + targetUrl;
}
}
if (c == null) {
throw new JspTagException(
Resources.getMessage(
"IMPORT_REL_WITHOUT_DISPATCHER", context, targetUrl));
}
// from this context, get a dispatcher
RequestDispatcher rd =
c.getRequestDispatcher(stripSession(targetUrl));
if (rd == null)
throw new JspTagException(stripSession(targetUrl));
// include the resource, using our custom wrapper
ImportResponseWrapper irw =
new ImportResponseWrapper(pageContext);
// spec mandates specific error handling form include()
try {
rd.include(pageContext.getRequest(), irw);
} catch (IOException ex) {
throw new JspException(ex);
} catch (RuntimeException ex) {
throw new JspException(ex);
} catch (ServletException ex) {
Throwable rc = ex.getRootCause();
if (rc == null)
throw new JspException(ex);
else
throw new JspException(rc);
}
// disallow inappropriate response codes per JSTL spec
if (irw.getStatus() < 200 || irw.getStatus() > 299) {
throw new JspTagException(irw.getStatus() + " " +
stripSession(targetUrl));
}
// recover the response String from our wrapper
return irw.getString();