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    
020    package org.apache.james.mime4j.message;
021    
022    import java.io.IOException;
023    import java.io.OutputStream;
024    
025    /**
026     * Abstract implementation of a single message body; that is, a body that does
027     * not contain (directly or indirectly) any other child bodies. It also provides
028     * the parent functionality required by bodies.
029     */
030    public abstract class SingleBody implements Body {
031    
032        private Entity parent = null;
033    
034        /**
035         * Sole constructor.
036         */
037        protected SingleBody() {
038        }
039    
040        /**
041         * @see org.apache.james.mime4j.message.Body#getParent()
042         */
043        public Entity getParent() {
044            return parent;
045        }
046    
047        /**
048         * @see org.apache.james.mime4j.message.Body#setParent(org.apache.james.mime4j.message.Entity)
049         */
050        public void setParent(Entity parent) {
051            this.parent = parent;
052        }
053    
054        /**
055         * Writes this single body to the given stream.
056         * 
057         * @param out
058         *            the stream to write to.
059         * @throws IOException
060         *             in case of an I/O error
061         */
062        public abstract void writeTo(OutputStream out) throws IOException;
063    
064        /**
065         * Returns a copy of this <code>SingleBody</code> (optional operation).
066         * <p>
067         * The general contract of this method is as follows:
068         * <ul>
069         * <li>Invoking {@link #getParent()} on the copy returns <code>null</code>.
070         * That means that the copy is detached from the parent entity of this
071         * <code>SingleBody</code>. The copy may get attached to a different
072         * entity later on.</li>
073         * <li>The underlying content does not have to be copied. Instead it may be
074         * shared between multiple copies of a <code>SingleBody</code>.</li>
075         * <li>If the underlying content is shared by multiple copies the
076         * implementation has to make sure that the content gets deleted when the
077         * last copy gets disposed of (and not before that).</li>
078         * </ul>
079         * <p>
080         * This implementation always throws an
081         * <code>UnsupportedOperationException</code>.
082         * 
083         * @return a copy of this <code>SingleBody</code>.
084         * @throws UnsupportedOperationException
085         *             if the <code>copy</code> operation is not supported by this
086         *             single body.
087         */
088        public SingleBody copy() {
089            throw new UnsupportedOperationException();
090        }
091    
092        /**
093         * Subclasses should override this method if they have allocated resources
094         * that need to be freed explicitly (e.g. cannot be simply reclaimed by the
095         * garbage collector).
096         * 
097         * The default implementation of this method does nothing.
098         * 
099         * @see org.apache.james.mime4j.message.Disposable#dispose()
100         */
101        public void dispose() {
102        }
103    
104    }