Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 Sonatype, Inc. All rights reserved. This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the Apache License Version 2.0 is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 
 package org.sonatype.security.rest;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.restlet.data.Reference;
 import  org.restlet.data.Request;
 import  org.restlet.data.Status;
 
 public abstract class AbstractSecurityPlexusResource
     extends AbstractPlexusResource
 {
 
     @Requirement
     private SecuritySystem securitySystem;
 
     protected static final String DEFAULT_SOURCE = "default";
 
     @Requirement
     protected ReferenceFactory referenceFactory;
 
     protected SecuritySystem getSecuritySystem()
     {
         return ;
     }
 
     protected ErrorResponse getErrorResponseString idString msg )
     {
         ErrorResponse ner = new ErrorResponse();
         ErrorMessage ne = new ErrorMessage();
         ne.setIdid );
         ne.setMsgmsg );
         ner.addErrorne );
         return ner;
     }
 
         throws PlexusResourceException
     {
         getLogger().warn"Configuration error!"e );
 
         ErrorResponse errorResponse;
 
         ValidationResponse vr = e.getValidationResponse();
 
         if ( vr != null && vr.getValidationErrors().size() > 0 )
         {
             ValidationMessage vm = vr.getValidationErrors().get( 0 );
             errorResponse = getErrorResponsevm.getKey(), vm.getShortMessage() );
         }
         else
         {
             errorResponse = getErrorResponse"*"e.getMessage() );
         }
 
         throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Configuration error."errorResponse );
     }
 
     protected UserResource securityToRestModelUser user, Request requestboolean appendResourceId )
     {
         UserResource resource = new UserResource();
        resource.setEmailuser.getEmailAddress() );
        resource.setNameuser.getName() );
        resource.setStatususer.getStatus().name() );
        resource.setUserIduser.getUserId() );
        String resourceId = "";
        if ( appendResourceId )
        {
            resourceId = resource.getUserId();
        }
        resource.setResourceURIthis.createChildReferencerequestresourceId ).toString() );
        resource.setUserManaged( !user.isReadOnly() );
        for ( RoleIdentifier role : user.getRoles() )
        {
            resource.addRolerole.getRoleId() );
        }
        return resource;
    }
    protected User restToSecurityModelUser userUserResource resource )
        throws InvalidConfigurationException
    {
        if ( user == null )
        {
            user = new DefaultUser();
        }
        // validate users Status, converting to an ENUM throws an exception, so we need to explicitly check it
        this.checkUsersStatusresource.getStatus() );
        user.setEmailAddressresource.getEmail() );
        user.setNameresource.getName() );
        user.setStatus( UserStatus.valueOfresource.getStatus() ) );
        user.setUserIdresource.getUserId() );
        // set the users source
        user.setSource );
        Set<RoleIdentifierroles = new HashSet<RoleIdentifier>();
        for ( String roleId : (List<String>) resource.getRoles() )
        {
            roles.addnew RoleIdentifierroleId ) );
        }
        user.setRolesroles );
        return user;
    }
    
    protected PlexusUserResource securityToRestModelUser user )
    {       
        PlexusUserResource resource = new PlexusUserResource();
        resource.setUserIduser.getUserId() );
        resource.setSourceuser.getSource() );
        resource.setNameuser.getName() );
        resource.setEmailuser.getEmailAddress() );
        for ( RoleIdentifier role : user.getRoles() )
        {
            resource.addRolethis.securityToRestModelrole ) );
        }
        return resource;
    }
    protected PlexusRoleResource securityToRestModelRole role )
    {
        PlexusRoleResource roleResource = new PlexusRoleResource();
        roleResource.setRoleIdrole.getRoleId() );
        roleResource.setNamerole.getName() );
        roleResource.setSourcerole.getSource() );
        return roleResource;
    }
    
    protected List<PlexusUserResourcesecurityToRestModelSet<Userusers )
    {
        List<PlexusUserResourcerestUsersList = new ArrayList<PlexusUserResource>();
        
        for ( User user : users )
        {
            restUsersList.addsecurityToRestModeluser ) );
        }
        return restUsersList;
    }
    
    // TODO: come back to this, we need to change the PlexusRoleResource
    {
        // TODO: We shouldn't be looking up the role name here anyway... this should get pushed up to the SecuritySystem.
        String roleName = role.getRoleId();
        
        SecuritySystem securitySystem = this.getSecuritySystem();
        try
        {
            AuthorizationManager authzManager = securitySystem.getAuthorizationManager );           
            roleName = authzManager.getRolerole.getRoleId() ).getName();
        }
        catchNoSuchAuthorizationManager e)
        {
          this.getLogger().warn(
          "Failed to lookup the users Role: " + role.getRoleId() + " source: "
              + role.getSource() + " but the user has this role."e );
        }
        catch ( NoSuchRoleException e )
        {
            // this is a Warning if the role's source is default, if its not, then we most of the time it would not be found anyway.
            if.equalsrole.getSource() ))
            {
                this.getLogger().warn(
                    "Failed to lookup the users Role: " + role.getRoleId() + " source: "
                        + role.getSource() + " but the user has this role."e );
            }
            else
            {
                this.getLogger().debug(
                    "Failed to lookup the users Role: " + role.getRoleId() + " source: "
                        + role.getSource() + " falling back to the roleId for the role's name."  );
            }
        }
        PlexusRoleResource roleResource = new PlexusRoleResource();
        roleResource.setRoleIdrole.getRoleId() );
        roleResource.setNameroleName );
        roleResource.setSourcerole.getSource() );
        return roleResource;
    }
   
    protected Reference getContextRoot( Request request )
    {
        return this..getContextRootrequest );
    }
    protected Reference createChildReference( Request requestString childPath )
    {
        return this..createChildReferencerequestchildPath );
    }
    protected void checkUsersStatusString status )
        throws InvalidConfigurationException
    {
        boolean found = false;
        for ( UserStatus userStatus : UserStatus.values() )
        {
            if ( userStatus.name().equalsstatus ) )
            {
                found = true;
            }
        }
        if ( !found )
        {
            ValidationResponse response = new ValidationResponse();
            response.addValidationErrornew ValidationMessage"status""Users status is not valid." ) );
            throw new InvalidConfigurationExceptionresponse );
        }
    }
New to GrepCode? Check out our FAQ X