use of ninja.utils.ResponseStreams in project ninja by ninjaframework.
the class DiagnosticErrorRenderer method renderResult.
public void renderResult(Context context, Result result) throws IOException {
String out = render();
// set context response content type
result.contentType("text/html");
result.charset("utf-8");
ResponseStreams responseStreams = context.finalizeHeaders(result);
try (Writer w = responseStreams.getWriter()) {
w.write(out);
w.flush();
w.close();
}
}
use of ninja.utils.ResponseStreams in project ninja by ninjaframework.
the class Result method renderRaw.
/**
* This method directly renders the byte array to the output. It
* completely bypasses any rendering engine.
*
* Thus you can render anything you want.
*
* Chaining of resultRaw().resultRaw()... is NOT supported. Mixing with render()
* is NOT supported.
*
* It is always recommended to implement your own RenderingEngine OR
* use existing rendering engines.
*
* @param bytes The bytes to render.
* @return A result that will render the string directly to the output stream.
*/
public Result renderRaw(final byte[] bytes) {
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
if (result.getContentType() == null) {
result.contentType(Result.APPLICATION_OCTET_STREAM);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
try (OutputStream outputStream = responseStreams.getOutputStream()) {
outputStream.write(bytes);
} catch (IOException ioException) {
throw new InternalServerErrorException(ioException);
}
}
};
render(renderable);
return this;
}
use of ninja.utils.ResponseStreams 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.utils.ResponseStreams in project ninja by ninjaframework.
the class TemplateEngineFreemarkerTest method before.
@Before
public void before() throws Exception {
//Setup that allows to to execute invoke(...) in a very minimal version.
when(ninjaProperties.getWithDefault(FREEMARKER_CONFIGURATION_FILE_SUFFIX, ".ftl.html")).thenReturn(".ftl.html");
templateEngineFreemarker = new TemplateEngineFreemarker(messages, lang, logger, templateEngineHelper, templateEngineManager, templateEngineFreemarkerReverseRouteMethod, templateEngineFreemarkerAssetsAtMethod, templateEngineFreemarkerWebJarsAtMethod, ninjaProperties);
when(lang.getLanguage(any(Context.class), any(Optional.class))).thenReturn(Optional.<String>empty());
Session session = Mockito.mock(Session.class);
when(session.isEmpty()).thenReturn(true);
when(context.getSession()).thenReturn(session);
when(context.getRoute()).thenReturn(route);
when(lang.getLocaleFromStringOrDefault(any(Optional.class))).thenReturn(Locale.ENGLISH);
FlashScope flashScope = Mockito.mock(FlashScope.class);
Map<String, String> flashScopeData = new HashMap<>();
when(flashScope.getCurrentFlashCookieData()).thenReturn(flashScopeData);
when(context.getFlashScope()).thenReturn(flashScope);
when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/template.ftl.html");
writer = new StringWriter();
ResponseStreams responseStreams = mock(ResponseStreams.class);
when(context.finalizeHeaders(any(Result.class))).thenReturn(responseStreams);
when(responseStreams.getWriter()).thenReturn(writer);
}
use of ninja.utils.ResponseStreams in project ninja by ninjaframework.
the class TemplateEngineJsonP method invoke.
@Override
public void invoke(Context context, Result result) {
ResponseStreams responseStreams = context.finalizeHeaders(result);
String callback = getCallbackName(context);
try (OutputStream outputStream = responseStreams.getOutputStream()) {
objectMapper.writeValue(outputStream, new JSONPObject(callback, result.getRenderable()));
} catch (IOException e) {
logger.error("Error while rendering jsonp.", e);
}
}
Aggregations