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