001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017package org.nuxeo.ecm.platform.preview.adapter.base;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.ecm.core.api.PropertyException;
028import org.nuxeo.ecm.core.api.model.Property;
029import org.nuxeo.ecm.platform.preview.api.PreviewException;
030
031/**
032 * Base class for preview adapters that will use preprocessed HTML preview that is stored inside the document.
033 *
034 * @author tiry
035 */
036public class PreprocessedHtmlPreviewAdapter extends AbstractHtmlPreviewAdapter {
037
038    private static final Log log = LogFactory.getLog(PreprocessedHtmlPreviewAdapter.class);
039
040    protected List<String> storedPreviewFieldsPaths = new ArrayList<String>();
041
042    public PreprocessedHtmlPreviewAdapter(List<String> fieldsPaths) {
043        storedPreviewFieldsPaths = fieldsPaths;
044    }
045
046    @Override
047    public List<Blob> getPreviewBlobs() throws PreviewException {
048
049        List<Blob> resultBlobs = new ArrayList<Blob>();
050
051        for (String xpath : storedPreviewFieldsPaths) {
052            try {
053                Property prop = adaptedDoc.getProperty(xpath);
054                if (prop.isComplex()) {
055                    Blob blob = (Blob) prop.getValue();
056                    try {
057                        blob.getStream().reset();
058                    } catch (IOException e) {
059                        log.error(e);
060                    }
061                    resultBlobs.add(blob);
062                } else {
063                    String data = (String) prop.getValue();
064                    resultBlobs.add(Blobs.createBlob(data));
065                }
066            } catch (PropertyException e) {
067                throw new PreviewException("Unable to get property " + xpath, e);
068            }
069        }
070        return resultBlobs;
071    }
072
073    @Override
074    public List<Blob> getPreviewBlobs(String xpath) throws PreviewException {
075        return getPreviewBlobs();
076    }
077
078    @Override
079    public void cleanup() {
080        // nothing to do
081    }
082
083    @Override
084    public boolean cachable() {
085        return false;
086    }
087
088    @Override
089    public boolean hasBlobToPreview() throws PreviewException {
090        return getPreviewBlobs().size() > 0;
091    }
092
093}