Search in sources :

Example 11 with NotFoundException

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

the class ResourceWebScriptHelper method executeResource.

/**
 * Executes a single related Resource.  The results are added to list of embedded results used by
 * the ExecutionResult object.
 *
 * @param api Api
 * @param params Params
 * @param uniqueEntityId String
 * @param resourceKey String
 * @param resource ResourceWithMetadata
 * @return Object
 */
private Object executeResource(final Api api, Params params, final String uniqueEntityId, final String resourceKey, final ResourceWithMetadata resource) {
    try {
        BeanPropertiesFilter paramFilter = null;
        final Object[] resultOfExecution = new Object[1];
        Map<String, BeanPropertiesFilter> filters = params.getRelationsFilter();
        if (filters != null) {
            paramFilter = filters.get(resourceKey);
        }
        final Params executionParams = Params.valueOf(paramFilter, uniqueEntityId, params.getRequest());
        final WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
        // Read only because this only occurs for GET requests
        Object result = executor.executeAction(resource, executionParams, callBack);
        return processAdditionsToTheResponse(null, api, null, executionParams, result);
    } catch (NotFoundException e) {
        // ignore, cannot access the object so don't embed it
        if (logger.isDebugEnabled()) {
            logger.debug("Ignored error, cannot access the object so can't embed it ", e);
        }
    } catch (PermissionDeniedException e) {
        // ignore, cannot access the object so don't embed it
        if (logger.isDebugEnabled()) {
            logger.debug("Ignored error, cannot access the object so can't embed it ", e);
        }
    } catch (Throwable throwable) {
        logger.warn("Failed to execute a RelatedResource for " + resourceKey + " " + throwable.getMessage());
    }
    // default
    return null;
}
Also used : Params(org.alfresco.rest.framework.resource.parameters.Params) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)

Example 12 with NotFoundException

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

the class ResourceLocatorTests method testLocateRelationshipProperties.

@Test
public void testLocateRelationshipProperties() {
    Api api3 = Api.valueOf("alfrescomock", "private", "3");
    Map<String, String> templateVars = new HashMap<String, String>();
    templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "goat");
    templateVars.put(ResourceLocator.ENTITY_ID, "herdId");
    templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "herd");
    ResourceWithMetadata collResource;
    try {
        collResource = locator.locateResource(api3, templateVars, HttpMethod.PUT);
        fail("Should throw an UnsupportedResourceOperationException");
    } catch (UnsupportedResourceOperationException error) {
    // this is correct
    }
    templateVars.put(ResourceLocator.PROPERTY, "content");
    collResource = locator.locateResource(api3, templateVars, HttpMethod.GET);
    assertNotNull(collResource);
    assertNotNull(collResource.getMetaData().getOperation(HttpMethod.GET));
    templateVars = new HashMap<String, String>();
    templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "sheep");
    templateVars.put(ResourceLocator.ENTITY_ID, "sheepId");
    templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "baaahh");
    templateVars.put(ResourceLocator.PROPERTY, "content");
    try {
        // Tests by passing invalid propery
        collResource = locator.locateResource(api, templateVars, HttpMethod.GET);
        fail("Should throw an NotFoundException");
    } catch (NotFoundException error) {
    // this is correct
    }
    templateVars.put(ResourceLocator.PROPERTY, "photo");
    collResource = locator.locateResource(api, templateVars, HttpMethod.GET);
    assertNotNull(collResource);
    assertNotNull(collResource.getMetaData().getOperation(HttpMethod.GET));
    assertNotNull(collResource.getMetaData().getOperation(HttpMethod.PUT));
    assertNotNull(collResource.getMetaData().getOperation(HttpMethod.DELETE));
}
Also used : HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) Api(org.alfresco.rest.framework.Api) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 13 with NotFoundException

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

the class NodesImpl method getThumbnailDefs.

private List<ThumbnailDefinition> getThumbnailDefs(Set<String> renditionNames) {
    List<ThumbnailDefinition> thumbnailDefs = null;
    if (renditionNames != null) {
        // If thumbnail generation has been configured off, then don't bother.
        if (!thumbnailService.getThumbnailsEnabled()) {
            throw new DisabledServiceException("Thumbnail generation has been disabled.");
        }
        thumbnailDefs = new ArrayList<>(renditionNames.size());
        ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
        for (String renditionName : renditionNames) {
            // Use the thumbnail registry to get the details of the thumbnail
            ThumbnailDefinition thumbnailDef = registry.getThumbnailDefinition(renditionName);
            if (thumbnailDef == null) {
                throw new NotFoundException(renditionName + " is not registered.");
            }
            thumbnailDefs.add(thumbnailDef);
        }
    }
    return thumbnailDefs;
}
Also used : DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)

Example 14 with NotFoundException

use of org.alfresco.rest.framework.core.exceptions.NotFoundException 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 15 with NotFoundException

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

NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)24 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)11 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)9 NodeRef (org.alfresco.service.cmr.repository.NodeRef)9 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)8 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)6 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)4 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)4 StaleEntityException (org.alfresco.rest.framework.core.exceptions.StaleEntityException)4 ContentData (org.alfresco.service.cmr.repository.ContentData)4 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)3 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)3 QName (org.alfresco.service.namespace.QName)3 Test (org.junit.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 RenditionDefinition2 (org.alfresco.repo.rendition2.RenditionDefinition2)2