Search in sources :

Example 1 with InsufficientStorageException

use of org.alfresco.rest.framework.core.exceptions.InsufficientStorageException in project alfresco-remote-api by Alfresco.

the class NodesImpl method writeContent.

private 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) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException)

Example 2 with InsufficientStorageException

use of org.alfresco.rest.framework.core.exceptions.InsufficientStorageException 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)

Example 3 with InsufficientStorageException

use of org.alfresco.rest.framework.core.exceptions.InsufficientStorageException in project alfresco-remote-api by Alfresco.

the class ExceptionResolverTests method testMatchException.

// 04180006 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get
@Test
public void testMatchException() {
    ErrorResponse response = assistant.resolveException(new ApiException(null));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InvalidArgumentException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new InvalidQueryException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new NotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new EntityNotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new RelationshipResourceNotFoundException(null, null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new PermissionDeniedException(null));
    // default to STATUS_FORBIDDEN
    assertEquals(403, response.getStatusCode());
    response = assistant.resolveException(new UnsupportedResourceOperationException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new DeletedResourceException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new ConstraintViolatedException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    response = assistant.resolveException(new StaleEntityException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    // Try a random exception
    response = assistant.resolveException(new FormNotFoundException(null));
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InsufficientStorageException(null));
    assertEquals(507, response.getStatusCode());
    response = assistant.resolveException(new IntegrityException(null));
    assertEquals(422, response.getStatusCode());
}
Also used : UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) Test(org.junit.Test)

Aggregations

InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ContentLimitViolationException (org.alfresco.repo.content.ContentLimitViolationException)2 RequestEntityTooLargeException (org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException)2 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)2 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)2 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)2 ContentQuotaException (org.alfresco.service.cmr.usage.ContentQuotaException)2 FormNotFoundException (org.alfresco.repo.forms.FormNotFoundException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 ErrorResponse (org.alfresco.rest.framework.core.exceptions.ErrorResponse)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1