Search in sources :

Example 66 with InvalidArgumentException

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

the class RecognizedParamsExtractorTest method findPagingTest.

@Test
public void findPagingTest() {
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("34");
    when(request.getParameter("maxItems")).thenReturn("50");
    Paging pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == 34);
    assertTrue(pagin.getMaxItems() == 50);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn(null);
    when(request.getParameter("maxItems")).thenReturn(null);
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
    assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("55");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == 55);
    assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn(null);
    when(request.getParameter("maxItems")).thenReturn("45");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getMaxItems() == 45);
    assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("apple");
    when(request.getParameter("maxItems")).thenReturn("pear");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("0");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    // Test Case cloud-2198
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("a");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("s");
    when(request.getParameter("maxItems")).thenReturn("5");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("-2");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("-3");
    when(request.getParameter("maxItems")).thenReturn("5");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("maxItems")).thenReturn("5");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue("skip count defaults to 0", pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
// End of Test Case cloud-2198
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Paging(org.alfresco.rest.framework.resource.parameters.Paging) Test(org.junit.Test)

Example 67 with InvalidArgumentException

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

the class ResourceLocatorTests method testApiValues.

@Test
public void testApiValues() {
    Api testApi;
    try {
        // Tests by passing invalid values
        testApi = Api.valueOf(null, null, null);
        fail("Should throw an InvalidArgumentException");
    } catch (InvalidArgumentException error) {
    // this is correct
    }
    try {
        // version as 0
        testApi = Api.valueOf("alfrescomock", "public", "0");
        fail("Should throw an InvalidArgumentException");
    } catch (InvalidArgumentException error) {
    // this is correct
    }
    try {
        // name as upper case
        testApi = Api.valueOf("AlfrescoMock", "public", "1");
        fail("Should throw an InvalidArgumentException");
    } catch (InvalidArgumentException error) {
    // this is correct
    }
    try {
        // scope as anything
        testApi = Api.valueOf("alfrescomock", "nonsense", "1");
        fail("Should throw an InvalidArgumentException");
    } catch (InvalidArgumentException error) {
    // this is correct
    }
    // scope as mixed case
    testApi = Api.valueOf("alfrescomock", "Public", "1");
    assertNotNull(testApi);
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Api(org.alfresco.rest.framework.Api) Test(org.junit.Test)

Example 68 with InvalidArgumentException

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

the class RenditionsImpl method validateNode.

public NodeRef validateNode(StoreRef storeRef, String nodeId) {
    if (nodeId == null) {
        throw new InvalidArgumentException("Missing nodeId");
    }
    final NodeRef nodeRef = nodes.validateNode(storeRef, nodeId);
    // check if the node represents a file
    isContentFile(nodeRef);
    return nodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 69 with InvalidArgumentException

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

the class RenditionsImpl method createRendition.

@Override
public void createRendition(NodeRef nodeRef, Rendition rendition, boolean executeAsync, Parameters parameters) {
    // If thumbnail generation has been configured off, then don't bother.
    if (!thumbnailService.getThumbnailsEnabled()) {
        throw new DisabledServiceException("Thumbnail generation has been disabled.");
    }
    final NodeRef sourceNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId());
    final NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, rendition.getId(), parameters);
    if (renditionNodeRef != null) {
        throw new ConstraintViolatedException(rendition.getId() + " rendition already exists.");
    }
    // Use the thumbnail registry to get the details of the thumbnail
    ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
    ThumbnailDefinition thumbnailDefinition = registry.getThumbnailDefinition(rendition.getId());
    if (thumbnailDefinition == null) {
        throw new NotFoundException(rendition.getId() + " is not registered.");
    }
    ContentData contentData = getContentData(sourceNodeRef, true);
    // Check if anything is currently available to generate thumbnails for the specified mimeType
    if (!registry.isThumbnailDefinitionAvailable(contentData.getContentUrl(), contentData.getMimetype(), contentData.getSize(), sourceNodeRef, thumbnailDefinition)) {
        throw new InvalidArgumentException("Unable to create thumbnail '" + thumbnailDefinition.getName() + "' for " + contentData.getMimetype() + " as no transformer is currently available.");
    }
    Action action = ThumbnailHelper.createCreateThumbnailAction(thumbnailDefinition, serviceRegistry);
    // Create thumbnail - or else queue for async creation
    actionService.executeAction(action, sourceNodeRef, true, executeAsync);
}
Also used : DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) Action(org.alfresco.service.cmr.action.Action) ContentData(org.alfresco.service.cmr.repository.ContentData) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 70 with InvalidArgumentException

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

the class RenditionsImpl method getContentNoValidation.

@Override
public BinaryResource getContentNoValidation(NodeRef sourceNodeRef, String renditionId, Parameters parameters) {
    NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, renditionId, parameters);
    // By default set attachment header (with rendition Id) unless attachment=false
    boolean attach = true;
    String attachment = parameters.getParameter("attachment");
    if (attachment != null) {
        attach = Boolean.valueOf(attachment);
    }
    final String attachFileName = (attach ? renditionId : null);
    if (renditionNodeRef == null) {
        boolean isPlaceholder = Boolean.valueOf(parameters.getParameter("placeholder"));
        if (!isPlaceholder) {
            throw new NotFoundException("Thumbnail was not found for [" + renditionId + ']');
        }
        String sourceNodeMimeType = null;
        try {
            sourceNodeMimeType = (sourceNodeRef != null ? getMimeType(sourceNodeRef) : null);
        } catch (InvalidArgumentException e) {
        // No content for node, e.g. ASSOC_AVATAR rather than ASSOC_PREFERENCE_IMAGE
        }
        // resource based on the content's mimeType and rendition id
        String phPath = scriptThumbnailService.getMimeAwarePlaceHolderResourcePath(renditionId, sourceNodeMimeType);
        if (phPath == null) {
            // 404 since no thumbnail was found
            throw new NotFoundException("Thumbnail was not found and no placeholder resource available for [" + renditionId + ']');
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Retrieving content from resource path [" + phPath + ']');
            }
            // get extension of resource
            String ext = "";
            int extIndex = phPath.lastIndexOf('.');
            if (extIndex != -1) {
                ext = phPath.substring(extIndex);
            }
            try {
                final String resourcePath = "classpath:" + phPath;
                InputStream inputStream = resourceLoader.getResource(resourcePath).getInputStream();
                // create temporary file
                File file = TempFileProvider.createTempFile(inputStream, "RenditionsApi-", ext);
                return new FileBinaryResource(file, attachFileName);
            } catch (Exception ex) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("Couldn't load the placeholder." + ex.getMessage());
                }
                throw new ApiException("Couldn't load the placeholder.");
            }
        }
    }
    Map<QName, Serializable> nodeProps = nodeService.getProperties(renditionNodeRef);
    ContentData contentData = (ContentData) nodeProps.get(ContentModel.PROP_CONTENT);
    Date modified = (Date) nodeProps.get(ContentModel.PROP_MODIFIED);
    org.alfresco.rest.framework.resource.content.ContentInfo contentInfo = null;
    if (contentData != null) {
        contentInfo = new ContentInfoImpl(contentData.getMimetype(), contentData.getEncoding(), contentData.getSize(), contentData.getLocale());
    }
    // add cache settings
    CacheDirective cacheDirective = new CacheDirective.Builder().setNeverCache(false).setMustRevalidate(false).setLastModified(modified).setETag(modified != null ? Long.toString(modified.getTime()) : null).setMaxAge(// one year (in seconds)
    Long.valueOf(31536000)).build();
    return new NodeBinaryResource(renditionNodeRef, ContentModel.PROP_CONTENT, contentInfo, attachFileName, cacheDirective);
}
Also used : Serializable(java.io.Serializable) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ContentData(org.alfresco.service.cmr.repository.ContentData) CacheDirective(org.alfresco.rest.framework.resource.content.CacheDirective) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) File(java.io.File) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Aggregations

InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)132 NodeRef (org.alfresco.service.cmr.repository.NodeRef)39 QName (org.alfresco.service.namespace.QName)37 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)31 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)24 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)20 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)17 Serializable (java.io.Serializable)12 SortColumn (org.alfresco.rest.framework.resource.parameters.SortColumn)12 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)11 Paging (org.alfresco.rest.framework.resource.parameters.Paging)10 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)9 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)9 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)9 Pair (org.alfresco.util.Pair)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Params (org.alfresco.rest.framework.resource.parameters.Params)8 RelationshipResourceNotFoundException (org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException)7