Search in sources :

Example 1 with InternalServerErrorException

use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.

the class DiagnosticErrorRenderer method tryToRender.

public static void tryToRender(Context context, Result result, DiagnosticError diagnosticError, boolean throwInternalServerExceptionOnError) {
    try {
        DiagnosticErrorRenderer errorRenderer = build(context, result, diagnosticError);
        errorRenderer.renderResult(context, result);
    } catch (IOException e) {
        // fallback to ninja system-wide error handler?
        if (throwInternalServerExceptionOnError) {
            throw new InternalServerErrorException(e);
        } else {
            logger.error("Something is really fishy. Unable to render diagnostic error", e);
        }
    }
}
Also used : InternalServerErrorException(ninja.exceptions.InternalServerErrorException) IOException(java.io.IOException)

Example 2 with InternalServerErrorException

use of ninja.exceptions.InternalServerErrorException 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;
}
Also used : ResponseStreams(ninja.utils.ResponseStreams) OutputStream(java.io.OutputStream) InternalServerErrorException(ninja.exceptions.InternalServerErrorException) IOException(java.io.IOException)

Example 3 with InternalServerErrorException

use of ninja.exceptions.InternalServerErrorException 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 4 with InternalServerErrorException

use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.

the class NinjaDefaultTest method testOnRouteRequestWhenInternalServerErrorExceptionInDiagnosticMode.

@Test
public void testOnRouteRequestWhenInternalServerErrorExceptionInDiagnosticMode() throws Exception {
    FilterChain filterChain = Mockito.mock(FilterChain.class);
    Mockito.when(route.getFilterChain()).thenReturn(filterChain);
    InternalServerErrorException internalServerErrorException = new InternalServerErrorException("That's an InternalServerErrorException that should be handled by onError!");
    Mockito.when(filterChain.next(contextImpl)).thenThrow(internalServerErrorException);
    when(ninjaProperties.isDev()).thenReturn(true);
    when(ninjaProperties.getBooleanWithDefault(NinjaConstant.DIAGNOSTICS_KEY_NAME, true)).thenReturn(true);
    ninjaDefault.onRouteRequest(contextImpl);
    Result localResult = ninjaDefault.getInternalServerErrorResult(contextImpl, internalServerErrorException);
    assertThat(localResult.getRenderable(), CoreMatchers.instanceOf(DiagnosticError.class));
}
Also used : DiagnosticError(ninja.diagnostics.DiagnosticError) InternalServerErrorException(ninja.exceptions.InternalServerErrorException) Test(org.junit.Test)

Example 5 with InternalServerErrorException

use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.

the class NinjaDefaultTest method testOnRouteRequestWhenInternalServerErrorException.

@Test
public void testOnRouteRequestWhenInternalServerErrorException() throws Exception {
    FilterChain filterChain = Mockito.mock(FilterChain.class);
    Mockito.when(route.getFilterChain()).thenReturn(filterChain);
    InternalServerErrorException internalServerErrorException = new InternalServerErrorException("That's an InternalServerErrorException that should be handled by onError!");
    Mockito.when(filterChain.next(contextImpl)).thenThrow(internalServerErrorException);
    ninjaDefault.onRouteRequest(contextImpl);
    verify(ninjaDefault).getInternalServerErrorResult(contextImpl, internalServerErrorException, null);
}
Also used : InternalServerErrorException(ninja.exceptions.InternalServerErrorException) Test(org.junit.Test)

Aggregations

InternalServerErrorException (ninja.exceptions.InternalServerErrorException)5 IOException (java.io.IOException)3 ResponseStreams (ninja.utils.ResponseStreams)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Context (ninja.Context)1 Renderable (ninja.Renderable)1 Result (ninja.Result)1 DiagnosticError (ninja.diagnostics.DiagnosticError)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1