Search in sources :

Example 1 with Renderable

use of ninja.Renderable 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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Context (ninja.Context)1 Renderable (ninja.Renderable)1 Result (ninja.Result)1 InternalServerErrorException (ninja.exceptions.InternalServerErrorException)1 ResponseStreams (ninja.utils.ResponseStreams)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1