001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mime4j.message;
021
022import org.apache.james.mime4j.dom.Multipart;
023import org.apache.james.mime4j.util.ByteSequence;
024import org.apache.james.mime4j.util.ContentUtil;
025
026/**
027 * Default implementation of {@link Multipart}.
028 */
029public class MultipartImpl extends AbstractMultipart {
030
031    private ByteSequence preamble;
032    private transient String preambleStrCache;
033    private transient boolean preambleComputed = false;
034    private ByteSequence epilogue;
035    private transient String epilogueStrCache;
036    private transient boolean epilogueComputed = false;
037
038    /**
039     * Creates a new empty <code>Multipart</code> instance.
040     */
041    public MultipartImpl(String subType) {
042        super(subType);
043        preamble = null;
044        preambleStrCache = null;
045        preambleComputed = true;
046        epilogue = null;
047        epilogueStrCache = null;
048        epilogueComputed = true;
049    }
050
051    // package private for now; might become public someday
052    public ByteSequence getPreambleRaw() {
053        return preamble;
054    }
055
056    public void setPreambleRaw(ByteSequence preamble) {
057        this.preamble = preamble;
058        this.preambleStrCache = null;
059        this.preambleComputed = false;
060    }
061
062    /**
063     * Gets the preamble.
064     *
065     * @return the preamble.
066     */
067    @Override
068    public String getPreamble() {
069        if (!preambleComputed) {
070            preambleStrCache = preamble != null ? ContentUtil.decode(preamble) : null;
071            preambleComputed = true;
072        }
073        return preambleStrCache;
074    }
075
076    /**
077     * Sets the preamble.
078     *
079     * @param preamble
080     *            the preamble.
081     */
082    @Override
083    public void setPreamble(String preamble) {
084        this.preamble = preamble != null ? ContentUtil.encode(preamble) : null;
085        this.preambleStrCache = preamble;
086        this.preambleComputed = true;
087    }
088
089    // package private for now; might become public someday
090    public ByteSequence getEpilogueRaw() {
091        return epilogue;
092    }
093
094    public void setEpilogueRaw(ByteSequence epilogue) {
095        this.epilogue = epilogue;
096        this.epilogueStrCache = null;
097        this.epilogueComputed = false;
098    }
099
100    /**
101     * Gets the epilogue.
102     *
103     * @return the epilogue.
104     */
105    @Override
106    public String getEpilogue() {
107        if (!epilogueComputed) {
108            epilogueStrCache = epilogue != null ? ContentUtil.decode(epilogue) : null;
109            epilogueComputed = true;
110        }
111        return epilogueStrCache;
112    }
113
114    /**
115     * Sets the epilogue.
116     *
117     * @param epilogue
118     *            the epilogue.
119     */
120    @Override
121    public void setEpilogue(String epilogue) {
122        this.epilogue = epilogue != null ? ContentUtil.encode(epilogue) : null;
123        this.epilogueStrCache = epilogue;
124        this.epilogueComputed = true;
125    }
126
127}