Search in sources :

Example 1 with BinaryResource

use of org.alfresco.rest.framework.resource.content.BinaryResource in project records-management by Alfresco.

the class RecordsEntityResource method readProperty.

/**
 * Download content
 *
 * @param recordId the id of the record to get the content from
 * @param parameters {@link Parameters}
 * @return binary content resource
 * @throws EntityNotFoundException
 */
@Override
@WebApiDescription(title = "Download content", description = "Download content for a record with id 'recordId'")
@BinaryProperties({ "content" })
public BinaryResource readProperty(String recordId, Parameters parameters) throws EntityNotFoundException {
    checkNotBlank("recordId", recordId);
    mandatory("parameters", parameters);
    NodeRef record = apiUtils.validateRecord(recordId);
    if (nodeService.getType(record).equals(RecordsManagementModel.TYPE_NON_ELECTRONIC_DOCUMENT)) {
        throw new IllegalArgumentException("Cannot read content from Non-electronic record " + recordId + ".");
    }
    BinaryResource content = apiUtils.getContent(record, parameters, true);
    NodeRef primaryParent = nodeService.getPrimaryParent(record).getParentRef();
    FileInfo info = fileFolderService.getFileInfo(record);
    apiUtils.postActivity(info, primaryParent, ActivityPoster.DOWNLOADED);
    return content;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource) FileInfo(org.alfresco.service.cmr.model.FileInfo) WebApiDescription(org.alfresco.rest.framework.WebApiDescription) BinaryProperties(org.alfresco.rest.framework.BinaryProperties)

Example 2 with BinaryResource

use of org.alfresco.rest.framework.resource.content.BinaryResource in project alfresco-remote-api by Alfresco.

the class AbstractResourceWebScript method execute.

public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) {
    final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData());
    final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod());
    final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), DEFAULT_JSON_CONTENT, CACHE_NEVER);
    // MNT-20308 - allow write transactions for authentication api
    RetryingTransactionHelper transHelper = getTransactionHelper(resource.getMetaData().getApi().getName());
    Object toReturn = transHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>() {

        @Override
        public Object execute() throws Throwable {
            Object result = executeAction(resource, params, callBack);
            if (result instanceof BinaryResource) {
                // don't postprocess it.
                return result;
            }
            return helper.processAdditionsToTheResponse(res, resource.getMetaData().getApi(), entityCollectionName, params, result);
        }
    }, isReadOnly, false);
    setResponse(res, callBack);
    return toReturn;
}
Also used : FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 3 with BinaryResource

use of org.alfresco.rest.framework.resource.content.BinaryResource in project alfresco-remote-api by Alfresco.

the class ResourceWebScriptGet method executeAction.

/**
 * Executes the action on the resource
 * @param resource ResourceWithMetadata
 * @param params parameters to use
 * @return anObject the result of the execute
 */
@Override
public Object executeAction(ResourceWithMetadata resource, Params params, WithResponse withResponse) throws Throwable {
    switch(resource.getMetaData().getType()) {
        case ENTITY:
            if (StringUtils.isBlank(params.getEntityId()) || params.isCollectionResource()) {
                // Get the collection
                if (EntityResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.Read<?> getter = (Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params);
                    return resources;
                } else if (EntityResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadWithResponse<?> getter = (EntityResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params, withResponse);
                    return resources;
                } else if (EntityResourceAction.DeleteSetWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.DeleteSetWithResponse.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSetWithResponse relationDeleter = (EntityResourceAction.DeleteSetWithResponse) resource.getResource();
                    relationDeleter.deleteSet(params, withResponse);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else if (EntityResourceAction.DeleteSet.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Delete.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSet relationDeleter = (EntityResourceAction.DeleteSet) resource.getResource();
                    relationDeleter.deleteSet(params);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            } else {
                if (EntityResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadById<?> entityGetter = (ReadById<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params);
                    return result;
                } else if (EntityResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadByIdWithResponse<?> entityGetter = (EntityResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            }
        case RELATIONSHIP:
            if (StringUtils.isBlank(params.getRelationshipId()) || (params.isCollectionResource())) {
                // Get the collection
                if (RelationshipResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.Read<?> relationGetter = (RelationshipResourceAction.Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params);
                    return relations;
                } else {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadWithResponse<?> relationGetter = (RelationshipResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params, withResponse);
                    return relations;
                }
            } else {
                if (RelationshipResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadById<?> relationGetter = (RelationshipResourceAction.ReadById<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params);
                    return result;
                } else if (RelationshipResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadByIdWithResponse<?> relationGetter = (RelationshipResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            }
        case PROPERTY:
            if (StringUtils.isNotBlank(params.getEntityId())) {
                if (BinaryResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.Read getter = (BinaryResourceAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params);
                    return prop;
                }
                if (BinaryResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.ReadWithResponse getter = (BinaryResourceAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params, withResponse);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.Read getter = (RelationshipResourceBinaryAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.ReadWithResponse getter = (RelationshipResourceBinaryAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return prop;
                }
            } else {
                throw new UnsupportedResourceOperationException();
            }
        default:
            throw new UnsupportedResourceOperationException("GET not supported for Actions");
    }
}
Also used : CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) Read(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) ReadById(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource)

Example 4 with BinaryResource

use of org.alfresco.rest.framework.resource.content.BinaryResource in project alfresco-remote-api by Alfresco.

the class AbstractResourceWebScript method execute.

@SuppressWarnings("rawtypes")
@Override
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
        final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
        final ResourceWithMetadata resource = locator.locateResource(api, templateVars, httpMethod);
        final boolean isReadOnly = HttpMethod.GET == httpMethod;
        // MNT-20308 - allow write transactions for authentication api
        RetryingTransactionHelper transHelper = getTransactionHelper(resource.getMetaData().getApi().getName());
        // encapsulate script within transaction
        RetryingTransactionHelper.RetryingTransactionCallback<Object> work = new RetryingTransactionHelper.RetryingTransactionCallback<Object>() {

            @Override
            public Object execute() throws Throwable {
                try {
                    final Params params = paramsExtractor.extractParams(resource.getMetaData(), req);
                    return AbstractResourceWebScript.this.execute(resource, params, res, isReadOnly);
                } catch (Exception e) {
                    if (req instanceof BufferedRequest) {
                        // Reset the request in case of a transaction retry
                        ((BufferedRequest) req).reset();
                    }
                    // re-throw original exception for retry
                    throw e;
                }
            }
        };
        // This execution usually takes place in a Retrying Transaction (see subclasses)
        final Object toSerialize = transHelper.doInTransaction(work, isReadOnly, true);
        // Outside the transaction.
        if (toSerialize != null) {
            if (toSerialize instanceof BinaryResource) {
                // TODO review (experimental) - can we move earlier & wrap complete execute ? Also for QuickShare (in MT/Cloud) needs to be tenant for the nodeRef (TBC).
                boolean noAuth = false;
                if (BinaryResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    noAuth = resource.getMetaData().isNoAuth(BinaryResourceAction.Read.class);
                } else if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    noAuth = resource.getMetaData().isNoAuth(RelationshipResourceBinaryAction.Read.class);
                } else {
                    logger.warn("Unexpected");
                }
                if (noAuth) {
                    String networkTenantDomain = TenantUtil.getCurrentDomain();
                    TenantUtil.runAsSystemTenant(new TenantUtil.TenantRunAsWork<Void>() {

                        public Void doWork() throws Exception {
                            streamResponse(req, res, (BinaryResource) toSerialize);
                            return null;
                        }
                    }, networkTenantDomain);
                } else {
                    streamResponse(req, res, (BinaryResource) toSerialize);
                }
            } else {
                renderJsonResponse(res, toSerialize, assistant.getJsonHelper());
            }
        }
    } catch (AlfrescoRuntimeException | ApiException | WebScriptException xception) {
        renderException(xception, res, assistant);
    } catch (RuntimeException runtimeException) {
        renderException(runtimeException, res, assistant);
    } finally {
        reportExecutionTimeMetric(startTime, req.getServicePath());
    }
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) BufferedRequest(org.alfresco.repo.web.scripts.BufferedRequest) Params(org.alfresco.rest.framework.resource.parameters.Params) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) TenantUtil(org.alfresco.repo.tenant.TenantUtil) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Aggregations

BinaryResource (org.alfresco.rest.framework.resource.content.BinaryResource)4 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)2 BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)2 RelationshipResourceBinaryAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction)2 FileBinaryResource (org.alfresco.rest.framework.resource.content.FileBinaryResource)2 NodeBinaryResource (org.alfresco.rest.framework.resource.content.NodeBinaryResource)2 IOException (java.io.IOException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 TenantUtil (org.alfresco.repo.tenant.TenantUtil)1 BufferedRequest (org.alfresco.repo.web.scripts.BufferedRequest)1 BinaryProperties (org.alfresco.rest.framework.BinaryProperties)1 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)1 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)1 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)1 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)1 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)1 Read (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read)1 ReadById (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById)1