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/11 17:05:28 UTC
svn commit: r1531319 - in /directory/escimo/branches/json-schema-experiment:
client/src/main/java/org/apache/directory/scim/
ldap/src/main/java/org/apache/directory/scim/ldap/
schema/src/main/java/org/apache/directory/scim/schema/
tests/src/test/java/o...
Author: kayyagari
Date: Fri Oct 11 15:05:27 2013
New Revision: 1531319
URL: http://svn.apache.org/r1531319
Log:
remaining files that were missed in the previous commit ;)
Added:
directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/EscimoClient.java
directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/FieldExclusionStrategy.java
directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/DoNotExpose.java
directory/escimo/branches/json-schema-experiment/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
Added: directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/EscimoClient.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/EscimoClient.java?rev=1531319&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/EscimoClient.java (added)
+++ directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/EscimoClient.java Fri Oct 11 15:05:27 2013
@@ -0,0 +1,166 @@
+/*
+ * 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;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.scim.schema.CoreResource;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+
+/**
+ * TODO EscimoClient.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class EscimoClient
+{
+ private String providerUrl;
+
+ private static String USERS_URI = "/Users";
+
+ private static String GROUPS_URI = "/Groups";
+
+ private Gson serializer;
+
+ private static final Logger LOG = LoggerFactory.getLogger( EscimoClient.class );
+
+
+ public EscimoClient( String providerUrl )
+ {
+ this.providerUrl = providerUrl;
+ GsonBuilder gb = new GsonBuilder();
+ gb.setExclusionStrategies( new FieldExclusionStrategy() );
+ // gb.setDateFormat( pattern );
+ serializer = gb.create();
+ }
+
+ public CoreResource addUser( CoreResource resource ) throws Exception
+ {
+ return addResource( resource, USERS_URI );
+ }
+
+ private CoreResource addResource( CoreResource resource, String uri ) throws Exception
+ {
+ if ( resource == null )
+ {
+ throw new IllegalArgumentException( "resource cannot be null" );
+ }
+
+ HttpPost post = new HttpPost( providerUrl + uri );
+
+ String payload = serialize( resource ).toString();
+
+ LOG.debug( "sending JSON payload to URI {} for adding resource:\n{}", uri, payload );
+
+ post.setEntity( new StringEntity( payload, ContentType.APPLICATION_JSON ) );
+
+ HttpClient client = HttpClients.createDefault();
+
+ try
+ {
+ HttpResponse resp = client.execute( post );
+ StatusLine sl = resp.getStatusLine();
+
+ if ( sl.getStatusCode() == 200 )
+ {
+ String retVal = EntityUtils.toString( resp.getEntity() );
+
+ return deserialize( retVal );
+ }
+ }
+ catch ( Exception e )
+ {
+ LOG.warn( "", e );
+ throw e;
+ }
+
+ return null;
+ }
+
+
+ private CoreResource deserialize( String json )
+ {
+ JsonParser parser = new JsonParser();
+ JsonObject obj = ( JsonObject ) parser.parse( json );
+
+ CoreResource top = serializer.fromJson( obj, CoreResource.class );
+
+ for ( java.util.Map.Entry<String, JsonElement> e : obj.entrySet() )
+ {
+ String key = e.getKey();
+
+ if ( key.startsWith( "urn:scim:schemas:" ) )
+ {
+ CoreResource ext = serializer.fromJson( e.getValue(), CoreResource.class );
+ top.addExtendedResource( ext );
+ }
+ }
+
+ return top;
+ }
+
+
+ private JsonObject serialize( CoreResource resource )
+ {
+ JsonObject json = ( JsonObject ) serializer.toJsonTree( resource );
+
+ List<CoreResource> exts = resource.getExtResources();
+
+ if ( exts != null )
+ {
+ for ( CoreResource e : exts )
+ {
+ JsonElement el = serializer.toJsonTree( resource );
+ json.add( e.getSchemaId(), el );
+ }
+ }
+
+ return json;
+ }
+
+
+ public static void main( String[] args )
+ {
+ EscimoClient ec = new EscimoClient( "http://example.com" );
+ String json = ec.serializer.toJson( ec );
+ System.out.println( json );
+
+ EscimoClient clone = ec.serializer.fromJson( json, EscimoClient.class );
+ System.out.println( clone );
+ }
+}
Added: directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/FieldExclusionStrategy.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/FieldExclusionStrategy.java?rev=1531319&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/FieldExclusionStrategy.java (added)
+++ directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/FieldExclusionStrategy.java Fri Oct 11 15:05:27 2013
@@ -0,0 +1,48 @@
+/*
+ * 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;
+
+import org.apache.directory.scim.schema.DoNotExpose;
+
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+
+/**
+ * TODO FieldExclusionStrategy.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class FieldExclusionStrategy implements ExclusionStrategy
+{
+
+ @Override
+ public boolean shouldSkipField( FieldAttributes f )
+ {
+ DoNotExpose dne = f.getAnnotation( DoNotExpose.class );
+ return ( dne != null );
+ }
+
+ @Override
+ public boolean shouldSkipClass( Class<?> clazz )
+ {
+ return false;
+ }
+
+}
Added: directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java?rev=1531319&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java (added)
+++ directory/escimo/branches/json-schema-experiment/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java Fri Oct 11 15:05:27 2013
@@ -0,0 +1,159 @@
+/*
+ * 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.ldap;
+
+import org.apache.directory.api.ldap.model.entry.Attribute;
+import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.schema.AttributeType;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.api.util.Base64;
+import org.apache.directory.api.util.Strings;
+import org.apache.directory.scim.RequestContext;
+import org.apache.directory.scim.ldap.schema.ComplexType;
+import org.apache.directory.scim.ldap.schema.MultiValType;
+import org.apache.directory.scim.ldap.schema.SimpleType;
+import org.apache.directory.scim.ldap.schema.SimpleTypeGroup;
+import org.apache.directory.scim.schema.BaseType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+/**
+ * TODO LdapUtil.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LdapUtil
+{
+ private static final Logger LOG = LoggerFactory.getLogger( LdapUtil.class );
+
+ public static void scimToLdapAttribute( BaseType bt, JsonElement el, Entry entry, RequestContext ctx ) throws LdapException
+ {
+ if( bt.isReadOnly() )
+ {
+ return;
+ }
+
+ SchemaManager ldapSchema = ( ( LdapResourceProvider ) ctx.getProviderService() ).getLdapSchema();
+
+ if( bt instanceof ComplexType )
+ {
+ ComplexType ct = ( ComplexType ) bt;
+ SimpleTypeGroup complexStg = ct.getAtGroup();
+
+ JsonObject jo = ( JsonObject ) el;
+ processSimpleTypeGroup( complexStg, jo, entry, ldapSchema, ct.getName() );
+ }
+ else if( bt instanceof MultiValType )
+ {
+ MultiValType mt = ( MultiValType ) bt;
+ SimpleTypeGroup multiStg = mt.getAtGroup();
+
+ JsonArray valArray = el.getAsJsonArray();
+
+ for( JsonElement je : valArray )
+ {
+ // for the cases where multivalued attribute comes as an array of primitives
+ // e.x "emails":['elecharny@apache.org', 'pajbam@apache.org']
+ if( je.isJsonPrimitive() )
+ {
+ storeSimpleAttribute( multiStg.getValueType(), je, entry, ldapSchema );
+ }
+ else
+ {
+ JsonObject jo = ( JsonObject ) je;
+ processSimpleTypeGroup( multiStg, jo, entry, ldapSchema, mt.getName() );
+ }
+ }
+ }
+ else
+ {
+ storeSimpleAttribute( ( SimpleType ) bt, el, entry, ldapSchema );
+ }
+ }
+
+ private static void processSimpleTypeGroup( SimpleTypeGroup stg, JsonObject jo, Entry entry, SchemaManager ldapSchema, String scimComplexAtName ) throws LdapException
+ {
+ for( java.util.Map.Entry<String, JsonElement> e : jo.entrySet() )
+ {
+ String scimAtName = e.getKey();
+
+ SimpleType st = null;
+
+ for( SimpleType temp : stg.getSubTypes() )
+ {
+ if( scimAtName.equals( temp.getName() ) )
+ {
+ st = temp;
+ break;
+ }
+ }
+
+ if( st != null )
+ {
+ storeSimpleAttribute( st, e.getValue(), entry, ldapSchema );
+ }
+ else
+ {
+ LOG.warn( "No LDAP mapping found for the sub attribute " + scimAtName + " of the complex attribute " + scimComplexAtName );
+ }
+ }
+ }
+
+ public static void storeSimpleAttribute( SimpleType st, JsonElement el, Entry entry, SchemaManager ldapSschema ) throws LdapException
+ {
+ if( st.isReadOnly() )
+ {
+ return;
+ }
+
+ String ldapAtName = st.getMappedTo();
+
+ if( Strings.isEmpty( ldapAtName ) )
+ {
+ throw new IllegalArgumentException( "Attribute " + st.getName() + " is not mapped to any LDAP attribute in the config" );
+ }
+
+ AttributeType ldapType = ldapSschema.getAttributeType( ldapAtName );
+
+ Attribute ldapAt = entry.get( ldapType );
+ if( ldapAt == null )
+ {
+ ldapAt = new DefaultAttribute( ldapAtName );
+ entry.add( ldapAt );
+ }
+
+ if( !ldapType.getSyntax().isHumanReadable() )
+ {
+ byte[] value = Base64.decode( el.getAsString().toCharArray() );
+ ldapAt.add( value );
+ }
+ else
+ {
+ ldapAt.add( el.getAsString() );
+ }
+ }
+
+}
Added: directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/DoNotExpose.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/DoNotExpose.java?rev=1531319&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/DoNotExpose.java (added)
+++ directory/escimo/branches/json-schema-experiment/schema/src/main/java/org/apache/directory/scim/schema/DoNotExpose.java Fri Oct 11 15:05:27 2013
@@ -0,0 +1,37 @@
+/*
+ * 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.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * TODO DoNotExpose.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface DoNotExpose
+{
+
+}
Added: directory/escimo/branches/json-schema-experiment/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java?rev=1531319&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java (added)
+++ directory/escimo/branches/json-schema-experiment/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java Fri Oct 11 15:05:27 2013
@@ -0,0 +1,67 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.scim.User.Email;
+import org.apache.directory.scim.User.Name;
+import org.junit.Test;
+
+/**
+ * TODO UserResourceTest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class UserResourceTest
+{
+ private EscimoClient client = new EscimoClient( "http://localhost:8080/v2" );
+
+ @Test
+ public void testAddUser() throws Exception
+ {
+ User user = new User();
+ user.setUserName( "test2" );
+ user.setDisplayName( "Test UserResource" );
+ user.setPassword( "secret01" );
+
+ Name name = new Name();
+ name.setFamilyName( "UserResource" );
+ name.setGivenName( "Test" );
+
+ user.setName( name );
+
+ List<Email> emails = new ArrayList<Email>();
+ Email mail = new Email();
+ mail.setValue( "test@example.com" );
+ emails.add( mail );
+ user.setEmails( emails );
+
+ User addedUser = ( User ) client.addUser( user );
+
+ assertNotNull( addedUser );
+
+ assertEquals( user.getUserName(), addedUser.getUserName() );
+ }
+}