use of ninja.Result in project ninja by ninjaframework.
the class TemplateEngineFreemarkerI18nMethodTest method testThatSingleKeyWithValueWorks.
@Test
public void testThatSingleKeyWithValueWorks() throws Exception {
Optional<Result> resultOptional = Optional.of(result);
Mockito.when(messages.get("my.message.key", context, resultOptional)).thenReturn(Optional.of("This simulates the translated message!"));
List args = new ArrayList();
args.add(new SimpleScalar("my.message.key"));
TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message!"));
Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
use of ninja.Result in project ninja by ninjaframework.
the class PrettyTimeController method index.
public Result index(Context context) {
// Only render the page. It contains some language specific strings.
// It will use the requested language (or a fallback language)
// from Accept-Language header
Result result = Results.html();
// just in case we set the language => we remove it...
lang.clearLanguage(result);
// 25 works for summertime, too.
LocalDateTime localDateTime = LocalDateTime.now().minusHours(25);
result.render("date", new Date(localDateTime.toDateTime().getMillis()));
return result;
}
use of ninja.Result in project ninja by ninjaframework.
the class UploadController method uploadFinish.
/**
* This upload method expects a file and simply displays the file in the
* multipart upload again to the user (in the correct mime encoding).
*
* @param context
* @return
* @throws Exception
*/
public Result uploadFinish(Context context) throws Exception {
// we are using a renderable inner class to stream the input again to
// the user
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
try {
// make sure the context really is a multipart context...
if (context.isMultipart()) {
// This is the iterator we can use to iterate over the
// contents
// of the request.
FileItemIterator fileItemIterator = context.getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
String contentType = item.getContentType();
if (contentType != null) {
result.contentType(contentType);
} else {
contentType = mimeTypes.getMimeType(name);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
ByteStreams.copy(stream, responseStreams.getOutputStream());
}
}
}
} catch (IOException | FileUploadException exception) {
throw new InternalServerErrorException(exception);
}
}
};
return new Result(200).render(renderable);
}
use of ninja.Result in project ninja by ninjaframework.
the class ApplicationController method flashAny.
@Timed
public Result flashAny(Context context, FlashScope flashScope) {
Result result = Results.html();
// sets a 18n flash message and adds a timestamp to make sure formatting works
Optional<String> flashMessage = messages.get("flashAny", context, Optional.of(result), "PLACEHOLDER");
if (flashMessage.isPresent()) {
flashScope.put("any", flashMessage.get());
}
return result;
}
use of ninja.Result in project ninja by ninjaframework.
the class ApplicationController method flashError.
@Timed
public Result flashError(Context context, FlashScope flashScope) {
Result result = Results.html();
// sets a 18n flash message and adds a timestamp to make sure formatting works
Optional<String> flashMessage = messages.get("flashError", context, Optional.of(result), "PLACEHOLDER");
if (flashMessage.isPresent()) {
flashScope.error(flashMessage.get());
}
return result;
}
Aggregations