Search in sources :

Example 1 with ParamParser

use of ninja.params.ParamParser in project ninja by ninjaframework.

the class FileItemProviderImpl2 method init.

public void init(final Class<? extends FileItemProvider> fileItemProviderClass) throws IOException, ServletException {
    //default setup for httpServlet request.
    //According to servlet spec the following will be returned:
    when(httpServletRequest.getContextPath()).thenReturn("");
    when(httpServletRequest.getRequestURI()).thenReturn("/");
    when(httpServletRequest.getContentType()).thenReturn("multipart/form-data");
    when(httpServletRequest.getMethod()).thenReturn("POST");
    NinjaPropertiesImpl properties = new NinjaPropertiesImpl(NinjaMode.test);
    this.ninjaProperties = properties;
    final FileItemIterator fileItemIterator = makeFileItemsIterator();
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(NinjaProperties.class).toInstance(ninjaProperties);
            bind(FileItemProvider.class).to(fileItemProviderClass);
        }
    });
    context = new NinjaServletContext(bodyParserEngineManager, flashCookie, ninjaProperties, resultHandler, sessionCookie, validation, injector, new ParamParsers(new HashSet<ParamParser>())) {

        public FileItemIterator getFileItemIterator() {
            return fileItemIterator;
        }

        ;
    };
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) ParamParser(ninja.params.ParamParser) ParamParsers(ninja.params.ParamParsers) Injector(com.google.inject.Injector) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) AbstractModule(com.google.inject.AbstractModule)

Example 2 with ParamParser

use of ninja.params.ParamParser in project ninja by ninjaframework.

the class BodyParserEnginePostTest method setUp.

@Before
public void setUp() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(NinjaProperties.class).toInstance(new NinjaPropertiesImpl(NinjaMode.test));
            Multibinder<ParamParser> parsersBinder = Multibinder.newSetBinder(binder(), ParamParser.class);
            parsersBinder.addBinding().to(NeedingInjectionParamParser.class);
        }
    });
    validation = new ValidationImpl();
    Mockito.when(this.context.getValidation()).thenReturn(this.validation);
    bodyParserEnginePost = injector.getInstance(BodyParserEnginePost.class);
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) ParamParser(ninja.params.ParamParser) NeedingInjectionParamParser(ninja.params.ControllerMethodInvokerTest.NeedingInjectionParamParser) Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) NeedingInjectionParamParser(ninja.params.ControllerMethodInvokerTest.NeedingInjectionParamParser) AbstractModule(com.google.inject.AbstractModule) ValidationImpl(ninja.validation.ValidationImpl) Before(org.junit.Before)

Example 3 with ParamParser

use of ninja.params.ParamParser in project ninja by ninjaframework.

the class BodyParserEnginePost method invoke.

// Allows to instantiate inner objects with a prefix for each parameter key
private <T> T invoke(Context context, Map<String, String[]> parameters, Class<T> classOfT, String paramPrefix) {
    T t = null;
    try {
        t = classOfT.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        logger.error("Can't create new instance of class {}", classOfT.getName(), e);
        return null;
    }
    for (String declaredField : getAllDeclaredFieldsAsStringSet(classOfT)) {
        try {
            Field field = classOfT.getDeclaredField(declaredField);
            Class<?> fieldType = field.getType();
            field.setAccessible(true);
            if (parameters.containsKey(paramPrefix + declaredField)) {
                String[] values = parameters.get(paramPrefix + declaredField);
                if (Collection.class.isAssignableFrom(fieldType) || List.class.isAssignableFrom(fieldType)) {
                    ListParamParser<?> parser = (ListParamParser<?>) paramParsers.getListParser(getGenericType(field));
                    if (parser == null) {
                        logger.warn("No parser defined for a collection of type {}", getGenericType(field).getCanonicalName());
                    } else {
                        field.set(t, parser.parseParameter(field.getName(), values, context.getValidation()));
                    }
                } else if (fieldType.isArray()) {
                    ArrayParamParser<?> parser = paramParsers.getArrayParser(fieldType);
                    if (parser == null) {
                        logger.warn("No parser defined for an array of type {}", fieldType.getComponentType().getCanonicalName());
                    } else {
                        field.set(t, parser.parseParameter(field.getName(), values, context.getValidation()));
                    }
                } else {
                    ParamParser<?> parser = (ParamParser<?>) paramParsers.getParamParser(fieldType);
                    if (parser == null) {
                        logger.warn("No parser defined for type {}", fieldType.getCanonicalName());
                    } else {
                        field.set(t, parser.parseParameter(field.getName(), values[0], context.getValidation()));
                    }
                }
            } else {
                // Check if we have one parameter key corresponding to one valued inner attribute of this object field
                for (String parameter : parameters.keySet()) {
                    if (parameter.startsWith(paramPrefix + declaredField + ".")) {
                        if (isEmptyParameter(parameters.get(parameter))) {
                            field.set(t, invoke(context, parameters, fieldType, paramPrefix + declaredField + "."));
                            break;
                        }
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            logger.warn("Error parsing incoming Post request into class {}. Key {} and value {}.", classOfT.getName(), paramPrefix + declaredField, parameters.get(paramPrefix + declaredField), e);
        }
    }
    return t;
}
Also used : ListParamParser(ninja.params.ParamParsers.ListParamParser) ParamParser(ninja.params.ParamParser) ArrayParamParser(ninja.params.ParamParsers.ArrayParamParser) ListParamParser(ninja.params.ParamParsers.ListParamParser) Field(java.lang.reflect.Field) Collection(java.util.Collection) List(java.util.List) ArrayParamParser(ninja.params.ParamParsers.ArrayParamParser)

Aggregations

ParamParser (ninja.params.ParamParser)3 AbstractModule (com.google.inject.AbstractModule)2 Injector (com.google.inject.Injector)2 NinjaPropertiesImpl (ninja.utils.NinjaPropertiesImpl)2 Multibinder (com.google.inject.multibindings.Multibinder)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 List (java.util.List)1 NeedingInjectionParamParser (ninja.params.ControllerMethodInvokerTest.NeedingInjectionParamParser)1 ParamParsers (ninja.params.ParamParsers)1 ArrayParamParser (ninja.params.ParamParsers.ArrayParamParser)1 ListParamParser (ninja.params.ParamParsers.ListParamParser)1 ValidationImpl (ninja.validation.ValidationImpl)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 Before (org.junit.Before)1