Translate

Mostrando entradas con la etiqueta spring data. Mostrar todas las entradas
Mostrando entradas con la etiqueta spring data. Mostrar todas las entradas

domingo, 23 de marzo de 2014

MongoDB Spring Security Code Example

Titulo: Ejemplo práctico de Spring Security con MongoDB con código fuente.

I have decided to change the main language of my blog in order to reach and share everything with more people.

In this post you can see the next topics

- SpringData With MongoDb Integration
- Spring MVC to manage navigation
- Spring Security with Custom user detais service to assign roles and web application authentication.




Everything is easy and separated in different files to be more understandable and you have all the code in github.

This example is based on the book Spring Security 3.x Cookbook  that cover a lot of spring security integrations with different technologies:
  1. ORM (Hibernate) and NoSQL Databases (MongoDB) 
  2. JSF
  3. JSP
  4. Struts 2
  5. GWT
  6. Grails
  7. Vaadin
  8. Wicked
  9. Facebook, Twitter
  10. WebServices: RESTFull, SOAP
  11. Captcha Integration
  12. and more all with code samples
After this recommendation, lets go to show the example, all the code you can find it in my GitHub repository.

Please download the code as ZIP or fork the repository to follow the example.

To run the example you have the following requirements:
1- Eclipse Kepler to import the project from github or zip file.
2 - Document data base MongoDB installed in the same machine as your application server.
3 - Application Server Apache Tomcat 7
4 - Configure your application path context to "/" in ordet to Spring Security secure your URL.



Import the project into eclipse like a Maven project and deploy it in a Tomcat server sever with the above path configuration.

Populate you MongoDB database with users data, you can do it easily executing this test file in the application.UserPopulateCollection.java

Remember to uncomment the //@Test line.



or insert in the Mongo Database the next data into User Collection




The application  is a 4 screens application, we have a Login page where you can login like:

1 - Admin User (Role 1 - ROLE_ADMIN): You can access to User and Campaign pages but not create new campaigns.
2 - Campaign User (Role 2 - ROLE_CAMPAIGN): You only can access to Campaign page and create new one, but you can not access to users pages.


You can access the application from the following url using any of these users

http://localhost:8080/login

User Admin
user: user1@antuansoft.es
pwd: 123

User Campaign
user: user2@antuansoft.es
pwd 123


Menu page with the different application´s options Users and Campaings.



Users page where you have all the info about your application users.




Campaing page where you can access to all information of your campaings and can add and delete it



I think it's time we have a look at the project configuration files,

A - pom.xml

In this file you can see al the dependencies that we need, all of this related with Spring and MongoDB.

B - web.xml

Here we have the main project configuration, where the application server starts reading de config files.

Open the file in github or in your eclipse project and you can see:
1- The Spring Security filter configuration to secure all the URLs
2 - Spring MVC servlet dispatcher to manager all the URL requests.
3 - Log4J configuration
4 - Spring modules (Spring Data, Spring Security, Spring MVC) configuration files

 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
      /WEB-INF/mvc-config.xml
      /WEB-INF/security-config.xml       
      /WEB-INF/mongo-config.xml
     </param-value>
 </context-param>
        

C - mvc-config.xml

In this file there are the basic MVC configuration and packages to scan.

1 - Set the Spring context to  manage requests with the Spring MVC Controllers with the tag        <mvc:annotation-driven />
2 - Set the packages to scan Spring beans annotated with @RequestMapping @Component, etc...

  <context:component-scan base-package="com.antuansoft.spring.mvc">  
  <context:component-scan base-package="com.antuansoft.spring.security">  

3 - Define the views configuration location ant type, in this case with jsp, using JSTL.

D - mongo-config.xml

Here we have the MongDB configuration

1 - We define the database parameters, host and port and the database name in the mongoDbFactory. This is the main configuration to connect with database.

2- Define the MongoRepositories, this is where Spring Data is going to scan to look for Mongo DAOS (Mongo Data Acces Objects). In this DAOS is where all the database operations (save, delete, etc.) and the domain objects (User, and Campaign) are defined.

<mongo:repositories base-package="com.antuansoft.mongodb.repositories" />

E - security-config.xml

The security configuration of the application is defined here.

1 - Enable the @PreAuthorize annotation that will be user in MainController class

2 - Secure the application´s  urls. Due to this config is important to change the application path context to "/" like we said above.
   - /login, /logout and /accessdenied urls are permited to authenticated and not  authenticated users.
   - to access to /menu you have to be authenticated with ROLE_ADMIN or ROLE_CAMPAIGN
   - to access to /listUsers you have to be authenticated with ROLE_ADMIN
   - to access to /listCampaigns you have to be authenticated with ROLE_ADMIN or ROLE_CAMPAIGN
   - Finally the spring security login form is too easy to understand.


3 - The most important part is the definition of a custom class (mongoUserDetailsService) to authenticate users application access. An the password encoding, here we use plain text but  i recommend  to use MD5.

<authentication-manager alias="authenticationManager">
 <authentication-provider user-service-ref="mongoUserDetailsService">
   <password-encoder hash="plaintext" />
  </authentication-provider>
 </authentication-manager>

After configuration files we are going to review the specific Java files to understand the Spring Data, Spring Mvc and Spring Security features.

Spring Security:

Open the file MongoUserDetailsService.java

This is the file we have defined in security-config.xml as the authentication provider, this class have to implement the SpringFramework interface UserDetailsService and create the method UserDetailsService.

This method is used to check if the user data send in login form is valid against your database info, this method have to return a org.springframework.security.core.userdetails.User class with the data of the user found in your database.

You have to query your user collection and get the user info, we use the method getUserDetail that call to userRepositoryDao.findByUsername(username);

After getting  user info and based on this, the user role is defined in getAuthorities method.and pass it to UserDetails object.

getUserDetail  and getAuthorities are two methods that you can customize to set the authentication and the user Rols according yo your requirements.

 private org.springframework.security.core.userdetails.User userdetails;
 public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;
        com.antuansoft.mongodb.domain.User user = getUserDetail(username);
        
        userdetails = new User(user.getUsername(), 
                    user.getPassword(),
                    enabled,
                    accountNonExpired,
                    credentialsNonExpired,
                    accountNonLocked,
                    getAuthorities(user.getRole()));
            return userdetails;
    }

    public List getAuthorities(Integer role) {
        List authList = new ArrayList();
        if (role.intValue() == 1) {
            authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

        } else if (role.intValue() == 2) {
            authList.add(new SimpleGrantedAuthority("ROLE_CAMPAIGN"));
        }
        System.out.println(authList);
        return authList;
    }

    public com.antuansoft.mongodb.domain.User getUserDetail(String username) {
     com.antuansoft.mongodb.domain.User user = userRepositoryDao.findByUsername(username);
        //System.out.println(user.toString());
        return user;
    }


MongoDB:

Here we have two things to consider, Domain classes and Dao interfaces.

1 - Domain classes

This classes are plain old java objects (pojos) to define the data of your database collections, in other words the objects that your are going to persists in your database.

Look at the com.antuansoft.mongodb.domain packge and you will find the two documents that we are going to use in this application. User and Campaign.

The only thing that is special in the classes is that they are annotated with @Document and specify a collection where this pojo will be stored.

@Document(collection = "campaign" )
public class Campaign {


You can open this file to see that.

Campaign.java
User.java

2 - DAO interfaces:

Dao interfacesare in com.antuansoft.mongodb.repositories package and here is where all the database operations (save, delete, etc.) and the domain objects (User, and Campaign) are defined.

In mongo-config.xml file we have defined this package to be scanned to look for mongo repositories, its means that Spring Context transform this interfaces in a DAO object with all the basic operations like create, read, update,delete where we can use in the application.

This classes only have to extend from org.springframework.data.repository.CrudRepository to work.

--
public interface UserRepositoryDao extends CrudRepository<User, String> {
 
 
 public User findByUsername(String username);

}

--

This Crud repository has two params <User, String>, here is  where bind the User domain pojo to this Repository Dao. Here we indicate that this Dao process User Object and the ID´s type is String.

In UserRepositoyDao there is something special, it is a special method user for authentication findByUsername. Another feature of SpringData is that you can define query methods without write any line of code.

Here we have defined a query to find a user by a username, only with key words. find+By+field to query in user pojo object (findByUsername). And it´s work!!!! is magic!!

Spring MVC:

Finally the only thing that we have to review is the controller that allows us to navigate through the application.

The MainController class is a basic class that use the SpringMVC annotations, @RequestMapping,  to navigate:

@RequestMapping(value = "/listCampaigns", method = RequestMethod.GET)
 public String listCampaigns(ModelMap map) {
  
  map.addAttribute("new_campaign", new Campaign());
  Iterable campaings= campaignRepositoryDao.findAll();
  map.addAttribute("campaigns",campaings);
  
  return "listCampaigns";
 }


and basically it means that when SpringMVC detects this urls the method will be executed, in this case the method pass to listCampaigns.jsp all the campaings and new campaing object.

In this class we have a little special thing that add a spring security functionality, it´s the @PreAuthorize annotation that only allow to ROLE_CAMPAIGN users to add a new Campaign.


@PreAuthorize("hasRole('ROLE_CAMPAIGN')")
 @RequestMapping(value = "/addCampaing", method = RequestMethod.POST)
 public String addCampaign(@ModelAttribute(value = "new_campaign") 
                                  Campaign new_campaign,
             BindingResult result){
  
  new_campaign.setId(UUID.randomUUID().toString());
  campaignRepositoryDao.save(new_campaign);
  
  return "redirect:/listCampaigns";
 }

And this its all, i thinks that it is a complete example a not too difficult to understand.

Remember that you have all the code in my GitHub repository.


References:

Mainly Spring Security 3.x Cookbook

Spring Data MongoDB
Spring Security
Spring MVC


viernes, 17 de mayo de 2013

SpringData MondoDb: Configuración de conexiones PRODUCCION

Establecemos los valores de la conexión de SpringData para MongoDb en producción basándonos en el siguiente artículo encontrado en StackOverflow: How to configure MongoDB Java driver MongoOptions for production use?

auto-connect-retry="true" : para que el servidor se reconecte se pierde la conexión.

connections-per-host="40"  :  Conexiones que tendrá nuestra aplicación, son la que va a establecer del driver de java para esta aplicación.

connectTimeout = "30000": El el tiempo que espera el servidor antes de abortar un intento de conexión con la base de datos. O sea es el tiempo que espera para conseguir una conexión libre. Debe de ser algo grande entre 15 y 30 segundos para que sea realista según recomiendan.

socketTimeout="60000" : Una vez iniciada un operación de lectura, es tiempo que el socket va  a esperar la respuesta, si se supera podremos tener errores del tipo:

--
Caused by: com.mongodb.MongoException$Network: can't say something
 at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:194)
 at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
 at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:349)
 at com.mongodb.DBCollection.update(DBCollection.java:177)
 at com.mongodb.DBCollection.save(DBCollection.java:817)
 at com.mongodb.DBCollection.save(DBCollection.java:785)
 at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:885)
 at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:388)

Caused by: java.net.SocketTimeoutException: Read timed out
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(SocketInputStream.java:150)
 at java.net.SocketInputStream.read(SocketInputStream.java:121)
 at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
 at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
 at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
 at org.bson.io.Bits.readFully(Bits.java:46)
--

Referencia útil: connectionTimeout VS SocketTimeout

maxWaitTime = "1500" : Milisegundos que un hilo esperará  para conseguir una conexión libre del pool de conexiones.

Aquí os dejo la configuración que por ahora nos funciona correctamente.
--
<mongo:mongo host="xxx.xxx.xxx" port="xx" id="mongo">
   <mongo:options connections-per-host="40"
            threads-allowed-to-block-for-connection-multiplier="1500"
            connect-timeout="30000"
            max-wait-time="1500"
            auto-connect-retry="true"
            socket-keep-alive="true"
            socket-timeout="60000"
            slave-ok="true"
            write-number="1"
            write-timeout="0"
            write-fsync="true"/>
</mongo:mongo>

--

jueves, 16 de agosto de 2012

Spring Data: Artículo imprescindible de introducción a Spring Data

Os dejo el enlace a este artículo que está muy bien para saber de que va el tema de Spring Data. Nosotros utilizamos SpringData Mongodb y SpringData redis.

http://www.infoq.com/articles/spring-data-intro

viernes, 10 de agosto de 2012

MongoDB: java.lang.NoClassDefFoundError: com/google/common/base/Function

Este error se produce por no tener las versiones correctas de las librerías de Spring-data y query-dsl


--
java.lang.NoClassDefFoundError: com/google/common/base/Function
 at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.createQueryFor(QueryDslMongoRepository.java:167)
 at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.count(QueryDslMongoRepository.java:154)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:334)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:319)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 at $Proxy26.count(Unknown Source)
 at com.antuansoft.contentapi.application.CourseContentTemplateImpl.countByAllowedUsers(CourseContentTemplateImpl.java:61)
 at com.antuansoft.contentapi.integration.CourseContentTemplateIntegrationTest.countByAllowedUsers(CourseContentTemplateIntegrationTest.java:157)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
 at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
 at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
 at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
 at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 ... 43 more

--

Si investigamos un poco en google la clase que nos fata la podemos encontrar en un jar llamado guava-12.0.jar este error se soluciona pero nos da otro


--
java.lang.NoSuchMethodError: com.mysema.query.mongodb.MongodbQuery.<init>(Lcom/mongodb/DBCollection;Lcom/google/common/base/Function;Lcom/mysema/query/mongodb/MongodbSerializer;)V
 at org.springframework.data.mongodb.repository.support.SpringDataMongodbQuery.<init>(SpringDataMongodbQuery.java:58)
 at org.springframework.data.mongodb.repository.support.SpringDataMongodbQuery.<init>(SpringDataMongodbQuery.java:44)
 at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.createQueryFor(QueryDslMongoRepository.java:167)
 at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.count(QueryDslMongoRepository.java:154)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:334)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:319)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

--
Para este error ya tengo un post publicado que este:

Spring JPA MongoDB: java.lang.NoSuchMethodError: com.mysema.query.mongodb.MongodbQuery

jueves, 9 de agosto de 2012

MongoDB Spring Data: Push de objetos en Arrays

Push an Object into an array (addToSet)

Si trabajáis con MongoDB ya sabréis que podemos utilizar el comando push para introducir valores en un arrary.

Por ejemplo si tenemos el siguiente objecto mapeado en MongoDB


import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.google.code.morphia.annotations.Entity;

@Entity
@Document
public class Course implements Serializable {
 
 
 private static final long serialVersionUID = -7251789829032925885L;
 
 
 @Id
 private String id = UUID.randomUUID().toString();
 private String name; 
 private String description;
 private long creationDate = new Date().getTime();
 private int duration;
 private int credits;
 private List<String> tags = new ArrayList<String>();
 private List<String> users = new ArrayList<User>();


Si queremos introducir un valor String en el array de tags es tan fácil como hace esto

 public void addTag(String courseId, String tag){
  
  

    
  WriteResult wr = mongoOperations.updateFirst(
   new Query(where("id").is(courseId)),   
   new Update().push("tags", tag),Course.class);
   
    

  
  
 }


El resultado en MongoDb debería ser algo parecido a esto


  {"_class": "com.antuansoft.Course",
  
  "_id": "953a878e-5aa6-4811-82e3-660b00ebafe2",
    
  "creationDate": 1344439353237,
  
  "credits": 10,
  
  "description": "Course DESCRIPTION",
  
  "duration": 100,
    
  "name": "Course 1",
    
  "tags": {
    
    "0": "tag1"
    "1": "tag2"
  
  }}

Al insertar en el array de tags objetos de la clase String no hay ningún problema ya que los entiende perfectamente.

El problema que he encontrado ocurre cuando tienes que insertar en el array Objectos de alguna clase que no sean tipos básicos. Por ejemplo un objecto de la clase User.

El problema que ocurre es que al insertarlo de la misma manera que un String normal no transforma el objeto y no pone las propiedades ni "_id" ni "_class", o sea que no lo transforma e introduce sólo las propiedades que tenga ese objeto en el array.

Para solucionar esto utilizamos una par de instrucciones que fuerzan a MongoDb a realizar esta transformación.

DBObject dbo = new BasicDBObject();
mongoOperations.getConverter().write(user, dbo);


public void addUser(String idCourse, User user){  
  
   
   DBObject dbo = new BasicDBObject();
   mongoOperations.getConverter().write(user, dbo);
   
   WriteResult wr = mongoOperations.updateFirst(
     new Query(where("id").is(idCourse)),   
     new Update().push("users", dbo),Course.class);


{"_class": "com.antuansoft.Course",
  
  "_id": "953a878e-5aa6-4811-82e3-660b00ebafe2",
    
  "creationDate": 1344439353237,
  
  "credits": 10,
  
  "description": "Course DESCRIPTION",
  
  "duration": 100,
    
  "name": "Course 1",
    
  "tags": {
    
    "0": "tag1"
    "1": "tag2"
  
  }

"users": {
    "0": {
        
        "_class": "com.antuansoft.User",
        
        "_id": "047cd911-356a-4851-948a-3087ea735353",
        "name": "AntuanSoft"
      }
  }
  
  }

Nota: Si no queremos que aparezcan valores repetidos en nuestra lista de objetos/strings lo mejor es utilizar el método  addToSet en vez de push. De este modo si introducimos un valor que ya existe, este no se insetará

viernes, 27 de julio de 2012

MongoDb: DataAccessResourceFailureException

Configurando uno de nuestros test sobre los datos almacenados en MongoDB sobre un nuevo dbmane (database-test) no creado ya en MongoDB me ha dado el siguiente error:

--
org.springframework.dao.DataAccessResourceFailureException: can't say something; nested exception is com.mongodb.MongoException$Network: can't say something
 at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:56)
 at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1545)
 at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368)
 at org.springframework.data.mongodb.core.MongoTemplate.doRemove(MongoTemplate.java:928)
 at org.springframework.data.mongodb.core.MongoTemplate.remove(MongoTemplate.java:919)
 at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.delete(SimpleMongoRepository.java:137)
 at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.delete(SimpleMongoRepository.java:146)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:334)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:319)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 at $Proxy52.delete(Unknown Source)

--

La configuración utilizada fue la siguiente:

--
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:flex="http://www.springframework.org/schema/flex" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:security="http://www.springframework.org/schema/security"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
  http://www.springframework.org/schema/flex
  http://www.springframework.org/schema/flex/spring-flex-1.5.xsd
  http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/task
  http://www.springframework.org/schema/task/spring-task-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/data/jpa
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

 
 <mongo:mongo host="apps.stratmind.net" port="27017" id="mongo">
         <mongo:options connections-per-host="8"
                    threads-allowed-to-block-for-connection-multiplier="4"
                    connect-timeout="1000"
                    max-wait-time="1500"
                    auto-connect-retry="true"
                    socket-keep-alive="true"
                    socket-timeout="1500"
                    slave-ok="true"
                    write-number="1"
                    write-timeout="0"
                    write-fsync="true"/>
    </mongo:mongo>

 <mongo:repositories base-package="com.antuansoft.mongo"/>

 <mongo:db-factory dbname="database-test" mongo-ref="mongo" id="mongoDbFactory"/>
 
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    <property name="writeResultChecking" value="EXCEPTION"/><!-- propiedad usada para que si hay error devuelva una excepción y no haya que comprobarlo -->    
 </bean>


</beans>
--


El error se produce cuando no tengo creada de antemano la base de datos  de mongo, si la base de datos ya está creada no hay problema.

Parece que Spring Data al crearla y al intentar acceder a la base de datos recien creada recibe un error del driver de MongoDb y a fecha de hoy 27/07/2012 (driver version: 2.7.3) existe un bug en este apartado.

Como referencia os dejo esta entrada en el JIRA de MongoDB en su versión del driver para java.

mongo driver should check whether socket still be available before use it?

jueves, 26 de julio de 2012

Problemas típicos de configuración de los Repositories de JPA y Mongodb combinados

Typical problems configuring JPA and Mongodb Repositories together

Utilizando Spring Data tenemos la posibilidad de acceder fácilmente a datos almacenados en base de datos relacionales como Mysql, Postgres, etc.. a través de Spring data JPA y también a base de datos NoSQL como por ejemplo MongoDB con Spring Data MongoDB.

Una de las principales carácterísticas de Spring Data es que ya no es necesaria la generación de classes DAO con las opreciones básicas como save, delete, update y find ya que ya vienen implementadas por defecto.

Para que eso funcione creamos una interfaz que heredará de una clase de Spring Data y ya tendrá todas las operaciones disponibles.


JPA (AnnouncementDao.java)
-
package com.antuansoft.daos.jpa
import org.springframework.data.jpa.repository.JpaRepository;

import com.antuansoft.Announcement;

public interface AnnouncementDao extends JpaRepository<Announcement, String>, CustomAnnouncementDao{


}


--

MongoDB (CompanuDao.java)


-
package com.antuansoft.daos.mongodb
import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;

import com.antuansoft.pojo.Company;

public interface CompanyDao extends MongoRepository<Company, String>, QueryDslPredicateExecutor<Company>{

}
--

Para que estos daos funcionen correctamente en nuestro contexto de Spring tenemos que  configurarlo en los xml a través de unos repositories que mapeen estas interfaces indicándole en la opción base-package que páquete(o alguno de sus hijos) contienen los DAOS.


Ambas opciones podemos combinarlas en nuestra arquitectura de datos pero lo normal cuando integramos las dos maneras de acceso dentro de un mismo proyecto es que nos den los siguientes errores:

1º Error típico:

-
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.antuansoft.pojos.Company
 at org.hibernate.ejb.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:171)
 at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:58)
 at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
 at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149)
 at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:87)
 at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:70)
 at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:137)
 at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
 at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
 at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
 ... 63 more

--


Este error ocurre por que JPA ha mapeado un DAO de Mongodb, esto ocurre cuando el base-package de ambas configuraciones es el mismo o el de mongo está dentro del de JPA. Por lo tanto JPA intentará configurar un DAO de Mongodb y no podrá procesar la Clase Company que es de Mongobd.

La configuración correcta sería que los paquetes donde están los daos sean distintos y no estén anidados.


JPA
-
<jpa:repositories base-package="com.antuansoft.daos.jpa"></jpa:repositories>
--


Mongodb
-
<mongo:repositories base-package="com.antuansoft.daos.mongodb"/>
--


2º Error típico:

El pojo del interface DAO no está configurados correctamente a través de la etiqueta  @Entity

Los pojos de JPA utilizan la @Entity
-
import javax.persistence.Entity;
--

Los pojos de MongoDB utilizan la @Entity
-
import com.google.code.morphia.annotations.Entity;
--

Si configuramos un pojo de MongoDb con una entity de JPA nos dará el siguiente error
-
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.antuansoft.pojos.Company
 at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:268)
 at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:223)
 at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:686)
 at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3977)
 at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3931)
 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
 at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
 at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
 at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
 at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
 at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:685)
 at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
--

Para finalizar os dejo unas referencias interesantes para ver como realizar estas configuraciones básicas.

Configuración basica de Spring Data Mongobd (mirad las otras 2 partes tb están bien)
Capa de persistencia con Spring Data JPA

miércoles, 11 de julio de 2012

Spring Data y QueryDSL operador like

QueryDSL like operator

Prerrequisitos: Conocimientos medios de QueryDsl.

El problema que resolvemos a continuación es el operador like usando QueryDSL. Si no quieres leerte todo el rollo te adelanto que el operador que buscas es containsIgnoreCase en vez de like pero tiene alguna pega que expongo a continuación.

Si has llegado hasta aquí buscando por google supongo que ya tendrás configurado tu entorno y QueryDsl para realizar tus consultas. Como ya sabemos la ventaja que te da QueryDsl es nos permite construir una query directamente sobre el código java de nuestro service.

Imaginemos que tenemos  el siguiente pojo

----
@Entity
@Document
public class Course implements Serializable {
 
 
 private static final long serialVersionUID = -7251789829032925885L;
 
 
 @Id
 private String id = UUID.randomUUID().toString();
 private String name; 
 private String description;
 private String companyOwner;
 private String metodology;

---

Una vez configurado QueryDsl automáticamente nos lo transforma nuestro pojo en el siguiente código.

----
/**
 * QCourse is a Querydsl query type for Course
 */
@Generated("com.mysema.query.codegen.EntitySerializer")
public class QCourse extends EntityPathBase<Course> {

    private static final long serialVersionUID = 430564814;

    public static final QCourse course = new QCourse("course");

    public final StringPath companyOwner = createString("companyOwner");

    public final StringPath description = createString("description");

    public final StringPath id = createString("id");

    public final StringPath metodology = createString("metodology");


    public QCourse(String variable) {
        super(Course.class, forVariable(variable));
    }

    public QCourse(Path<? extends Course> entity) {
        super(entity.getType(), entity.getMetadata());
    }

    public QCourse(PathMetadata<?> metadata) {
        super(Course.class, metadata);
    }


---

Esto nos permite hacer queries directamente con código java

----
BooleanBuilder builder = new BooleanBuilder();
   
   builder.and(QCourse.course.id.eq("id1"));
   builder.and(QCourse.course.name.like("Matematica"));

List<Course> data = (List<Course>) courseDao.findAll(builder.getValue()); 
      
   return data;

---


El problema es cuando queremos hacer una búsqueda sobre un campo String con lo que en SQL sería el operador LIKE, como por ejemplo Name like %Matematica% 

QueryDsl tiene el operador like pero su lo utilizamos nos da el siguiente error:
 
 
----
java.lang.UnsupportedOperationException: Illegal operation course.name like Matemáticas
 at com.mysema.query.mongodb.MongodbSerializer.visit(MongodbSerializer.java:225)
 at com.mysema.query.mongodb.MongodbSerializer.visit(MongodbSerializer.java:34)
 at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:52)
 at com.mysema.query.mongodb.MongodbSerializer.handle(MongodbSerializer.java:39)
 at com.mysema.query.mongodb.MongodbSerializer.visit(MongodbSerializer.java:98)
 at com.mysema.query.mongodb.MongodbSerializer.visit(MongodbSerializer.java:34)
 at com.mysema.query.types.OperationImpl.accept(OperationImpl.java:81)
 at com.mysema.query.mongodb.MongodbSerializer.handle(MongodbSerializer.java:39)
 at com.mysema.query.mongodb.MongodbQuery.createQuery(MongodbQuery.java:330)
 at com.mysema.query.mongodb.MongodbQuery.createCursor(MongodbQuery.java:240)
 at com.mysema.query.mongodb.MongodbQuery.createCursor(MongodbQuery.java:235)
 at com.mysema.query.mongodb.MongodbQuery.list(MongodbQuery.java:221)
 at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.findAll(QueryDslMongoRepository.java:113)


---
 
   
  La alternativa para que la operación sea la que deseamos es containsIgnoreCase


----

builder.and(QCourse.course.name.containsIgnoreCase("Matemati"));

---
 
  
  Esta búsqueda recuperará cualquier palabra en el nombre que contenga Matemati como por ejemplo, Matematica, Matematico, Matematicas ya esté en mayúsculas o minúsculas.


Lo que no encontrará este tipo de búsqueda son acentos, por ejemplo Matemáticas, así que no es una búsqueda full text pero se aproxima bastante.




miércoles, 4 de julio de 2012

Spring Data Mongodb Configurando WriteResultChecking Policy para lanzar excepciones

Setting WriteResultChecking Policy in order to throw Exceptions

Cuando trabajamos con Mongodb a la hora de hacer Queries debemos de comprobar el objeto WriteResult resultante para comprobar que la operación se ha realizado correctamente.


WriteResult wr = mongoOperations.updateFirst(
 new Query(where("id").is("objectId")),   
 new Update().push("favouriteUsers", idUser),Course.class);
  
 String error = wr.getError();




Para que no tengmos que hacer esto y si ocurre algún problema se lance una excepcion debemos de configurar Spring Data Mongo Db de la siguiente manera.

<mongo:mongo host="localhost" port="27017" id="mongo">
         <mongo:options connections-per-host="8"
                    threads-allowed-to-block-for-connection-multiplier="4"
                    connect-timeout="1000"
                    max-wait-time="1500"
                    auto-connect-retry="true"
                    socket-keep-alive="true"
                    socket-timeout="1500"
                    slave-ok="true"
                    write-number="1"
                    write-timeout="0"
                    write-fsync="true"/>
    </mongo:mongo>

 <mongo:repositories base-package="xx.xxxx.xxx"/>

 <mongo:db-factory dbname="database" mongo-ref="mongo" id="mongoDbFactory"/>
 
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    <property name="writeResultChecking" value="EXCEPTION"/>
<!-- propiedad usada para que si hay error devuelva una excepción y no haya que comprobarlo -->    
 </bean>


La clave está en la propiedad writeResultChecking que puede configurar con los valores NONE, LOG y EXCEPTION que es el caso que nos interesa.

Referencias:

Spring data mongo de documentacion

Ejemplo de configuración