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.credential;
025
026import microsoft.exchange.webservices.data.core.request.HttpWebRequest;
027import microsoft.exchange.webservices.data.core.exception.misc.InvalidOperationException;
028
029import javax.xml.stream.XMLStreamException;
030import javax.xml.stream.XMLStreamWriter;
031
032import java.io.ByteArrayOutputStream;
033import java.net.URI;
034import java.net.URISyntaxException;
035
036/**
037 * Base class of Exchange credential types.
038 */
039public abstract class ExchangeCredentials {
040
041  /**
042   * Performs an implicit conversion from <see
043   * cref="System.Net.NetworkCredential"/> to <see
044   * cref="Microsoft.Exchange.WebServices.Data.ExchangeCredentials"/>. This
045   * allows a NetworkCredential object to be implictly converted to an
046   * ExchangeCredential which is useful when setting credential on an
047   * ExchangeService.
048   *
049   * @param userName Account user name.
050   * @param password Account password.
051   * @param domain   Account domain.
052   * @return The result of the conversion.
053   */
054  public static ExchangeCredentials
055  getExchangeCredentialsFromNetworkCredential(
056      String userName, String password, String domain) {
057    return new WebCredentials(userName, password, domain);
058  }
059
060
061  /**
062   * Return the url without ws-security address.
063   *
064   * @param url The url
065   * @return The absolute uri base.
066   */
067  protected static String getUriWithoutWSSecurity(URI url) {
068    String absoluteUri = url.toString();
069    int index = absoluteUri.indexOf("/wssecurity");
070
071    if (index == -1) {
072      return absoluteUri;
073    } else {
074      return absoluteUri.substring(0, index);
075    }
076  }
077
078  /**
079   * This method is called to pre-authenticate credential before a service
080   * request is made.
081   */
082  public void preAuthenticate() {
083    // do nothing by default.
084  }
085
086  /**
087   * This method is called to apply credential to a service request before
088   * the request is made.
089   *
090   * @param client The request.
091   * @throws java.net.URISyntaxException the uRI syntax exception
092   */
093  public void prepareWebRequest(HttpWebRequest client)
094      throws URISyntaxException {
095    // do nothing by default.
096  }
097
098  /**
099   * Emit any extra necessary namespace aliases for the SOAP:header block.
100   *
101   * @param writer the writer
102   * @throws XMLStreamException the XML stream exception
103   */
104  public void emitExtraSoapHeaderNamespaceAliases(XMLStreamWriter writer)
105      throws XMLStreamException {
106    // do nothing by default.
107  }
108
109  /**
110   * Serialize any extra necessary SOAP headers. This is used for
111   * authentication schemes that rely on WS-Security, or for endpoints
112   * requiring WS-Addressing.
113   *
114   * @param writer the writer
115   * @param webMethodName the Web method being called
116   * @throws XMLStreamException the XML stream exception
117   */
118  public void serializeExtraSoapHeaders(XMLStreamWriter writer, String webMethodName) throws XMLStreamException {
119    // do nothing by default.
120  }
121
122  /**
123   * Adjusts the URL endpoint based on the credential.
124   *
125   * @param url The URL.
126   * @return Adjust URL.
127   */
128  public URI adjustUrl(URI url) throws URISyntaxException {
129    return new URI(getUriWithoutWSSecurity(url));
130  }
131
132  /**
133   * Gets the flag indicating whether any sign action need taken.
134   */
135  public boolean isNeedSignature() {
136    return false;
137  }
138
139  /**
140   * Add the signature element to the memory stream.
141   *
142   * @param memoryStream The memory stream.
143   */
144  public void sign(ByteArrayOutputStream memoryStream) throws Exception {
145    throw new InvalidOperationException();
146  }
147
148
149
150  /**
151   * Serialize SOAP headers used for authentication schemes that rely on WS-Security.
152   *
153   * @param writer the writer
154   * @throws XMLStreamException the XML stream exception
155   */
156  public void serializeWSSecurityHeaders(XMLStreamWriter writer)
157      throws XMLStreamException {
158    // do nothing by default.
159  }
160
161}