All Downloads are FREE. Search and download functionalities are using the official Maven repository.

internal.org.springframework.content.fragments.RenderableImpl Maven / Gradle / Ivy

There is a newer version: 3.0.15
Show newest version
package internal.org.springframework.content.fragments;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.fragments.ContentStoreAware;
import org.springframework.content.commons.mappingcontext.ContentProperty;
import org.springframework.content.commons.mappingcontext.MappingContext;
import org.springframework.content.commons.property.PropertyPath;
import org.springframework.content.commons.renditions.Renderable;
import org.springframework.content.commons.renditions.RenditionProvider;
import org.springframework.content.commons.renditions.RenditionService;
import org.springframework.content.commons.repository.ContentStore;
import org.springframework.content.commons.repository.StoreInvoker;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeType;

public class RenderableImpl implements Renderable, RenditionService, ContentStoreAware {

	private static final Log LOGGER = LogFactory.getLog(RenderableImpl.class);

	private List providers = new ArrayList();
	private ContentStore contentStore;
	private Class domainClass;
	private Class idClass;
	private StoreInvoker storeInvoker;

    private MappingContext mappingContext;

	public RenderableImpl() {
       this.mappingContext = new MappingContext(".", ".");
	}

	@Override
	public void setDomainClass(Class domainClass) {
		this.domainClass = domainClass;
	}

	@Override
	public void setIdClass(Class idClass) {
		this.idClass = idClass;
	}

	@Override
    public void setContentStore(ContentStore store) {
		this.contentStore = store;
	}

	@Autowired(required = false)
	public void setProviders(RenditionProvider... providers) {
		for (RenditionProvider provider : providers) {
			this.providers.add(provider);
		}
	}

	@Override
	public boolean canConvert(String fromMimeType, String toMimeType) {
		for (RenditionProvider provider : providers) {
			if (MimeType.valueOf(fromMimeType).includes(MimeType.valueOf(provider.consumes()))) {
				for (String produce : provider.produces()) {
					if (MimeType.valueOf(toMimeType).includes(MimeType.valueOf(produce))) {
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	public String[] conversions(String fromMimeType) {
		Set conversions = new HashSet<>();
		for (RenditionProvider provider : providers) {
			if (provider.consumes().equals(fromMimeType)) {
				conversions.addAll(Arrays.asList(provider.produces()));
			}
		}
		return conversions.toArray(new String[] {});
	}

	@Override
	public InputStream convert(String fromMimeType, InputStream fromInputSource,
			String toMimeType) {
		for (RenditionProvider provider : providers) {
			if (MimeType.valueOf(fromMimeType)
					.includes(MimeType.valueOf(provider.consumes()))) {
				for (String produce : provider.produces()) {
					if (MimeType.valueOf(toMimeType)
							.includes(MimeType.valueOf(produce))) {
						return provider.convert(fromInputSource, toMimeType);
					}
				}
			}
		}
		return null;
	}

	@Override
	public InputStream getRendition(Object entity, String mimeType) {
		String fromMimeType = null;
		fromMimeType = (String) BeanUtils.getFieldWithAnnotation(entity, org.springframework.content.commons.annotations.MimeType.class);
		if (fromMimeType == null) {
			return null;
		}

		if (this.canConvert(fromMimeType, mimeType)) {
			InputStream content = null;
			try {
				content = contentStore.getContent(entity);
				if (content != null) {
					return this.convert(fromMimeType, content, mimeType);
				}
			}
			catch (Exception e) {
				LOGGER.error(String.format("Failed to get rendition from %s to %s", fromMimeType, mimeType), e);
			}
		}
		return null;
	}

    @Override
    public InputStream getRendition(Object entity, PropertyPath propertyPath, String mimeType) {

        Object fromMimeType = null;

        ContentProperty property = this.mappingContext.getContentProperty(entity.getClass(), propertyPath.getName());
        // todo: property == null

        fromMimeType = property.getMimeType(entity);

        if (fromMimeType == null) {
            return null;
        }

        if (this.canConvert(fromMimeType.toString(), mimeType)) {
            InputStream content = null;
            try {
                Resource r = contentStore.getResource(entity, propertyPath);
                if (r != null) {
                    content = r.getInputStream();
                    if (content != null) {
                        return this.convert(fromMimeType.toString(), content, mimeType);
                    }
                }
            }
            catch (Exception e) {
                LOGGER.error(String.format("Failed to get rendition from %s to %s", fromMimeType, mimeType), e);
            }
        }
        return null;
    }

    @Override
    public boolean hasRendition(Object entity, String mimeType) {

        String fromMimeType = null;
        fromMimeType = (String) BeanUtils.getFieldWithAnnotation(entity, org.springframework.content.commons.annotations.MimeType.class);
        if (fromMimeType == null) {
            return false;
        }

        return this.canConvert(fromMimeType, mimeType);
    }

    @Override
    public boolean hasRendition(Object entity, PropertyPath propertyPath, String mimeType) {

        ContentProperty property = this.mappingContext.getContentProperty(entity.getClass(), propertyPath.getName());
        // todo: property == null

        Object fromMimeType = null;
        fromMimeType = property.getMimeType(entity);
        if (fromMimeType == null) {
            return false;
        }

        return this.canConvert(fromMimeType.toString(), mimeType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy