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