View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  
27  
28  /**
29   * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517.
30   * <p>
31   * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with
32   * the following bytes :
33   * <pre>
34   * 0xFF 0xD8 (SOI, Start Of Image)
35   * 0xFF 0xE0 (App0 for JFIF) or 0xDD 0xE1 (App1 for Exif)
36   * 0xNN 0xNN (Header length)
37   * "JFIF\0" (JFIF string with an ending \0)
38   * some other bytes which are related to the image.
39   * <pre>
40   * 
41   * We will check for those 11 bytes, except the length.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  @SuppressWarnings("serial")
46  public final class JpegSyntaxChecker extends SyntaxChecker
47  {
48      /**
49       * A static instance of JpegSyntaxChecker
50       */
51      public static final JpegSyntaxChecker INSTANCE = new JpegSyntaxChecker( SchemaConstants.JPEG_SYNTAX );
52      
53      /**
54       * A static Builder for this class
55       */
56      public static final class Builder extends SCBuilder<JpegSyntaxChecker>
57      {
58          /**
59           * The Builder constructor
60           */
61          private Builder()
62          {
63              super( SchemaConstants.JPEG_SYNTAX );
64          }
65          
66          
67          /**
68           * Create a new instance of JpegSyntaxChecker
69           * @return A new instance of JpegSyntaxChecker
70           */
71          @Override
72          public JpegSyntaxChecker build()
73          {
74              return new JpegSyntaxChecker( oid );
75          }
76      }
77  
78      
79      /**
80       * Creates a new instance of JpegSyntaxChecker.
81       * 
82       * @param oid The OID to use for this SyntaxChecker
83       */
84      private JpegSyntaxChecker( String oid )
85      {
86          super( oid );
87      }
88  
89      
90      /**
91       * @return An instance of the Builder for this class
92       */
93      public static Builder builder()
94      {
95          return new Builder();
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public boolean isValidSyntax( Object value )
104     {
105         if ( value == null )
106         {
107             if ( LOG.isDebugEnabled() )
108             {
109                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
110             }
111             
112             return false;
113         }
114 
115         // The value must be a byte array
116         if ( !( value instanceof byte[] ) )
117         {
118             if ( LOG.isDebugEnabled() )
119             {
120                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
121             }
122             
123             return false;
124         }
125 
126         byte[] bytes = ( byte[] ) value;
127 
128         // The header must be at least 11 bytes long
129         if ( bytes.length < 11 )
130         {
131             if ( LOG.isDebugEnabled() )
132             {
133                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134             }
135             
136             return false;
137         }
138 
139         // SOI or APP0 or APP1 format
140         if ( ( bytes[0] == ( byte ) 0x00FF )
141             && ( bytes[1] == ( byte ) 0x00D8 )
142             && ( bytes[2] == ( byte ) 0x00FF ) )
143         {
144             // JFIF format
145             if ( ( bytes[3] == ( byte ) 0x00E0 )
146                 && ( bytes[6] == 'J' )
147                 && ( bytes[7] == 'F' )
148                 && ( bytes[8] == 'I' )
149                 && ( bytes[9] == 'F' )
150                 && ( bytes[10] == 0x00 ) )
151             {
152                 if ( LOG.isDebugEnabled() )
153                 {
154                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
155                 }
156                 
157                 return true;
158             }
159         // EXIF Format
160             else if ( ( bytes[3] == ( byte ) 0x00E1 ) 
161                     && ( bytes[6] == 'E' )
162                     && ( bytes[7] == 'x' )
163                     && ( bytes[8] == 'i' )
164                     && ( bytes[9] == 'f' )
165                     && ( bytes[10] == 0x00 ) )
166             {
167                 if ( LOG.isDebugEnabled() )
168                 {
169                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
170                 }
171                 
172                 return true;
173             }
174         }
175 
176         if ( LOG.isDebugEnabled() )
177         {
178             LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
179         }
180         
181         return false;
182     }
183 }