Search in sources :

Example 6 with Result

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());
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TemplateModel(freemarker.template.TemplateModel) SimpleScalar(freemarker.template.SimpleScalar) Result(ninja.Result) Test(org.junit.Test)

Example 7 with Result

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;
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Date(java.util.Date) Result(ninja.Result)

Example 8 with 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);
}
Also used : Context(ninja.Context) Renderable(ninja.Renderable) ResponseStreams(ninja.utils.ResponseStreams) FileItemStream(org.apache.commons.fileupload.FileItemStream) InputStream(java.io.InputStream) InternalServerErrorException(ninja.exceptions.InternalServerErrorException) IOException(java.io.IOException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException) Result(ninja.Result)

Example 9 with Result

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;
}
Also used : Result(ninja.Result) Timed(ninja.metrics.Timed)

Example 10 with 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;
}
Also used : Result(ninja.Result) Timed(ninja.metrics.Timed)

Aggregations

Result (ninja.Result)37 Test (org.junit.Test)26 TemplateModel (freemarker.template.TemplateModel)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Cookie (ninja.Cookie)6 SimpleScalar (freemarker.template.SimpleScalar)4 Context (ninja.Context)3 Timed (ninja.metrics.Timed)3 ResponseStreams (ninja.utils.ResponseStreams)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 StringModel (freemarker.ext.beans.StringModel)2 Date (java.util.Date)2 ConstraintViolation (ninja.validation.ConstraintViolation)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1