📄 cmsjsploader.java
字号:
boolean streaming,
boolean top) {
CmsFlexController controller = null;
if (top) {
// only check for existing contoller if this is the "top" request/response
controller = CmsFlexController.getController(req);
}
if (controller == null) {
// create new request / response wrappers
controller = new CmsFlexController(cms, resource, m_cache, req, res, streaming, top);
CmsFlexController.setController(req, controller);
CmsFlexRequest f_req = new CmsFlexRequest(req, controller);
CmsFlexResponse f_res = new CmsFlexResponse(res, controller, streaming, true);
controller.push(f_req, f_res);
} else if (controller.isForwardMode()) {
// reset CmsObject (because of URI) if in forward mode
controller = new CmsFlexController(cms, controller);
CmsFlexController.setController(req, controller);
}
return controller;
}
/**
* Parses the JSP and modifies OpenCms critical directive information.<p>
*
* @param byteContent the original JSP content
* @param encoding the encoding to use for the JSP
* @param controller the controller for the JSP integration
* @param includes a Set containing all JSP pages that have been already updated
* @param isHardInclude indicated if this page is actually a "hard" include with <code><%@ include file="..." ></code>
* @return the modified JSP content
* @throws UnsupportedEncodingException
*/
private byte[] parseJsp(
byte[] byteContent,
String encoding,
CmsFlexController controller,
Set includes,
boolean isHardInclude) {
String content;
// make sure encoding is set correctly
try {
content = new String(byteContent, encoding);
} catch (UnsupportedEncodingException e) {
// encoding property is not set correctly
LOG.error(Messages.get().getBundle().key(
Messages.LOG_UNSUPPORTED_ENC_1,
controller.getCurrentRequest().getElementUri()), e);
try {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
content = new String(byteContent, encoding);
} catch (UnsupportedEncodingException e2) {
// should not happen since default encoding is always a valid encoding (checked during system startup)
content = new String(byteContent);
}
}
// parse for special <%@cms file="..." %> tag
content = parseJspCmsTag(content, controller, includes);
// parse for included files in tags
content = parseJspIncludes(content, controller, includes);
// parse for <%@page pageEncoding="..." %> tag
content = parseJspEncoding(content, encoding, isHardInclude);
// convert the result to bytes and return it
try {
return content.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// should not happen since encoding was already checked
return content.getBytes();
}
}
/**
* Parses the JSP content for the special <code><%cms file="..." %></code> tag.<p>
*
* @param content the JSP content to parse
* @param controller the current JSP controller
* @param includes a set of already parsed includes
* @return the parsed JSP content
*/
private String parseJspCmsTag(String content, CmsFlexController controller, Set includes) {
// check if a JSP directive occurs in the file
int i1 = content.indexOf(DIRECTIVE_START);
if (i1 < 0) {
// no directive occurs
return content;
}
StringBuffer buf = new StringBuffer(content.length());
int p0 = 0, i2 = 0, slen = DIRECTIVE_START.length(), elen = DIRECTIVE_END.length();
while (i1 >= 0) {
// parse the file and replace JSP filename references
i2 = content.indexOf(DIRECTIVE_END, i1 + slen);
if (i2 < 0) {
// wrong syntax (missing end directive) - let the JSP compiler produce the error message
return content;
} else if (i2 > i1) {
String directive = content.substring(i1 + slen, i2);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_DIRECTIVE_DETECTED_3,
DIRECTIVE_START,
directive,
DIRECTIVE_END));
}
int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
while (directive.charAt(t1) == ' ') {
t1++;
}
String argument = null;
if (directive.startsWith("cms", t1)) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "cms"));
}
t2 = directive.indexOf("file", t1 + 3);
t5 = 4;
}
if (t2 > 0) {
String sub = directive.substring(t2 + t5);
char c1 = sub.charAt(t3);
while ((c1 == ' ') || (c1 == '=') || (c1 == '"')) {
c1 = sub.charAt(++t3);
}
t4 = t3;
while (c1 != '"') {
c1 = sub.charAt(++t4);
}
if (t4 > t3) {
argument = sub.substring(t3, t4);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_DIRECTIVE_ARG_1, argument));
}
}
if (argument != null) {
// try to update the referenced file
String jspname = updateJsp(argument, controller, includes);
if (jspname != null) {
directive = jspname;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_DIRECTIVE_CHANGED_3,
DIRECTIVE_START,
directive,
DIRECTIVE_END));
}
}
// cms directive was found
buf.append(content.substring(p0, i1));
buf.append(directive);
p0 = i2 + elen;
i1 = content.indexOf(DIRECTIVE_START, p0);
} else {
// cms directive was not found
buf.append(content.substring(p0, i1 + slen));
buf.append(directive);
p0 = i2;
i1 = content.indexOf(DIRECTIVE_START, p0);
}
}
}
if (i2 > 0) {
// the content of the JSP was changed
buf.append(content.substring(p0, content.length()));
content = buf.toString();
}
return content;
}
/**
* Parses the JSP content for the <code><%page pageEncoding="..." %></code> tag
* and ensures that the JSP page encoding is set according to the OpenCms
* "content-encoding" property value of the JSP.<p>
*
* @param content the JSP content to parse
* @param encoding the encoding to use for the JSP
* @param isHardInclude indicated if this page is actually a "hard" include with <code><%@ include file="..." ></code>
* @return the parsed JSP content
*/
private String parseJspEncoding(String content, String encoding, boolean isHardInclude) {
// check if a JSP directive occurs in the file
int i1 = content.indexOf(DIRECTIVE_START);
if (i1 < 0) {
// no directive occurs
if (isHardInclude) {
return content;
}
}
StringBuffer buf = new StringBuffer(content.length() + 64);
int p0 = 0, i2 = 0, slen = DIRECTIVE_START.length();
boolean found = false;
if (i1 < 0) {
// no directive found at all, append content to buffer
buf.append(content);
}
while (i1 >= 0) {
// parse the file and set/replace page encoding
i2 = content.indexOf(DIRECTIVE_END, i1 + slen);
if (i2 < 0) {
// wrong syntax (missing end directive) - let the JSP compiler produce the error message
return content;
} else if (i2 > i1) {
String directive = content.substring(i1 + slen, i2);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_DIRECTIVE_DETECTED_3,
DIRECTIVE_START,
directive,
DIRECTIVE_END));
}
int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
while (directive.charAt(t1) == ' ') {
t1++;
}
String argument = null;
if (directive.startsWith("page", t1)) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "page"));
}
t2 = directive.indexOf("pageEncoding", t1 + 4);
t5 = 12;
if (t2 > 0) {
found = true;
}
}
if (t2 > 0) {
String sub = directive.substring(t2 + t5);
char c1 = sub.charAt(t3);
while ((c1 == ' ') || (c1 == '=') || (c1 == '"')) {
c1 = sub.charAt(++t3);
}
t4 = t3;
while (c1 != '"') {
c1 = sub.charAt(++t4);
}
if (t4 > t3) {
argument = sub.substring(t3, t4);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_DIRECTIVE_ARG_1, argument));
}
}
if (argument != null) {
// a pageEncoding setting was found, changes have to be made
String pre = directive.substring(0, t2 + t3 + t5);
String suf = directive.substring(t2 + t3 + t5 + argument.length());
// change the encoding
directive = pre + encoding + suf;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_DIRECTIVE_CHANGED_3,
DIRECTIVE_START,
directive,
DIRECTIVE_END));
}
}
buf.append(content.substring(p0, i1 + slen));
buf.append(directive);
p0 = i2;
i1 = content.indexOf(DIRECTIVE_START, p0);
}
}
if (i2 > 0) {
// the content of the JSP was changed
buf.append(content.substring(p0, content.length()));
}
if (found) {
content = buf.toString();
} else if (!isHardInclude) {
// encoding setting was not found
// if this is not a "hard" include then add the encoding to the top of the page
// checking for the hard include is important to prevent errors with
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -