Search in sources :

Example 11 with ContentIOException

use of org.alfresco.service.cmr.repository.ContentIOException in project alfresco-remote-api by Alfresco.

the class RepositoryContainerTest method testHideExceptionsConfiguration.

public void testHideExceptionsConfiguration() throws Exception {
    final StringBuilder commandExecutionResult = new StringBuilder(128);
    commandExecutionResult.append("Execution result: \n").append("   os:         ").append(System.getProperty("os.name")).append("\n").append("   command:    <TEST_COMMAND>").append("   succeeded:  ").append(false).append("\n").append("   exit code:  ").append(1).append("\n").append("   out:        ").append("<EMPTY>").append("\n").append("   err:        ").append("ERROR");
    RepositoryContainer repoContainer = (RepositoryContainer) getServer().getApplicationContext().getBean("webscripts.container");
    RepositoryContainer repoContainerMock = Mockito.spy(repoContainer);
    Class<?>[] notPublicExceptions = repoContainerMock.getNotPublicExceptions();
    List<Class<?>> defaultConfiguration = Arrays.asList(notPublicExceptions);
    List<Class<?>> testExceptions = new LinkedList<Class<?>>();
    testExceptions.add(SQLException.class);
    testExceptions.add(ContentIOException.class);
    repoContainerMock.setNotPublicExceptions(testExceptions);
    try {
        // Case: MNT-12794 - test default configuration
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new ContentIOException(commandExecutionResult.toString());
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNull("'ContentIOException' cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { ContentIOException.class }));
            assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // Case: MNT-12794 - ContentIOException in AlfrescoRuntimeException and default configuration
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new ContentIOException(commandExecutionResult.toString()));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNull("'ContentIOException' cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { ContentIOException.class }));
            assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: AlfrescoRuntimeException without ContentIOException
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new NullPointerException("NullPointerException"));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNotNull("'NullPointerException' cause should be visible for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: RuntimeException with several different exceptions which should be shown
        List<Class<?>> updatedNotPublicExceptions = new LinkedList<Class<?>>(defaultConfiguration);
        updatedNotPublicExceptions.add(NullPointerException.class);
        repoContainerMock.setNotPublicExceptions(updatedNotPublicExceptions);
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new NullPointerException("NullPointerException"));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertNull("'NullPointerException' cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
            assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: all exceptions must be shown
        repoContainerMock.setNotPublicExceptions(null);
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new ContentIOException(commandExecutionResult.toString(), new NullPointerException("NullPointerException")));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertTrue("'AlfrescoRuntimeException' cause should not be hidden for client. Exception class: " + e.getClass().getName(), e instanceof AlfrescoRuntimeException);
            assertNotNull("'ContentIOException' cause should not be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { ContentIOException.class }));
            assertNotNull("'NullPointerException' cause should not be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
        // case: all exceptions must be shown
        repoContainerMock.setNotPublicExceptions(defaultConfiguration);
        repoContainerMock.setNotPublicExceptions(new LinkedList<Class<?>>());
        Mockito.doAnswer(new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) {
                throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new ContentIOException(commandExecutionResult.toString(), new NullPointerException("NullPointerException")));
            }
        }).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
        try {
            repoContainerMock.executeScript(null, null, null);
        } catch (Exception e) {
            assertTrue("'AlfrescoRuntimeException' cause should not be hidden for client. Exception class: " + e.getClass().getName(), e instanceof AlfrescoRuntimeException);
            assertNotNull("'ContentIOException' cause should not be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { ContentIOException.class }));
            assertNotNull("'NullPointerException' cause should not be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
            assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
        }
    } finally {
        repoContainerMock.setNotPublicExceptions(defaultConfiguration);
    }
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) LinkedList(java.util.LinkedList) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) SQLException(java.sql.SQLException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) FormException(org.alfresco.repo.forms.FormException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Authenticator(org.springframework.extensions.webscripts.Authenticator)

Example 12 with ContentIOException

use of org.alfresco.service.cmr.repository.ContentIOException in project alfresco-remote-api by Alfresco.

the class GetMethodTest method readContentLogsLegitimateExceptions.

@Test
public void readContentLogsLegitimateExceptions() throws IOException, WebDAVServerException {
    RuntimeException rEx = new RuntimeException("Some sort of proper error");
    ContentIOException contentEx = new ContentIOException("Wrapping the exception.", rEx);
    doThrow(contentEx).when(reader).getContent(any(OutputStream.class));
    try {
        getMethod.readContent(fileInfo, reader);
        fail("Exception should have been thrown.");
    } catch (WebDAVServerException e) {
        verify(logger).error(anyString(), same(contentEx));
        verify(logger, never()).debug(anyString(), same(contentEx));
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getHttpStatusCode());
        // Avoids logging stacking trace
        assertNull(e.getCause());
    }
}
Also used : OutputStream(java.io.OutputStream) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) Test(org.junit.Test)

Example 13 with ContentIOException

use of org.alfresco.service.cmr.repository.ContentIOException in project alfresco-remote-api by Alfresco.

the class GetMethodTest method readContentDoesNotLogClientAbortException.

@Test
public void readContentDoesNotLogClientAbortException() throws IOException, WebDAVServerException {
    IOException caEx = new ClientAbortException();
    ContentIOException contentEx = new ContentIOException("Wrapping the socket exception.", caEx);
    // Reader.getContent() will throw a ContentIOException when a client aborts.
    doThrow(contentEx).when(reader).getContent(any(OutputStream.class));
    try {
        getMethod.readContent(fileInfo, reader);
        fail("Exception should have been thrown.");
    } catch (WebDAVServerException e) {
        verify(logger, never()).error(anyString(), same(contentEx));
        // Error will only be seen when debugging.
        verify(logger).debug(anyString(), same(contentEx));
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getHttpStatusCode());
        // Avoids logging stacking trace
        assertNull(e.getCause());
    }
}
Also used : OutputStream(java.io.OutputStream) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) Test(org.junit.Test)

Example 14 with ContentIOException

use of org.alfresco.service.cmr.repository.ContentIOException in project alfresco-remote-api by Alfresco.

the class GetMethodTest method readContentDoesNotLogSocketExceptions.

@Test
public void readContentDoesNotLogSocketExceptions() throws IOException, WebDAVServerException {
    SocketException sockEx = new SocketException("Client aborted connection");
    ContentIOException contentEx = new ContentIOException("Wrapping the socket exception.", sockEx);
    // Reader.getContent() will throw a ContentIOException when a client aborts.
    doThrow(contentEx).when(reader).getContent(any(OutputStream.class));
    try {
        getMethod.readContent(fileInfo, reader);
        fail("Exception should have been thrown.");
    } catch (WebDAVServerException e) {
        verify(logger, never()).error(anyString(), same(contentEx));
        // Error will only be seen when debugging.
        verify(logger).debug(anyString(), same(contentEx));
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getHttpStatusCode());
        // Avoids logging stacking trace
        assertNull(e.getCause());
    }
}
Also used : SocketException(java.net.SocketException) OutputStream(java.io.OutputStream) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) Test(org.junit.Test)

Example 15 with ContentIOException

use of org.alfresco.service.cmr.repository.ContentIOException in project records-management by Alfresco.

the class FilePlanComponentsApiUtils method writeContent.

/**
 * Write content to file
 *
 * @param nodeRef  the node to write the content to
 * @param fileName  the name of the file (used for guessing the file's mimetype)
 * @param stream  the input stream to write
 * @param guessEncoding  whether to guess stream encoding
 */
public void writeContent(NodeRef nodeRef, String fileName, InputStream stream, boolean guessEncoding) {
    try {
        ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
        String mimeType = mimetypeService.guessMimetype(fileName);
        if ((mimeType != null) && (!mimeType.equals(MimetypeMap.MIMETYPE_BINARY))) {
            // quick/weak guess based on file extension
            writer.setMimetype(mimeType);
        } else {
            // stronger guess based on file stream
            writer.guessMimetype(fileName);
        }
        InputStream is = null;
        if (guessEncoding) {
            is = new BufferedInputStream(stream);
            is.mark(1024);
            writer.setEncoding(guessEncoding(is, mimeType, false));
            try {
                is.reset();
            } catch (IOException ioe) {
                if (LOGGER.isWarnEnabled()) {
                    LOGGER.warn("Failed to reset stream after trying to guess encoding: " + ioe.getMessage());
                }
            }
        } else {
            is = stream;
        }
        writer.putContent(is);
    } catch (ContentQuotaException cqe) {
        throw new InsufficientStorageException();
    } catch (ContentLimitViolationException clv) {
        throw new RequestEntityTooLargeException(clv.getMessage());
    } catch (ContentIOException cioe) {
        if (cioe.getCause() instanceof NodeLockedException) {
            throw (NodeLockedException) cioe.getCause();
        }
        throw cioe;
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) ContentLimitViolationException(org.alfresco.repo.content.ContentLimitViolationException) RequestEntityTooLargeException(org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException)

Aggregations

ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)15 IOException (java.io.IOException)6 OutputStream (java.io.OutputStream)5 SocketException (java.net.SocketException)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ContentReader (org.alfresco.service.cmr.repository.ContentReader)3 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)3 Test (org.junit.Test)3 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 ContentLimitViolationException (org.alfresco.repo.content.ContentLimitViolationException)2 HttpRangeProcessor (org.alfresco.repo.web.util.HttpRangeProcessor)2 InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)2 RequestEntityTooLargeException (org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException)2 ServiceRegistry (org.alfresco.service.ServiceRegistry)2 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)2 ContentService (org.alfresco.service.cmr.repository.ContentService)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2