Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
SIROCCO Copyright (C) 2011 France Telecom Contact: sirocco@ow2.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id$
 
 
 package org.ow2.sirocco.cloudmanager.core.impl;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import  javax.ejb.EJB;
 import  javax.ejb.Local;
 import  javax.ejb.Remote;
 import  javax.ejb.SessionContext;
 import  javax.ejb.Stateless;
 
 
 @Stateless
 @Local(ICredentialsManager.class)
 public class CredentialsManager implements ICredentialsManager {
 
     private static Logger logger = LoggerFactory.getLogger(CredentialsManager.class.getName());
 
     @PersistenceContext(unitName = "persistence-unit/main", type = .)
     private EntityManager em;
 
     @Resource
     private SessionContext ctx;
 
     @EJB
     private IUserManager userManager;
 
     @Resource
     public void setSessionContext(final SessionContext ctx) {
         this. = ctx;
     }
 
     private User getUser() throws CloudProviderException {
         String username = this..getCallerPrincipal().getName();
         return this..getUserByUsername(username);
     }
 
     private void validateCredentials(final Credentials credthrows CloudProviderException {
         if (cred.getUserName() != null) {
             if (cred.getUserName().length() < 1) {
                 throw new InvalidRequestException("Too short user name ");
             }
             if (Pattern.matches("[\\w]*$"cred.getUserName()) == false) {
                 throw new InvalidRequestException("Non word characters in user name ");
             }
         }
         if (cred.getPassword() != null) {
             if (cred.getPassword().length() < 1) {
                 throw new InvalidRequestException("Too short password ");
             }
             if (Pattern.matches("[\\w]*$"cred.getPassword()) == false) {
                throw new InvalidRequestException("Non word characters in user name ");
            }
            if (Pattern.matches("[^\\s]*$"cred.getPassword()) == false) {
                throw new InvalidRequestException("Spaces in password ");
            }
        }
    }
    public Credentials createCredentials(final CredentialsCreate credentialsCreatethrows CloudProviderException {
        ..info("validateCredentials ");
        Credentials credentials = new Credentials();
        credentials.setCreated(new Date());
        credentials.setName(credentialsCreate.getName());
        credentials.setDescription(credentialsCreate.getDescription());
        credentials.setProperties(credentialsCreate.getProperties());
        credentials.setUser(this.getUser());
        credentials.setUserName(credentialsCreate.getCredentialsTemplate().getUserName());
        credentials.setPassword(credentialsCreate.getCredentialsTemplate().getPassword());
        credentials.setPublicKey(credentialsCreate.getCredentialsTemplate().getPublicKey());
        this.validateCredentials(credentials);
        ..info("Persist credentials  " + credentials.getName());
        this..persist(credentials);
        this..flush();
        ..info("Persist credentials return " + credentials.getId());
        return credentials;
    }
    public void updateCredentials(final Credentials credentialsthrows CloudProviderException {
        User user = this.getUser();
        Credentials c = this..find(Credentials.classcredentials.getId());
        if (c == null) {
            throw new ResourceNotFoundException(" Could not find credential " + credentials.getId());
        }
        if (c.getUser().getUsername() != user.getUsername()) {
            throw new CloudProviderException(" Unauthorized to change creds " + user.getUsername());
        }
        this.validateCredentials(credentials);
        this..merge(credentials);
    }
    public Credentials getCredentialsById(final String credentialsIdthrows CloudProviderException {
        User user = this.getUser();
        if (credentialsId == null) {
            throw new InvalidRequestException("null credentials id");
        }
        Credentials cred = this..find(Credentials.class, Integer.valueOf(credentialsId));
        if (cred == null) {
            throw new ResourceNotFoundException("Credentials " + credentialsId + " not found");
        }
        return cred;
    }
    @Override
    public Credentials getCredentialsAttributes(final String credentialsIdfinal List<Stringattributes)
        Credentials cred = this.getCredentialsById(credentialsId);
        return UtilsForManagers.fillResourceAttributes(credattributes);
    }
    public void deleteCredentials(final String credentialsIdthrows ResourceNotFoundExceptionInvalidRequestException,
        CloudProviderException {
        User user = this.getUser();
        if (credentialsId == null) {
            throw new InvalidRequestException("null credentials id");
        }
        Credentials cred = this..find(Credentials.class, Integer.valueOf(credentialsId));
        if (cred == null) {
            throw new ResourceNotFoundException(" Invalid credential id " + credentialsId);
        }
        
if anymachine template refers to this credential do not delete.
        List<MachineTemplatemts = null;
        try {
            mts = this..createQuery("FROM MachineTemplate t WHERE t.credential=:cred").setParameter("cred"cred)
                .getResultList();
        } catch (Exception e) {
            throw new CloudProviderException(" Internal Error");
        }
        if (mts.size() != 0) {
            throw new InvalidRequestException(" Credential " + credentialsId + " is in use");
        }

        
delete credentials
        this..remove(cred);
        this..flush();
        return;
    }
    public void updateCredentialsAttributes(final String credentialsIdfinal Map<StringObjectattributes)
        if (credentialsId == null) {
            throw new InvalidRequestException("null credentials id");
        }
        Credentials cred = this..find(Credentials.class, Integer.valueOf(credentialsId));
        if (cred == null) {
            throw new ResourceNotFoundException("Credentials " + credentialsId + " not found");
        }
        try {
            UtilsForManagers.fillObject(credattributes);
        } catch (Exception e) {
            throw new CloudProviderException(e.getMessage());
        }
        this..merge(cred);
        this..flush();
    }
    @Override
        return this..createQuery("SELECT c FROM Credentials c WHERE c.user.id=:userid")
            .setParameter("userid"this.getUser().getId()).getResultList();
    }
    @Override
    public QueryResult<CredentialsgetCredentials(final int firstfinal int lastfinal List<Stringfilters,
        final List<Stringattributesthrows InvalidRequestExceptionCloudProviderException {
        QueryHelper.QueryParamsBuilder params = QueryHelper.QueryParamsBuilder.builder("Credentials"Credentials.class);
        return QueryHelper.getEntityList(this.,
            params.userName(this.getUser().getUsername()).first(first).last(last).filter(filters).attributes(attributes));
    }
    @Override
    public CredentialsTemplate createCredentialsTemplate(final CredentialsTemplate credentialsTemplate)
        throws CloudProviderException {
        User user = this.getUser();
        credentialsTemplate.setUser(user);
        credentialsTemplate.setCreated(new Date());
        ..info("Persist credentialsTemplate  " + credentialsTemplate.getName());
        this..persist(credentialsTemplate);
        this..flush();
        return credentialsTemplate;
    }
    @Override
    public void updateCredentialsTemplate(final CredentialsTemplate credentialsTemplatethrows CloudProviderException {
        // TODO Auto-generated method stub
    }
    @Override
    public CredentialsTemplate getCredentialsTemplateById(final String credentialsTemplateIdthrows CloudProviderException {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public CredentialsTemplate getCredentialsTemplateAttributes(final String credentialsTemplateId,
        final List<Stringattributesthrows ResourceNotFoundExceptionCloudProviderException {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void deleteCredentialsTemplate(final String credentialsTemplateIdthrows ResourceNotFoundException,
        // TODO Auto-generated method stub
    }
    @Override
    public void updateCredentialsTemplateAttributes(final String credentialsTemplateIdfinal Map<StringObjectattributes)
        // TODO Auto-generated method stub
    }
    @Override
        return this..createQuery("SELECT c FROM CredentialsTemplate c WHERE c.user.id=:userid")
            .setParameter("userid"this.getUser().getId()).getResultList();
    }
    @Override
    public QueryResult<CredentialsTemplategetCredentialsTemplates(final int firstfinal int last,
        final List<Stringfiltersfinal List<Stringattributesthrows InvalidRequestExceptionCloudProviderException {
        QueryHelper.QueryParamsBuilder params = QueryHelper.QueryParamsBuilder.builder("CredentialsTemplate",
            CredentialsTemplate.class);
        return QueryHelper.getEntityList(this.,
            params.userName(this.getUser().getUsername()).first(first).last(last).filter(filters).attributes(attributes)
                .filterEmbbededTemplate());
    }
New to GrepCode? Check out our FAQ X