1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthState.java,v 1.2 2004/04/18 23:51:36 jsdever Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2004/04/18 23:51:36 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.auth;
  30. /**
  31. * This class provides detailed information about the state of the
  32. * authentication process.
  33. *
  34. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  35. * @since 3.0
  36. */
  37. public class AuthState {
  38. /** Actual authentication scheme */
  39. private AuthScheme authScheme = null;
  40. /** Whether preemtive authentication is attempted */
  41. private boolean preemptive = false;
  42. /**
  43. * Default constructor.
  44. *
  45. */
  46. public AuthState() {
  47. super();
  48. }
  49. /**
  50. * Preemptively assigns Basic authentication scheme.
  51. */
  52. public void setPreemptive() {
  53. if (this.authScheme != null) {
  54. throw new IllegalStateException("Authentication state already initialized");
  55. }
  56. this.authScheme = AuthPolicy.getAuthScheme("basic");
  57. this.preemptive = true;
  58. }
  59. /**
  60. * Invalidates the authentication state by resetting its parameters.
  61. */
  62. public void invalidate() {
  63. this.authScheme = null;
  64. this.preemptive = false;
  65. }
  66. /**
  67. * Tests if preemptive authentication is used.
  68. *
  69. * @return <tt>true</tt> if using the default Basic {@link AuthScheme
  70. * authentication scheme}, <tt>false</tt> otherwise.
  71. */
  72. public boolean isPreemptive() {
  73. return this.preemptive;
  74. }
  75. /**
  76. * Assigns the given {@link AuthScheme authentication scheme}.
  77. *
  78. * @param authScheme the {@link AuthScheme authentication scheme}
  79. */
  80. public void setAuthScheme(final AuthScheme authScheme) {
  81. this.authScheme = authScheme;
  82. this.preemptive = false;
  83. }
  84. /**
  85. * Returns the {@link AuthScheme authentication scheme}.
  86. *
  87. * @return {@link AuthScheme authentication scheme}
  88. */
  89. public AuthScheme getAuthScheme() {
  90. return authScheme;
  91. }
  92. /**
  93. * Returns the authentication realm.
  94. *
  95. * @return the name of the authentication realm
  96. */
  97. public String getRealm() {
  98. if (this.authScheme != null) {
  99. return this.authScheme.getRealm();
  100. } else {
  101. return null;
  102. }
  103. }
  104. }