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.ecm.platform.importer.mqueues.pattern.message;
020
021import org.nuxeo.ecm.platform.importer.mqueues.pattern.Message;
022
023import java.io.IOException;
024import java.io.ObjectInput;
025import java.io.ObjectOutput;
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    private String mimetype;
035    private String encoding;
036    private String filename;
037    private String path;
038    private String content;
039
040    public BlobMessage() {
041    }
042
043    private BlobMessage(StringMessageBuilder builder) {
044        mimetype = builder.mimetype;
045        encoding = builder.encoding;
046        filename = builder.filename;
047        path = builder.path;
048        content = builder.content;
049        if ((path == null || path.isEmpty()) &&
050                (content == null) || content.isEmpty()) {
051            throw new IllegalArgumentException("BlobMessage must be initialized with a file path or content");
052        }
053    }
054
055    @Override
056    public String getId() {
057        return filename;
058    }
059
060    public String getMimetype() {
061        return mimetype;
062    }
063
064    public String getFilename() {
065        return filename;
066    }
067
068    public String getContent() {
069        return content;
070    }
071
072    public String getPath() {
073        return path;
074    }
075
076    public String getEncoding() {
077        return encoding;
078    }
079
080    public static class StringMessageBuilder {
081        private String mimetype;
082        private String encoding;
083        private String filename;
084        private String path;
085        private String content;
086
087        /**
088         * Create a string blob with a content
089         *
090         */
091        public StringMessageBuilder(String content) {
092            this.content = content;
093        }
094
095        /**
096         * Set the name of the file.
097         */
098        public StringMessageBuilder setFilename(String filename) {
099            this.filename = filename;
100            return this;
101        }
102
103        /**
104         * Set the path of the file containing the blob content.
105         */
106        public StringMessageBuilder setEncoding(String encoding) {
107            this.encoding = encoding;
108            return this;
109        }
110
111
112        /**
113         * Set the mime-type of the file.
114         */
115        public StringMessageBuilder setMimetype(String mimetype) {
116            this.mimetype = mimetype;
117            return this;
118        }
119
120        protected StringMessageBuilder setPath(String path) {
121            this.path = path;
122            // either a path or a content not both
123            this.content = null;
124            return this;
125        }
126
127        public BlobMessage build() {
128            return new BlobMessage(this);
129        }
130    }
131
132    public static class FileMessageBuilder extends StringMessageBuilder {
133        /**
134         * Create a blob from a file
135         *
136         */
137        public FileMessageBuilder(String path) {
138            super(null);
139            this.setPath(path);
140        }
141
142    }
143
144
145    @Override
146    public void writeExternal(ObjectOutput out) throws IOException {
147        out.writeObject(mimetype);
148        out.writeObject(encoding);
149        out.writeObject(filename);
150        out.writeObject(path);
151        out.writeObject(content);
152    }
153
154    @Override
155    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
156        mimetype = (String) in.readObject();
157        encoding = (String) in.readObject();
158        filename = (String) in.readObject();
159        path = (String) in.readObject();
160        content = (String) in.readObject();
161    }
162
163    //TODO: impl hashCode, equals, toString
164}
165