001/*
002 * (C) Copyright 2016 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 *     bdelbosc
018 */
019package org.nuxeo.importer.stream.message;
020
021import java.io.IOException;
022import java.io.ObjectInput;
023import java.io.ObjectOutput;
024import java.nio.file.Files;
025import java.nio.file.Paths;
026
027import org.nuxeo.lib.stream.pattern.Message;
028
029/**
030 * A message holding info to build a StringBlob.
031 *
032 * @since 9.1
033 */
034public class BlobMessage implements Message {
035    static final long serialVersionUID = 20170529L;
036
037    protected String mimeType;
038
039    protected String encoding;
040
041    protected String filename;
042
043    protected String path;
044
045    protected String content;
046
047    public BlobMessage() {
048    }
049
050    protected BlobMessage(StringMessageBuilder builder) {
051        mimeType = builder.mimetype;
052        encoding = builder.encoding;
053        filename = builder.filename;
054        path = builder.path;
055        content = builder.content;
056        if ((path == null || path.isEmpty()) && ((content == null) || content.isEmpty())) {
057            throw new IllegalArgumentException("BlobMessage must be initialized with a file path or content");
058        }
059    }
060
061    @Override
062    public String getId() {
063        return filename;
064    }
065
066    public String getMimeType() {
067        return mimeType;
068    }
069
070    public String getFilename() {
071        return filename;
072    }
073
074    public String getContent() {
075        return content;
076    }
077
078    public String getPath() {
079        return path;
080    }
081
082    public String getEncoding() {
083        return encoding;
084    }
085
086    public static class StringMessageBuilder {
087        protected String mimetype;
088
089        protected String encoding;
090
091        protected String filename;
092
093        protected String path;
094
095        protected String content;
096
097        /**
098         * Create a string blob with a content
099         */
100        public StringMessageBuilder(String content) {
101            this.content = content;
102        }
103
104        /**
105         * Set the name of the file.
106         */
107        public StringMessageBuilder setFilename(String filename) {
108            this.filename = filename;
109            return this;
110        }
111
112        /**
113         * Set the path of the file containing the blob content.
114         */
115        public StringMessageBuilder setEncoding(String encoding) {
116            this.encoding = encoding;
117            return this;
118        }
119
120        /**
121         * Set the mime-type of the file.
122         */
123        public StringMessageBuilder setMimetype(String mimetype) {
124            this.mimetype = mimetype;
125            return this;
126        }
127
128        protected StringMessageBuilder setPath(String path) {
129            this.path = path;
130            // either a path or a content not both
131            this.content = null;
132            return this;
133        }
134
135        public BlobMessage build() {
136            return new BlobMessage(this);
137        }
138    }
139
140    public static class FileMessageBuilder extends StringMessageBuilder {
141        /**
142         * Create a blob from a file
143         */
144        public FileMessageBuilder(String path) {
145            super(null);
146            this.setPath(path);
147            this.filename = Paths.get(path).getFileName().toString();
148            this.mimetype = guessMimeType();
149        }
150
151        protected String guessMimeType() {
152            try {
153                return Files.probeContentType(Paths.get(path));
154            } catch (IOException e) {
155                return "application/octet-stream";
156            }
157        }
158    }
159
160    @Override
161    public void writeExternal(ObjectOutput out) throws IOException {
162        out.writeObject(mimeType);
163        out.writeObject(encoding);
164        out.writeObject(filename);
165        out.writeObject(path);
166        out.writeObject(content);
167    }
168
169    @Override
170    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
171        mimeType = (String) in.readObject();
172        encoding = (String) in.readObject();
173        filename = (String) in.readObject();
174        path = (String) in.readObject();
175        content = (String) in.readObject();
176    }
177
178    @Override
179    public boolean equals(Object o) {
180        if (this == o)
181            return true;
182        if (o == null || getClass() != o.getClass())
183            return false;
184
185        BlobMessage that = (BlobMessage) o;
186
187        if (mimeType != null ? !mimeType.equals(that.mimeType) : that.mimeType != null)
188            return false;
189        if (encoding != null ? !encoding.equals(that.encoding) : that.encoding != null)
190            return false;
191        if (filename != null ? !filename.equals(that.filename) : that.filename != null)
192            return false;
193        if (path != null ? !path.equals(that.path) : that.path != null)
194            return false;
195        return content != null ? content.equals(that.content) : that.content == null;
196    }
197
198    @Override
199    public int hashCode() {
200        int result = mimeType != null ? mimeType.hashCode() : 0;
201        result = 31 * result + (encoding != null ? encoding.hashCode() : 0);
202        result = 31 * result + (filename != null ? filename.hashCode() : 0);
203        result = 31 * result + (path != null ? path.hashCode() : 0);
204        result = 31 * result + (content != null ? content.hashCode() : 0);
205        return result;
206    }
207
208    @Override
209    public String toString() {
210        return "BlobMessage{" + "mimeType='" + mimeType + '\'' + ", encoding='" + encoding + '\'' + ", filename='"
211                + filename + '\'' + ", path='" + path + '\'' + ", content='" + content + '\'' + '}';
212    }
213}