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.credential;
025    
026    import microsoft.exchange.webservices.data.core.request.HttpWebRequest;
027    
028    /**
029     * WebCredentials is used for password-based authentication schemes such as
030     * basic, digest, NTLM, and Kerberos authentication.
031     */
032    public final class WebCredentials extends ExchangeCredentials {
033    
034      /**
035       * The domain.
036       */
037      private String domain;
038    
039      /**
040       * The user.
041       */
042      private String user;
043    
044      /**
045       * The pwd.
046       */
047      private String pwd;
048    
049      /**
050       * The use default credential.
051       */
052      private boolean useDefaultCredentials = true;
053    
054      /**
055       * Gets the domain.
056       *
057       * @return the domain
058       */
059      public String getDomain() {
060        return domain;
061      }
062    
063      /**
064       * Gets the user.
065       *
066       * @return the user
067       */
068      public String getUser() {
069        return user;
070      }
071    
072      /**
073       * Gets the pwd.
074       *
075       * @return the pwd
076       */
077      public String getPwd() {
078        return pwd;
079      }
080    
081      /**
082       * Checks if is use default credential.
083       *
084       * @return true, if is use default credential
085       */
086      public boolean isUseDefaultCredentials() {
087        return useDefaultCredentials;
088      }
089    
090      /**
091       * Initializes a new instance to use default network credential.
092       */
093      public WebCredentials() {
094        useDefaultCredentials = true;
095        this.user = null;
096        this.pwd = null;
097        this.domain = null;
098      }
099    
100      /**
101       * Initializes a new instance to use specified credential.
102       *
103       * @param userName Account user name.
104       * @param password Account password.
105       * @param domain   Account domain.
106       */
107      public WebCredentials(String userName, String password, String domain) {
108        if (userName == null || password == null) {
109          throw new IllegalArgumentException(
110              "User name or password can not be null");
111        }
112    
113        this.domain = domain;
114        this.user = userName;
115        this.pwd = password;
116        useDefaultCredentials = false;
117      }
118    
119      /**
120       * Initializes a new instance to use specified credential.
121       *
122       * @param username The user name.
123       * @param password The password.
124       */
125      public WebCredentials(String username, String password) {
126        this(username, password, "");
127      }
128    
129      /**
130       * This method is called to apply credential to a service request before
131       * the request is made.
132       *
133       * @param request The request.
134       */
135      @Override public void prepareWebRequest(HttpWebRequest request) {
136        if (useDefaultCredentials) {
137          request.setUseDefaultCredentials(true);
138        } else {
139          request.setCredentials(domain, user, pwd);
140        }
141      }
142    }