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