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;
}
;
};
}
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);
}
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;
}
Aggregations