Search in sources :

Example 1 with ObjectStringConverter

use of org.webpieces.router.api.ObjectStringConverter in project webpieces by deanhiller.

the class ObjectToParamTranslator method formMap.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Map<String, String> formMap(Method method, List<String> pathParamNames, Map<String, Object> redirectArgs) {
    if (pathParamNames.size() != redirectArgs.size())
        throw new IllegalReturnValueException("The Redirect object returned from method='" + method + "' has the wrong number of arguments. args.size=" + redirectArgs.size() + " should be size=" + pathParamNames.size());
    Map<String, String> nameToValue = new HashMap<>();
    for (int i = 0; i < pathParamNames.size(); i++) {
        String key = pathParamNames.get(i);
        Object value = redirectArgs.get(key);
        //value can't be null on redirect
        if (value == null)
            throw new IllegalArgumentException("Controller did not set key='" + key + "' or passed in null" + " for '" + key + "' and this is not allowed as you end up with the word 'null' in your url");
        ObjectStringConverter function = translator.getConverterFor(value);
        nameToValue.put(key, function.objectToString(value));
    }
    return nameToValue;
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) HashMap(java.util.HashMap) ObjectStringConverter(org.webpieces.router.api.ObjectStringConverter)

Example 2 with ObjectStringConverter

use of org.webpieces.router.api.ObjectStringConverter in project webpieces by deanhiller.

the class ParamToObjectTranslatorImpl method translate.

private Object translate(RouterRequest req, Method method, ParamNode valuesToUse, Meta fieldMeta, Validation validator) {
    Class<?> fieldClass = fieldMeta.getFieldClass();
    ObjectStringConverter<?> converter = objectTranslator.getConverter(fieldClass);
    if (converter != null) {
        return convert(req, method, valuesToUse, fieldMeta, converter, validator);
    } else if (fieldClass.isArray()) {
        throw new UnsupportedOperationException("not done yet...let me know and I will do it=" + fieldMeta);
    } else if (fieldClass.isEnum()) {
        throw new UnsupportedOperationException("You need to install a " + ObjectStringConverter.class.getSimpleName() + " for this enum " + fieldMeta);
    } else if (List.class.isAssignableFrom(fieldClass)) {
        if (valuesToUse == null)
            return new ArrayList<>();
        else if (valuesToUse instanceof ArrayNode) {
            List<ParamNode> paramNodes = ((ArrayNode) valuesToUse).getList();
            return createList(req, method, fieldMeta, validator, paramNodes);
        } else if (valuesToUse instanceof ValueNode) {
            List<ParamNode> paramNodes = new ArrayList<>();
            paramNodes.add(valuesToUse);
            return createList(req, method, fieldMeta, validator, paramNodes);
        }
        throw new IllegalArgumentException("Found List on field or param=" + fieldMeta + " but did not find ArrayNode type");
    } else if (valuesToUse instanceof ArrayNode) {
        throw new IllegalArgumentException("Incoming array need a type List but instead found type=" + fieldClass + " on field=" + fieldMeta);
    } else if (valuesToUse instanceof ValueNode) {
        ValueNode v = (ValueNode) valuesToUse;
        String fullName = v.getFullName();
        throw new IllegalArgumentException("Could not convert incoming value=" + v.getValue() + " of key name=" + fullName + " field=" + fieldMeta);
    } else if (valuesToUse == null) {
        //validate if null is ok or not
        fieldMeta.validateNullValue();
        return null;
    } else if (!(valuesToUse instanceof ParamTreeNode)) {
        throw new IllegalStateException("Bug, must be missing a case. v=" + valuesToUse + " type to field=" + fieldMeta);
    }
    ParamTreeNode tree = (ParamTreeNode) valuesToUse;
    EntityLookup pluginLookup = fetchPluginLoader(fieldClass);
    Object bean = null;
    if (pluginLookup != null) {
        bean = pluginLookup.find(fieldMeta, tree, c -> createBean(c));
        if (bean == null)
            throw new IllegalStateException("plugin=" + pluginLookup.getClass() + " failed to create bean.  This is a plugin bug");
    } else
        bean = createBean(fieldClass);
    for (Map.Entry<String, ParamNode> entry : tree.entrySet()) {
        String key = entry.getKey();
        ParamNode value = entry.getValue();
        Field field = findBeanFieldType(bean.getClass(), key, new ArrayList<>());
        FieldMeta nextFieldMeta = new FieldMeta(field);
        Object translatedValue = translate(req, method, value, nextFieldMeta, validator);
        nextFieldMeta.setValueOnBean(bean, translatedValue);
    }
    return bean;
}
Also used : ObjectStringConverter(org.webpieces.router.api.ObjectStringConverter) Validation(org.webpieces.ctx.api.Validation) Set(java.util.Set) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Singleton(javax.inject.Singleton) BodyContentBinder(org.webpieces.router.api.BodyContentBinder) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) List(java.util.List) RouterRequest(org.webpieces.ctx.api.RouterRequest) HttpMethod(org.webpieces.ctx.api.HttpMethod) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RequestContext(org.webpieces.ctx.api.RequestContext) EntityLookup(org.webpieces.router.api.EntityLookup) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) NotFoundException(org.webpieces.router.api.exceptions.NotFoundException) Annotation(java.lang.annotation.Annotation) DataMismatchException(org.webpieces.router.api.exceptions.DataMismatchException) Method(java.lang.reflect.Method) ClientDataError(org.webpieces.router.api.exceptions.ClientDataError) EntityLookup(org.webpieces.router.api.EntityLookup) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ObjectStringConverter

use of org.webpieces.router.api.ObjectStringConverter in project webpieces by deanhiller.

the class HibernateLookup method find.

@SuppressWarnings("unchecked")
@Override
public <T> T find(Meta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate) {
    if (!(paramMeta instanceof ParamMeta))
        throw new UnsupportedOperationException("this plugin does not support type=" + paramMeta.getClass());
    ParamMeta m = (ParamMeta) paramMeta;
    Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
    EntityManager entityManager = Em.get();
    Metamodel metamodel = entityManager.getMetamodel();
    ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
    EntityTypeImpl<T> entityType = (EntityTypeImpl<T>) managedType;
    Class<?> idClazz = entityType.getIdType().getJavaType();
    SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
    String name = idAttribute.getName();
    ParamNode paramNode = tree.get(name);
    String value = null;
    if (paramNode != null) {
        if (!(paramNode instanceof ValueNode))
            throw new IllegalStateException("The id field in the hibernate entity should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ".  bad multipart form?  (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
        ValueNode node = (ValueNode) paramNode;
        value = node.getValue();
    }
    if (value == null)
        return beanCreate.apply(paramTypeToCreate);
    @SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
    Object id = unmarshaller.stringToObject(value);
    UseQuery namedQuery = fetchUseQuery(m.getAnnotations());
    if (namedQuery == null)
        return entityManager.find(paramTypeToCreate, id);
    Query query = entityManager.createNamedQuery(namedQuery.value());
    query.setParameter(namedQuery.id(), id);
    return (T) query.getSingleResult();
}
Also used : Query(javax.persistence.Query) ParamNode(org.webpieces.router.impl.params.ParamNode) EntityManager(javax.persistence.EntityManager) ParamMeta(org.webpieces.router.impl.params.ParamMeta) EntityTypeImpl(org.hibernate.metamodel.internal.EntityTypeImpl) ValueNode(org.webpieces.router.impl.params.ValueNode) Metamodel(javax.persistence.metamodel.Metamodel) ObjectStringConverter(org.webpieces.router.api.ObjectStringConverter)

Example 4 with ObjectStringConverter

use of org.webpieces.router.api.ObjectStringConverter in project webpieces by deanhiller.

the class CookieScopeImpl method put.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void put(String key, Object value) {
    hasModifiedData = true;
    ObjectStringConverter marshaller = objectTranslator.getConverterFor(value);
    String strValue = marshaller.objectToString(value);
    cookie.put(key, strValue);
}
Also used : ObjectStringConverter(org.webpieces.router.api.ObjectStringConverter)

Aggregations

ObjectStringConverter (org.webpieces.router.api.ObjectStringConverter)4 HashMap (java.util.HashMap)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 Metamodel (javax.persistence.metamodel.Metamodel)1 EntityTypeImpl (org.hibernate.metamodel.internal.EntityTypeImpl)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 RequestContext (org.webpieces.ctx.api.RequestContext)1