本文整理汇总了Java中org.apache.sling.api.resource.Resource.adaptTo方法的典型用法代码示例。如果您正苦于以下问题:Java Resource.adaptTo方法的具体用法?Java Resource.adaptTo怎么用?Java Resource.adaptTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.sling.api.resource.Resource
的用法示例。
在下文中一共展示了Resource.adaptTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
public IndexEntry create(String path, @Nonnull ResourceResolver resolver) {
String[] indexRules = getIndexRules(PRIMARY_TYPE_VALUE);
if (ArrayUtils.isNotEmpty(indexRules)) {
Resource res = resolver.getResource(path);
if (res != null) {
Asset asset = res.adaptTo(Asset.class);
if (asset != null) {
IndexEntry ret = new IndexEntry("idx", "asset", path);
ret.addContent(getProperties(res, indexRules));
return ret;
}
LOG.error("Could not adapt asset");
}
}
return null;
}
示例2: process
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
public void process(final ExecutionContext executionContext, final TemplateContentModelImpl contentModel)
throws ProcessException {
SlingHttpServletRequest request = (SlingHttpServletRequest) executionContext.get(SLING_HTTP_REQUEST);
Resource resource = request.getResource();
Map<String, Object> content = (contentModel.has(RESOURCE_CONTENT_KEY)) ? ((Map<String, Object>) contentModel.get(RESOURCE_CONTENT_KEY)) : new HashMap<String, Object>();
if (resource != null) {
Node node = resource.adaptTo(Node.class);
if (node != null) {
String componentContentId = DigestUtils.md5Hex(resource.getPath());
content.put(XK_CONTENT_ID_CP, componentContentId);
} else {
content.put(XK_CONTENT_ID_CP, "_NONE");
}
content.put(PATH, resource.getPath());
content.put(NAME, resource.getName());
}
}
示例3: getSinglePropertyAs
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
/**
* Takes type, resource, and propertyName and returns its value of the object based on the given type
*
* @param type This is type parameter
* @param resource The resource to fetch the value from
* @param propertyName The property name to be used to fetch the value from the resource
* @return value The value of the object based on the given type
*/
private static <T> T getSinglePropertyAs(Class<T> type, Resource resource, String propertyName) {
T val = null;
try {
if (null != resource) {
Node node = resource.adaptTo(Node.class);
if (null != node) {
if (node.hasProperty(propertyName)) {
Property property = node.getProperty(propertyName);
if (!property.isMultiple()) {
Value value = property.getValue();
val = PropertyUtils.as(type, value);
}
}
}
}
} catch (Exception e) {
LOG.error(ERROR, e);
}
return val;
}
示例4: getMultiplePropertyAs
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
/**
* Takes type, resource, and property name and returns the list of value of the object based on the given type
*
* @param type This is type parameter
* @param resource The resource to fetch the value from
* @param propertyName The property name to be used to fetch the value from the resource
* @return valueList The list of values of the object based on the given type
*/
private static <T> List<T> getMultiplePropertyAs(Class<T> type, Resource resource, String propertyName) {
List<T> val = Collections.EMPTY_LIST;
try {
if (null != resource) {
Node node = resource.adaptTo(Node.class);
if (null != node) {
if (node.hasProperty(propertyName)) {
Property property = node.getProperty(propertyName);
if (property.isMultiple()) {
Value[] value = property.getValues();
val = PropertyUtils.as(type, value);
}
}
}
}
} catch (Exception e) {
LOG.error(ERROR, e);
}
return val;
}
示例5: isResourceType
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
private boolean isResourceType(Resource resource, String resourceType) {
if (StringUtils.isBlank(resourceType)) {
return true;
}
if (resource.isResourceType(resourceType)) {
return true;
}
if (!isValidType(resourceType)) {
return false;
}
Node node = resource.adaptTo(Node.class);
try {
if (node != null) {
return node.isNodeType(resourceType);
}
} catch (RepositoryException e) {
LOG.error("Can't check node type", e);
}
return false;
}
示例6: adaptTo
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type.isAssignableFrom(Map.class)) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (Entry<String, Resource> e : children.entrySet()) {
Resource o = e.getValue();
String[] stringArray = o.adaptTo(String[].class);
String stringValue = o.adaptTo(String.class);
if (stringValue != null) {
map.put(e.getKey(), stringValue);
} else if (stringArray != null) {
map.put(e.getKey(), stringArray);
}
}
return (AdapterType) map;
} else {
return null;
}
}
示例7: doGet
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws IOException, ServletException {
//TODO: put all of the logic in a context processor (need to fix templating support filter bug first)
String transformName = BLANK;
if (PathInfoUtil.getSuffixSegments(request).length == 2) {
String firstSuffixSegment = PathInfoUtil.getFirstSuffixSegment(request);
if (this.namedImageTransformers.keySet().contains(firstSuffixSegment)) {
transformName = firstSuffixSegment;
}
}
//Adds the asset binary to the inputStream
try {
Resource assetResource = getAssetResource(request);
if (DamUtil.isAsset(assetResource)) {
Binary binary;
String mimeType = BLANK;
Asset asset = DamUtil.resolveToAsset(assetResource);
Resource original = asset.getOriginal();
Node assetNode = original.adaptTo(Node.class);
if (assetNode.hasNode(JCR_CONTENT)) {
Node assetInfo = assetNode.getNode(JCR_CONTENT);
if (assetInfo.hasProperty(JCR_MIMETYPE)) {
mimeType = assetInfo.getProperty(JCR_MIMETYPE).getString();
}
if (StringUtils.isNotBlank(mimeType)) {
response.setContentType(mimeType);
}
binary = assetInfo.getProperty(JCR_DATA).getBinary();
InputStream inputStream = binary.getStream();
OutputStream outputStream = response.getOutputStream();
boolean shouldTransform = StringUtils.isNotBlank(transformName);
if (shouldTransform && ImageUtils.isImage(assetResource)) {
double quality = 1;
double maxGifQuality = 255;
// Transform the image
final Layer layer = new Layer(inputStream, new Dimension(maxWidth, maxHeight));
Layer newLayer = null;
try {
final NamedImageTransformer namedImageTransformer = this.namedImageTransformers.get(transformName);
newLayer = namedImageTransformer.transform(layer);
if (StringUtils.isBlank(mimeType) || !ImageIO.getImageWritersByMIMEType(mimeType).hasNext()) {
mimeType = getImageMimeType(layer, asset.getName());
response.setContentType(mimeType);
}
// For GIF images the colors will be reduced according to the quality argument.
if (StringUtils.equals(mimeType, GIF_MIME_TYPE)) {
quality = quality * maxGifQuality;
}
newLayer.write(mimeType, quality, outputStream);
} finally {
if (layer != null) {
layer.dispose();
}
if (newLayer != null) {
newLayer.dispose();
}
}
} else {
ByteStreams.copy(inputStream, outputStream);
}
response.flushBuffer();
outputStream.close();
}
}
} catch (RepositoryException repoException) {
LOGGER.error("Repository Exception. ", repoException);
}
}
示例8: process
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
public void process(final ExecutionContext executionContext, final TemplateContentModel contentModel)
throws ProcessException {
try {
SlingHttpServletRequest request = (SlingHttpServletRequest) executionContext.get(SLING_HTTP_REQUEST);
Resource resource = request.getResource();
Configuration config = configurationProvider.getFor(request.getResource().getResourceType());
Collection<String> models = config.asStrings(SLING_MODELS_CONFIG_PROPERTY_NAME, Mode.MERGE);
Object model;
String modelName = "";
Map<String, Object> modelData = new HashMap<>();
for (String modelId : models) {
Class<?> modelClass= Class.forName(modelId);
if (modelFactory.isModelClass(modelClass)) {
model = resource.adaptTo(modelClass);
modelData = getModelProperties(model);
modelName = modelClass.getSimpleName();
contentModel.set(SLING_MODEL_PROPERTIES_KEY + DOT + modelName, modelData);
} else {
log.error("{} is not a Sling Model", modelClass);
}
}
} catch (Exception e) {
throw new ProcessException(e);
}
}
示例9: process
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
public void process(final ExecutionContext executionContext, final TemplateContentModelImpl contentModel)
throws ProcessException {
try {
SlingHttpServletRequest request = (SlingHttpServletRequest) executionContext.get(SLING_HTTP_REQUEST);
Resource resource = request.getResource();
Map<String, Object> content = new HashMap<>();
if (resource != null) {
Node node = resource.adaptTo(Node.class);
if (node != null) {
String componentContentId = DigestUtils.md5Hex(resource.getPath());
content.put(XK_CONTENT_ID_CP, componentContentId);
Map<String, Object> propsMap = propsToMap(node.getProperties());
for (String propertyName : propsMap.keySet()) {
if (!StringUtils.startsWithAny(propertyName, RESERVED_SYSTEM_NAME_PREFIXES)) {
content.put(propertyName, propsMap.get(propertyName));
}
}
content.put(ID, DigestUtils.md5Hex(resource.getPath()));
} else {
// the resource doesn't exist so we clear the content
content.clear();
}
} else {
content.put(XK_CONTENT_ID_CP, "_NONE");
}
content.put(PATH, resource.getPath());
content.put(NAME, resource.getName());
contentModel.set(RESOURCE_CONTENT_KEY, content);
} catch (Exception e) {
throw new ProcessException(e);
}
}
示例10: loadConfigHierarchy
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
private void loadConfigHierarchy(final ComponentManager componentManager, final String resourceType)
throws Exception {
com.day.cq.wcm.api.components.Component component = componentManager.getComponent(resourceType);
if (component != null) {
if (!configCache.containsKey(component.getResourceType())) {
while (component != null) {
Resource configResource = component.getLocalResource(XK_CONFIG_RESOURCE_NAME);
if (configResource == null)
break;
Resource foundComponentResource = configResource.getParent();
if (!foundComponentResource.getPath().equals(component.getPath())) {
component = componentManager.getComponent(foundComponentResource.getPath());
}
Node node = configResource.adaptTo(Node.class);
Map<String, InertProperty> propsMap = getNodePropertiesMap(node, new HashMap<String, InertProperty>(), BLANK);
configMembers.put(component.getResourceType(), propsMap);
component = component.getSuperComponent();
}
configCache.put(component.getResourceType(), configMembers);
} else {
configMembers = configCache.get(component.getResourceType());
}
}
propNamesDeepCache = names(false);
}
示例11: getGlobalPropertiesPath
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
/**
* Takes resource and resource resolver return the global property path from the resource
*
* @param resource The resource to get the global property path from
* @return globalPropertiesPath
*/
public static String getGlobalPropertiesPath(Resource resource, ResourceResolver resourceResolver)
throws RepositoryException, PersistenceException {
String globalPropertiesPath = "";
Designer designer = resourceResolver.adaptTo(Designer.class);
Style style = designer.getStyle(resource);
Design design;
if (null != style) {
design = style.getDesign();
if (null != design) {
if (null != design.getContentResource()) {
if (null != design.getContentResource().getPath()) {
//add global node in design when it does not exist
Resource designResource = resourceResolver.getResource(design.getContentResource().getPath());
Node designNode = designResource.adaptTo(Node.class);
if (!designNode.hasNode(GLOBAL_PROPERTIES_KEY)) {
designNode.addNode(GLOBAL_PROPERTIES_KEY);
resourceResolver.commit();
}
// set global path
globalPropertiesPath = design.getContentResource().getPath() + GLOBAL_PATH;
}
}
}
}
return globalPropertiesPath;
}
示例12: findParentAs
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
/**
* Takes resource and resource type return the parent resource based on the given type
*
* @param resource The resource to get the parent from
* @return resource
*/
public static Resource findParentAs(final Resource resource, final String resourceType) {
try {
if (resource != null) {
Node parentNode = resource.adaptTo(Node.class);
return (resourceType != null && !resourceType.isEmpty() && !ResourceUtil.isNonExistingResource(resource) && (resource.isResourceType(resourceType) || parentNode.getPrimaryNodeType().isNodeType(resourceType))) ? resource : findParentAs(resource.getParent(), resourceType);
}
} catch (Exception ew) {
LOG.error(ERROR, ew);
}
return null;
}
示例13: isEqualToValue
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
private boolean isEqualToValue(Resource property) {
final String[] multiProperty = property.adaptTo(String[].class);
if (multiProperty != null) {
for (String p : multiProperty) {
if (operator.accepts(p, value)) {
return true;
}
}
return false;
} else {
return operator.accepts(property.adaptTo(String.class), value);
}
}
示例14: getDataFromFile
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
public static String getDataFromFile(String filePath, ResourceResolver resourceResolver) throws IOException {
if (StringUtils.isBlank(filePath)) {
return null;
}
final Resource dataContentResource = resourceResolver.getResource(filePath + SLASH + JCR_CONTENT);
if (dataContentResource == null) {
return null;
}
final ValueMap dataProperties = dataContentResource.adaptTo(ValueMap.class);
final InputStream inputStream = dataProperties.get(JCR_DATA, InputStream.class);
return IOUtils.toString(inputStream);
}
示例15: process
import org.apache.sling.api.resource.Resource; //导入方法依赖的package包/类
@Override
public void process(final ExecutionContext executionContext, TemplateContentModelImpl contentModel)
throws ProcessException {
try {
SlingHttpServletRequest request = (SlingHttpServletRequest) executionContext.get(SLING_HTTP_REQUEST);
Resource resource = request.getResource();
if (resource != null) {
Map<String, Object> authenticationInfo = new HashMap<String, Object>();
authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, CONFIG_SERVICE);
ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(resource);
if (component != null) {
Configuration configuration = configurationProvider.getFor(resource.getResourceType());
contentModel.setAsIsolated(CONFIG_PROPERTIES_KEY, wrap(configuration.toMap()));
Resource componentResource = component.adaptTo(Resource.class);
Node componentNode = componentResource.adaptTo(Node.class);
Map<String, Object> componentProps = PropertyUtils.propsToMap(componentNode.getProperties());
componentProps.put(PATH, componentResource.getPath());
componentProps.put(APP_NAME_PROPERTY_KEY, ResourceUtils.getAppName(resource));
String globalDialogPath = getGlobalDialogPath(resource, resourceResolver, request);
if (!BLANK.equals(globalDialogPath)) {
componentProps.put(GLOBAL_DIALOG_PATH_PROPERTY_KEY, globalDialogPath);
}
String globalPropertiesPath = ResourceUtils.getGlobalPropertiesPath(resource, resourceResolver);
if (!BLANK.equals(globalPropertiesPath)) {
componentProps.put(GLOBAL_PATH_PROPERTY_KEY, globalPropertiesPath);
}
contentModel.setAsIsolated(COMPONENT_PROPERTIES_KEY, componentProps);
}
resourceResolver.close();
}
} catch (Exception e) {
throw new ProcessException(e);
}
}