directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1533068 - in /directory/escimo/trunk: client/src/main/java/org/apache/directory/scim/ common/src/main/java/org/apache/directory/scim/ ldap/src/main/java/org/apache/directory/scim/ldap/ server/src/main/java/org/apache/directory/scim/rest/ t...
Date Thu, 17 Oct 2013 13:10:29 GMT
Author: kayyagari
Date: Thu Oct 17 13:10:29 2013
New Revision: 1533068

URL: http://svn.apache.org/r1533068
Log:
o added PUT service
o added tests

Modified:
    directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
    directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/GroupService.java
    directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
    directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java

Modified: directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
(original)
+++ directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
Thu Oct 17 13:10:29 2013
@@ -32,6 +32,7 @@ import org.apache.http.client.HttpClient
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
 import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.HttpClients;
@@ -111,6 +112,18 @@ public class EscimoClient
         return deleteResource( id, GROUPS_URI );
     }
 
+    public CoreResource putUser( CoreResource resource ) throws Exception
+    {
+        return putResource( resource, USERS_URI );
+    }
+
+    
+    public CoreResource putGroup( CoreResource resource ) throws Exception
+    {
+        return putResource( resource, GROUPS_URI );
+    }
+
+    
     private boolean deleteResource( String id, String uri ) throws Exception
     {
 
@@ -219,6 +232,44 @@ public class EscimoClient
     }
 
 
+    private CoreResource putResource( CoreResource resource, String uri ) throws Exception
+    {
+        if ( resource == null )
+        {
+            throw new IllegalArgumentException( "resource cannot be null" );
+        }
+
+        HttpPut put = new HttpPut( providerUrl + uri );
+
+        String payload = serialize( resource ).toString();
+
+        LOG.debug( "sending JSON payload to URI {} for adding resource:\n{}", uri, payload
);
+
+        put.setEntity( new StringEntity( payload, ContentType.APPLICATION_JSON ) );
+
+        HttpClient client = HttpClients.createDefault();
+
+        try
+        {
+            HttpResponse resp = client.execute( put );
+            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();

Modified: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
(original)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
Thu Oct 17 13:10:29 2013
@@ -48,11 +48,11 @@ public interface ProviderService 
     
     void deleteGroup( String id ) throws Exception;
     
-    /*UserResource putUser( String jsonData, RequestContext ctx ) throws Exception;
+    UserResource putUser( String jsonData, RequestContext ctx ) throws Exception;
     
     GroupResource putGroup( String jsonData, RequestContext ctx ) throws Exception;
 
-    UserResource patchUser( String jsonData, RequestContext ctx ) throws Exception;
+    /*UserResource patchUser( String jsonData, RequestContext ctx ) throws Exception;
     
     GroupResource patchGroup( String jsonData, RequestContext ctx ) throws Exception;*/
 }

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
(original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
Thu Oct 17 13:10:29 2013
@@ -48,6 +48,7 @@ import org.apache.directory.api.ldap.mod
 import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
 import org.apache.directory.api.ldap.model.message.ModifyResponse;
 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
+import org.apache.directory.api.ldap.model.name.Dn;
 import org.apache.directory.api.ldap.model.schema.AttributeType;
 import org.apache.directory.api.ldap.model.schema.LdapSyntax;
 import org.apache.directory.api.ldap.model.schema.SchemaManager;
@@ -76,6 +77,8 @@ import org.apache.directory.scim.ServerR
 import org.apache.directory.scim.SimpleAttribute;
 import org.apache.directory.scim.SimpleAttributeGroup;
 import org.apache.directory.scim.UserResource;
+import org.apache.directory.scim.ldap.LdapSchemaMapper;
+import org.apache.directory.scim.ldap.LdapUtil;
 import org.apache.directory.scim.ldap.schema.ComplexType;
 import org.apache.directory.scim.ldap.schema.GroupSchema;
 import org.apache.directory.scim.ldap.schema.MultiValType;
@@ -238,6 +241,18 @@ public class LdapResourceProvider implem
     }
 
 
+    public UserResource putUser( String jsonData, RequestContext ctx ) throws Exception
+    {
+        return ( UserResource ) replaceResource( jsonData, ctx, userSchema );
+    }
+
+
+    public GroupResource putGroup( String jsonData, RequestContext ctx ) throws Exception
+    {
+        return ( GroupResource ) replaceResource( jsonData, ctx, groupSchema );
+    }
+
+
     public InputStream getUserPhoto( String id, String atName ) throws MissingParameterException
     {
         if ( Strings.isEmpty( id ) )
@@ -357,8 +372,11 @@ public class LdapResourceProvider implem
                 dn = userIdName + "=" + userName + "," + userSchema.getBaseDn();
             }
             
-            _resourceToEntry( entry, obj, ctx, userSchema, dn );
+            _resourceToEntry( entry, obj, ctx, userSchema );
             
+            entry.setDn( dn );
+            connection.add( entry );
+
             entry = connection.lookup( entry.getDn(), SchemaConstants.ALL_ATTRIBUTES_ARRAY
);
 
             UserResource addedUser = new UserResource();
@@ -404,7 +422,10 @@ public class LdapResourceProvider implem
                 dn = groupNameAt + "=" + groupName + "," + groupSchema.getBaseDn();
             }
             
-            _resourceToEntry( entry, obj, ctx, groupSchema, dn );
+            _resourceToEntry( entry, obj, ctx, groupSchema );
+            
+            entry.setDn( dn );
+            connection.add( entry );
             
             entry = connection.lookup( entry.getDn(), SchemaConstants.ALL_ATTRIBUTES_ARRAY
);
 
@@ -425,7 +446,7 @@ public class LdapResourceProvider implem
     }
 
 
-    private void _resourceToEntry( Entry entry, JsonObject obj, RequestContext ctx, ResourceSchema
resourceSchema, String dn ) throws Exception
+    private void _resourceToEntry( Entry entry, JsonObject obj, RequestContext ctx, ResourceSchema
resourceSchema ) throws Exception
     {
 
         // process the core attributes first
@@ -446,11 +467,6 @@ public class LdapResourceProvider implem
         {
             entry.add( SchemaConstants.OBJECT_CLASS, oc );
         }
-        
-        
-        entry.setDn( dn );
-        
-        connection.add( entry );
     }
     
     
@@ -464,7 +480,7 @@ public class LdapResourceProvider implem
         
         Entry entry = new DefaultEntry( ldapSchema );
         
-        _resourceToEntry( entry, obj, ctx, resourceSchema, "" );
+        _resourceToEntry( entry, obj, ctx, resourceSchema );
         
         Entry existingEntry = fetchEntryById( resourceId, resourceSchema );
         
@@ -498,6 +514,12 @@ public class LdapResourceProvider implem
         if( st != null )
         {
             existingUserNameAt = existingEntry.get( st.getMappedTo() );
+            
+            if( existingUserNameAt != null )
+            {
+                existingEntry.remove( existingUserNameAt );
+            }
+            
             newUserNameAt = entry.get( st.getMappedTo() );
             
             if( newUserNameAt != null )

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
(original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
Thu Oct 17 13:10:29 2013
@@ -150,7 +150,7 @@ public class LdapUtil
         Attribute ldapAt = entry.get( ldapType );
         if( ldapAt == null )
         {
-            ldapAt = new DefaultAttribute( ldapAtName );
+            ldapAt = new DefaultAttribute( ldapType );
             entry.add( ldapAt );
         }
         

Modified: directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/GroupService.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/GroupService.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/GroupService.java
(original)
+++ directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/GroupService.java
Thu Oct 17 13:10:29 2013
@@ -20,11 +20,14 @@
 package org.apache.directory.scim.rest;
 
 
+import static org.apache.directory.scim.ScimUtil.exceptionToStr;
+
 import java.net.URI;
 
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -39,14 +42,12 @@ import javax.ws.rs.core.UriInfo;
 import org.apache.directory.scim.GroupResource;
 import org.apache.directory.scim.ProviderService;
 import org.apache.directory.scim.RequestContext;
-import org.apache.directory.scim.ServerResource;
 import org.apache.directory.scim.ResourceNotFoundException;
+import org.apache.directory.scim.ServerResource;
 import org.apache.directory.scim.json.ResourceSerializer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.directory.scim.ScimUtil.*;
-
 /**
  * TODO GroupService.
  *
@@ -138,4 +139,39 @@ public class GroupService
         return rb.build();
     }
 
+    
+    @PUT
+    @Produces({MediaType.APPLICATION_JSON})
+    public Response putGroup( String jsonData, @Context UriInfo uriInfo, @Context HttpHeaders
headers )
+    {
+        ResponseBuilder rb = null;
+
+        if( ( jsonData == null ) || ( jsonData.trim().length() == 0 ) )
+        {
+            rb = Response.status( Status.BAD_REQUEST ).entity( "No data is present with the
call to " + uriInfo.getAbsolutePath() );
+            return rb.build();
+        }
+        
+        LOG.debug( "Data received at the URI {}\n{}", uriInfo.getAbsolutePath(), jsonData
);
+        
+        try
+        {
+            RequestContext ctx = new RequestContext( provider, uriInfo, headers );
+            
+            ServerResource res = provider.putGroup( jsonData, ctx );
+            
+            String json = ResourceSerializer.serialize( res );
+            
+            URI location = uriInfo.getBaseUriBuilder().build( res.getId() );
+            
+            rb = Response.ok().entity( json ).location( location );
+        }
+        catch( Exception e )
+        {
+            rb = Response.status( Status.INTERNAL_SERVER_ERROR ).entity( exceptionToStr(
e ) );
+        }
+        
+        return rb.build();
+    }
+
 }

Modified: directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
(original)
+++ directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
Thu Oct 17 13:10:29 2013
@@ -27,7 +27,9 @@ import java.net.URI;
 
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.HttpMethod;
 import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -45,8 +47,8 @@ import javax.ws.rs.core.UriInfo;
 import org.apache.directory.scim.MissingParameterException;
 import org.apache.directory.scim.ProviderService;
 import org.apache.directory.scim.RequestContext;
-import org.apache.directory.scim.ServerResource;
 import org.apache.directory.scim.ResourceNotFoundException;
+import org.apache.directory.scim.ServerResource;
 import org.apache.directory.scim.UserResource;
 import org.apache.directory.scim.json.ResourceSerializer;
 import org.slf4j.Logger;
@@ -142,6 +144,42 @@ public class UserService
         
         return rb.build();
     }
+
+    
+    @PUT
+    @Produces({MediaType.APPLICATION_JSON})
+    public Response putUser( String jsonData, @Context UriInfo uriInfo, @Context HttpHeaders
headers )
+    {
+        ResponseBuilder rb = null;
+
+        if( ( jsonData == null ) || ( jsonData.trim().length() == 0 ) )
+        {
+            rb = Response.status( Status.BAD_REQUEST ).entity( "No data is present with the
call to " + uriInfo.getAbsolutePath() );
+            return rb.build();
+        }
+        
+        LOG.debug( "Data received at the URI {}\n{}", uriInfo.getAbsolutePath(), jsonData
);
+        
+        try
+        {
+            RequestContext ctx = new RequestContext( provider, uriInfo, headers );
+            
+            ServerResource res = provider.putUser( jsonData, ctx );
+            
+            String json = ResourceSerializer.serialize( res );
+            
+            URI location = uriInfo.getBaseUriBuilder().build( res.getId() );
+            
+            rb = Response.ok().entity( json ).location( location );
+        }
+        catch( Exception e )
+        {
+            rb = Response.status( Status.INTERNAL_SERVER_ERROR ).entity( exceptionToStr(
e ) );
+        }
+        
+        return rb.build();
+    }
+
     
     @GET
     @Produces({MediaType.APPLICATION_OCTET_STREAM})

Modified: directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java?rev=1533068&r1=1533067&r2=1533068&view=diff
==============================================================================
--- directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
(original)
+++ directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
Thu Oct 17 13:10:29 2013
@@ -19,8 +19,7 @@
  */
 package org.apache.directory.scim;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -113,6 +112,44 @@ public class UserResourceTest
         assertEquals( addedUser.getUserName(), fetchedUser.getUserName() );
         assertEquals( addedUser.getId(), fetchedUser.getId() );
         
-//        client.
+        client.deleteUser( fetchedUser.getId() );
+        fetchedUser = ( User ) client.getUser( addedUser.getId() );
+        assertNull( fetchedUser );
+    }
+    
+    @Test
+    public void testPut() 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 );
+
+        addedUser.getEmails().clear();
+        
+        Email newEmail = new Email();
+        newEmail.setValue( "newemail@example.com" );
+        addedUser.getEmails().add( newEmail );
+        
+        User replacedUser = ( User ) client.putUser( addedUser );
+
+        assertNotNull( replacedUser );
+        assertEquals( 1, replacedUser.getEmails().size() );
+        assertEquals( newEmail.getValue(), replacedUser.getEmails().get( 0 ).getValue() );
     }
 }



Mime
View raw message