org.apache.sling.api.resource.Resource类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(144)

本文整理了Java中org.apache.sling.api.resource.Resource类的一些代码示例,展示了Resource类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Resource类的具体详情如下:
包路径:org.apache.sling.api.resource.Resource
类名称:Resource

Resource介绍

[英]Resources are pieces of content on which Sling acts

The Resource is also an Adaptable to get adapters to other types. A JCR based resource might support adapting to the JCR Node on which the resource is based.

A Resource object is valid for as long as the ResourceResolver that provided this instance is valid. The same applies in general to all objects returned by this instance, especially those returned by a call to #adaptTo(Class).

All implementations must support returning a value map from #getValueMap(), even if the map is empty.

Implementor's Note: It is recommended to not implement this interface directly. Rather consider either extending from AbstractResource or ResourceWrapper. This will make sure your implementation will not be suffering from missing method problems should the Sling Resource API be extended in the future.
[中]资源是Sling作用的内容片段
Resource也是一款可用于将适配器连接到其他类型的设备。基于JCR的资源可能支持适应资源所基于的JCR节点。
只要提供此实例的ResourceResolver有效,Resource对象就有效。这一点通常适用于此实例返回的所有对象,尤其是通过调用#adapteto(类)返回的对象。
所有实现都必须支持从#getValueMap()返回值映射,即使映射为空。
实现者注意:建议不要直接实现这个接口。而是考虑从抽象资源或RealCub包装器扩展。这将确保如果Sling资源API在将来被扩展,您的实现不会遇到缺少方法的问题。

代码示例

代码示例来源:origin: adobe/aem-core-wcm-components

public ResourceListItemImpl(@Nonnull SlingHttpServletRequest request, @Nonnull Resource resource) {
  ValueMap valueMap = resource.adaptTo(ValueMap.class);
  if (valueMap != null) {
    title = valueMap.get(JcrConstants.JCR_TITLE, String.class);
    description = valueMap.get(JcrConstants.JCR_DESCRIPTION, String.class);
    lastModified = valueMap.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
  }
  path = resource.getPath();
  name = resource.getName();
  url = null;
}

代码示例来源:origin: org.apache.sling/org.apache.sling.cms.core

private Resource getComponentEditPath(Resource component) {
  if (component != null) {
    if (component.getChild("edit") != null) {
      return component.getChild("edit");
    } else {
      String parentResourceType = component.getResourceResolver().getParentResourceType(component);
      if (StringUtils.isNotBlank(parentResourceType)) {
        component = component.getResourceResolver().getResource(parentResourceType);
        if (component != null) {
          return getComponentEditPath(component);
        }
      }
    }
  }
  return null;
}

代码示例来源:origin: adobe/aem-core-wcm-components

@Nonnull
@Override
public String getExportedType() {
  return request.getResource().getResourceType();
}

代码示例来源:origin: org.apache.sling/org.apache.sling.api

public DeepReadValueMapDecorator(final Resource resource, final ValueMap base) {
  super(base);
  this.pathPrefix = resource.getPath() + "/";
  this.resolver = resource.getResourceResolver();
  this.base = base;
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

public String getMetadataValue(final String name) {
  Resource meta = frozenResource.getChild(METADATA_PATH);
  if (meta == null) {
    return null;
  }
  return meta.getValueMap().get(name, String.class);
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

protected Resource getFragmentTemplateResource(ResourceResolver rr, String templatePath) {
    Resource template = rr.resolve(templatePath);
    if (template.adaptTo(FragmentTemplate.class) != null) {
      return template;
    } else {
      return template.getChild("jcr:content");
    }
  }
}

代码示例来源:origin: adobe/aem-core-wcm-components

@Nonnull
@Override
public String getExportedType() {
  if (StringUtils.isEmpty(resourceType)) {
    resourceType = pageProperties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class);
    if (StringUtils.isEmpty(resourceType)) {
      resourceType = currentPage.getContentResource().getResourceType();
    }
  }
  return resourceType;
}

代码示例来源:origin: adobe/aem-core-wcm-components

public ImageResourceWrapper(@Nonnull Resource resource, @Nonnull String resourceType) {
  super(resource);
  if (StringUtils.isEmpty(resourceType)) {
    throw new IllegalArgumentException("The " + ImageResourceWrapper.class.getName() + " needs to override the resource type of " +
        "the wrapped resource, but the resourceType argument was null or empty.");
  }
  this.resourceType = resourceType;
  valueMap = new ValueMapDecorator(new HashMap<>(resource.getValueMap()));
  valueMap.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, resourceType);
}

代码示例来源:origin: io.wcm/io.wcm.handler.media

@SuppressWarnings("null")
private void setContentDisposition(SlingHttpServletRequest request, SlingHttpServletResponse response) {
 Resource resource = request.getResource();
 // get mimetype from nt:file resource
 String mimeType = resource.getValueMap().get(JCR_CONTENT + "/" + JCR_MIMETYPE, String.class);
 // if mimetype is not blacklisted, or empty (and this is allowed) send "inline" content disposition header
 if ((StringUtils.isNotBlank(mimeType) && !mimetypeBlacklist.contains(mimeType.toLowerCase()))
   || (StringUtils.isBlank(mimeType) && allowEmptyMime)) {
  response.setHeader(HEADER_CONTENT_DISPOSITION, "inline");
 }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

/**
 * Disables an index, so it's no longer updated by Oak.
 *
 * @param oakIndex the index
 * @throws PersistenceException
 */
public void disableIndex(@Nonnull Resource oakIndex) throws PersistenceException {
  final ModifiableValueMap oakIndexProperties = oakIndex.adaptTo(ModifiableValueMap.class);
  oakIndexProperties.put(PN_TYPE, DISABLED);
  log.info("Disabled index at {}", oakIndex.getPath());
}

代码示例来源:origin: adobe/aem-core-wcm-components

public MockContentPolicyMapping(Resource contentPolicyMappingResource) {
  this.contentPolicyMappingResource = contentPolicyMappingResource;
  contentPolicyPath = contentPolicyMappingResource.getValueMap().get("cq:policy", String.class);
  if (StringUtils.isEmpty(contentPolicyPath)) {
    throw new IllegalArgumentException("Resource " + contentPolicyMappingResource.getPath() + " does not contain a valid " +
        "cq:policy property");
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

private static Set<String> getPathsFromQuery(ResourceResolver resourceResolver, String language, String query) {
  if (StringUtils.isBlank(query)) {
    return Collections.EMPTY_SET;
  }
  Set<String> paths = new HashSet<String>();
  language = StringUtils.defaultIfEmpty(language, "xpath");
  Iterator<Resource> resources = resourceResolver.findResources(query, language);
  while (resources.hasNext()) {
    paths.add(resources.next().getPath());
  }
  return paths;
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

private WorkflowModel getWorkflowModel(WorkflowSession workflowSession, String workflowModelId) {
  workflowModelId = StringUtils.stripToEmpty(workflowModelId);
  WorkflowModel workflowModel = null;
  if (StringUtils.isNotBlank(workflowModelId)) {
    if (!workflowModelId.endsWith("/jcr:content/model")) {
      ResourceResolver resourceResolver = workflowHelper.getResourceResolver(workflowSession);
      Resource resource = resourceResolver.getResource(workflowModelId + "/jcr:content/model");
      if (resource != null
          && StringUtils.equals(resource.getValueMap().get(JcrConstants.JCR_PRIMARYTYPE, String.class),"cq:WorkflowModel")) {
        workflowModelId = resource.getPath();
      }
    }
    try {
      workflowModel = workflowSession.getModel(workflowModelId);
    } catch (WorkflowException e) {
      log.warn("Could not find Workflow Model for [ {} ]", workflowModelId);
    }
  }
  return workflowModel;
}

代码示例来源:origin: com.citytechinc.aem.apps.ionic/ionic-aem-apps-core

public <AdapterType> AdapterType getResourceAdapter(Resource resource, Class<AdapterType> type) {
  try {
    if (StringUtils.isNotBlank(resource.getResourceType()) && getAdministrativeResourceResolver().getResource(resource.getResourceType()) != null) {
      return (AdapterType) new DefaultTypedResource(resource, getAdministrativeResourceResolver());
    }
  } catch (LoginException e) {
    LOG.error("Login Exception encountered acquiring an administrative Resource Resolver", e);
  }
  return null;
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

public String getWorkflowInstanceId() {
  if (StringUtils.isBlank(workflowInstanceId)) {
    resource.getResourceResolver().refresh();
    workflowInstanceId = properties.get(PN_WORKFLOW_INSTANCE_ID, String.class);
  }
  return reference(workflowInstanceId);
}

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.sightly

private void includeResource(SlingHttpServletRequest request, final Bindings bindings, PrintWriter out, String path,
               RequestDispatcherOptions requestDispatcherOptions) {
  if (StringUtils.isEmpty(path)) {
    throw new SightlyException("Resource path cannot be empty");
  } else {
    Resource includeRes = request.getResourceResolver().resolve(path);
    if (ResourceUtil.isNonExistingResource(includeRes)) {
      String resourceType = request.getResource().getResourceType();
      if (requestDispatcherOptions.containsKey(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE)) {
        resourceType = requestDispatcherOptions.getForceResourceType();
      }
      includeRes = new SyntheticResource(request.getResourceResolver(), path, resourceType);
    }
    includeResource(request, bindings, out, includeRes, requestDispatcherOptions);
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

public void addFailure(String payloadPath, String trackPath, Calendar failedAt) throws RepositoryException {
  Node failure = JcrUtils.getOrCreateByPath(resource.getChild(Workspace.NN_FAILURES).adaptTo(Node.class),
      Workspace.NN_FAILURE, true, Workspace.NT_UNORDERED, Workspace.NT_UNORDERED, false);
  JcrUtil.setProperty(failure, Failure.PN_PAYLOAD_PATH, payloadPath);
  if (StringUtils.isNotBlank(trackPath)) {
    JcrUtil.setProperty(failure, Failure.PN_PATH, Payload.dereference(trackPath));
  }
  if (failedAt != null) {
    JcrUtil.setProperty(failure, Failure.PN_FAILED_AT, failedAt);
  }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

@Override
public String getValue(Object result) {
 
 Resource resource = (Resource) result;
 
 TagManager tagMgr = resource.getResourceResolver().adaptTo(TagManager.class);
 log.debug("Loading tags from {}@{}", resource.getPath(), property);
 List<String> tags = new ArrayList<String>();
 String[] values = resource.getValueMap().get(property, String[].class);
 if (values != null) {
  for (String value : values) {
   tags.add(tagMgr.resolve(value).getTitle());
  }
 }
 log.debug("Loaded {} tags", tags);
 return StringUtils.join(tags,";");
}

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.jsp.taglib

@Override
public int doEndTag() {
  log.trace("doEndTag");
  Resource parent = null;
  if (level != null) {
    String[] segments = resource.getPath().split("\\/");
    int end = Integer.parseInt(level, 10);
    String parentPath = "/"+StringUtils.join(Arrays.copyOfRange(segments, 1, end + 1), "/");
    log.debug("Retrieving {} parent resource at path {}", level, parentPath);
    parent = resource.getResourceResolver().getResource(parentPath);
  } else {
    log.debug("Retrieving parent resource");
    parent = resource.getParent();
  }
  log.debug("Saving {} to variable {}", parent, var);
  pageContext.setAttribute(var, parent);
  return EVAL_PAGE;
}

代码示例来源:origin: org.apache.sling/org.apache.sling.cms.core

public String getTitle() {
  if (StringUtils.isNotEmpty(title)) {
    return title;
  } else {
    return resource.getName();
  }
}

相关文章