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 2012-2016 ForgeRock AS.
016 */
017package org.opends.server.schema;
018
019import static org.opends.server.schema.SchemaConstants.*;
020
021import java.util.List;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.opendj.config.server.ConfigChangeResult;
025import org.forgerock.opendj.config.server.ConfigException;
026import org.forgerock.opendj.ldap.schema.Schema;
027import org.forgerock.opendj.ldap.schema.SchemaOptions;
028import org.forgerock.opendj.ldap.schema.Syntax;
029import org.forgerock.opendj.config.server.ConfigurationChangeListener;
030import org.forgerock.opendj.server.config.server.JPEGAttributeSyntaxCfg;
031import org.opends.server.api.AttributeSyntax;
032import org.opends.server.core.ServerContext;
033import org.opends.server.types.DirectoryException;
034
035/**
036 * This class implements the JPEG attribute syntax.  This is actually
037 * two specifications - JPEG and JFIF. As an extension we allow JPEG
038 * and Exif, which is what most digital cameras use. We only check for
039 * valid JFIF and Exif headers.
040 */
041public class JPEGSyntax
042       extends AttributeSyntax<JPEGAttributeSyntaxCfg>
043       implements ConfigurationChangeListener<JPEGAttributeSyntaxCfg>
044{
045
046  /** The current configuration for this JPEG syntax. */
047  private volatile JPEGAttributeSyntaxCfg config;
048
049  private ServerContext serverContext;
050
051  /**
052   * Creates a new instance of this syntax.  Note that the only thing that
053   * should be done here is to invoke the default constructor for the
054   * superclass.  All initialization should be performed in the
055   * <CODE>initializeSyntax</CODE> method.
056   */
057  public JPEGSyntax()
058  {
059    super();
060  }
061
062  @Override
063  public void initializeSyntax(JPEGAttributeSyntaxCfg configuration, ServerContext serverContext)
064      throws ConfigException, DirectoryException
065  {
066    this.config = configuration;
067    this.serverContext = serverContext;
068    serverContext.getSchema().updateSchemaOption(SchemaOptions.ALLOW_MALFORMED_JPEG_PHOTOS, !config.isStrictFormat());
069    config.addJPEGChangeListener(this);
070  }
071
072  @Override
073  public Syntax getSDKSyntax(Schema schema)
074  {
075    return schema.getSyntax(SchemaConstants.SYNTAX_JPEG_OID);
076  }
077
078  @Override
079  public String getName()
080  {
081    return SYNTAX_JPEG_NAME;
082  }
083
084  @Override
085  public String getOID()
086  {
087    return SYNTAX_JPEG_OID;
088  }
089
090  @Override
091  public String getDescription()
092  {
093    return SYNTAX_JPEG_DESCRIPTION;
094  }
095
096  @Override
097  public boolean isConfigurationChangeAcceptable(
098                      JPEGAttributeSyntaxCfg configuration,
099                      List<LocalizableMessage> unacceptableReasons)
100  {
101    // The configuration will always be acceptable.
102    return true;
103  }
104
105  @Override
106  public ConfigChangeResult applyConfigurationChange(
107              JPEGAttributeSyntaxCfg configuration)
108  {
109    this.config = configuration;
110    final ConfigChangeResult ccr = new ConfigChangeResult();
111    try
112    {
113      serverContext.getSchema().updateSchemaOption(SchemaOptions.ALLOW_MALFORMED_JPEG_PHOTOS, !config.isStrictFormat());
114    }
115    catch (DirectoryException e)
116    {
117      ccr.setResultCode(e.getResultCode());
118      ccr.addMessage(e.getMessageObject());
119    }
120    return ccr;
121  }
122}
123