javascript - 无法通过请求加载 GET 文件
问题描述
您需要创建一个 GET Web 脚本,该脚本将使用 nodeRef 加载文件的内容。示例网址:
localhost:8080/alfresco/s/get-custom-file-content/workspace://SpacesStore/3b3597e5-b5ec-41d5-b63b-54b050dccd1b
只需要在浏览器地址栏输入请求下载请求中记录的文件即可。下载ContentWebscript.java
public class DownloadContentWebscript extends StreamContent {
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadContentWebscript.class);
private ContentService contentService;
private AuthenticationService authenticationService;
private SiteService siteService;
private AuthorityService authorityService;
@Override
public void execute(final WebScriptRequest request,
final WebScriptResponse response) throws IOException {
LOGGER.info("Started executing DownloadContentWebscript...");
try {
final NodeRef nodeRef = getParameterAsNodeRef(request, "nodeRef");
final String userName = authenticationService.getCurrentUserName();
if(isNotAuthorised(nodeRef, userName, siteService, permissionService, authorityService)) {
response.setStatus(401);
response.getWriter().write("User is unauthorised to download the requested content!");
} else {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Processing the download requested by: {}", userName);
}
final boolean attach = Boolean.valueOf(request.getParameter("attach"));
processDownload(request, response, nodeRef, attach, ContentModel.PROP_CONTENT);
}
} catch (AccessDeniedException accessDenied) {
LOGGER.error("Access denied while downloading content", accessDenied);
throw new WebScriptException(Status.STATUS_UNAUTHORIZED,
accessDenied.getMessage(), accessDenied);
} catch (IOException | AlfrescoRuntimeException
| InvalidNodeRefException excp) {
LOGGER.error("Exception occurred while downloading content", excp);
throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR,
excp.getMessage(), excp);
}
LOGGER.info("Existing from DownloadContentWebscript...");
}
private void processDownload(final WebScriptRequest request,
final WebScriptResponse response, final NodeRef nodeRef, final boolean attach,
final QName propertyQName) throws IOException {
String userAgent = request.getHeader("User-Agent");
userAgent = StringUtils.isNotBlank(userAgent) ? userAgent.toLowerCase(Locale.ENGLISH) : StringUtils.EMPTY;
final boolean isClientSupported= userAgent.contains("msie")
|| userAgent.contains(" trident/")
|| userAgent.contains(" chrome/")
|| userAgent.contains(" firefox/");
if (attach && isClientSupported) {
String fileName = (String) this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
if (userAgent.contains("msie") || userAgent.contains(" trident/")) {
final String mimeType = contentService.getReader(nodeRef, propertyQName).getMimetype();
if (!(this.mimetypeService.getMimetypes(FilenameUtils.getExtension(fileName)).contains(mimeType))) {
fileName = FilenameUtils.removeExtension(fileName)+ FilenameUtils.EXTENSION_SEPARATOR_STR
+ this.mimetypeService.getExtension(mimeType);
}
}
streamContent(request, response, nodeRef, propertyQName, attach, fileName, null);
} else {
streamContent(request, response, nodeRef, propertyQName, attach, null, null);
}
}
private NodeRef getParameterAsNodeRef(final WebScriptRequest req, final String paramName) {
final String nodeRefStr = StringUtils.trimToNull(req.getParameter(paramName));
if (StringUtils.isBlank(nodeRefStr)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing " + paramName + " parameter");
}
if (!NodeRef.isNodeRef(nodeRefStr)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Incorrect format for " + paramName + " paramater");
}
final NodeRef nodeRef = new NodeRef(nodeRefStr);
if (!nodeService.exists(nodeRef)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, paramName + " not found");
}
return nodeRef;
}
private boolean isNotAuthorised(final NodeRef nodeRef,
final String userName, final SiteService siteService,
final PermissionService permissionService,
final AuthorityService authorityService) {
boolean isNotAuthorised = false;
final SiteInfo siteInfo = siteService.getSite(nodeRef);
if (null != siteInfo) {
if (siteService.isMember(siteInfo.getShortName(), userName)) {
final Set<AccessPermission> permissions = permissionService.getAllSetPermissions(nodeRef);
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Checking isNotAuthorised, Available access permissions are: {}", permissions);
}
for (final AccessPermission permission : permissions) {
if (permission.getPermission().equals("SiteConsumer")
|| permission.getPermission().equals("Consumer")) {
if (permission.getAuthorityType().equals("USER")
&& permission.getAuthority().equals(userName)) {
isNotAuthorised = true;
break;
} else if (permission.getAuthorityType().toString().equals("GROUP")) {
AuthenticationUtil.setRunAsUserSystem();
final Set<String> authorities = authorityService.getAuthoritiesForUser(userName);
AuthenticationUtil.clearCurrentSecurityContext();
AuthenticationUtil.setFullyAuthenticatedUser(userName);
for (final String authority : authorities) {
if (authority.equals(permission.getAuthority())) {
isNotAuthorised = true;
break;
}
}
}
}
}
} else {
isNotAuthorised = true;
}
}
return isNotAuthorised;
}
public void setContentService(final ContentService contentService) {
this.contentService = contentService;
}
public void setAuthenticationService(final AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
public void setAuthorityService(final AuthorityService authorityService) {
this.authorityService = authorityService;
}
public void setSiteService(final SiteService siteService) {
this.siteService = siteService;
}
}
下载Content.get.desc.xml
<webscript>
<shortname>Download Content</shortname>
<description>
</description>
<url>/get-custom-file-content?nodeRef={nodeRef}&attach={attach?}</url>
<format default="">argument</format>
<authentication runas="admin"/>
<transaction allow="readonly" />
<family>common</family>
</webscript>
webscript-context.xml
<bean class="alfresco.extension.templates.webscripts.repository.DownloadContentWebscript"
parent="templates.webscripts.repository">
<property name="permissionService" ref="PermissionService" />
<property name="nodeService" ref="NodeService" />
<property name="mimetypeService" ref="MimetypeService" />
<property name="delegate" ref="webscript.content.streamer" />
<property name="repository" ref="repositoryHelper" />
<property name="contentService" ref="ContentService" />
<property name="authenticationService" ref="AuthenticationService" />
<property name="siteService" ref="SiteService" />
<property name="authorityService" ref="AuthorityService"/>
</bean>
发生错误:
404 描述:请求的资源不可用。
我如何下载文件 GET 查询。请帮忙!
解决方案
推荐阅读
- javascript - 仅当表单不为空时如何重定向,JavaScript 验证
- python - 使用 rowspan 在 tkinter 中画布未正确排列
- javascript - 使用 Laravel 和 Vue JS 上传多个文件
- node.js - Google Cloud Functions 错误:“找不到模块 'sharp'”,但它在我的 package.json 中
- javascript - TypeError:无法读取 Discord.js 中未定义的属性“id”
- java - 空列表上的 Java ArrayList 方法 addAll() 抛出 UnsupportedOperationException
- java - Spring MVC 测试抛出 415 HttpMediaTypeNotSupportedException
- yii2 - 如果在选择查询中使用别名,如何计算所有记录?
- python - 为什么我们必须在python中对和对象使用两次datetime函数
- php - 使用 PHP 在图像中包含 html 文件