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