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