Search in sources :

Example 1 with Creator

use of org.directwebremoting.extend.Creator in project ma-core-public by infiniteautomation.

the class SpringConfigurator method configure.

/* (non-Javadoc)
     * @see org.directwebremoting.Configurator#configure(org.directwebremoting.Container)
     */
public void configure(Container container) {
    AccessControl accessControl = (AccessControl) container.getBean(AccessControl.class.getName());
    AjaxFilterManager ajaxFilterManager = (AjaxFilterManager) container.getBean(AjaxFilterManager.class.getName());
    ConverterManager converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName());
    CreatorManager creatorManager = (CreatorManager) container.getBean(CreatorManager.class.getName());
    // Configure the creator types
    if (creatorTypes != null) {
        for (Iterator it = creatorTypes.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String typeName = (String) entry.getKey();
            String className = (String) entry.getValue();
            creatorManager.addCreatorType(typeName, className);
        }
    }
    // Configure the converter types
    if (converterTypes != null) {
        for (Iterator it = converterTypes.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String typeName = (String) entry.getKey();
            String className = (String) entry.getValue();
            converterManager.addConverterType(typeName, className);
        }
    }
    // Configure the creators
    if (creators != null) {
        try {
            for (Iterator it = creators.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry entry = (Map.Entry) it.next();
                String scriptName = (String) entry.getKey();
                CreatorConfig creatorConfig = (CreatorConfig) entry.getValue();
                if (creatorConfig.getCreator() != null) {
                    Creator creator = creatorConfig.getCreator();
                    creatorManager.addCreator(scriptName, creator);
                } else {
                    String creatorName = creatorConfig.getCreatorType();
                    Map params = creatorConfig.getParams();
                    creatorManager.addCreator(scriptName, creatorName, params);
                }
                List excludes = creatorConfig.getExcludes();
                for (Iterator eit = excludes.iterator(); eit.hasNext(); ) {
                    String exclude = (String) eit.next();
                    accessControl.addExcludeRule(scriptName, exclude);
                }
                List includes = creatorConfig.getIncludes();
                for (Iterator iit = includes.iterator(); iit.hasNext(); ) {
                    String include = (String) iit.next();
                    accessControl.addIncludeRule(scriptName, include);
                }
                Properties auth = creatorConfig.getAuth();
                for (Iterator ait = auth.entrySet().iterator(); ait.hasNext(); ) {
                    Map.Entry aentry = (Map.Entry) ait.next();
                    String methodName = (String) aentry.getKey();
                    String role = (String) aentry.getValue();
                    accessControl.addRoleRestriction(scriptName, methodName, role);
                }
                List filters = creatorConfig.getFilters();
                for (Iterator fit = filters.iterator(); fit.hasNext(); ) {
                    Object obj = fit.next();
                    if (obj instanceof String) {
                        String filterName = (String) obj;
                        AjaxFilter filter = (AjaxFilter) LocalUtil.classNewInstance(filterName, filterName, AjaxFilter.class);
                        if (filter != null) {
                            ajaxFilterManager.addAjaxFilter(filter, scriptName);
                        }
                    } else if (obj instanceof AjaxFilter) {
                        AjaxFilter filter = (AjaxFilter) obj;
                        ajaxFilterManager.addAjaxFilter(filter, scriptName);
                    } else {
                        throw new IllegalArgumentException(Messages.getString("SpringConfigurator.InvalidFilter", scriptName, obj));
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IllegalArgumentException(ex.toString());
        }
    }
    // Configure the converters
    if (converters != null) {
        try {
            for (Iterator it = converters.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry entry = (Map.Entry) it.next();
                String match = (String) entry.getKey();
                ConverterConfig converterConfig = (ConverterConfig) entry.getValue();
                Map params = converterConfig.getParams();
                if (converterConfig.getIncludes().size() > 0) {
                    params.put("include", StringUtils.collectionToCommaDelimitedString(converterConfig.getIncludes()));
                }
                if (converterConfig.getExcludes().size() > 0) {
                    params.put("exclude", StringUtils.collectionToCommaDelimitedString(converterConfig.getExcludes()));
                }
                // params.put("force", Boolean.valueOf(converterConfig.isForce()));
                if (StringUtils.hasText(converterConfig.getJavascriptClassName())) {
                    params.put("javascript", converterConfig.getJavascriptClassName());
                }
                converterManager.addConverter(match, converterConfig.getType(), params);
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException(Messages.getString("SpringConfigurator.ConfigureConverterError"));
        }
    }
    // Configure the signatures
    if (StringUtils.hasText(signatures)) {
        SignatureParser sigp = new SignatureParser(converterManager, creatorManager);
        sigp.parse(signatures);
    }
}
Also used : Creator(org.directwebremoting.extend.Creator) Properties(java.util.Properties) AccessControl(org.directwebremoting.extend.AccessControl) AjaxFilterManager(org.directwebremoting.extend.AjaxFilterManager) ConverterManager(org.directwebremoting.extend.ConverterManager) Iterator(java.util.Iterator) List(java.util.List) CreatorManager(org.directwebremoting.extend.CreatorManager) Map(java.util.Map) SignatureParser(org.directwebremoting.impl.SignatureParser) AjaxFilter(org.directwebremoting.AjaxFilter)

Example 2 with Creator

use of org.directwebremoting.extend.Creator in project ma-core-public by infiniteautomation.

the class DefaultCreatorManager method addCreator.

/* (non-Javadoc)
     * @see org.directwebremoting.CreatorManager#addCreator(java.lang.String, java.lang.String, java.util.Map)
     */
public void addCreator(String scriptName, String typeName, Map params) throws InstantiationException, IllegalAccessException, IllegalArgumentException {
    if (!LocalUtil.isJavaIdentifier(scriptName)) {
        log.error("Illegal identifier: '" + scriptName + "'");
        return;
    }
    Class clazz = (Class) creatorTypes.get(typeName);
    if (clazz == null) {
        log.error("Missing creator: " + typeName + " (while initializing creator for: " + scriptName + ".js)");
        return;
    }
    Creator creator = (Creator) clazz.newInstance();
    LocalUtil.setParams(creator, params, ignore);
    creator.setProperties(params);
    // add the creator for the script name
    addCreator(scriptName, creator);
}
Also used : Creator(org.directwebremoting.extend.Creator)

Example 3 with Creator

use of org.directwebremoting.extend.Creator in project ma-core-public by infiniteautomation.

the class DefaultCreatorManager method getCreator.

/* (non-Javadoc)
     * @see org.directwebremoting.CreatorManager#getCreator(java.lang.String)
     */
public Creator getCreator(String scriptName) throws SecurityException {
    Creator creator = (Creator) creators.get(scriptName);
    if (creator == null) {
        StringBuffer buffer = new StringBuffer("Names of known classes are: ");
        for (Iterator it = creators.keySet().iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            buffer.append(key);
            buffer.append(' ');
        }
        log.warn(buffer.toString());
        throw new SecurityException(Messages.getString("DefaultCreatorManager.MissingName", scriptName));
    }
    return creator;
}
Also used : Iterator(java.util.Iterator) Creator(org.directwebremoting.extend.Creator)

Example 4 with Creator

use of org.directwebremoting.extend.Creator in project ma-core-public by infiniteautomation.

the class DefaultDebugPageGenerator method generateIndexPage.

/* (non-Javadoc)
     * @see org.directwebremoting.DebugPageGenerator#generateIndexPage(java.lang.String)
     */
public String generateIndexPage(final String root) throws SecurityException {
    if (!creatorManager.isDebug()) {
        log.warn("Failed attempt to access test pages outside of debug mode. Set the debug init-parameter to true to enable.");
        throw new SecurityException(Messages.getString("DefaultDebugPageGenerator.AccessDenied"));
    }
    StringBuffer buffer = new StringBuffer();
    buffer.append("<html>\n");
    buffer.append("<head><title>DWR Test Index</title></head>\n");
    buffer.append("<body>\n");
    buffer.append("<h2>Classes known to DWR:</h2>\n");
    buffer.append("<ul>\n");
    for (Iterator it = creatorManager.getCreatorNames().iterator(); it.hasNext(); ) {
        String name = (String) it.next();
        Creator creator = creatorManager.getCreator(name);
        buffer.append("<li><a href='");
        buffer.append(root);
        buffer.append(testHandlerUrl);
        buffer.append(name);
        buffer.append("'>");
        buffer.append(name);
        buffer.append("</a> (");
        buffer.append(creator.getType().getName());
        buffer.append(")</li>\n");
    }
    buffer.append("</ul>\n");
    buffer.append("</body></html>\n");
    return buffer.toString();
}
Also used : Iterator(java.util.Iterator) Creator(org.directwebremoting.extend.Creator)

Example 5 with Creator

use of org.directwebremoting.extend.Creator in project ma-core-public by infiniteautomation.

the class DefaultDebugPageGenerator method generateTestPage.

/* (non-Javadoc)
     * @see org.directwebremoting.DebugPageGenerator#generateTestPage(java.lang.String, java.lang.String)
     */
public String generateTestPage(final String root, final String scriptName) throws SecurityException {
    if (!creatorManager.isDebug()) {
        log.warn("Failed attempt to access test pages outside of debug mode. Set the debug init-parameter to true to enable.");
        throw new SecurityException(Messages.getString("DefaultAccessControl.AccessDenied"));
    }
    String interfaceURL = root + interfaceHandlerUrl + scriptName + PathConstants.EXTENSION_JS;
    String engineURL = root + engineHandlerUrl;
    String utilURL = root + utilHandlerUrl;
    String proxyInterfaceURL = PATH_UP + interfaceHandlerUrl + scriptName + PathConstants.EXTENSION_JS;
    String proxyEngineURL = PATH_UP + engineHandlerUrl;
    String proxyUtilURL = PATH_UP + utilHandlerUrl;
    Creator creator = creatorManager.getCreator(scriptName);
    Method[] methods = creator.getType().getMethods();
    StringBuffer buffer = new StringBuffer();
    buffer.append("<html>\n");
    buffer.append("<head>\n");
    buffer.append("  <title>DWR Test</title>\n");
    buffer.append("  <!-- These paths use .. so that they still work behind a path mapping proxy. The fully qualified version is more cut and paste friendly. -->\n");
    buffer.append("  <script type='text/javascript' src='" + proxyInterfaceURL + "'></script>\n");
    buffer.append("  <script type='text/javascript' src='" + proxyEngineURL + "'></script>\n");
    buffer.append("  <script type='text/javascript' src='" + proxyUtilURL + "'></script>\n");
    buffer.append("  <script type='text/javascript'>\n");
    buffer.append("  function objectEval(text)\n");
    buffer.append("  {\n");
    buffer.append("    // eval() breaks when we use it to get an object using the { a:42, b:'x' }\n");
    buffer.append("    // syntax because it thinks that { and } surround a block and not an object\n");
    buffer.append("    // So we wrap it in an array and extract the first element to get around\n");
    buffer.append("    // this.\n");
    buffer.append("    // This code is only needed for interpreting the parameter input fields,\n");
    buffer.append("    // so you can ignore this for normal use.\n");
    buffer.append("    // The regex = [start of line][whitespace]{[stuff]}[whitespace][end of line]\n");
    buffer.append("    text = text.replace(/\\n/g, ' ');\n");
    buffer.append("    text = text.replace(/\\r/g, ' ');\n");
    buffer.append("    if (text.match(/^\\s*\\{.*\\}\\s*$/))\n");
    buffer.append("    {\n");
    buffer.append("      text = '[' + text + '][0]';\n");
    buffer.append("    }\n");
    buffer.append("    return eval(text);\n");
    buffer.append("  }\n");
    buffer.append("  </script>\n");
    buffer.append("  <style>\n");
    buffer.append("    input.itext { font-size: smaller; background: #E4E4E4; border: 0; }\n");
    buffer.append("    input.ibutton { font-size: xx-small; border: 1px outset; margin: 0px; padding: 0px; }\n");
    buffer.append("    span.reply { background: #ffffdd; white-space: pre; }\n");
    buffer.append("    span.warning { font-size: smaller; color: red; }\n");
    buffer.append("  </style>\n");
    buffer.append("</head>\n");
    buffer.append("<body onload='dwr.util.useLoadingMessage()'>\n");
    buffer.append(BLANK);
    buffer.append("<h2>Methods For: " + scriptName + " (" + creator.getType().getName() + ")</h2>\n");
    buffer.append("<p>To use this class in your javascript you will need the following script includes:</p>\n");
    buffer.append("<pre>\n");
    buffer.append("  &lt;script type='text/javascript' src='<a href='" + interfaceURL + "'>" + interfaceURL + "</a>'&gt;&lt;/script&gt;\n");
    buffer.append("  &lt;script type='text/javascript' src='<a href='" + engineURL + "'>" + engineURL + "</a>'&gt;&lt;/script&gt;\n");
    buffer.append("</pre>\n");
    buffer.append("<p>In addition there is an optional utility script:</p>\n");
    buffer.append("<pre>\n");
    buffer.append("  &lt;script type='text/javascript' src='<a href='" + utilURL + "'>" + utilURL + "</a>'&gt;&lt;/script&gt;\n");
    buffer.append("</pre>\n");
    buffer.append("<p>Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.<br/>\n");
    buffer.append("The inputs are evaluated as Javascript so strings must be quoted before execution.</p>\n");
    buffer.append("<p>There are " + methods.length + " declared methods:</p><ul>\n");
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        // Is it on the list of banned names
        if (JavascriptUtil.isReservedWord(methodName)) {
            buffer.append(BLANK);
            buffer.append("<li style='color: #88A;'>" + methodName + "() is not available because it is a reserved word.</li>\n");
            continue;
        }
        buffer.append(BLANK);
        buffer.append("<li>\n");
        buffer.append("  " + methodName + '(');
        Class[] paramTypes = method.getParameterTypes();
        for (int j = 0; j < paramTypes.length; j++) {
            Class paramType = paramTypes[j];
            // The special type that we handle transparently
            if (LocalUtil.isServletClass(paramType)) {
                buffer.append("AUTO");
            } else {
                String value = BLANK;
                if (paramType == String.class) {
                    value = "\"\"";
                } else if (paramType == Boolean.class || paramType == Boolean.TYPE) {
                    value = "true";
                } else if (paramType == Integer.class || paramType == Integer.TYPE || paramType == Short.class || paramType == Short.TYPE || paramType == Long.class || paramType == Long.TYPE || paramType == Byte.class || paramType == Byte.TYPE) {
                    value = "0";
                } else if (paramType == Float.class || paramType == Float.TYPE || paramType == Double.class || paramType == Double.TYPE) {
                    value = "0.0";
                } else if (paramType.isArray() || Collection.class.isAssignableFrom(paramType)) {
                    value = "[]";
                } else if (Map.class.isAssignableFrom(paramType)) {
                    value = "{}";
                }
                buffer.append("    <input class='itext' type='text' size='10' value='" + value + "' id='p" + i + j + "' title='Will be converted to: " + paramType.getName() + "'/>");
            }
            buffer.append(j == paramTypes.length - 1 ? BLANK : ", \n");
        }
        buffer.append("  );\n");
        String onclick = scriptName + '.' + methodName + "(";
        for (int j = 0; j < paramTypes.length; j++) {
            if (!LocalUtil.isServletClass(paramTypes[j])) {
                onclick += "objectEval($(\"p" + i + j + "\").value), ";
            }
        }
        onclick += "reply" + i + ");";
        buffer.append("  <input class='ibutton' type='button' onclick='" + onclick + "' value='Execute'  title='Calls " + scriptName + '.' + methodName + "(). View source for details.'/>\n");
        buffer.append("  <script type='text/javascript'>\n");
        buffer.append("    var reply" + i + " = function(data)\n");
        buffer.append("    {\n");
        buffer.append("      if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));\n");
        buffer.append("      else dwr.util.setValue('d" + i + "', dwr.util.toDescriptiveString(data, 1));\n");
        buffer.append("    }\n");
        buffer.append("  </script>\n");
        buffer.append("  <span id='d" + i + "' class='reply'></span>\n");
        // Print a warning if this method is overloaded
        boolean overloaded = false;
        for (int j = 0; j < methods.length; j++) {
            if (j != i && methods[j].getName().equals(methodName)) {
                overloaded = true;
            }
        }
        if (overloaded) {
            buffer.append("<br/><span class='warning'>(Warning: overloaded methods are not recommended. See <a href='#overloadedMethod'>below</a>)</span>\n");
        }
        // Print a warning if the method uses un-marshallable types
        for (int j = 0; j < paramTypes.length; j++) {
            if (!converterManager.isConvertable(paramTypes[j])) {
                buffer.append("<br/><span class='warning'>(Warning: No Converter for " + paramTypes[j].getName() + ". See <a href='#missingConverter'>below</a>)</span>\n");
            }
        }
        if (!converterManager.isConvertable(method.getReturnType())) {
            buffer.append("<br/><span class='warning'>(Warning: No Converter for " + method.getReturnType().getName() + ". See <a href='#missingConverter'>below</a>)</span>\n");
        }
        // See also the call to getReasonToNotExecute() above
        try {
            accessControl.assertIsDisplayable(creator, scriptName, method);
        } catch (SecurityException ex) {
            buffer.append("<br/><span class='warning'>(Warning: " + methodName + "() is excluded: " + ex.getMessage() + ". See <a href='#excludedMethod'>below</a>)</span>\n");
        }
        // We don't need to call assertExecutionIsPossible() because those
        // checks should be done by assertIsDisplayable() above
        // accessControl.assertExecutionIsPossible(creator, scriptName, method);
        buffer.append("</li>\n");
    }
    buffer.append(BLANK);
    buffer.append("</ul>\n");
    buffer.append("<h2>Other Links</h2>\n");
    buffer.append("<ul>\n");
    buffer.append("<li>Back to <a href='" + root + "/'>class index</a>.</li>\n");
    buffer.append("</ul>\n");
    synchronized (scriptCache) {
        String output = (String) scriptCache.get(PathConstants.FILE_HELP);
        if (output == null) {
            InputStream raw = getClass().getResourceAsStream(DwrConstants.PACKAGE + PathConstants.FILE_HELP);
            if (raw == null) {
                log.error(Messages.getString("DefaultProcessor.MissingHelp", PathConstants.FILE_HELP));
                output = "<p>Failed to read help text from resource file. Check dwr.jar is built to include html files.</p>";
            } else {
                StringBuffer fileBuffer = new StringBuffer();
                BufferedReader in = new BufferedReader(new InputStreamReader(raw));
                while (true) {
                    try {
                        String line = in.readLine();
                        if (line == null) {
                            break;
                        }
                        fileBuffer.append(line);
                        fileBuffer.append('\n');
                    } catch (IOException ex) {
                        fileBuffer.append(ex.toString());
                        fileBuffer.append('\n');
                        break;
                    }
                }
                output = fileBuffer.toString();
            }
            scriptCache.put(PathConstants.FILE_HELP, output);
        }
        buffer.append(output);
    }
    buffer.append("</body></html>\n");
    return buffer.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Creator (org.directwebremoting.extend.Creator)14 Iterator (java.util.Iterator)8 Method (java.lang.reflect.Method)6 Map (java.util.Map)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 WebContext (org.directwebremoting.WebContext)3 CreatorManager (org.directwebremoting.extend.CreatorManager)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 AjaxFilter (org.directwebremoting.AjaxFilter)2 AccessControl (org.directwebremoting.extend.AccessControl)2 AjaxFilterManager (org.directwebremoting.extend.AjaxFilterManager)2 ConverterManager (org.directwebremoting.extend.ConverterManager)2 TypeHintContext (org.directwebremoting.extend.TypeHintContext)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1