Search in sources :

Example 6 with TypeHintContext

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

the class SignatureParser method parseDeclarationLine.

/**
 * Parse a single declaration line.
 * Where line is defined as being everything in between 2 ; chars.
 * @param line The line to parse
 */
private void parseDeclarationLine(String line) {
    int openBrace = line.indexOf('(');
    int closeBrace = line.indexOf(')');
    if (openBrace == -1) {
        log.error("Missing ( in declaration: " + line);
        return;
    }
    if (closeBrace == -1) {
        log.error("Missing ) in declaration: " + line);
        return;
    }
    if (openBrace > closeBrace) {
        log.error("( Must come before ) in declaration: " + line);
        return;
    }
    // Class name and method name come before the opening (
    String classMethod = line.substring(0, openBrace).trim();
    Method method = findMethod(classMethod);
    if (method == null) {
        // Debug is done by findMethod()
        return;
    }
    // Now we need to get a list of all the parameters
    String paramDecl = line.substring(openBrace + 1, closeBrace);
    String[] paramNames = split(paramDecl);
    // Check that we have the right number
    if (method.getParameterTypes().length != paramNames.length) {
        log.error("Parameter mismatch parsing signatures section in dwr.xml on line: " + line);
        log.info("- Reflected method had: " + method.getParameterTypes().length + " parameters: " + method.toString());
        log.info("- Signatures section had: " + paramNames.length + " parameters");
        log.info("- This can be caused by method overloading which is not supported by Javascript or DWR");
        return;
    }
    for (int i = 0; i < paramNames.length; i++) {
        String[] genericList = getGenericParameterTypeList(paramNames[i]);
        for (int j = 0; j < genericList.length; j++) {
            String type = genericList[j].trim();
            Class clazz = findClass(type);
            if (clazz != null) {
                TypeHintContext thc = new TypeHintContext(converterManager, method, i).createChildContext(j);
                converterManager.setExtraTypeInfo(thc, clazz);
                if (log.isDebugEnabled()) {
                    log.debug("- " + thc + " = " + clazz.getName());
                }
            } else {
                log.warn("Missing class (" + type + ") while parsing signature section on line: " + line);
            }
        }
    }
}
Also used : TypeHintContext(org.directwebremoting.extend.TypeHintContext) Method(java.lang.reflect.Method)

Example 7 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project Gemma by PavlidisLab.

the class CharacteristicConverter method convertInbound.

@SuppressWarnings("unchecked")
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        Map<String, String> tokens = extractInboundTokens(paramType, value);
        Object bean;
        // usually there is a "null" valueUri.
        if (tokens.containsKey("valueUri")) {
            String[] split = ParseUtil.splitInbound(tokens.get("valueUri"));
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            if (StringUtils.isBlank(nested.getValue()) || "null".equals(nested.getValue())) {
                bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
            } else {
                bean = ubic.gemma.model.common.description.VocabCharacteristic.Factory.newInstance();
            }
        } else {
            bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
        }
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        Map<String, Property> properties = getPropertyMapFromObject(bean, false, true);
        for (Iterator<Entry<String, String>> it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, String> entry = it.next();
            String key = entry.getKey();
            String val = entry.getValue();
            Property property = properties.get(key);
            if (property == null) {
                // log.debug( "Fields exist for (" + all + ")." );
                continue;
            }
            Class<?> propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            // Collection
            if ((key.equals("properties")) && (output instanceof ArrayList)) {
                ArrayList<Object> propertyList = (ArrayList<Object>) output;
                output = new HashSet<Object>(propertyList);
            }
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : TypeHintContext(org.directwebremoting.extend.TypeHintContext) ArrayList(java.util.ArrayList) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Entry(java.util.Map.Entry) MarshallException(org.directwebremoting.extend.MarshallException) Property(org.directwebremoting.extend.Property) Map(java.util.Map)

Example 8 with TypeHintContext

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

the class MapConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new IllegalArgumentException(Messages.getString("MapConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new IllegalArgumentException(Messages.getString("MapConverter.FormatError", ProtocolConstants.INBOUND_MAP_END));
    }
    value = value.substring(1, value.length() - 1);
    try {
        // Maybe we ought to check that the paramType isn't expecting a more
        // distinct type of Map and attempt to create that?
        Map map;
        // If paramType is concrete then just use whatever we've got.
        if (!paramType.isInterface() && !Modifier.isAbstract(paramType.getModifiers())) {
            // If there is a problem creating the type then we have no way
            // of completing this - they asked for a specific type and we
            // can't create that type. I don't know of a way of finding
            // subclasses that might be instaniable so we accept failure.
            map = (Map) paramType.newInstance();
        } else {
            map = new HashMap();
        }
        // Get the extra type info
        TypeHintContext thc = inctx.getCurrentTypeHintContext();
        TypeHintContext keyThc = thc.createChildContext(0);
        Class keyType = keyThc.getExtraTypeInfo();
        TypeHintContext valThc = thc.createChildContext(1);
        Class valType = valThc.getExtraTypeInfo();
        // We should put the new object into the working map in case it
        // is referenced later nested down in the conversion process.
        inctx.addConverted(iv, paramType, map);
        InboundContext incx = iv.getLookup();
        // Loop through the property declarations
        StringTokenizer st = new StringTokenizer(value, ",");
        int size = st.countTokens();
        for (int i = 0; i < size; i++) {
            String token = st.nextToken();
            if (token.trim().length() == 0) {
                continue;
            }
            int colonpos = token.indexOf(ProtocolConstants.INBOUND_MAP_ENTRY);
            if (colonpos == -1) {
                throw new MarshallException(paramType, Messages.getString("MapConverter.MissingSeparator", ProtocolConstants.INBOUND_MAP_ENTRY, token));
            }
            // Convert the value part of the token by splitting it into the
            // type and value (as passed in by Javascript)
            String valStr = token.substring(colonpos + 1).trim();
            String[] splitIv = ParseUtil.splitInbound(valStr);
            String splitIvValue = splitIv[LocalUtil.INBOUND_INDEX_VALUE];
            String splitIvType = splitIv[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable valIv = new InboundVariable(incx, null, splitIvType, splitIvValue);
            Object val = config.convertInbound(valType, valIv, inctx, valThc);
            // Keys (unlike values) do not have type info passed with them
            // Could we have recurrsive key? - I don't think so because keys
            // must be strings in Javascript
            String keyStr = token.substring(0, colonpos).trim();
            // String[] keySplit = LocalUtil.splitInbound(keyStr);
            // InboundVariable keyIv = new InboundVariable(incx, splitIv[LocalUtil.INBOUND_INDEX_TYPE], splitIv[LocalUtil.INBOUND_INDEX_VALUE]);
            InboundVariable keyIv = new InboundVariable(incx, null, ProtocolConstants.TYPE_STRING, keyStr);
            Object key = config.convertInbound(keyType, keyIv, inctx, keyThc);
            map.put(key, val);
        }
        return map;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : TypeHintContext(org.directwebremoting.extend.TypeHintContext) HashMap(java.util.HashMap) InboundContext(org.directwebremoting.extend.InboundContext) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) StringTokenizer(java.util.StringTokenizer) MarshallException(org.directwebremoting.extend.MarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with TypeHintContext

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

the class BaseCallMarshaller method marshallInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.extend.Marshaller#marshallInbound(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
public Calls marshallInbound(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
    // We must parse the parameters before we setup the conduit because it's
    // only after doing this that we know the scriptSessionId
    WebContext webContext = WebContextFactory.get();
    Batch batch = (Batch) request.getAttribute(ATTRIBUTE_BATCH);
    if (batch == null) {
        batch = new Batch(request, crossDomainSessionSecurity, allowGetForSafariButMakeForgeryEasier, sessionCookieName);
        // Save calls for retry exception
        request.setAttribute(ATTRIBUTE_BATCH, batch);
    }
    // Various bits of the Batch need to be stashed away places
    storeParsedRequest(request, webContext, batch);
    Calls calls = batch.getCalls();
    // Debug the environment
    if (log.isDebugEnabled() && calls.getCallCount() > 0) {
        // We can just use 0 because they are all shared
        InboundContext inctx = (InboundContext) batch.getInboundContexts().get(0);
        StringBuffer buffer = new StringBuffer();
        for (Iterator it = inctx.getInboundVariableNames(); it.hasNext(); ) {
            String key = (String) it.next();
            InboundVariable value = inctx.getInboundVariable(key);
            if (key.startsWith(ProtocolConstants.INBOUND_CALLNUM_PREFIX) && key.indexOf(ProtocolConstants.INBOUND_CALLNUM_SUFFIX + ProtocolConstants.INBOUND_KEY_ENV) != -1) {
                buffer.append(key);
                buffer.append('=');
                buffer.append(value.toString());
                buffer.append(", ");
            }
        }
        if (buffer.length() > 0) {
            log.debug("Environment:  " + buffer.toString());
        }
    }
    callLoop: for (int callNum = 0; callNum < calls.getCallCount(); callNum++) {
        Call call = calls.getCall(callNum);
        InboundContext inctx = (InboundContext) batch.getInboundContexts().get(callNum);
        // Get a list of the available matching methods with the coerced
        // parameters that we will use to call it if we choose to use
        // that method.
        Creator creator = creatorManager.getCreator(call.getScriptName());
        // Which method are we using?
        Method method = findMethod(call, inctx);
        if (method == null) {
            String name = call.getScriptName() + '.' + call.getMethodName();
            String error = Messages.getString("BaseCallMarshaller.UnknownMethod", name);
            log.warn("Marshalling exception: " + error);
            call.setMethod(null);
            call.setParameters(null);
            call.setException(new IllegalArgumentException(error));
            continue callLoop;
        }
        call.setMethod(method);
        // Check this method is accessible
        accessControl.assertExecutionIsPossible(creator, call.getScriptName(), method);
        // Convert all the parameters to the correct types
        Object[] params = new Object[method.getParameterTypes().length];
        for (int j = 0; j < method.getParameterTypes().length; j++) {
            try {
                Class paramType = method.getParameterTypes()[j];
                InboundVariable param = inctx.getParameter(callNum, j);
                TypeHintContext incc = new TypeHintContext(converterManager, method, j);
                params[j] = converterManager.convertInbound(paramType, param, inctx, incc);
            } catch (MarshallException ex) {
                log.warn("Marshalling exception", ex);
                call.setMethod(null);
                call.setParameters(null);
                call.setException(ex);
                continue callLoop;
            }
        }
        call.setParameters(params);
    }
    return calls;
}
Also used : Call(org.directwebremoting.extend.Call) WebContext(org.directwebremoting.WebContext) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundContext(org.directwebremoting.extend.InboundContext) Calls(org.directwebremoting.extend.Calls) InboundVariable(org.directwebremoting.extend.InboundVariable) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator)

Aggregations

TypeHintContext (org.directwebremoting.extend.TypeHintContext)9 InboundVariable (org.directwebremoting.extend.InboundVariable)7 MarshallException (org.directwebremoting.extend.MarshallException)7 Map (java.util.Map)5 Iterator (java.util.Iterator)4 Property (org.directwebremoting.extend.Property)4 Method (java.lang.reflect.Method)3 StringTokenizer (java.util.StringTokenizer)3 TreeMap (java.util.TreeMap)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DynaClass (org.apache.commons.beanutils.DynaClass)2 Creator (org.directwebremoting.extend.Creator)2 InboundContext (org.directwebremoting.extend.InboundContext)2 FlexClass (org.jaffa.flexfields.FlexClass)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1