package org.directwebremoting.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.extend.AccessControl;
import org.directwebremoting.extend.ConverterManager;
import org.directwebremoting.extend.DebugPageGenerator;
import org.directwebremoting.extend.MethodDeclaration;
import org.directwebremoting.extend.Module;
import org.directwebremoting.extend.ModuleManager;
import org.directwebremoting.util.CopyUtils;
import org.directwebremoting.util.JavascriptUtil;
import org.directwebremoting.util.LocalUtil;
public class DefaultDebugPageGenerator implements DebugPageGenerator {
protected String engineHandlerUrl;
protected String utilHandlerUrl;
protected String testHandlerUrl;
protected String interfaceHandlerUrl;
protected ConverterManager converterManager = null;
protected ModuleManager moduleManager = null;
protected AccessControl accessControl = null;
protected final Map scriptCache = new HashMap();
private Collection availableLibraries = null;
private boolean debug = false;
private static final String PATH_UP = "..";
private static final Log log = LogFactory.getLog(DefaultDebugPageGenerator.class);
public String generateIndexPage(String root) throws SecurityException {
if(!this.debug) {
log.warn("Failed attempt to access test pages outside of debug mode. Set the debug init-parameter to true to enable.");
throw new SecurityException("Access to debug pages is denied.");
} else {
StringBuffer buffer = new StringBuffer();
buffer.append("\n");
buffer.append("
DWR Test Index \n");
buffer.append("\n");
buffer.append("Modules known to DWR: \n");
buffer.append("\n");
Iterator i$ = this.moduleManager.getModuleNames(false).iterator();
while(i$.hasNext()) {
String name = (String)i$.next();
Module module = this.moduleManager.getModule(name, false);
buffer.append("");
buffer.append(name);
buffer.append(" (");
buffer.append(module.toString());
buffer.append(") \n");
}
buffer.append(" \n");
buffer.append("\n");
return buffer.toString();
}
}
public String generateTestPage(String root, String scriptName) throws SecurityException {
if(!this.debug) {
log.warn("Failed attempt to access test pages outside of debug mode. Set the debug init-parameter to true to enable.");
throw new SecurityException("Access to debug pages is denied.");
} else {
String interfaceURL = root + this.interfaceHandlerUrl + scriptName + ".js";
String engineURL = root + this.engineHandlerUrl;
String utilURL = root + this.utilHandlerUrl;
String proxyInterfaceURL = ".." + this.interfaceHandlerUrl + scriptName + ".js";
String proxyEngineURL = ".." + this.engineHandlerUrl;
String proxyUtilURL = ".." + this.utilHandlerUrl;
for(int slashPos = -1; (slashPos = scriptName.indexOf(47, slashPos + 1)) != -1; proxyUtilURL = "../" + proxyUtilURL) {
proxyInterfaceURL = "../" + proxyInterfaceURL;
proxyEngineURL = "../" + proxyEngineURL;
}
Module module = this.moduleManager.getModule(scriptName, true);
MethodDeclaration[] methods = module.getMethods();
StringBuffer buffer = new StringBuffer();
buffer.append("\n");
buffer.append("\n");
buffer.append(" DWR Test \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
buffer.append("\n");
buffer.append("\n");
buffer.append("Methods For: " + scriptName + " (" + module.toString() + ") \n");
buffer.append("To use this class in your javascript you will need the following script includes:
\n");
buffer.append("\n");
buffer.append(" <script type=\'text/javascript\' src=\'" + engineURL + " \'></script>\n");
buffer.append(" <script type=\'text/javascript\' src=\'" + interfaceURL + " \'></script>\n");
buffer.append(" \n");
buffer.append("In addition there is an optional utility script:
\n");
buffer.append("\n");
buffer.append(" <script type=\'text/javascript\' src=\'" + utilURL + " \'></script>\n");
buffer.append(" \n");
buffer.append("Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise. \n");
buffer.append("The inputs are evaluated as Javascript so strings must be quoted before execution.
\n");
for(int i = 0; i < methods.length; ++i) {
MethodDeclaration output = methods[i];
String raw = output.getName();
if(JavascriptUtil.isReservedWord(raw)) {
buffer.append("" + raw + "() is not available because it is a reserved word. \n");
} else {
buffer.append("\n");
buffer.append(" " + raw + '(');
Class[] in = output.getParameterTypes();
for(int ex = 0; ex < in.length; ++ex) {
Class overloaded = in[ex];
if(LocalUtil.isServletClass(overloaded)) {
buffer.append("AUTO");
} else {
String ex1 = "";
if(overloaded == String.class) {
ex1 = "\"\"";
} else if(overloaded != Boolean.class && overloaded != Boolean.TYPE) {
if(overloaded != Integer.class && overloaded != Integer.TYPE && overloaded != Short.class && overloaded != Short.TYPE && overloaded != Long.class && overloaded != Long.TYPE && overloaded != Byte.class && overloaded != Byte.TYPE) {
if(overloaded != Float.class && overloaded != Float.TYPE && overloaded != Double.class && overloaded != Double.TYPE) {
if(!overloaded.isArray() && !Collection.class.isAssignableFrom(overloaded)) {
if(Map.class.isAssignableFrom(overloaded)) {
ex1 = "{}";
}
} else {
ex1 = "[]";
}
} else {
ex1 = "0.0";
}
} else {
ex1 = "0";
}
} else {
ex1 = "true";
}
buffer.append(" ");
}
buffer.append(ex == in.length - 1?"":", \n");
}
buffer.append(" );\n");
String var31 = (LocalUtil.isJavaIdentifierWithPackage(scriptName)?scriptName:"dwr.engine._getObject(\"" + scriptName + "\")") + "." + raw + "(";
for(int var32 = 0; var32 < in.length; ++var32) {
if(!LocalUtil.isServletClass(in[var32])) {
var31 = var31 + "objectEval($(\"p" + i + var32 + "\").value), ";
}
}
var31 = var31 + "reply" + i + ");";
buffer.append(" \n");
buffer.append(" \n");
buffer.append(" \n");
boolean var34 = false;
for(int var35 = 0; var35 < methods.length; ++var35) {
if(var35 != i && methods[var35].getName().equals(raw)) {
var34 = true;
}
}
if(var34) {
buffer.append("(Warning: overloaded methods are not recommended. See below ) \n");
}
Class[] var36 = in;
int len$ = in.length;
for(int i$ = 0; i$ < len$; ++i$) {
Class paramType1 = var36[i$];
if(!this.converterManager.isConvertable(paramType1)) {
buffer.append("(Warning: No Converter for " + paramType1.getName() + ". See below ) \n");
}
}
if(!this.converterManager.isConvertable(output.getReturnType())) {
buffer.append("(Warning: No Converter for " + output.getReturnType().getName() + ". See below ) \n");
}
try {
this.accessControl.assertGeneralDisplayable(scriptName, output);
} catch (SecurityException var25) {
buffer.append("(Warning: " + raw + "() is excluded: " + var25.getMessage() + ". See below ) \n");
}
buffer.append(" \n");
}
}
buffer.append("\n");
buffer.append("Other Links \n");
buffer.append("\n");
buffer.append("Back to module index . \n");
buffer.append(" \n");
Map var27 = this.scriptCache;
synchronized(this.scriptCache) {
String var28 = (String)this.scriptCache.get("/help.html");
if(var28 == null) {
InputStream var29 = LocalUtil.getInternalResourceAsStream("/org/directwebremoting/help.html");
if(var29 == null) {
log.error("Missing file /help.html. Check the dwr.jar file was built to include html files.");
var28 = "Failed to read help text from resource file. Check dwr.jar is built to include html files.
";
} else {
BufferedReader var30 = new BufferedReader(new InputStreamReader(var29));
try {
StringWriter var33 = new StringWriter();
CopyUtils.copy(var30, var33);
var28 = var33.toString();
} catch (IOException var24) {
log.error("Failed to read help text from resource file.", var24);
var28 = "Failed to read help text from resource file.";
}
}
this.scriptCache.put("/help.html", var28);
}
buffer.append(var28);
}
buffer.append("\n");
return buffer.toString();
}
}
/** @deprecated */
@Deprecated
public String generateInterfaceUrl(String root, String scriptName) {
return root + this.interfaceHandlerUrl + scriptName + ".js";
}
/** @deprecated */
@Deprecated
public String generateEngineUrl(String root) {
return root + this.engineHandlerUrl;
}
/** @deprecated */
@Deprecated
public String generateLibraryUrl(String root, String library) {
return root + library;
}
/** @deprecated */
@Deprecated
public Collection getAvailableLibraries() {
if(this.availableLibraries == null) {
this.availableLibraries = Collections.unmodifiableCollection(Arrays.asList(new String[]{this.utilHandlerUrl}));
}
return this.availableLibraries;
}
public void setConverterManager(ConverterManager converterManager) {
this.converterManager = converterManager;
}
public void setModuleManager(ModuleManager moduleManager) {
this.moduleManager = moduleManager;
}
public void setAccessControl(AccessControl accessControl) {
this.accessControl = accessControl;
}
public void setEngineHandlerUrl(String engineHandlerUrl) {
this.engineHandlerUrl = engineHandlerUrl;
}
public void setUtilHandlerUrl(String utilHandlerUrl) {
this.utilHandlerUrl = utilHandlerUrl;
}
public void setTestHandlerUrl(String testHandlerUrl) {
this.testHandlerUrl = testHandlerUrl;
}
public void setInterfaceHandlerUrl(String interfaceHandlerUrl) {
this.interfaceHandlerUrl = interfaceHandlerUrl;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
}