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