Search in sources :

Example 1 with Context

use of ninja.Context 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 2 with Context

use of ninja.Context in project ninja by ninjaframework.

the class TemplateEngineJsonPTest method setUp.

@Before
public void setUp() throws IOException {
    logger = mock(Logger.class);
    properties = mock(NinjaProperties.class);
    context = mock(Context.class);
    responseStreams = mock(ResponseStreams.class);
    result = Results.jsonp().render(Collections.singletonList(123));
    objectMapper = new ObjectMapper();
    outputStream = new ByteArrayOutputStream();
    when(properties.getWithDefault("ninja.jsonp.callbackParameter", TemplateEngineJsonP.DEFAULT_CALLBACK_PARAMETER_NAME)).thenReturn("callback");
    when(context.finalizeHeaders(result)).thenReturn(responseStreams);
    when(responseStreams.getOutputStream()).thenReturn(outputStream);
}
Also used : Context(ninja.Context) ResponseStreams(ninja.utils.ResponseStreams) NinjaProperties(ninja.utils.NinjaProperties) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 3 with Context

use of ninja.Context in project ninja by ninjaframework.

the class TemplateEngineJsonTest method setUp.

@Before
public void setUp() throws IOException {
    context = mock(Context.class);
    responseStreams = mock(ResponseStreams.class);
    result = mock(Result.class);
    objectMapper = new ObjectMapper();
    outputStream = new ByteArrayOutputStream();
    TestObject testObj = new TestObject();
    testObj.field1 = "field_one";
    testObj.field2 = "field_two";
    when(result.getRenderable()).thenReturn(testObj);
    when(context.finalizeHeaders(result)).thenReturn(responseStreams);
    when(responseStreams.getOutputStream()).thenReturn(outputStream);
}
Also used : Context(ninja.Context) ResponseStreams(ninja.utils.ResponseStreams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Result(ninja.Result) Before(org.junit.Before)

Example 4 with Context

use of ninja.Context in project ninja by ninjaframework.

the class TemplateEngineTextTest method setUp.

@Before
public void setUp() throws IOException {
    context = mock(Context.class);
    responseStreams = mock(ResponseStreams.class);
    result = mock(Result.class);
    Map<String, String> map = new TreeMap<String, String>() {

        {
            put("apples", "oranges");
            put("cars", "trucks");
        }
    };
    when(result.getRenderable()).thenReturn(map);
    writer = new StringWriter();
    when(context.finalizeHeaders(result)).thenReturn(responseStreams);
    when(responseStreams.getWriter()).thenReturn(writer);
}
Also used : Context(ninja.Context) ResponseStreams(ninja.utils.ResponseStreams) StringWriter(java.io.StringWriter) TreeMap(java.util.TreeMap) Result(ninja.Result) Before(org.junit.Before)

Example 5 with Context

use of ninja.Context in project ninja by ninjaframework.

the class LambdasTest method anonymousMethodReference.

@Test
public void anonymousMethodReference() throws Exception {
    ControllerMethod1<Context> lambda = (Context context) -> Results.html().renderRaw("".getBytes(StandardCharsets.UTF_8));
    LambdaInfo lambdaInfo = Lambdas.reflect(lambda);
    assertThat(lambdaInfo.getKind(), is(Kind.ANONYMOUS_METHOD_REFERENCE));
    SerializedLambda serializedLambda = lambdaInfo.getSerializedLambda();
    assertThat(serializedLambda.getFunctionalInterfaceMethodName(), is("apply"));
    assertThat(serializedLambda.getImplClass().replace('/', '.'), is(LambdasTest.class.getCanonicalName()));
    assertThat(serializedLambda.getImplMethodName(), startsWith("lambda$"));
    assertThat(serializedLambda.getInstantiatedMethodType(), is("(Lninja/Context;)Lninja/Result;"));
    // includes captured args btw...
    assertThat(serializedLambda.getImplMethodSignature(), is("(Lninja/Context;)Lninja/Result;"));
    // 6 = REF_invokeStatic
    assertThat(serializedLambda.getImplMethodKind(), is(6));
    assertThat(serializedLambda.getCapturedArgCount(), is(0));
}
Also used : Context(ninja.Context) LambdaInfo(ninja.utils.Lambdas.LambdaInfo) SerializedLambda(java.lang.invoke.SerializedLambda) Test(org.junit.Test)

Aggregations

Context (ninja.Context)6 ResponseStreams (ninja.utils.ResponseStreams)4 Result (ninja.Result)3 Before (org.junit.Before)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LambdaInfo (ninja.utils.Lambdas.LambdaInfo)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 SerializedLambda (java.lang.invoke.SerializedLambda)1 TreeMap (java.util.TreeMap)1 ControllerMethod1 (ninja.ControllerMethods.ControllerMethod1)1 Renderable (ninja.Renderable)1 InternalServerErrorException (ninja.exceptions.InternalServerErrorException)1 NinjaProperties (ninja.utils.NinjaProperties)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1