001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.controls; 028 import org.opends.messages.Message; 029 030 031 032 import org.opends.server.protocols.asn1.ASN1OctetString; 033 import org.opends.server.protocols.ldap.LDAPResultCode; 034 import org.opends.server.types.Control; 035 import org.opends.server.types.DebugLogLevel; 036 import org.opends.server.types.LDAPException; 037 038 import static org.opends.server.loggers.debug.DebugLogger.*; 039 import org.opends.server.loggers.debug.DebugTracer; 040 import static org.opends.messages.ProtocolMessages.*; 041 import static org.opends.server.util.ServerConstants.*; 042 import static org.opends.server.util.StaticUtils.*; 043 044 045 046 /** 047 * This class implements the Netscape password expiring control, which serves as 048 * a warning to clients that the user's password is about to expire. The only 049 * element contained in the control value is a string representation of the 050 * number of seconds until expiration. 051 */ 052 public class PasswordExpiringControl 053 extends Control 054 { 055 /** 056 * The tracer object for the debug logger. 057 */ 058 private static final DebugTracer TRACER = getTracer(); 059 060 061 062 063 // The length of time in seconds until the password actually expires. 064 private int secondsUntilExpiration; 065 066 067 068 /** 069 * Creates a new instance of the password expiring control with the provided 070 * information. 071 * 072 * @param secondsUntilExpiration The length of time in seconds until the 073 * password actually expires. 074 */ 075 public PasswordExpiringControl(int secondsUntilExpiration) 076 { 077 super(OID_NS_PASSWORD_EXPIRING, false, 078 new ASN1OctetString(String.valueOf(secondsUntilExpiration))); 079 080 081 this.secondsUntilExpiration = secondsUntilExpiration; 082 } 083 084 085 086 /** 087 * Creates a new instance of the password expiring control with the provided 088 * information. 089 * 090 * @param oid The OID to use for this control. 091 * @param isCritical Indicates whether support for this control 092 * should be considered a critical part of the 093 * client processing. 094 * @param secondsUntilExpiration The length of time in seconds until the 095 * password actually expires. 096 */ 097 public PasswordExpiringControl(String oid, boolean isCritical, 098 int secondsUntilExpiration) 099 { 100 super(oid, isCritical, 101 new ASN1OctetString(String.valueOf(secondsUntilExpiration))); 102 103 104 this.secondsUntilExpiration = secondsUntilExpiration; 105 } 106 107 108 109 /** 110 * Creates a new instance of the password expiring control with the provided 111 * information. 112 * 113 * @param oid The OID to use for this control. 114 * @param isCritical Indicates whether support for this control 115 * should be considered a critical part of the 116 * client processing. 117 * @param secondsUntilExpiration The length of time in seconds until the 118 * password actually expires. 119 * @param encodedValue The pre-encoded value for this control. 120 */ 121 private PasswordExpiringControl(String oid, boolean isCritical, 122 int secondsUntilExpiration, 123 ASN1OctetString encodedValue) 124 { 125 super(oid, isCritical, encodedValue); 126 127 128 this.secondsUntilExpiration = secondsUntilExpiration; 129 } 130 131 132 133 /** 134 * Creates a new password expiring control from the contents of the provided 135 * control. 136 * 137 * @param control The generic control containing the information to use to 138 * create this password expiring control. 139 * 140 * @return The password expiring control decoded from the provided control. 141 * 142 * @throws LDAPException If this control cannot be decoded as a valid 143 * password expiring control. 144 */ 145 public static PasswordExpiringControl decodeControl(Control control) 146 throws LDAPException 147 { 148 if (! control.hasValue()) 149 { 150 Message message = ERR_PWEXPIRING_NO_CONTROL_VALUE.get(); 151 throw new LDAPException(LDAPResultCode.PROTOCOL_ERROR, message); 152 } 153 154 155 int secondsUntilExpiration; 156 try 157 { 158 secondsUntilExpiration = 159 Integer.parseInt(control.getValue().stringValue()); 160 } 161 catch (Exception e) 162 { 163 if (debugEnabled()) 164 { 165 TRACER.debugCaught(DebugLogLevel.ERROR, e); 166 } 167 168 Message message = ERR_PWEXPIRING_CANNOT_DECODE_SECONDS_UNTIL_EXPIRATION. 169 get(getExceptionMessage(e)); 170 throw new LDAPException(LDAPResultCode.PROTOCOL_ERROR, message); 171 } 172 173 174 return new PasswordExpiringControl(control.getOID(), control.isCritical(), 175 secondsUntilExpiration, 176 control.getValue()); 177 } 178 179 180 181 /** 182 * Retrieves the length of time in seconds until the password actually 183 * expires. 184 * 185 * @return The length of time in seconds until the password actually expires. 186 */ 187 public int getSecondsUntilExpiration() 188 { 189 return secondsUntilExpiration; 190 } 191 192 193 194 /** 195 * Retrieves a string representation of this password expiring control. 196 * 197 * @return A string representation of this password expiring control. 198 */ 199 public String toString() 200 { 201 StringBuilder buffer = new StringBuilder(); 202 toString(buffer); 203 return buffer.toString(); 204 } 205 206 207 208 /** 209 * Appends a string representation of this password expiring control to the 210 * provided buffer. 211 * 212 * @param buffer The buffer to which the information should be appended. 213 */ 214 public void toString(StringBuilder buffer) 215 { 216 buffer.append("PasswordExpiringControl(secondsUntilExpiration="); 217 buffer.append(secondsUntilExpiration); 218 buffer.append(")"); 219 } 220 } 221