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
024package microsoft.exchange.webservices.data.misc;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027import microsoft.exchange.webservices.data.core.XmlElementNames;
028import microsoft.exchange.webservices.data.core.enumeration.misc.ConnectingIdType;
029import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
030import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031
032/**
033 * Represents an impersonated user Id.
034 */
035public final class ImpersonatedUserId {
036
037  /**
038   * The id type.
039   */
040  private ConnectingIdType idType;
041
042  /**
043   * The id.
044   */
045  private String id;
046
047  /**
048   * Instantiates a new impersonated user id.
049   */
050  public ImpersonatedUserId() {
051  }
052
053  /**
054   * Initializes a new instance of ConnectingId.
055   *
056   * @param idType The type of this Id.
057   * @param id     The user Id.
058   */
059  public ImpersonatedUserId(ConnectingIdType idType, String id) {
060    this();
061    this.idType = idType;
062    this.id = id;
063  }
064
065  /**
066   * Writes to XML.
067   *
068   * @param writer The writer
069   * @throws Exception the exception
070   */
071  public void writeToXml(EwsServiceXmlWriter writer) throws Exception {
072    if (this.id == null || this.id.isEmpty()) {
073      throw new Exception("The Id property must be set.");
074    }
075
076    writer.writeStartElement(XmlNamespace.Types,
077        XmlElementNames.ExchangeImpersonation);
078    writer.writeStartElement(XmlNamespace.Types,
079        XmlElementNames.ConnectingSID);
080
081    // For 2007 SP1, use PrimarySmtpAddress for type SmtpAddress
082    String connectingIdTypeLocalName = (this.idType ==
083        ConnectingIdType.SmtpAddress) &&
084        (writer.getService().getRequestedServerVersion() ==
085            ExchangeVersion.Exchange2007_SP1) ?
086        XmlElementNames.PrimarySmtpAddress :
087        this.getIdType().toString();
088
089    writer.writeElementValue(XmlNamespace.Types, connectingIdTypeLocalName,
090        this.id);
091
092    writer.writeEndElement(); // ConnectingSID
093    writer.writeEndElement(); // ExchangeImpersonation
094  }
095
096  /**
097   * Gets  the type of the Id.
098   *
099   * @return the id type
100   */
101  public ConnectingIdType getIdType() {
102    return idType;
103  }
104
105  /**
106   * Sets the id type.
107   *
108   * @param idType the new id type
109   */
110  public void setIdType(ConnectingIdType idType) {
111    this.idType = idType;
112  }
113
114  /**
115   * Gets  the user Id.
116   *
117   * @return the id
118   */
119  public String getId() {
120    return id;
121  }
122
123  /**
124   * Sets the id.
125   *
126   * @param id the new id
127   */
128  public void setId(String id) {
129    this.id = id;
130  }
131
132}