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/08/13 09:01:18 UTC

svn commit: r1513367 - in /directory/escimo/trunk: common/src/main/java/org/apache/directory/scim/ ldap/src/main/java/org/apache/directory/scim/ldap/ ldap/src/main/java/org/apache/directory/scim/ldap/handlers/ server/src/main/java/org/apache/directory/...

Author: kayyagari
Date: Tue Aug 13 07:01:17 2013
New Revision: 1513367

URL: http://svn.apache.org/r1513367
Log:
added a path to serve the photos

Added:
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/MissingParameterException.java
Modified:
    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/handlers/GroupsAttributeHandler.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java
    directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java

Added: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/MissingParameterException.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/MissingParameterException.java?rev=1513367&view=auto
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/MissingParameterException.java (added)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/MissingParameterException.java Tue Aug 13 07:01:17 2013
@@ -0,0 +1,63 @@
+/*
+ *   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;
+
+
+/**
+ * TODO MissingParameterException.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MissingParameterException extends Exception
+{
+    String message;
+
+
+    public MissingParameterException( String message )
+    {
+        this.message = message;
+    }
+
+
+    public MissingParameterException( Throwable t )
+    {
+        super( t );
+    }
+
+
+    public MissingParameterException( String message, Throwable t )
+    {
+        super( t );
+        this.message = message;
+    }
+
+
+    @Override
+    public String getMessage()
+    {
+        if ( message == null )
+        {
+            return super.getMessage();
+        }
+
+        return message;
+    }
+
+}

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=1513367&r1=1513366&r2=1513367&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 Tue Aug 13 07:01:17 2013
@@ -18,6 +18,8 @@
  */
 package org.apache.directory.scim;
 
+import java.io.InputStream;
+
 /**
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
@@ -25,6 +27,10 @@ package org.apache.directory.scim;
 public interface ProviderService 
 {
     void init() throws Exception;
-    void stop();;
+    
+    void stop();
+    
     User getUser( RequestContext ctx, String userId ) throws ResourceNotFoundException;
+    
+    InputStream getUserPhoto( String id, String atName ) throws MissingParameterException;
 }

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=1513367&r1=1513366&r2=1513367&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 Tue Aug 13 07:01:17 2013
@@ -23,6 +23,7 @@ package org.apache.directory.scim.ldap;
 import static org.apache.directory.api.ldap.model.constants.SchemaConstants.ALL_ATTRIBUTES_ARRAY;
 import static org.apache.directory.api.ldap.model.message.SearchScope.SUBTREE;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -62,6 +63,7 @@ import org.apache.directory.ldap.client.
 import org.apache.directory.ldap.client.api.LdapNetworkConnection;
 import org.apache.directory.scim.AttributeHandler;
 import org.apache.directory.scim.ComplexAttribute;
+import org.apache.directory.scim.MissingParameterException;
 import org.apache.directory.scim.MultiValAttribute;
 import org.apache.directory.scim.ProviderService;
 import org.apache.directory.scim.RequestContext;
@@ -175,43 +177,54 @@ public class LdapResourceProvider implem
 
     public User getUser( RequestContext ctx, String id ) throws ResourceNotFoundException
     {
-        SimpleType st = ( SimpleType ) userSchema.getCoreAttribute( "id" );
-        String userIdName = st.getMappedTo();
-
-        String filter = "(" + userIdName + "=" + id + ")";
+        Entry entry = fetchEntryById( id );
 
-        Entry entry = null;
+        if ( entry == null )
+        {
+            throw new ResourceNotFoundException( "No User resource found with the ID " + id );
+        }
 
         try
         {
-            EntryCursor cursor = connection.search( userSchema.getBaseDn(), filter, SUBTREE, ALL_ATTRIBUTES_ARRAY );
-
-            if ( cursor.next() )
-            {
-                entry = cursor.get();
-            }
-
-            cursor.close();
-
+            return toUser( ctx, entry );
         }
         catch ( Exception e )
         {
             throw new ResourceNotFoundException( e );
         }
+    }
 
-        if ( entry == null )
+
+    @Override
+    public InputStream getUserPhoto( String id, String atName ) throws MissingParameterException
+    {
+        if( Strings.isEmpty( id ) )
         {
-            throw new ResourceNotFoundException( "No User resource found with the ID " + id );
+            throw new MissingParameterException( "id cannot be null or empty" );
+        }
+        
+        if( Strings.isEmpty( atName ) )
+        {
+            throw new MissingParameterException( "atName cannot be null or empty" );
         }
 
-        try
+        Entry entry = fetchEntryById( id );
+        
+        if( entry == null )
         {
-            return toUser( ctx, entry );
+            return null;
         }
-        catch ( Exception e )
+        
+        Attribute phtoAt = entry.get( atName );
+        
+        if( phtoAt == null )
         {
-            throw new ResourceNotFoundException( e );
+            return null;
         }
+        
+        ByteArrayInputStream bin = new ByteArrayInputStream( phtoAt.get().getBytes() );
+        
+        return bin;
     }
 
 
@@ -513,7 +526,7 @@ public class LdapResourceProvider implem
     }
 
 
-    public Entry fetchEntry( String dn )
+    public Entry fetchEntryByDn( String dn )
     {
         try
         {
@@ -527,6 +540,39 @@ public class LdapResourceProvider implem
         return null;
     }
     
+    public Entry fetchEntryById( String id )
+    {
+        EntryCursor cursor = null;
+        
+        SimpleType st = ( SimpleType ) userSchema.getCoreAttribute( "id" );
+        String userIdName = st.getMappedTo();
+
+        String filter = "(" + userIdName + "=" + id + ")";
+
+        Entry entry = null;
+
+        try
+        {
+            cursor = connection.search( userSchema.getBaseDn(), filter, SUBTREE, ALL_ATTRIBUTES_ARRAY );
+
+            if ( cursor.next() )
+            {
+                entry = cursor.get();
+            }
+        }
+        catch ( Exception e )
+        {
+            LOG.debug( "Failed while fetching the entry by id {}", id, e );
+        }
+        finally
+        {
+            cursor.close();
+        }
+        
+        return entry;
+    }
+    
+    
     public static void main( String[] args ) throws Exception
     {
         System.setProperty( StandaloneLdapApiService.CONTROLS_LIST,

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java?rev=1513367&r1=1513366&r2=1513367&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java (original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/GroupsAttributeHandler.java Tue Aug 13 07:01:17 2013
@@ -136,7 +136,7 @@ public class GroupsAttributeHandler impl
         while ( itr.hasNext() )
         {
             Value<?> dn = itr.next();
-            Entry entry = provider.fetchEntry( dn.getString() );
+            Entry entry = provider.fetchEntryByDn( dn.getString() );
             if ( entry != null )
             {
                 members.add( entry );

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java?rev=1513367&r1=1513366&r2=1513367&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java (original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/PhotosAttributeHandler.java Tue Aug 13 07:01:17 2013
@@ -81,7 +81,7 @@ public class PhotosAttributeHandler impl
         List<TypedType> ttList = mt.getTypedList();
 
         String photoUrlBase = ctx.getUriInfo().getBaseUri().toString();
-        photoUrlBase += "Users/photos?atName=%s&id=%s";
+        photoUrlBase += "Users/photo?atName=%s&id=%s";
 
         if ( stg != null )
         {
@@ -167,4 +167,11 @@ public class PhotosAttributeHandler impl
             throw new RuntimeException( e );
         }
     }
+    
+    public static void main( String[] args )
+    {
+        byte[] bytes = new byte[]{(byte)0xff, (byte)0xd8};
+        System.out.println(bytes[0] & 0xff);
+        System.out.println(bytes[1] & 0xff);
+    }
 }

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=1513367&r1=1513366&r2=1513367&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 Tue Aug 13 07:01:17 2013
@@ -18,19 +18,27 @@
  */
 package org.apache.directory.scim.rest;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
 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.StreamingOutput;
 import javax.ws.rs.core.UriInfo;
-import javax.ws.rs.core.Response.ResponseBuilder;
 
-import org.apache.directory.scim.RequestContext;
+import org.apache.directory.scim.MissingParameterException;
 import org.apache.directory.scim.ProviderService;
+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;
@@ -68,4 +76,65 @@ public class UserService
         
         return rb.build();
     }
+    
+    
+    @GET
+    @Produces({MediaType.APPLICATION_OCTET_STREAM})
+    @Path("photo")
+    public Response getPhoto( @QueryParam("atName") String atName, @QueryParam("id") String id )
+    {
+        final ResponseBuilder rb = Response.ok();
+        
+        try
+        {
+            final InputStream in = provider.getUserPhoto( id, atName );
+            if( in == null )
+            {
+                rb.status( Status.NOT_FOUND );
+            }
+            else
+            {
+                StreamingOutput streamOut = new StreamingOutput()
+                {
+                    
+                    @Override
+                    public void write( OutputStream output ) throws IOException, WebApplicationException
+                    {
+                        byte[] buf = new byte[1024];
+                        int read = -1;
+                        try
+                        {
+                            while( true )
+                            {
+                                read = in.read( buf );
+                                if( read <= 0 )
+                                {
+                                    break;
+                                }
+                                
+                                output.write( buf, 0, read );
+                            }
+                        }
+                        catch( IOException e )
+                        {
+                            rb.status( Status.INTERNAL_SERVER_ERROR );
+                        }
+                        finally
+                        {
+                            in.close();
+                        }
+                    }
+                };
+                
+                rb.entity( streamOut );
+            }
+        }
+        catch( MissingParameterException e )
+        {
+            rb.status( Status.BAD_REQUEST );
+        }
+        
+        return rb.build();
+
+    }
 }