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.core.request;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.PropertySet;
030    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
031    import microsoft.exchange.webservices.data.core.XmlElementNames;
032    import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
033    import microsoft.exchange.webservices.data.core.response.GetAttachmentResponse;
034    import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
035    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
036    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
037    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
038    import microsoft.exchange.webservices.data.property.complex.Attachment;
039    import microsoft.exchange.webservices.data.property.definition.PropertyDefinitionBase;
040    
041    import javax.xml.stream.XMLStreamException;
042    
043    import java.util.ArrayList;
044    import java.util.List;
045    
046    /**
047     * Represents a GetAttachment request.
048     */
049    public final class GetAttachmentRequest extends
050        MultiResponseServiceRequest<GetAttachmentResponse> {
051    
052      /**
053       * The attachments.
054       */
055      private List<Attachment> attachments = new ArrayList<Attachment>();
056    
057      /**
058       * The additional property.
059       */
060      private List<PropertyDefinitionBase> additionalProperties =
061          new ArrayList<PropertyDefinitionBase>();
062    
063      /**
064       * The body type.
065       */
066      private BodyType bodyType;
067    
068      /**
069       * Initializes a new instance of the GetAttachmentRequest class.
070       *
071       * @param service           The service.
072       * @param errorHandlingMode Indicates how errors should be handled.
073       * @throws Exception
074       */
075      public GetAttachmentRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
076          throws Exception {
077        super(service, errorHandlingMode);
078      }
079    
080      /**
081       * Validate request.
082       *
083       * @throws Exception the exception
084       */
085      @Override
086      protected void validate() throws Exception {
087        super.validate();
088        EwsUtilities.validateParamCollection(this.getAttachments().iterator(), "Attachments");
089        for (int i = 0; i < this.getAdditionalProperties().size(); i++) {
090          EwsUtilities.validateParam(this.getAdditionalProperties().get(i),
091              String.format("AdditionalProperties[%d]", i));
092        }
093      }
094    
095      /**
096       * Creates the service response.
097       *
098       * @param service       The service.
099       * @param responseIndex Index of the response.
100       * @return Service response.
101       */
102      @Override
103      protected GetAttachmentResponse createServiceResponse(
104          ExchangeService service, int responseIndex) {
105        return new GetAttachmentResponse(this.getAttachments().get(
106            responseIndex));
107      }
108    
109      /**
110       * Gets the expected response message count.
111       *
112       * @return Number of expected response messages.
113       */
114      @Override
115      protected int getExpectedResponseMessageCount() {
116        return this.getAttachments().size();
117      }
118    
119      /**
120       * Gets the name of the XML element.
121       *
122       * @return XML element name
123       */
124      @Override public String getXmlElementName() {
125        return XmlElementNames.GetAttachment;
126      }
127    
128      /**
129       * Gets the name of the response XML element.
130       *
131       * @return XML element name
132       */
133      @Override
134      protected String getResponseXmlElementName() {
135        return XmlElementNames.GetAttachmentResponse;
136      }
137    
138      /**
139       * Gets the name of the response message XML element.
140       *
141       * @return XML element name
142       */
143      @Override
144      protected String getResponseMessageXmlElementName() {
145        return XmlElementNames.GetAttachmentResponseMessage;
146      }
147    
148      /**
149       * Writes XML elements.
150       *
151       * @param writer the writer
152       * @throws XMLStreamException the XML stream exception
153       * @throws ServiceXmlSerializationException the service xml serialization exception
154       */
155      @Override
156      protected void writeElementsToXml(EwsServiceXmlWriter writer)
157          throws XMLStreamException, ServiceXmlSerializationException {
158        if ((this.getBodyType() != null)
159            || this.getAdditionalProperties().size() > 0) {
160          writer.writeStartElement(XmlNamespace.Messages,
161              XmlElementNames.AttachmentShape);
162    
163          if (this.getBodyType() != null) {
164            writer.writeElementValue(XmlNamespace.Types,
165                XmlElementNames.BodyType, this.getBodyType());
166          }
167    
168          if (this.getAdditionalProperties().size() > 0) {
169            PropertySet.writeAdditionalPropertiesToXml(writer, this.getAdditionalProperties().iterator());
170          }
171    
172          writer.writeEndElement(); // AttachmentShape
173        }
174    
175        writer.writeStartElement(XmlNamespace.Messages,
176            XmlElementNames.AttachmentIds);
177    
178        for (Attachment attachment : this.getAttachments()) {
179          writer.writeStartElement(XmlNamespace.Types,
180              XmlElementNames.AttachmentId);
181          writer
182              .writeAttributeValue(XmlAttributeNames.Id, attachment
183                  .getId());
184          writer.writeEndElement();
185        }
186    
187        writer.writeEndElement();
188      }
189    
190      /**
191       * Gets the request version.
192       *
193       * @return Earliest Exchange version in which this request is supported.
194       */
195      @Override
196      protected ExchangeVersion getMinimumRequiredServerVersion() {
197        return ExchangeVersion.Exchange2007_SP1;
198      }
199    
200      /**
201       * Gets the attachments.
202       *
203       * @return the attachments
204       */
205      public List<Attachment> getAttachments() {
206        return this.attachments;
207      }
208    
209      /**
210       * Gets the additional property.
211       *
212       * @return the additional property
213       */
214      public List<PropertyDefinitionBase> getAdditionalProperties() {
215        return this.additionalProperties;
216      }
217    
218      /**
219       * Gets  the type of the body.
220       *
221       * @return the body type
222       */
223      public BodyType getBodyType() {
224    
225        return this.bodyType;
226    
227      }
228    
229      /**
230       * Sets the body type.
231       *
232       * @param bodyType the new body type
233       */
234      public void setBodyType(BodyType bodyType) {
235        this.bodyType = bodyType;
236      }
237    
238    }