001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.property.complex;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.EwsUtilities;
029    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030    import microsoft.exchange.webservices.data.core.XmlElementNames;
031    import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
032    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlDeserializationException;
033    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
034    import org.apache.commons.logging.Log;
035    import org.apache.commons.logging.LogFactory;
036    
037    import javax.xml.stream.XMLStreamException;
038    
039    /**
040     * Represents the body of a message.
041     */
042    public final class MessageBody extends ComplexProperty {
043    
044      private static final Log log = LogFactory.getLog(MessageBody.class);
045    
046      /**
047       * The body type.
048       */
049      private BodyType bodyType;
050    
051      /**
052       * The text.
053       */
054      private String text;
055    
056      /**
057       * Initializes a new instance.
058       */
059      public MessageBody() {
060    
061      }
062    
063      /**
064       * Initializes a new instance.
065       *
066       * @param bodyType The type of the message body's text.
067       * @param text     The text of the message body.
068       */
069      public MessageBody(BodyType bodyType, String text) {
070        this();
071        this.bodyType = bodyType;
072        this.text = text;
073      }
074    
075      /**
076       * Initializes a new instance.
077       *
078       * @param text The text of the message body, assumed to be HTML.
079       */
080      public MessageBody(String text) {
081        this(BodyType.HTML, text);
082      }
083    
084      /**
085       * Defines an implicit conversation between a string and MessageBody.
086       *
087       * @param textBody The string to convert to MessageBody, assumed to be HTML.
088       * @return A MessageBody initialized with the specified string.
089       */
090      public static MessageBody getMessageBodyFromText(String textBody) {
091        return new MessageBody(BodyType.HTML, textBody);
092      }
093    
094      /**
095       * Defines an implicit conversion of MessageBody into a string.
096       *
097       * @param messageBody The MessageBody to convert to a string.
098       * @return A string containing the text of the MessageBody.
099       * @throws Exception the exception
100       */
101      public static String getStringFromMessageBody(MessageBody messageBody)
102          throws Exception {
103        EwsUtilities.validateParam(messageBody, "messageBody");
104        return messageBody.text;
105      }
106    
107      /**
108       * Reads attribute from XML.
109       *
110       * @param reader The reader.
111       * @throws Exception the exception
112       */
113      public void readAttributesFromXml(EwsServiceXmlReader reader)
114          throws Exception {
115        this.bodyType = reader.readAttributeValue(BodyType.class,
116            XmlAttributeNames.BodyType);
117      }
118    
119      /**
120       * Reads text value from XML.
121       *
122       * @param reader the reader
123       * @throws XMLStreamException the XML stream exception
124       * @throws ServiceXmlDeserializationException the service xml deserialization exception
125       */
126      @Override
127      public void readTextValueFromXml(EwsServiceXmlReader reader)
128          throws XMLStreamException, ServiceXmlDeserializationException {
129        if (log.isDebugEnabled()) {
130           log.debug("Reading text value from XML. BodyType = " + this.getBodyType() +
131              ", keepWhiteSpace = " +
132              ((this.getBodyType() == BodyType.Text) ? "true." : "false."));
133        }
134        this.text = reader.readValue(this.getBodyType() == BodyType.Text);
135        if (log.isDebugEnabled()) {
136           log.debug("Text value read:\n---\n" + this.text + "\n---");
137        }
138      }
139    
140      /**
141       * Writes attribute to XML.
142       *
143       * @param writer The writer.
144       * @throws ServiceXmlSerializationException the service xml serialization exception
145       */
146      @Override
147      public void writeAttributesToXml(EwsServiceXmlWriter writer)
148          throws ServiceXmlSerializationException {
149        writer.writeAttributeValue(XmlAttributeNames.BodyType, this
150            .getBodyType());
151      }
152    
153      /**
154       * Writes elements to XML.
155       *
156       * @param writer The writer.
157       * @throws ServiceXmlSerializationException the service xml serialization exception
158       */
159      @Override
160      public void writeElementsToXml(EwsServiceXmlWriter writer)
161          throws ServiceXmlSerializationException {
162        if (null != this.text && !this.text.isEmpty()) {
163          writer.writeValue(this.getText(), XmlElementNames.Body);
164        }
165      }
166    
167      /**
168       * Gets the type of the message body's text.
169       *
170       * @return BodyType enum
171       */
172      public BodyType getBodyType() {
173        return this.bodyType;
174      }
175    
176      /**
177       * Sets the type of the message body's text.
178       *
179       * @param bodyType BodyType enum
180       */
181      public void setBodyType(BodyType bodyType) {
182        if (this.canSetFieldValue(this.bodyType, bodyType)) {
183          this.bodyType = bodyType;
184          this.changed();
185        }
186      }
187    
188      /**
189       * Gets the text of the message body.
190       *
191       * @return message body text
192       */
193      private String getText() {
194        return this.text;
195      }
196    
197      /**
198       * Sets the text of the message body.
199       *
200       * @param text message body text
201       */
202      public void setText(String text) {
203        if (this.canSetFieldValue(this.text, text)) {
204          this.text = text;
205          this.changed();
206        }
207      }
208    
209      /**
210       * Returns a String that represents the current Object.
211       *
212       * @return the string
213       */
214      @Override
215      public String toString() {
216        return (this.text == null) ? "" : this.text;
217      }
218    }