001/*
002 * (C) Copyright 2006-2011 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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.core.convert.api;
021
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
028import org.nuxeo.ecm.core.blob.ManagedBlob;
029
030/**
031 * Base exception raised by the {@link ConversionService}.
032 *
033 * @author tiry
034 */
035public class ConversionException extends NuxeoException {
036
037    private static final long serialVersionUID = 1L;
038
039    public ConversionException() {
040        super();
041    }
042
043    public ConversionException(String message) {
044        super(message);
045    }
046
047    public ConversionException(String message, Throwable cause) {
048        super(message, cause);
049    }
050
051    public ConversionException(Throwable cause) {
052        super(cause);
053    }
054
055    /**
056     * @since 11.1
057     */
058    public ConversionException(String message, BlobHolder blobHolder) {
059        super(message);
060        addInfos(blobHolder);
061    }
062
063    /**
064     * @since 11.1
065     */
066    public ConversionException(String message, BlobHolder blobHolder, Throwable cause) {
067        super(message, cause);
068        addInfos(blobHolder);
069    }
070
071    /**
072     * @since 11.1
073     */
074    public ConversionException(BlobHolder blobHolder, Throwable cause) {
075        super(cause);
076        addInfos(blobHolder);
077    }
078
079    /**
080     * @since 11.1
081     */
082    public ConversionException(String message, Blob blob, Throwable cause) {
083        super(message, cause);
084        addInfo(blob);
085    }
086
087    /**
088     * @since 11.1
089     */
090    public ConversionException(String message, Blob blob) {
091        super(message);
092        addInfo(blob);
093    }
094
095    protected void addInfos(BlobHolder blobHolder) {
096        if (blobHolder instanceof DocumentBlobHolder) {
097            DocumentBlobHolder documentBlobHolder = (DocumentBlobHolder) blobHolder;
098            addInfo(String.format("Document: %s", documentBlobHolder.getDocument()));
099        }
100
101        List<Blob> blobs = blobHolder.getBlobs();
102        if (blobs != null) {
103            blobs.forEach(this::addInfo);
104        }
105    }
106
107    protected void addInfo(Blob blob) {
108        if (blob instanceof ManagedBlob) {
109            ManagedBlob managedBlob = (ManagedBlob) blob;
110            addInfo(String.format("Blob Key/Provider: %s/%s", managedBlob.getKey(), managedBlob.getProviderId()));
111        }
112    }
113}