You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2013/10/06 06:25:00 UTC
svn commit: r1529564 - in /directory/escimo/branches/json-schema-experiment:
common/ common/src/main/java/org/apache/directory/scim/
common/src/main/java/org/apache/directory/scim/schema/
ldap/src/main/java/org/apache/directory/scim/ldap/ ldap/src/main...
Author: kayyagari
Date: Sun Oct 6 04:25:00 2013
New Revision: 1529564
URL: http://svn.apache.org/r1529564
Log:
o added a class for holding json schema
o added readOnly variable to the schema models
o added dependency on gson in schema
o renamed handle() to read()
Added:
directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/JsonSchema.java
Modified:
directory/escimo/branches/json-schema-experiment/common/pom.xml
directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/AttributeHandler.java
directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/ProviderService.java
directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/schema/BaseType.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ActiveAttributeHandler.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MetaAttributeHandler.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java
directory/escimo/branches/json-schema-experiment/schema/pom.xml
directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/SchemaUtil.java
directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/SchemaService.java
directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/ServerInitializer.java
Modified: directory/escimo/branches/json-schema-experiment/common/pom.xml
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/common/pom.xml?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/common/pom.xml (original)
+++ directory/escimo/branches/json-schema-experiment/common/pom.xml Sun Oct 6 04:25:00 2013
@@ -41,10 +41,6 @@
<artifactId>wink-server</artifactId>
<version>${wink.version}</version>
</dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- </dependency>
</dependencies>
<build>
Modified: directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/AttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/AttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/AttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/AttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -30,5 +30,5 @@ import org.apache.directory.scim.schema.
*/
public interface AttributeHandler
{
- void handle( BaseType bt, Object srcResource, RequestContext ctx );
+ void read( BaseType bt, Object srcResource, RequestContext ctx );
}
Modified: directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/ProviderService.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/ProviderService.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/ProviderService.java (original)
+++ directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/ProviderService.java Sun Oct 6 04:25:00 2013
@@ -20,6 +20,8 @@ package org.apache.directory.scim;
import java.io.InputStream;
+import org.apache.directory.scim.schema.JsonSchema;
+
/**
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
@@ -35,4 +37,6 @@ public interface ProviderService
InputStream getUserPhoto( String id, String atName ) throws MissingParameterException;
Group getGroup( RequestContext ctx, String groupId ) throws ResourceNotFoundException;
+
+ JsonSchema getSchema( String uriId );
}
Modified: directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/schema/BaseType.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/schema/BaseType.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/schema/BaseType.java (original)
+++ directory/escimo/branches/json-schema-experiment/common/src/main/java/org/apache/directory/scim/schema/BaseType.java Sun Oct 6 04:25:00 2013
@@ -38,6 +38,8 @@ public abstract class BaseType
private String atHandlerName;
+ private boolean readOnly = false;
+
public BaseType( String uri, String name, boolean show )
{
this.uri = uri;
@@ -114,4 +116,22 @@ public abstract class BaseType
return false;
}
+
+ /**
+ * @return the readOnly
+ */
+ public boolean isReadOnly()
+ {
+ return readOnly;
+ }
+
+
+ /**
+ * @param readOnly the readOnly to set
+ */
+ public void setReadOnly( boolean readOnly )
+ {
+ this.readOnly = readOnly;
+ }
+
}
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java Sun Oct 6 04:25:00 2013
@@ -26,10 +26,13 @@ import static org.apache.directory.api.l
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import org.apache.directory.api.ldap.model.constants.SchemaConstants;
@@ -81,6 +84,8 @@ import org.apache.directory.scim.ldap.sc
import org.apache.directory.scim.ldap.schema.TypedType;
import org.apache.directory.scim.ldap.schema.UserSchema;
import org.apache.directory.scim.schema.BaseType;
+import org.apache.directory.scim.schema.JsonSchema;
+import org.apache.directory.scim.schema.SchemaUtil;
import org.apache.directory.scim.util.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -107,6 +112,8 @@ public class LdapResourceProvider implem
private SchemaManager ldapSchema;
+ private Map<String,JsonSchema> schemas = new HashMap<String, JsonSchema>();
+
private static final Logger LOG = LoggerFactory.getLogger( LdapResourceProvider.class );
@@ -124,6 +131,27 @@ public class LdapResourceProvider implem
public void init() throws Exception
{
LOG.info( "Initializing LDAP resource provider" );
+
+ try
+ {
+ JsonParser parser = new JsonParser();
+
+ List<URL> urls = SchemaUtil.getDefaultSchemas();
+ for( URL u : urls )
+ {
+ JsonSchema json = SchemaUtil.getSchemaJson( u );
+ schemas.put( json.getId(), json );
+ }
+
+ // TODO load custom schemas
+ }
+ catch( Exception e )
+ {
+ RuntimeException re = new RuntimeException( "Failed to load the default schemas" );
+ re.initCause( e );
+ throw re;
+ }
+
if ( connection == null )
{
createConnection();
@@ -134,7 +162,8 @@ public class LdapResourceProvider implem
( ( LdapNetworkConnection ) connection ).loadSchema( new JarLdifSchemaLoader() );
}
- schemaMapper = new LdapSchemaMapper();
+ Map<String,JsonSchema> jsonSchemaCopy = new HashMap<String, JsonSchema>( schemas );
+ schemaMapper = new LdapSchemaMapper( jsonSchemaCopy );
schemaMapper.loadMappings();
userSchema = schemaMapper.getUserSchema();
groupSchema = schemaMapper.getGroupSchema();
@@ -292,8 +321,13 @@ public class LdapResourceProvider implem
}
- private void processAttributeData( BaseType bt, JsonElement el, Entry entry ) throws LdapException
+ private void scimToLdapAttribute( BaseType bt, JsonElement el, Entry entry ) throws LdapException
{
+ if( bt.isReadOnly() )
+ {
+ return;
+ }
+
if( bt instanceof SimpleType )
{
SimpleType st = ( SimpleType ) bt;
@@ -448,7 +482,7 @@ public class LdapResourceProvider implem
if ( atHandler != null )
{
AttributeHandler handler = userSchema.getHandler( atHandler );
- handler.handle( ct, entry, ctx );
+ handler.read( ct, entry, ctx );
continue;
}
@@ -474,7 +508,7 @@ public class LdapResourceProvider implem
if ( atHandler != null )
{
AttributeHandler handler = userSchema.getHandler( atHandler );
- handler.handle( bt, entry, ctx );
+ handler.read( bt, entry, ctx );
continue;
}
@@ -586,7 +620,7 @@ public class LdapResourceProvider implem
if ( atHandler != null )
{
AttributeHandler handler = userSchema.getHandler( atHandler );
- handler.handle( st, entry, ctx );
+ handler.read( st, entry, ctx );
return null;
}
else
@@ -739,6 +773,11 @@ public class LdapResourceProvider implem
return entry;
}
+
+ public JsonSchema getSchema( String uri )
+ {
+ return schemas.get( uri );
+ }
public static void main( String[] args ) throws Exception
{
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java Sun Oct 6 04:25:00 2013
@@ -43,12 +43,16 @@ import org.apache.directory.scim.ldap.sc
import org.apache.directory.scim.ldap.schema.SimpleType;
import org.apache.directory.scim.ldap.schema.SimpleTypeGroup;
import org.apache.directory.scim.ldap.schema.UserSchema;
+import org.apache.directory.scim.schema.JsonSchema;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
/**
* TODO LdapSchemaMapper.
@@ -65,9 +69,11 @@ public class LdapSchemaMapper implements
private UserSchema userSchema;
-
- public LdapSchemaMapper()
+ private Map<String,JsonSchema> jsonSchemas;
+
+ public LdapSchemaMapper( Map<String,JsonSchema> jsonSchemas )
{
+ this.jsonSchemas = jsonSchemas;
}
@@ -216,6 +222,8 @@ public class LdapSchemaMapper implements
resourceSchema.addUri( uri );
+ JsonSchema json = jsonSchemas.get( uri );
+
List<Element> simpleAtElmList = schemaRoot.elements( "attribute" );
for ( Element el : simpleAtElmList )
@@ -223,6 +231,7 @@ public class LdapSchemaMapper implements
SimpleType st = parseSimpleType( el, uri );
if ( st != null )
{
+ st.setReadOnly( json.isReadOnly( st.getName() ) );
resourceSchema.addAttributeType( st.getName(), st );
}
}
@@ -243,7 +252,7 @@ public class LdapSchemaMapper implements
boolean show = getShowVal( elmComplex );
Element atGrpElm = elmComplex.element( "at-group" );
- SimpleTypeGroup stg = parseAtGroup( atGrpElm, uri );
+ SimpleTypeGroup stg = parseAtGroup( atGrpElm, uri, name );
ComplexType ct = null;
if ( stg != null )
{
@@ -266,6 +275,7 @@ public class LdapSchemaMapper implements
if( ct != null )
{
ct.setAtHandlerName( handlerRef );
+ ct.setReadOnly( json.isReadOnly( name ) );
resourceSchema.addAttributeType( name, ct );
}
}
@@ -288,15 +298,15 @@ public class LdapSchemaMapper implements
boolean showMultiVal = getShowVal( elmMultiVal );
- MultiValType ct = null;
+ MultiValType mt = null;
Element elmAtGroup = elmMultiVal.element( "at-group" );
if ( elmAtGroup != null )
{
- SimpleTypeGroup stg = parseAtGroup( elmAtGroup, uri );
+ SimpleTypeGroup stg = parseAtGroup( elmAtGroup, uri, name );
if ( stg != null )
{
- ct = new MultiValType( uri, name, showMultiVal, stg, baseDn, filter );
+ mt = new MultiValType( uri, name, showMultiVal, stg, baseDn, filter );
}
}
@@ -308,26 +318,29 @@ public class LdapSchemaMapper implements
handlerRef = null;
}
- if( ( ct == null ) && ( handlerRef != null ) )
+ if( ( mt == null ) && ( handlerRef != null ) )
{
- ct = new MultiValType( uri, name, showMultiVal, ( SimpleTypeGroup ) null, baseDn, filter );
+ mt = new MultiValType( uri, name, showMultiVal, ( SimpleTypeGroup ) null, baseDn, filter );
}
- if( ct != null )
+ if( mt != null )
{
- ct.setAtHandlerName( handlerRef );
- resourceSchema.addAttributeType( name, ct );
+ mt.setAtHandlerName( handlerRef );
+ mt.setReadOnly( json.isReadOnly( name ) );
+ resourceSchema.addAttributeType( name, mt );
}
}
}
- private SimpleTypeGroup parseAtGroup( Element elmAtGroup, String uri )
+ private SimpleTypeGroup parseAtGroup( Element elmAtGroup, String uri, String parentAtName )
{
SimpleTypeGroup stg = null;
List<SimpleType> lstSTypes = null;
-
+
+ JsonSchema json = jsonSchemas.get( uri );
+
if ( elmAtGroup != null )
{
lstSTypes = new ArrayList<SimpleType>();
@@ -338,6 +351,7 @@ public class LdapSchemaMapper implements
SimpleType st = parseSimpleType( elmAt, uri );
if ( st != null )
{
+ st.setReadOnly( json.isReadOnly( parentAtName + "." + st.getName() ) );
lstSTypes.add( st );
}
}
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ActiveAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ActiveAttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ActiveAttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ActiveAttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -41,7 +41,7 @@ public class ActiveAttributeHandler impl
private static final Logger LOG = LoggerFactory.getLogger( ActiveAttributeHandler.class );
@Override
- public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+ public void read( BaseType bt, Object srcResource, RequestContext ctx )
{
if( !bt.getName().equals( "active" ) )
{
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -63,7 +63,7 @@ public class GroupsAttributeHandler impl
@Override
- public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+ public void read( BaseType bt, Object srcResource, RequestContext ctx )
{
if ( !bt.getName().equals( "groups" ) )
{
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -62,7 +62,7 @@ public class MembersAttributeHandler imp
@Override
- public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+ public void read( BaseType bt, Object srcResource, RequestContext ctx )
{
if ( !bt.getName().equals( "members" ) )
{
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MetaAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MetaAttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MetaAttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MetaAttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -48,7 +48,7 @@ public class MetaAttributeHandler implem
private static final Logger LOG = LoggerFactory.getLogger( ActiveAttributeHandler.class );
@Override
- public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+ public void read( BaseType bt, Object srcResource, RequestContext ctx )
{
Entry entry = ( Entry ) srcResource;
Modified: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java (original)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java Sun Oct 6 04:25:00 2013
@@ -54,7 +54,7 @@ public class PhotosAttributeHandler impl
@Override
- public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+ public void read( BaseType bt, Object srcResource, RequestContext ctx )
{
if ( !bt.getName().equals( "photos" ) )
{
Modified: directory/escimo/branches/json-schema-experiment/schema/pom.xml
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/schema/pom.xml?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/schema/pom.xml (original)
+++ directory/escimo/branches/json-schema-experiment/schema/pom.xml Sun Oct 6 04:25:00 2013
@@ -9,6 +9,11 @@
</parent>
<artifactId>escimo-schema</artifactId>
<name>eSCIMo Schema Resources</name>
+
<dependencies>
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ </dependency>
</dependencies>
</project>
Added: directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/JsonSchema.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/JsonSchema.java?rev=1529564&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/JsonSchema.java (added)
+++ directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/JsonSchema.java Sun Oct 6 04:25:00 2013
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.scim.schema;
+
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.scim.schema.SchemaUtil;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+
+/**
+ * TODO JsonSchema.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class JsonSchema
+{
+ private String rawJson;
+
+ private String id;
+
+ private String name;
+
+ private String desc;
+
+ private Map<String, JsonObject> attributes;
+
+
+ private JsonSchema( String rawJson )
+ {
+ this.rawJson = rawJson;
+ this.attributes = new HashMap<String, JsonObject>();
+ }
+
+
+ public static JsonSchema parse( String rawJson )
+ {
+ JsonSchema schema = new JsonSchema( rawJson );
+ schema._parse();
+
+ return schema;
+ }
+
+
+ private void _parse()
+ {
+ JsonParser parser = new JsonParser();
+ JsonObject obj = ( JsonObject ) parser.parse( rawJson );
+
+ this.id = obj.get( "id" ).getAsString();
+ this.name = obj.get( "name" ).getAsString();
+ this.desc = obj.get( "description" ).getAsString();
+
+ _readAttributeDef( obj );
+ }
+
+
+ private void _readAttributeDef( JsonObject obj )
+ {
+ JsonArray atArray = null;
+
+ String parentName = null;
+
+ if ( obj.has( "attributes" ) )
+ {
+ atArray = obj.get( "attributes" ).getAsJsonArray();
+ }
+ else
+ {
+ atArray = obj.get( "subAttributes" ).getAsJsonArray();
+ parentName = obj.get( "name" ).getAsString();
+ }
+
+ for ( JsonElement je : atArray )
+ {
+ JsonObject attribute = ( JsonObject ) je;
+ String type = attribute.get( "type" ).getAsString();
+ String name = attribute.get( "name" ).getAsString();
+
+ if ( parentName != null )
+ {
+ name = parentName + "." + name;
+ }
+
+ attributes.put( name, attribute );
+
+ if ( type.equals( "complex" ) )
+ {
+ _readAttributeDef( attribute );
+ }
+ }
+ }
+
+ /**
+ * gives the schema definition of an attribute with the given name.
+ * JSON dot notation is also supported in the attribute's name.
+ * e.x emails.value will give the definition of 'value' sub-attribute
+ * of the 'emails' attribute
+ *
+ * @param name the name of the attribute, e.x 'userName', 'emails.value' etc.
+ * @return a JSON object containing the definition of attribute's schema
+ */
+ public JsonObject getAttributeDef( String name )
+ {
+ return attributes.get( name );
+ }
+
+
+ /**
+ * tells if an attribute with the given name is read-only.
+ *
+ * JSON dot notation is also supported in the attribute's name.
+ * e.x emails.value will tell if 'value' sub-attribute of the
+ * 'emails' attribute is read-only
+ *
+ *
+ * @param name the name of the attribute, e.x 'userName', 'emails.value' etc.
+ * @return true if attribute is read-only, false when 'readOnly' value is not
+ * specified or is set to false
+ */
+ public boolean isReadOnly( String name )
+ {
+ JsonObject jo = getAttributeDef( name );
+
+ if( jo == null )
+ {
+ if( name.equals( "meta" ) )
+ {
+ return true;
+ }
+
+ throw new IllegalArgumentException( "Unknown attribute name " + name );
+ }
+
+ JsonElement je = jo.get( "readOnly" );
+
+ if( je != null )
+ {
+ return je.getAsBoolean();
+ }
+
+ return false;
+ }
+
+
+ /**
+ * @return the rawJson
+ */
+ public String getRawJson()
+ {
+ return rawJson;
+ }
+
+
+ /**
+ * @return the id
+ */
+ public String getId()
+ {
+ return id;
+ }
+
+
+ /**
+ * @return the name
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+
+ /**
+ * @return the desc
+ */
+ public String getDesc()
+ {
+ return desc;
+ }
+
+
+ @Override
+ public String toString()
+ {
+ return "JsonSchema [id=" + id + ", name=" + name + ", desc=" + desc + "]";
+ }
+
+
+ public static void main( String[] args ) throws Exception
+ {
+ URL url = SchemaUtil.getDefaultSchemas().get( 0 );
+ JsonSchema json = SchemaUtil.getSchemaJson( url );
+ System.out.println(json);
+ }
+
+}
Modified: directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/SchemaUtil.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/SchemaUtil.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/SchemaUtil.java (original)
+++ directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/SchemaUtil.java Sun Oct 6 04:25:00 2013
@@ -52,7 +52,7 @@ public class SchemaUtil
}
- public static String getSchemaJson( URL url ) throws IOException
+ public static JsonSchema getSchemaJson( URL url ) throws IOException
{
BufferedReader br = null;
try
@@ -67,7 +67,7 @@ public class SchemaUtil
sb.append( s );
}
- return sb.toString();
+ return JsonSchema.parse( sb.toString() );
}
finally
{
Modified: directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/SchemaService.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/SchemaService.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/SchemaService.java (original)
+++ directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/SchemaService.java Sun Oct 6 04:25:00 2013
@@ -27,13 +27,10 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
-import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
-import org.apache.directory.scim.RequestContext;
-import org.apache.directory.scim.ResourceNotFoundException;
-import org.apache.directory.scim.User;
-import org.apache.directory.scim.json.ResourceSerializer;
+import org.apache.directory.scim.ProviderService;
+import org.apache.directory.scim.schema.JsonSchema;
/**
* TODO SchemaService.
@@ -44,6 +41,9 @@ import org.apache.directory.scim.json.Re
public class SchemaService
{
+ private ProviderService provider = ServerInitializer.getProvider();
+
+
@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("{uri}")
@@ -51,11 +51,11 @@ public class SchemaService
{
ResponseBuilder rb = null;
- String json = ServerInitializer.getSchema( schemaUri );
+ JsonSchema jsonSchema = provider.getSchema( schemaUri );
- if( json != null )
+ if( jsonSchema != null )
{
- rb = Response.ok( json, MediaType.APPLICATION_JSON );
+ rb = Response.ok( jsonSchema.getRawJson(), MediaType.APPLICATION_JSON );
}
else
{
Modified: directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/ServerInitializer.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/ServerInitializer.java?rev=1529564&r1=1529563&r2=1529564&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/ServerInitializer.java (original)
+++ directory/escimo/branches/json-schema-experiment/server/src/main/java/org/apache/directory/scim/rest/ServerInitializer.java Sun Oct 6 04:25:00 2013
@@ -40,8 +40,6 @@ public class ServerInitializer
{
private static ProviderService provider;
- private static Map<String,String> schemas = new HashMap<String, String>();
-
private static void init()
{
String fqcn = System.getProperty( "escimo.resource.provider", "org.apache.directory.scim.ldap.LdapResourceProvider" );
@@ -62,29 +60,6 @@ public class ServerInitializer
throw re;
}
- try
- {
- JsonParser parser = new JsonParser();
-
- List<URL> urls = SchemaUtil.getDefaultSchemas();
- for( URL u : urls )
- {
- String json = SchemaUtil.getSchemaJson( u );
- JsonObject obj = ( JsonObject ) parser.parse( json );
- String uri = obj.get( "id" ).getAsString();
-
- schemas.put( uri, json );
- }
-
- // TODO load custom schemas
- }
- catch( Exception e )
- {
- RuntimeException re = new RuntimeException( "Failed to load the default schemas" );
- re.initCause( e );
- throw re;
- }
-
}
public static ProviderService getProvider()
@@ -96,10 +71,4 @@ public class ServerInitializer
return provider;
}
-
-
- public static String getSchema( String uri )
- {
- return schemas.get( uri );
- }
}