001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.controls; 018import org.forgerock.i18n.LocalizableMessage; 019 020 021 022import org.forgerock.opendj.io.ASN1Writer; 023import org.opends.server.types.*; 024import org.forgerock.opendj.ldap.ResultCode; 025import org.forgerock.opendj.ldap.ByteString; 026import org.forgerock.i18n.slf4j.LocalizedLogger; 027import static org.opends.messages.ProtocolMessages.*; 028import static org.opends.server.util.ServerConstants.*; 029import static org.opends.server.util.StaticUtils.*; 030 031import java.io.IOException; 032 033 034/** 035 * This class implements the Netscape password expiring control, which serves as 036 * a warning to clients that the user's password is about to expire. The only 037 * element contained in the control value is a string representation of the 038 * number of seconds until expiration. 039 */ 040public class PasswordExpiringControl 041 extends Control 042{ 043 /** ControlDecoder implementation to decode this control from a ByteString. */ 044 private static final class Decoder 045 implements ControlDecoder<PasswordExpiringControl> 046 { 047 @Override 048 public PasswordExpiringControl decode(boolean isCritical, ByteString value) 049 throws DirectoryException 050 { 051 if (value == null) 052 { 053 LocalizableMessage message = ERR_PWEXPIRING_NO_CONTROL_VALUE.get(); 054 throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message); 055 } 056 057 int secondsUntilExpiration; 058 try 059 { 060 secondsUntilExpiration = 061 Integer.parseInt(value.toString()); 062 } 063 catch (Exception e) 064 { 065 logger.traceException(e); 066 067 LocalizableMessage message = ERR_PWEXPIRING_CANNOT_DECODE_SECONDS_UNTIL_EXPIRATION. 068 get(getExceptionMessage(e)); 069 throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message); 070 } 071 072 073 return new PasswordExpiringControl(isCritical, 074 secondsUntilExpiration); 075 } 076 077 @Override 078 public String getOID() 079 { 080 return OID_NS_PASSWORD_EXPIRING; 081 } 082 083 } 084 085 /** The Control Decoder that can be used to decode this control. */ 086 public static final ControlDecoder<PasswordExpiringControl> DECODER = 087 new Decoder(); 088 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 089 090 091 092 093 /** The length of time in seconds until the password actually expires. */ 094 private int secondsUntilExpiration; 095 096 097 098 /** 099 * Creates a new instance of the password expiring control with the provided 100 * information. 101 * 102 * @param secondsUntilExpiration The length of time in seconds until the 103 * password actually expires. 104 */ 105 public PasswordExpiringControl(int secondsUntilExpiration) 106 { 107 this(false, secondsUntilExpiration); 108 } 109 110 111 112 /** 113 * Creates a new instance of the password expiring control with the provided 114 * information. 115 * 116 * @param isCritical Indicates whether support for this control 117 * should be considered a critical part of the 118 * client processing. 119 * @param secondsUntilExpiration The length of time in seconds until the 120 * password actually expires. 121 */ 122 public PasswordExpiringControl(boolean isCritical, int secondsUntilExpiration) 123 { 124 super(OID_NS_PASSWORD_EXPIRING, isCritical); 125 126 127 this.secondsUntilExpiration = secondsUntilExpiration; 128 } 129 130 @Override 131 public void writeValue(ASN1Writer writer) throws IOException { 132 writer.writeOctetString(String.valueOf(secondsUntilExpiration)); 133 } 134 135 136 137 /** 138 * Retrieves the length of time in seconds until the password actually 139 * expires. 140 * 141 * @return The length of time in seconds until the password actually expires. 142 */ 143 public int getSecondsUntilExpiration() 144 { 145 return secondsUntilExpiration; 146 } 147 148 149 150 /** 151 * Appends a string representation of this password expiring control to the 152 * provided buffer. 153 * 154 * @param buffer The buffer to which the information should be appended. 155 */ 156 @Override 157 public void toString(StringBuilder buffer) 158 { 159 buffer.append("PasswordExpiringControl(secondsUntilExpiration="); 160 buffer.append(secondsUntilExpiration); 161 buffer.append(")"); 162 } 163} 164