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.XmlAttributeNames;
029 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlDeserializationException;
030 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
031 import org.apache.commons.codec.binary.Base64;
032
033 import javax.xml.stream.XMLStreamException;
034
035 /**
036 * Represents the MIME content of an item.
037 */
038 public final class MimeContent extends ComplexProperty {
039
040 /**
041 * The character set.
042 */
043 private String characterSet;
044
045 /**
046 * The content.
047 */
048 private byte[] content;
049
050 /**
051 * Initializes a new instance of the class.
052 */
053 public MimeContent() {
054 }
055
056 /**
057 * Initializes a new instance of the class.
058 *
059 * @param characterSet the character set
060 * @param content the content
061 */
062 public MimeContent(String characterSet, byte[] content) {
063 this();
064 this.characterSet = characterSet;
065 this.content = content;
066 }
067
068 /**
069 * Reads attribute from XML.
070 *
071 * @param reader the reader
072 * @throws Exception the exception
073 */
074 @Override
075 public void readAttributesFromXml(EwsServiceXmlReader reader)
076 throws Exception {
077 this.characterSet = reader.readAttributeValue(String.class,
078 XmlAttributeNames.CharacterSet);
079 }
080
081 /**
082 * Reads text value from XML.
083 *
084 * @param reader the reader
085 * @throws XMLStreamException the XML stream exception
086 * @throws ServiceXmlDeserializationException the service xml deserialization exception
087 */
088 @Override
089 public void readTextValueFromXml(EwsServiceXmlReader reader)
090 throws XMLStreamException, ServiceXmlDeserializationException {
091 this.content = Base64.decodeBase64(reader.readValue());
092 }
093
094 /**
095 * Writes attribute to XML.
096 *
097 * @param writer the writer
098 * @throws ServiceXmlSerializationException the service xml serialization exception
099 */
100 @Override
101 public void writeAttributesToXml(EwsServiceXmlWriter writer)
102 throws ServiceXmlSerializationException {
103 writer.writeAttributeValue(XmlAttributeNames.CharacterSet,
104 this.characterSet);
105 }
106
107 /**
108 * Writes elements to XML.
109 *
110 * @param writer the writer
111 * @throws XMLStreamException the XML stream exception
112 */
113 public void writeElementsToXml(EwsServiceXmlWriter writer)
114 throws XMLStreamException {
115 if (this.content != null && this.content.length > 0) {
116 writer.writeBase64ElementValue(this.content);
117 }
118 }
119
120 /**
121 * Gets the character set of the content.
122 *
123 * @return the character set
124 */
125 public String getCharacterSet() {
126 return this.characterSet;
127 }
128
129 /**
130 * Sets the character set.
131 *
132 * @param characterSet the new character set
133 */
134 public void setCharacterSet(String characterSet) {
135 this.canSetFieldValue(this.characterSet, characterSet);
136 }
137
138 /**
139 * Gets the character set of the content.
140 *
141 * @return the content
142 */
143 public byte[] getContent() {
144 return this.content;
145 }
146
147 /**
148 * Sets the content.
149 *
150 * @param content the new content
151 */
152 public void setContent(byte[] content) {
153 this.canSetFieldValue(this.content, content);
154 }
155
156 /**
157 * Writes attribute to XML.
158 *
159 * @return the string
160 */
161 @Override
162 public String toString() {
163 if (this.getContent() == null) {
164 return "";
165 } else {
166 try {
167
168 // Try to convert to original MIME content using specified
169 // charset. If this fails,
170 // return the Base64 representation of the content.
171 // Note: Encoding.GetString can throw DecoderFallbackException
172 // which is a subclass
173 // of ArgumentException.
174 String charSet = (this.getCharacterSet() == null ||
175 this.getCharacterSet().isEmpty()) ?
176 "UTF-8" : this.getCharacterSet();
177 return new String(this.getContent(), charSet);
178 } catch (Exception e) {
179 return Base64.encodeBase64String(this.getContent());
180 }
181 }
182 }
183
184 }