001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema.syntaxCheckers; 021 022 023import java.util.regex.Pattern; 024 025import org.apache.directory.api.i18n.I18n; 026import org.apache.directory.api.ldap.model.constants.SchemaConstants; 027import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 028import org.apache.directory.api.util.Strings; 029 030 031/** 032 * A SyntaxChecker which verifies that a value is a UTC time 033 * according to RFC 4517. 034 * <p> 035 * From RFC 4517 : 036 * <pre> 037 * UTCTime = year month day hour minute [ second ] [ u-time-zone ] 038 * u-time-zone = %x5A ; "Z" | u-differential 039 * u-differential = ( MINUS | PLUS ) hour minute 040 * 041 * year = 2(%x30-39) ; "00" to "99" 042 * month = ( %x30 %x31-39 ) ; "01" (January) to "09" 043 * | ( %x31 %x30-32 ) ; "10" to "12" 044 * day = ( %x30 %x31-39 ) ; "01" to "09" 045 * | ( %x31-32 %x30-39 ) ; "10" to "29" 046 * | ( %x33 %x30-31 ) ; "30" to "31" 047 * hour = ( %x30-31 %x30-39 ) 048 * | ( %x32 %x30-33 ) ; "00" to "23" 049 * minute = %x30-35 %x30-39 ; "00" to "59" 050 * 051 * second = ( %x30-35 %x30-39 ) ; "00" to "59" 052 * 053 * g-time-zone = %x5A ; "Z" 054 * | g-differential 055 * g-differential = ( MINUS / PLUS ) hour [ minute ] 056 * MINUS = %x2D ; minus sign ("-") 057 * 058 * From RFC 4512 : 059 * PLUS = %x2B ; plus sign ("+") 060 * </pre> 061 * 062 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 063 */ 064@SuppressWarnings("serial") 065public final class UtcTimeSyntaxChecker extends SyntaxChecker 066{ 067 /** The GeneralizedDate pattern matching */ 068 private static final String UTC_TIME_PATTERN = 069 // year : 00 to 99 070 "^\\d{2}" 071 // month : 01 to 12 072 + "(0[1-9]|1[0-2])" 073 // day : 01 to 31 074 + "(0[1-9]|[12]\\d|3[01])" 075 // hour: 00 to 23 076 + "([01]\\d|2[0-3])" 077 // minute : 00 to 59 078 + "([0-5]\\d)" 079 + "(" 080 // optional second : 00 to 59 081 + "([0-5]\\d)?" 082 // optional time-zone 083 + "(Z|([+-]([01]\\d|2[0-3])[0-5]\\d))?" 084 + ")$"; 085 086 // The regexp pattern, java.util.regex.Pattern is immutable so only one instance is needed. 087 private static final Pattern DATE_PATTERN = Pattern.compile( UTC_TIME_PATTERN ); 088 089 /** 090 * A static instance of UtcTimeSyntaxChecker 091 */ 092 public static final UtcTimeSyntaxChecker INSTANCE = new UtcTimeSyntaxChecker( SchemaConstants.UTC_TIME_SYNTAX ); 093 094 /** 095 * A static Builder for this class 096 */ 097 public static final class Builder extends SCBuilder<UtcTimeSyntaxChecker> 098 { 099 /** 100 * The Builder constructor 101 */ 102 private Builder() 103 { 104 super( SchemaConstants.UTC_TIME_SYNTAX ); 105 } 106 107 108 /** 109 * Create a new instance of UtcTimeSyntaxChecker 110 * @return A new instance of UtcTimeSyntaxChecker 111 */ 112 @Override 113 public UtcTimeSyntaxChecker build() 114 { 115 return new UtcTimeSyntaxChecker( oid ); 116 } 117 } 118 119 120 /** 121 * Creates a new instance of UtcTimeSyntaxChecker. 122 * 123 * @param oid The OID to use for this SyntaxChecker 124 */ 125 private UtcTimeSyntaxChecker( String oid ) 126 { 127 super( oid ); 128 } 129 130 131 /** 132 * @return An instance of the Builder for this class 133 */ 134 public static Builder builder() 135 { 136 return new Builder(); 137 } 138 139 140 /** 141 * {@inheritDoc} 142 */ 143 @Override 144 public boolean isValidSyntax( Object value ) 145 { 146 String strValue; 147 148 if ( value == null ) 149 { 150 if ( LOG.isDebugEnabled() ) 151 { 152 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 153 } 154 155 return false; 156 } 157 158 if ( value instanceof String ) 159 { 160 strValue = ( String ) value; 161 } 162 else if ( value instanceof byte[] ) 163 { 164 strValue = Strings.utf8ToString( ( byte[] ) value ); 165 } 166 else 167 { 168 strValue = value.toString(); 169 } 170 171 // A generalized time must have a minimal length of 11 172 if ( strValue.length() < 11 ) 173 { 174 if ( LOG.isDebugEnabled() ) 175 { 176 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 177 } 178 179 return false; 180 } 181 182 // Start the date parsing 183 boolean result; 184 185 synchronized ( DATE_PATTERN ) 186 { 187 result = DATE_PATTERN.matcher( strValue ).find(); 188 } 189 190 if ( result ) 191 { 192 if ( LOG.isDebugEnabled() ) 193 { 194 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 195 } 196 } 197 else 198 { 199 if ( LOG.isDebugEnabled() ) 200 { 201 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 202 } 203 } 204 205 return result; 206 } 207}