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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client.model;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.nuxeo.ecm.automation.client.jaxrs.util.Base64;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public abstract class Blob implements OperationInput {
031
032    private static final long serialVersionUID = 1L;
033
034    public static Blob fromBase64String(String fileName, String content) {
035        return fromBase64String(fileName, content, null);
036    }
037
038    public static Blob fromBase64String(String fileName, String content, String mimeType) {
039        return new StreamBlob(new ByteArrayInputStream(Base64.decode(content)), fileName, mimeType);
040    }
041
042    protected String mimeType;
043
044    protected String fileName;
045
046    protected Blob() {
047
048    }
049
050    public Blob(String fileName) {
051        this(fileName, null);
052    }
053
054    public Blob(String fileName, String mimeType) {
055        this.fileName = fileName;
056        setMimeType(mimeType);
057    }
058
059    public String getMimeType() {
060        return mimeType;
061    }
062
063    public String getFileName() {
064        return fileName;
065    }
066
067    public void setFileName(String fileName) {
068        this.fileName = fileName;
069    }
070
071    public void setMimeType(String mimeType) {
072        this.mimeType = mimeType == null ? "application/octet-stream" : mimeType;
073    }
074
075    public int getLength() {
076        return -1;
077    }
078
079    public String getInputType() {
080        return "blob";
081    }
082
083    public String getInputRef() {
084        return null;
085    }
086
087    public boolean isBinary() {
088        return true;
089    }
090
091    public abstract InputStream getStream() throws IOException;
092
093    protected String formatLength(int len) {
094        int k = len / 1024;
095        if (k <= 0) {
096            return len + " B";
097        } else if (k < 1024) {
098            return k + " K";
099        } else {
100            return (k / 1024) + " M";
101        }
102    }
103
104    @Override
105    public String toString() {
106        return fileName + " - " + mimeType + " - " + formatLength(getLength());
107    }
108}