com.adobe.cq.social.srp.internal.SocialPropertyResourceImpl Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2014 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.cq.social.srp.internal;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.List;
import org.apache.sling.api.resource.AbstractResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import com.adobe.cq.social.srp.SocialResource;
import com.adobe.cq.social.srp.SocialResourceProvider;
/**
* A resource implementation for properties.
*/
public class SocialPropertyResourceImpl extends AbstractResource implements SocialResource {
private final ResourceResolver resourceResolver;
private final String path;
private final String resourceType;
private final ResourceMetadata metadata;
private final SocialResourceProvider provider;
private final Object propertyValue;
/**
* Ctor.
* @param path the resource path
* @param parentResourceType the parent resource type
* @param provider the provider
* @param resourceResolver the resource resolver
* @param propertyName the property name
* @param propertyValue the property value
*/
public SocialPropertyResourceImpl(final String path, final String parentResourceType,
final SocialResourceProvider provider, final ResourceResolver resourceResolver, final String propertyName,
final Object propertyValue) {
this.resourceResolver = resourceResolver;
this.path = path;
this.resourceType = parentResourceType + "/" + propertyName;
this.metadata = new ResourceMetadata();
this.metadata.setResolutionPath(path);
this.provider = provider;
this.propertyValue = propertyValue;
}
@Override
public String getPath() {
return path;
}
@Override
public String getResourceType() {
return resourceType;
}
@Override
public String getResourceSuperType() {
return null;
}
@Override
public ResourceMetadata getResourceMetadata() {
return metadata;
}
@Override
public ResourceResolver getResourceResolver() {
return resourceResolver;
}
@Override
public SocialResourceProvider getResourceProvider() {
return provider;
}
@Override
public Resource getRootJCRNode() {
// hmmm. is this right?
return null;
}
private String getString(final Object o) {
return o.toString();
}
private Boolean getBoolean(final Object o) {
return o instanceof Boolean ? (Boolean) o : Boolean.valueOf(o.toString());
}
private Long getLong(final Object o) {
return o instanceof Long ? (Long) o : Long.valueOf(Long.parseLong(o.toString()));
}
private Double getDouble(final Object o) {
return o instanceof Double ? (Double) o : new Double(Double.parseDouble(o.toString()));
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public AdapterType adaptTo(final Class type) {
if (type == InputStream.class) {
try {
return (AdapterType) new ByteArrayInputStream(getString(propertyValue).getBytes("UTF-8"));
} catch (final UnsupportedEncodingException e) {
return null;
}
} else if (type == String.class) {
return (AdapterType) getString(propertyValue);
} else if (type == Boolean.class) {
return (AdapterType) getBoolean(propertyValue);
} else if (type == Long.class) {
return (AdapterType) getLong(propertyValue);
} else if (type == Double.class) {
return (AdapterType) getDouble(propertyValue);
} else if (type == Calendar.class) {
if (propertyValue instanceof Calendar) {
return (AdapterType) propertyValue;
}
} else if (type == String[].class) {
if (propertyValue instanceof List) {
final String[] result = new String[((List) propertyValue).size()];
for (int i = 0; i < ((List) propertyValue).size(); i++) {
result[i] = getString(((List) propertyValue).get(i));
}
return (AdapterType) result;
}
return (AdapterType) new String[]{getString(propertyValue)};
} else if (type == Boolean[].class) {
if (propertyValue instanceof List) {
final Boolean[] result = new Boolean[((List) propertyValue).size()];
for (int i = 0; i < ((List) propertyValue).size(); i++) {
result[i] = getBoolean(((List) propertyValue).get(i));
}
return (AdapterType) result;
}
return (AdapterType) new Boolean[]{getBoolean(propertyValue)};
} else if (type == Long[].class) {
if (propertyValue instanceof List) {
final Long[] result = new Long[((List) propertyValue).size()];
for (int i = 0; i < ((List) propertyValue).size(); i++) {
result[i] = getLong(((List) propertyValue).get(i));
}
return (AdapterType) result;
}
return (AdapterType) new Long[]{getLong(propertyValue)};
} else if (type == Double[].class) {
if (propertyValue instanceof List) {
final Double[] result = new Double[((List) propertyValue).size()];
for (int i = 0; i < ((List) propertyValue).size(); i++) {
result[i] = getDouble(((List) propertyValue).get(i));
}
return (AdapterType) result;
}
return (AdapterType) new Double[]{getDouble(propertyValue)};
}
// try to use adapter factories
return super.adaptTo(type);
}
@Override
public boolean checkPermissions(final String permission) {
throw new UnsupportedOperationException(
"Attempting to check permissions on a property. This is not supported.");
}
}