Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, 30 August 2013

Encode & Decode URL Parameters In Java

A small code example describing how to encode and decode URL query strings properly from Java. The code example is available from GitHub in the in the Java-Core-Examples directory.
String paramValue = "with spaces and special &!=; characters";

String encoded = URLEncoder.encode(paramValue, "UTF-8");
String decoded = URLDecoder.decode(encoded, "UTF-8");

System.out.println(paramValue);
System.out.println(encoded);
System.out.println(decoded);
The output is:
with spaces and special &!=; characters
with+spaces+and+special+%26%21%3D%3B+characters
with spaces and special &!=; characters

Thursday, 18 April 2013

Calling Javascript From Java (Example)

This post explains how to call Javascript from Java. The code example is available at GitHub in the Java-Javascript-Calling directory.

Code Example

We create some small Javascript code in a string. This code dumps "Hello World!" and defines a function called myFunction() adding 3 to the provided value:
public class JavascriptCalling {

    public static final String MY_JS
        = "print(\"Hello world!\\n\");"
        + "var myFunction = function(x) { return x+3; };";

    public static void main(String[] args)
            throws ScriptException, NoSuchMethodException {

        // Retrieving the Javascript engine
        ScriptEngine se = new ScriptEngineManager()
            .getEngineByName("javascript");

        if ( Compilable.class.isAssignableFrom(se.getClass()) ) {

            // We can compile our JS code
            Compilable c = (Compilable) se;
            CompiledScript cs = c.compile(MY_JS);

            System.out.println("From compiled JS:");
            cs.eval();

        } else {

            // We can't compile our JS code
            System.out.println("From interpreted JS:");
            se.eval(MY_JS);

        }

        // Can we invoke myFunction()?
        if ( Invocable.class.isAssignableFrom(se.getClass()) ) {

            Invocable i = (Invocable) se;
            System.out.println("myFunction(2) returns: "
                + i.invokeFunction("myFunction", 2));

        } else {

            System.out.println(
                "Method invocation not supported!");

        }

    }

}
The above starts by retrieving the Javascript engine. Next, if one can compile the Javascript, our code is compiled. The script is executed, which prints "Hello World!". Last, if the engine allows invocation, we call our function defined in our Javascript code.

The result is:

Tuesday, 2 April 2013

Creating A Customized Spring User & Persistence

This post explains how to create a customized Spring user and persistence mechanism for authentication. The code is available at GitHub in the Spring-MVC-Customized-User directory.

Things To Take Into Consideration

Before creating a customized user, we need to remind what a user is in Spring. We know from here that a user is an implementation of the UserDetails interface. Spring security also requires a loading mechanism, as an implementation of the UserDetailsService interface. To keep this example simple, we are not going to implement login/logout or other security features.

The UserDetails interface is a bit incomplete in order to define a user in a real application. It defines getters and no setters. It is not a big deal, but the real pain is that the username (a string) is considered as 'the' key. Most software developers will prefer a long id.

To solve these issues, we define a PracticalUser interface:
public interface PracticalUserDetails
        extends UserDetails, CredentialsContainer {

    long getId();

    void setPassword(String password);

    void setAccountExpired(boolean b);

    void setAccountLocked(boolean b);

    void setCredentialsExpired(boolean b);

    void setEnabled(boolean b);

    void setAuthorities(
        Collection<? extends GrantedAuthority> authorities);

}
Typically, one would use JPA annotations on the implementation bean and save it in a database in a real application. However, for this example, the DAO will register users in an in-memory map as described further.

The UserDetailsService interface does not provide a get user per name or id, which is necessary for an administrator (for example). More, it does not allow to retrieve all existing users' id and name, or to update or insert users, or even to delete them.

Therefore, we create a PracticalUserDetailsService interface to solve these issues:
public interface PracticalUserDetailsService
        extends UserDetailsService {

    Set<PracticalUserDetailsDAO.UserIdentifiers> getUsers();

    void deleteUser(long id);

    void upsertUser(PracticalUserDetails user);

    PracticalUserDetails getUser(long id);

    PracticalUserDetails getUser(String username);

}
The corresponding implementation is called PracticalUserDetailsServiceInMemory for this example.

In-Memory DAO

To keep this example simple, we define a simple PracticalUserDetailsDAO:
public interface PracticalUserDetailsDAO<U extends PracticalUserDetails> {

    void create(U user);

    boolean contains(U user);

    U read(String username);

    U read(long id);

    void update(U user);

    void delete(String username);

    void delete(long id);

    interface UserIdentifiers {
        long getId();
        String getUsername();
    }

    Set<UserIdentifiers> getUsers();

}
We also define a special user id and name identifier interface to retrieve the set of existing user data, without returning all users. Our implementation of PracticalUserDetailsDAO is PracticalUserDetailsDAOInMemory.

In a real implementation, using a JPA Repository is more appropriate.

Configuration

In the security.xml file, we define our practical user detail service (it will be registered in the authentication manager) and the in-memory DAO bean:
<beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http auto-config="true">
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref='practicalUserDetailsServiceInMemory' />
    </authentication-manager>

    <beans:bean id="pud"
        class="com.jverstry.DAO.PracticalUserDetailsDAOInMemory">
    </beans:bean>

</beans:beans>

The JSP Pages

We use two pages. The main page displays registered users (together with a delete link) and registration form:


The second page is displayed when the Create User button is clicked to check the name and the password:


The Controller

The controller is used to check the username and password:
@Controller
public class MyController {

    @Autowired
    private PracticalUserDetailsServiceInMemory pudm;

    @RequestMapping(value = "/")
    public ModelAndView index() {

        ModelAndView result = new ModelAndView("index");

        result.addObject("users", this.pudm.getUsers());

        return result;

    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value="id") String id) {

        this.pudm.deleteUser(Long.parseLong(id));

        return "redirect:/";

    }

    @RequestMapping(value = "/create")
    @SuppressWarnings("AssignmentToMethodParameter")
    public ModelAndView add(
            @RequestParam(value="name")
            String name,
            @RequestParam(value="password")
            String password) {

        name = StringUtils.replace(name, " ", "");
        password = StringUtils.replace(password, " ", "");

        String errorMsg = "";

        if ( name.length() == 0 ) {
            errorMsg += "Name is empty ";
        }

        if ( password.length() == 0 ) {
            errorMsg += "Password is empty ";
        }

        if ( errorMsg.isEmpty() ) {
            this.pudm.upsertUser(new PracticalUserDetailsImpl(name, password));
        }

        ModelAndView result = new ModelAndView("create");

        result.addObject("errorMsg", errorMsg);
        result.addObject("username", name);

        return result;

    }

}

Running The Example

One can run it using the maven tomcat:run goal. Then, browse:

  http://localhost:9191/spring-mvc-customized-user/

More Spring related posts here.

Execute Spring ACL SQL Script In Memory

This post describes how to execute the Spring ACL script to create ACL tables in an in-memory HSQL database instance. The code is available at GitHub in the Execute-ACL-SQL-Scripts-In-Memory directory.

In-Memory DataSource

We are using a configuration classe implementing the DisposableBean interface to shut down the created HSQL embedded database nicely.
@Configuration
public class InMemoryDataSource implements DisposableBean {

    private EmbeddedDatabase ed;

    @Bean(name="hsqlInMemory")
    public EmbeddedDatabase hsqlInMemory() {

        if ( this.ed == null ) {

            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();

            this.ed = builder.setType(EmbeddedDatabaseType.HSQL)
                .addScript("aclSchema.sql").build();

        }

        return this.ed;

    }

    @Override
    public void destroy() {

        if ( this.ed != null ) {
            this.ed.shutdown();
        }

    }

}
When creating the database, we also have the aclSchema.sql script executed. It must be located at the root of the resource directory:


The content of the script is taken from the Spring documentation appendix.

Checking Table Creation

We extract the list table of tables in the controller. Technically speaking, we should do this at the service/DAO level, but for the sake of this example, we'll keep it simple:
@Controller
public class MyController {

    @Autowired
    EmbeddedDatabase hsqlInMemory;

    @RequestMapping(value = "/")
    public ModelAndView home() {

        ModelAndView result = new ModelAndView("index");

        ArrayList<String> tables = new ArrayList<String>();

        JdbcTemplate tplate = new JdbcTemplate(hsqlInMemory);

        SqlRowSet retr = tplate.queryForRowSet(
            "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");

        while (retr.next()) {
            tables.add(retr.getString(1));
        }

        result.addObject("tables", tables);

        return result;

    }

}

Running The Example

One can run it using the maven tomcat:run goal. Then, browse:

  http://localhost:8585/execute-acl-sql-scripts-in-memory/

We find the tables we have created:


More Spring related posts here.

Thursday, 28 March 2013

Java ServiceLoader Example

Defining an API and developing the corresponding implementation has become an uber mainstream practice when developing large Java applications. Modularization is such an useful design principle, especially to avoid spaghetti code, for testing and debugging, and for re-implementation of old code.

Many developers seek to separate API interfaces and abstract classes from their implementation in separate packages. Yet, this almost always leads to cycles between java packages (api refers to impl, and vice-versa). There has been many frameworks, such as OSGi, supporting modularization, but the documentation has not always been good and complete. Many developers have struggled with class loading issues too.

Fortunately, Java has delivered the ServiceLoader utility since release 6. The example described in this post is available from Github in the Java-ServiceLoader directory. It is inspired from here.

Service & Implementation

We define a simple interface and its implementation:
package com.service.API;

public interface MyService {
    long getTime();
}


package com.service.API.Impl;

import com.service.API.MyService;

public final class MyServiceImpl implements MyService {

    public MyServiceImpl() { };

    @Override
    public long getTime() {
        return System.currentTimeMillis();
    }

}
The API and the implementation are located in different packages. Only the implementation refers to the API. The implementation must have a public parameterless constructor.

Service Loading

Next, we declare the implementation in a file having the fully qualified name of the API under the META-INF/services directory in the .jar:


The file contains fully qualified name of the implementation:

    com.service.API.Impl.MyServiceImpl

Next, we load the service using the ServiceLoader:
public class MyApp {

    public static <T> T loadService(Class<T> api) {

        T result = null;

        ServiceLoader<T> impl = ServiceLoader.load(api);

        for (T loadedImpl : impl) {
            result = loadedImpl;
            if ( result != null ) break;
        }

        if ( result == null ) throw new RuntimeException(
            "Cannot find implementation for: " + api);

        return result;

    }

    public static final MyService myService = loadService(MyService.class);

    public static void main(String[] args) {

        System.out.println("Time is: " + myService.getTime());

    }

}
The service implementation is loaded with the loadService static method. The main method prints the current time in milliseconds. It is that simple!

Sunday, 24 March 2013

Java TestNG Example

This post illustrates some of the feature of TestNG. The code example is available at GitHub in the Java-TestNG directory. We use maven. Therefore, the configuration will located in the pom.xml.

Source Code

We use a very simple class returning integers for testing purposes:
public class Generate {

    private Generate() { }

    public static int aOne() {
        return 1;
    }

    public static int aTwo() {
        return 2;
    }

    ...

}

Test Code

The test code for the above class is the following:
public class GenerateNGTest {

    public GenerateNGTest() { }

    @Test(groups = {"testGroupA"})
    public void testAOne() {
        assertEquals(1,Generate.aOne());
    }

    @Test(groups = {"testGroupA", "testGroupB", "windows.QTests"})
    public void testATwo() {
        assertEquals(2,Generate.aTwo());
    }

    @Test(groups = {"testGroupA", "testGroupC", "windows.PTests"})
    public void testAThree() {
        assertEquals(3,Generate.aThree());
    }

    @Parameters({ "testNgParam" })
    @Test(groups = {"testGroupA"})
    public void testAFour(@Optional("4") String testNgParam) {
        assertEquals(Integer.parseInt(testNgParam),Generate.aFour());
    }

    @Parameters({ "testNgParam2" })
    @Test(groups = {"testGroupA"})
    public void testASix(String testNgParam2) {
        assertEquals(Integer.parseInt(testNgParam2),Generate.aSix());
    }

    public class DataForTests {

        @DataProvider(name = "forTestAFive")
        public Object[][] forTestAFive() {
            return new Object[][] {
                { new Integer(5) },
                { new Integer(6) }
            };
        }

    }

    @Test(dataProvider = "forTestAFive", dataProviderClass = DataForTests.class)
    public void testAFive(int param) {
        assertEquals(param,Generate.aFive());
    }

    @Test(dependsOnGroups = { "windows.*" })
    public void testASeven() {
        assertEquals(7,Generate.aSeven());
    }

}
  • Some test groups are defined for test methods. These can be activated in the configuration described further.
  • The testAFour() method takes in a parameter and defaults to 4 if none is provided.
  • Rather than setting parameters in the configuration, one can define a DataForTests class and use it to pass parameters as in the testAFive() method.
In this example, we do not cover factories and automatic test generation, programmatic executions of tests and test methods interceptors.    

Configuration

In the pom.xml:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.14</version>
  <configuration>
    <groups>testGroupA</groups>
    <systemPropertyVariables>
      <testNgParam>4</testNgParam>
      <testNgParam2>6</testNgParam2>
    </systemPropertyVariables>
    <parallel>methods</parallel>
    <threadCount>2</threadCount>
  </configuration>
</plugin>
We activate tests belonging to testGroupA and we pass parameter values for testNgParam and testNgParam2. We also let TestNG run each test method in parallel using 2 threads. This can significantly accelerate the testing phase when compiling the project.

Wednesday, 20 March 2013

'package javax.servlet.http does not exist' Solution

This is a common error which occurs when trying to compile a web project. This package is usually provided by the container. However, it is not (always) available at compile time and one should not include it in the final .war too.

If you are using Maven, the solution is simple. Add the following dependency with the provided scope:
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
Your IDE will be able to compile your project and won't include the library in your .war.

You can also use the following dependency:
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency>

Thursday, 7 March 2013

Spring JpaRepository Example (In-Memory)

This post describes a simple Spring JpaRepository example using an in memory HSQL database. The code example is available from GitHub in the Spring-JpaRepository directory. It is based on the Spring-MVC-With-Annotations example and information available here.

JPA Repository

We implement a dummy bean for this example:
@Entity
@AutoProperty
public class SomeItem {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long Id;

    private String someText;

    /* ...Setters & Getters */

}
and the corresponding JpaRepository:
@Transactional
public interface SomeItemRepository
        extends JpaRepository<SomeItem, Long> {

}

Service & Controller

Next, we implement a service where our repository will be injected. We also populate the repository with dummy data:
@Service
@Repository
public class SomeItemService {

    @Autowired
    private SomeItemRepository someItemRepository;

    @PostConstruct
    @Transactional
    public void populate() {

        SomeItem si = new SomeItem();
        si.setSomeText("aaa");
        someItemRepository.saveAndFlush(si);

        si = new SomeItem();
        si.setSomeText("bbb");
        someItemRepository.saveAndFlush(si);

        si = new SomeItem();
        si.setSomeText("ccc");
        someItemRepository.saveAndFlush(si);

    }

    @Transactional(readOnly=true)
    public List<SomeItem> getAll() {

        return someItemRepository.findAll();

    }

    @SuppressWarnings("AssignmentToMethodParameter")
    @Transactional
    public SomeItem saveAndFlush(SomeItem si) {

        if ( si != null ) {
            si = someItemRepository.saveAndFlush(si);
        }

        return si;

    }

    @Transactional
    public void delete(long id) {

        someItemRepository.delete(id);

    }

}
and a controller:
@Controller
public class MyController {

    @Autowired
    private SomeItemService someItemService;

    @RequestMapping(value = "/")
    public ModelAndView index() {

        ModelAndView result = new ModelAndView("index");
        result.addObject("items", this.someItemService.getAll());

        return result;

    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(
        @PathVariable(value="id") String id) {

        this.someItemService.delete(Long.parseLong(id));

        return "redirect:/";

    }

    @RequestMapping(value = "/create")
    @SuppressWarnings("AssignmentToMethodParameter")
    public String add() {

        SomeItem si = new SomeItem();
        si.setSomeText("Time is: " + System.currentTimeMillis());

        this.someItemService.saveAndFlush(si);

        return "redirect:/";

    }

}

JPA Configuration

On top of creating an entity manager based on an in-memeory instance of HSQL database, we enable JPA repositories with the @EnableJpaRepositories annotation:
@Configuration
@EnableJpaRepositories(basePackages={"com.jverstry"})
@EnableTransactionManagement
public class JpaConfig implements DisposableBean {

    private EmbeddedDatabase ed;

    @Bean(name="hsqlInMemory")
    public EmbeddedDatabase hsqlInMemory() {

        if ( this.ed == null ) {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            this.ed = builder.setType(EmbeddedDatabaseType.HSQL).build();
        }

        return this.ed;

    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){

        LocalContainerEntityManagerFactoryBean lcemfb
            = new LocalContainerEntityManagerFactoryBean();

        lcemfb.setDataSource(this.hsqlInMemory());
        lcemfb.setPackagesToScan(new String[] {"com.jverstry"});

        lcemfb.setPersistenceUnitName("MyPU");

        HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
        lcemfb.setJpaVendorAdapter(va);

        Properties ps = new Properties();
        ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        ps.put("hibernate.hbm2ddl.auto", "create");
        lcemfb.setJpaProperties(ps);

        lcemfb.afterPropertiesSet();

        return lcemfb;

    }

    @Bean
    public PlatformTransactionManager transactionManager(){

        JpaTransactionManager tm = new JpaTransactionManager();

        tm.setEntityManagerFactory(
            this.entityManagerFactory().getObject() );

        return tm;

    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Override
    public void destroy() {

        if ( this.ed != null ) {
            this.ed.shutdown();
        }

    }

}

The JSP Page

We create a simple page to list existing items with a delete link, and the possibility to create new items:

 Running The Example

One can run it using the maven tomcat:run goal. Then, browse:

  http://localhost:9191/spring-jparepository/

More Spring related posts here.

Monday, 11 February 2013

Securing A Service And JSP Pages

Securing A Spring Service

When a service is implemented in Spring, it can be secured with the @Secured annotation. It has a parameter where the list of roles can be defined. In order to enable this annotation, one must add the the following line in the Spring Security configuration XML file:

  <security:global-method-security secured-annotations="enabled"/>

A complete example is available here.

Securing A JSP Page

Spring defines its own set of JSP tags to control what is displayed to users. This is achieved with the Authorization tag. The Authentication tag can be used to retrieve user details data too.

Tuesday, 5 February 2013

Introduction To Java EE Concepts

This post aims at clarifying acronyms and concepts used in the Java EE paradigm, where EE stands for Entreprise Edition. It enables the creation of modular Java applications to be deployed on application servers. It relies on Java SE, a core set of Java libraries upon which all Java applications are implemented.

General Concepts

Before we dive into Java EE, here is a reminder of general concepts:
  • Multitier Application - A multitier application (or multitier architecture) is an application divided into multiple logical parts which are implemented (most often) on multiple servers. For example, the 3-tier model with a user and its browser, the application server and the database located on a separate server dealing with application server requests.
  • Multitier Service - When thinking about services in the 3-tier model, the application server is a service providing responses to user requests. The database server provides answers to queries made by the application server.
  • Java Bean - These are Java classes containing private properties, made accessible with getters and setter methods. Technically speaking, these classes must also have a no parameter constructor and implement the Serializable interface.

Java EE 5 Concepts

Java EE Multitier Application (from the Java EE 5 Tutorial)

  • Java EE Application Model - This model describes a multitier architecture to implement services. The boundary defining what a service is is not defined clearly. In fact, a service is a logical concept and its concrete/real counterpart depends on the application implementing those concepts. For example, it is a server in the client/server model.
  • Java EE Server - It is the application server of the 3-tier paradigm, in the Java EE paradigm. It is composed of the web tier (serving JSP pages) and the business tier (managing enterprise Java beans).
  • Java EE Components - 'Components' mean independent software unit used in combination with other components to build  an application.
    • Web Component - Part of the web tier generating pages or whatever is returned to user queries. Typically, these are Java Servlets, JavaServer Faces and Java Server Pages.
    • Applet - A small Java software application sent as part of a response to a user request, and which is meant to run in the user's browser. Applets are in competition with Javascript.
    • Application Client - For example, a desktop application written in Java.
    • Enterprise Java Beans - See below.
  • Java EE Clients - This concepts regroups three other concepts:
    • Web Client (or thin client) - In (not so) old times, web pages returned to client requests used to contain code scripts which were executed on the application server before returning the result to clients. These pages were considered as EE clients. For example, one could query a list of employee and display different results depending on the querying user. Today, most developers do not include scripts in their web pages anymore. It is considered a bad practice. They tend to use MVC design principles, which prohibit scripts in those pages.
    • Applet
    • Application Client
  • Java EE Server Communication - This refers to the communication happening between thin clients and application clients with the application server. Typically, thin clients and applets communicate with the web tier, while application clients communicate with EJBs.
You don't find this very clear too? You think it is cumbersome? Welcome to the club! This is why REST principles, together with MVC design principles are prevailing in web application development nowadays.
  • EJB (Enterprise Java Beans) - This is a logical concept which has nothing to do with traditional Java Beans. It basically tries to encapsulate business logic on the backend (i.e., server-side) of multi-tier applications. One key issue it was originally trying to solve is transactional integrity with the database. Warning: there has been several versions of EJB and version 3.0 is a clear breakaway from earlier versions.
  • JavaBean Component - It is a synonym of Java Bean mostly used in the JSP paradigm.
  • Java EE Container - This is where assembled Java EE components are deployed (i.e., on the application server). One defines web containers containing servelts and JSP pages, and EJB containers containing Entreprise Java Beans. One also defines application client containers for standalone applications and applet containers in browsers.
  • Deployment Descriptor - This is a XML file providing deployement information for packaged applications. See here for more information.
  • SOAP (Simple Object Access Protocol) - It is a protocol to exchange messages across services offered on the web. The messages are structured according to XML and can be exchanged via many transportation layers, but most often HTTP. SOAP competes with JSON and REST-like services.
  • WSLD (Web Service Description Language) - It describes network services using XML documents (name, location, communication mode).
  • UDDI (Universal Description, Discovery and Integration) an ebXML - Lets on publish information about products and service online.
  • Java Servlet - A servlet is a Java class processing incoming user HTTP requests and returning a result.
  • JSP (JavaServer Page) - A text based HTML document (i.e., a kind of template) processed to produce static content. It can contain snippets of script code which is executed to render the final static documents.
  • JSTL (JavaServer Page Standard Tag Library) - A set of standard tags used in JSP to mimic scripting code functionalities. For example, looping over a list of clients to display their corresponding information. Nowadays, such tags have replaced scripting code in JSP. They are mostly used to generated to populate pages with information to display to users.
  • JSF (Java Server Face) - These are JavaServer Page including JavaServer Face tags (which are similar to JSTL tags). In addition, JSF allows the definition of navigation models between pages (something similar to Spring web flow). It also includes Facelets.
  • Facelets - Facelets is the MVC view part of JSF. It basically converts templates into HTML documents using provided data (for example with a list of clients). It can also extract parts of generated documents (for example a HTML body) and include it in another template. This is equivalent to what Sitemesh does too.
  • The Java API for XML-based Web Services (JAX-WS) - A mean to define web services using XML communication.
  • The Java API for XML Binding (JAXB) - A way to convert (typically) Java beans into XML documents back and forth. See here for more details.
  • JTA (Java Transaction API) - Mean to delimit database transactions with autocommit and rollback.
  • JMS (Java Message Service) - An API allowing exchanges of messages reliably and asynchronously between application and services over the internet. ActiveMQ implements JMS.
  • JavaMail API - A mean to send emails from an application.
  • JAXP (Java API for XML Processing) - Enables the processing of XML documents.
  • JAXR (Java API for XML Registries) - An API enabling access to registries containing metadata. It is based on XML.
  • JDBC (Java Database Connectivity) - This API lets an application perform SQL transaction directly with databases.
  • JCA (Java EE Connector Architecture) - It helps applications obtain connection to information systems (often database). See this post for more details.
  • JPA (Java Persistence API) - It is an ORM (Object/relational mapping) definition to help store objects in databases. See here for examples.
  • JNDI (Java Naming and Directory Interface) - A mean to store and retrieve resources or access to resources using their name. See this post for more details.
  • JAAS (Java Authentication and Authorization Service) - A mean to authentication and control access to services.

Java EE 6 Additional Concepts

With J2EE 6, a lot of the configuration can be performed with annotations in the Java source code.
  • JAX-RS - API to define REST-like services.
  • Managed Beans - A java object where injections can be performed. In Spring, these would be objects with properties annotated with @Autowired (for example).
  • CDI (Contexts and Dependency Injection) -The mecanism implementing and executing dependencies injection. In Spring, one would mention application context, inversion of control (IoC) and dependency injection (DI).
  • Bean Validation - A mean to make sure a Java Beans state is valid according to predefined rules.
  • JACC (Java Authorization Contract for Containers) - Specifies relationships and transactions between EE containers and authorization providers.
  • JASPIC (Java Authentication Service Provider Interface) - This is the complementary authentication SPI (i.e., Service API) to JACC. It defines how applications can access traditional authentication services.
Overall, Spring and Java EE slowly converge in the same direction.

Tuesday, 29 January 2013

What Is JNDI, SPI, CCI, LDAP And JCA?

JNDI stands for Java Naming and Directory Interface. It is an API to providing access to a directory service, that is, a service mapping name (strings) with objects, reference to remote objects or simple data. This is called binding. The set of bindings is called the context. Applications use the JNDI interface to access resources.

To put it very simply, it is like a hashmap with a String key and Object values representing resources on the web. Often, these resources are organized according to a hierarchy in directory services. Levels are defined with separators (for example '.' for DNS, ',' for LDAP). This is a naming convention. Each context has its naming convention.

SPI stands for Service Provider Interface. In other words, these are APIs for services. JNDI specifies a SPI to implement directory services. Objects stored in directories can have attributes (id and value). CRUD operations can be performed on these attributes.

Rather than providing a name, one can also search for objects according to their attributes, if the directory allows it. The information provided by user applications is called a search filter.

What Issues Does JNDI Solve?

Without JNDI, the location or access information of remote resources would have to be hard-coded in applications or made available in a configuration. Maintaining this information is quite tedious and error prone.

If a resources has been relocated on another server, with another IP address, for example, all applications using this resource would have to be updated with this new information. With JNDI, this is not necessary. Only the corresponding resource binding has to be updated. Applications can still access it with its name and the relocation is transparent.

Another common use is when applications are moved from a development environment, to a testing environment and finally to production. At each stage, one may want to use a different database for development, testing and production. In each context, one can make a different binding to each database. The application does not need to be udpated.

What is LDAP?

LDAP stands for Lightweight Directory Application Protocol. It is often used as a directory service in JNDI. Today, companies set LDAP server dedicated to responding to JNDI requests. A common use is to maintain a list of company employees, together with their emails and access credentials to miscellaneous application.

By centralizing this information, each application does not have to store multiple copies of employees information in their own databases, which is easier to maintain and less prone to errors and incoherencies.

What About JCA and CCI?

JCA stands for Java EE Connector Architecture. It is a Java technology helping application servers and their applications connect to other information systems, by providing them with connections to these. JCA defines its own SPI for the connector service. CCI stands for Common Client Interface. It is defined as part of JCA. It is the API user applications use to access JCA connection services.

JCA helps integrating information systems developed separately. Typically, instead of using JDBC to access databases, which is more or less equivalent to hard-coding configurations, a user application can use JCA to connect to these databases (or information systems). The JCA instance can be registered in the JDNI directory and retrieved by the user applications too.

What About Web Applications?

Typically, web applications run in containers called application servers. Web applications can create their own JNDI service to store objects, but they can also retrieve these from the container itself by using their corresponding name. In this case, the resource (often a database) is configured at the container level.

Tuesday, 8 January 2013

Spring Selenium Tests With Annotations

This post describes how to implement Selenium tests in Java. It is inspired from the post by Alex Collins, with annotations. The code is available on GitHub in the Spring-Selenium-Test directory. Some alternative and much lighter techniques are available to unit test a Spring MVC application. To unit test services, see here.

Page, Configuration & Controller

We create a simple page with 'Hello World':
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Welcome !!!</title>
</head>
<body>
  <h1>
   Hello World !
  </h1>
</body>
</html>
We keep our controller very simple:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.jverstry")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}
and our controller too:
@Controller
public class MyController {

    @RequestMapping(value = "/")
    public String home() {
        return "index";
    }

}

For Selenium Testing

We create a configuration for testing. It provides the URL to open the application locally. The application is opened with Firefox:
@Configuration
public class TestConfig {

    @Bean
    public URI getSiteBase() throws URISyntaxException {
        return new URI("http://localhost:10001/spring-selenium-test-1.0.0");
    }

    @Bean(destroyMethod="quit")
    public FirefoxDriver getDrv() {
        return new FirefoxDriver();
    }

}
We also define an abstract class as a basis for all tests. It automatically closes Firefox after the test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ TestConfig.class })
public abstract class AbstractTestIT {

    @Autowired
    protected URI siteBase;

    @Autowired
    protected WebDriver drv;

    {
        Runtime.getRuntime().addShutdownHook(new Thread() {
           @Override
           public void run() {
                drv.close();
           }
        });
    }

}
And we implement a selenium test where we make sure our page contains 'Hello World':
public class SeleniumTestIT extends AbstractTestIT {

    @Test
    public void testWeSeeHelloWorld() {
        drv.get(siteBase.toString());
        assertTrue(drv.getPageSource().contains("Hello World"));
    }

}
The maven dependencies are the same as those described in Alex Collins's post.

Building The Application

If you build the application, it will open and close firefox automatically. The test will be successful.

Wednesday, 2 January 2013

Javascript Variable Scope Caveats

I ran into a strange Javascript variable scope issue. I am not satisfied with the documentation I found so far, hence this post:
  • A variable aaa declared outside of a function is global.
  • If a function f1 declares a aaa variable in its code using var, all modifications made to this variable is local and the global aaa variable is not impacted.
  • If one uses the f1 local aaa variable in f1 code before it is declared, Javascript will consider it undefined.
  • If a function f2 does not declare a local aaa variable in its code, it can access the global aaa variable by using its name. All modifications are registered in the global variable.
  • Passing a global variable as a parameter to a function, and modifying this parameter inside that function does not modify the global variable.

Thursday, 27 December 2012

How To Import/Index Large Blogs On Blogger?

Sharing some of the experience behind creating the Find-Word blog series.

In order to create those blogs, I first tried to use the Google APIs, but unfortunately, these are not suited for Java standalone applications. The authentication mecanism is way too overkill compared to real needs and it does not seem to have been tested properly with standalone applications. I moved to Google's older Data APIs and they worked like a charm. The only issue was the limitation on the number of authorized posts per day (50, then a captcha is displayed). I finally decided to reverse engineer the import-export blogger functionality.

About importing posts:
  • One can split posts into multiple import files, and load them one by one.
  • Blogger won't be able to load files bigger than 30 MB (it will crash).
  • Once loaded, the posts have the imported status.
  • One can publish at most 1800 posts the first day, then about 500 the next day.
  • Crossing those limits will flag your blog as potential spam.
  • One cannot publish posts anymore and a request for review must be introduced, else the blog will be deleted.
About indexing:
  • Of course, it is not possible to force Google to index all blog posts. One can only have Google crawl your pages. Google will then decide what goes into the Index.
  • The default Blogger sitemap only sees the last 26 posts. So, unless there are external or internal links to the remaining posts, Google will not be able to access them. They won't be crawled or indexed.
  • The solution is to add atom sitemaps (for example: /atom.xml?redirect=false&start-index=1&max-results=500 for the first 500 posts) using Google Webmaster's Optimization > Sitemaps page. Multiple atom sitemaps must be added if necessary:
Atom Sitemaps
  • If your blog posts are lengthy and Google can't process your sitemaps, configure your blog to allow short blog feeds in Settings > Other. If your blog does not allow feeds, Google will not be able to process the sitemaps too!
  • The Sitemaps page will tells you whether sitemaps have been processed successfully.
  • Google Webmaster has a page indicating which links have been indexed in Health > Index Status. It is updated about once per week, which is too slow for useful feedback.
  • The solution is to use the site:myblog.blogspot.com command from the Google search page to have an estimation of how many pages Google knows about your blog. It also tells you whether crawling is successful or not.
  • Google Webmaster has a Crawled Stats page which tells you (with a 2 days delay) how many pages it has been crawling per day. This is also a good indicator of the reachability of posts.
  • Be patient!
That's it !!!

Sunday, 18 November 2012

Spring Internationalization Example (with Annotations)

Spring InternationalizationThis post describes how to implement message internationalization in Spring. The code example is available from GitHub in the Spring-MVC-Internationalization directory. It is based on the Spring MVC with annotations example.

Internationalization

We define two resource bundles (setA and setB) containing string translations for German, French and English. These are created in the src/main/resources maven directory.

Configuration

We need to create:
  • a ResourceBundleMessageSource bean to load the string translations
  • a LocaleChangeInterceptor bean which will intercept requests and extract a parameter value (if available) to detect language changes
  • a SessionLocaleResolver bean to store the user's locale preference in the session
  • to register the LocaleChangeInterceptor in the interceptor registry
 We extend our web configuration:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.jverstry")
public class WebConfig extends WebMvcConfigurerAdapter {

    ...

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource result
            = new ResourceBundleMessageSource();

        String[] basenames = {
            "i18n.setA.setA",
            "i18n.setB.setB"
        };

        result.setBasenames(basenames);

        return result;

    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {

        LocaleChangeInterceptor result = new LocaleChangeInterceptor();
        result.setParamName("lang");

        return result;

    }

    @Bean
    public LocaleResolver localeResolver() {

        SessionLocaleResolver result = new SessionLocaleResolver();
        result.setDefaultLocale(Locale.ENGLISH);

        return result;

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

}

Controller

We simply our controller a lot:
@Controller
public class MyController {

    @RequestMapping(value = "/")
    public String home() {
        return "index";
    }

}

JSP Page

We only keep the index.jsp page, where we add links to change the language of displayed messages:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!doctype html>
<html lang="en">
<head>
  <title>Welcome To Spring MVC Internationalization !!!</title>
</head>
<body>
  <h1>Spring MVC Internationalization !!!</h1>
  <p>Choose:
      <a href="<c:url value='?lang=en'/>">English</a>
      | <a href="<c:url value='?lang=fr'/>">French</a>
      | <a href="<c:url value='?lang=de'/>">German</a>
  </p>

  <p>Greetings: <spring:message code="greetings" text="missing" /></p>
  <p>Text 2: <spring:message code="text2" text="missing" /></p>
  <p>Current: <c:out value="${pageContext.response.locale}" /></p>
</body>
</html>

Running The Example

Once compiled, the example can be run with mvn tomcat:run. Then, browse:

  http://localhost:8383/spring-mvc-internationalization/

The main page will be displayed:


If you click on German, the German text is displayed:


More Spring related posts here.

Spring Bean XML To Annotation Configuration

Here is an example of converting an XML based bean configuration to a annotation based configuration.

Assuming an XML files contain the following:
<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>
<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
       <ref bean="localeChangeInterceptor" />
   </property>
</bean>
The second beans refers to the first one for initialization.

The corresponding Java with annotation configuration is:
@Configuration
public class MyConfig {

    // ...

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {

        LocaleChangeInterceptor result
            = new LocaleChangeInterceptor();

        result.setParamName("lang");

        return result;

    }

    @Bean
    public DefaultAnnotationHandlerMapping handlerMapping() {

        DefaultAnnotationHandlerMapping result
            = new DefaultAnnotationHandlerMapping();

        Object[] interceptors = {
            localeChangeInterceptor()
        };

        result.setInterceptors(interceptors);

        return result;

    }

}

More Spring related posts here.

Saturday, 17 November 2012

Introduction To Java Internationalization

This post is a quick introduction/reminder about internationalization (i18n) in Java. The code examples are available from GitHub in the Java-Internationalization directory.

Introduction

Making a Java application international is about dealing with:
  • Displayed text - Proper text according to user language.
  • Dates - Formatting, for example month, day year, or day, month, year?
  • Currencies - Using a comma or a dot as a separator? number of significant digits?

Locale

Localization is the process of adapting displayed text, dates and currencies to a country, region or language. Java defines the Locale class, which represents a language, and eventually a country or region associated to it.

The following describes how to list available Locale:
for ( Locale l : Locale.getAvailableLocales() ) {
    String s = l.getDisplayLanguage(Locale.ENGLISH)
        + " " + l.getDisplayCountry(Locale.ENGLISH);
    System.out.println(s);
}
The language and corresponding country is displayed (if available):
Japanese Japan
Spanish Peru
English
...

Resource Bundle & Property Files

ResourceBundles are created by providing a Locale. They contain objects and strings internationalized according to the provided locale.

Internationalized strings can be loaded from property files too:

Resource Bundles in Maven Java

Here is the content of MessageBundle_fr_FR:
greetings = Bonjour.
farewell = Au revoir.
inquiry = Comment allez-vous?
The following code describes how to create locales and load corresponding property files:
// Creating locales
Locale l_de_DE = new Locale("de", "DE");
Locale l_fr_FR = new Locale("fr", "FR");

// Resource bundles based on property files
ResourceBundle rb_de_DE
    = ResourceBundle.getBundle(
        "com.jverstry.ResourceBundles.MessagesBundle",
            l_de_DE);

ResourceBundle rb_fr_FR
    = ResourceBundle.getBundle(
        "com.jverstry.ResourceBundles.MessagesBundle",
            l_fr_FR);

// Fetching translated text
System.out.println(rb_de_DE.getString("inquiry"));
System.out.println(rb_fr_FR.getString("inquiry"));
System.out.println(rb_de_DE.getString("greetings"));
System.out.println(rb_fr_FR.getString("greetings"));

Notice that one must provide the "package path" to load the property files.

The output is:
Wie geht's?
Comment allez-vous?
Hallo.
Bonjour.

Dates & Currencies

In an earlier post, we have already described how to format currencies.

The following code describes how to format a date using locales:
// Creating locales
Locale l_de_DE = new Locale("de", "DE");
Locale l_fr_FR = new Locale("fr", "FR");

// Retrieving date formatters
DateFormat df_de_DE
    = DateFormat.getDateInstance(DateFormat.FULL, l_de_DE);

DateFormat df_fr_FR
    = DateFormat.getDateInstance(DateFormat.FULL, l_fr_FR);

Date date = new Date();

System.out.println(df_de_DE.format(date));
System.out.println(df_fr_FR.format(date));
The output is:
Samstag, 17. November 2012
samedi 17 novembre 2012

More Java related posts here.

Sunday, 11 November 2012

Introduction To Spring JPA Data Features

This post is a quick introduction to Spring JPA Data's (SJD) features. This Spring module is built on top of the Spring Data Commons module, which is a prerequisite read in order to understand this post.

Features

This module offers several features:
  • JpaRepository<T, ID extends Serializable> - This interface extends the CrudRepository and PageAndSortingRepository interfaces of the Spring Data Commons module. It offers a couple of extra flush, find all and delete operations. See here for an operational example.
  • JPA Query Methods - This is a powerful mechanism allowing Spring to create queries from method names in classes/interfaces implementing Repository. For example: List<Invoice> findByStartDateAfter(Date date); is automatically translated into select i from Invoice i where u.startDate > ?1.
  • @Query - Queries can be associated to methods in Repository classes/interfaces. For example, a method can be annotated with @Query("select i from Invoice i where u.startDate > ?1")
  • @Modifying - This annotation can be used in combination with @Query to indicate that the corresponding query will perform modifications. Hence, any outdated entities are cleared first.
  • @Lock - This annotation is used to set the lock mode type (none, optimistic, pessimistic, etc...) for a given @Query.
  • JpaSpecificationExecutor and Specification - This interface adds a couple find and count of methods to repository classes/interfaces. All, have a Specification  parameter, which add predicates (i.e., where clauses) to corresponding queries.
  • Auditable, AbstractPersistable and AbstractAuaditable - The Auditable interface allows one to track modifications made to an entity (creation, last modification...). The AbstractPersistable and AbstractAuditable are abstract class facilities avoiding the boilerplate code.
  • MergingPersistenceUnitManager - If a developer decides to modularize his/her application, he/she may still want to use a unique persistence unit, even though they are declared in separate XML file. The MergingPersistenceUnitManager solves this issue.
At last, to enable JPA repositories, the:

    @EnableJpaRepositories("com.my.repositories")

should be set on a Java @Configuration class.

More Spring related posts here.

Introduction To Spring Data Commons Features

This post is a quick introduction to Spring Data Common's (SDC) features.

Features

This Spring module provides a set of interfaces to manipulate data in repositories:
  • Repository - This is a marker interface indicating which object are going to access the repository.
  • CrudRepository<T, ID extends Serializable> - This interface extends Repository and provides a set of CRUD (Create, Read, Update, Delete) methods to manipulate T objects having ID as a key.
  • PageAndSortingRepository<T, ID extends Serializable> - This interface extends CrudRepository and provides method to fetch T objects in a sorted way or using pages.
  • Pageable & PageRequest - The Pageable interface is implemented by PageRequest. Page requests are created to indicate which page and page sizes should be used to fetch objects from the PageAndSortingRepository interface.
  • Page<T> - This interface contains the fetched objects returned for a given page request.
The above repository interfaces can expose too many methods for some developers. For example, one may not be comfortable with the idea of exposing write methods. The solution is to create your own customized repository interface and annotate it with:

    @RepositoryDefinition(
        domainClass=MyClass.class,
        idClass=MyClassID.class)

Then, copy the the repository methods you want to expose in this customized interface and Spring will deal with them.

There are situations where developers want to implement their own repository interfaces by extending the repository interfaces available in SDC. However, these interfaces should not be instantiated in their applications. To prevent this, they can use the @NoRepositoryBean on these customized interfaces.

More Spring related posts here.

The Authenticated User Concept In Spring Security

A user, in the Spring Security context, is an instance of a class implementing the UserDetails interface. One can use it to check whether:
  • the user account is expired or locked
  • the user is enabled or not
  • credentials are expired or not
As a reminder, authentication requests are managed by an authentication manager delegating these to authentication providers. The laters can be used to authenticate authentication requests.

By default, Spring configures a DaoAuthenticationProvider instance, and registers it in the default authentication manager. The main purpose of this provider is to let software developers choose the way they want to store UserDetails by setting an object implementing UserDetailsService. Such services have one function: load a user's details from its name. That's it! It can be a database, an in-memory database, etc...

If you want to implement your own UserDetailsService, Marc Serrano has provided a detailed example using a JPA Repository which eliminates a lot of the boiler-plate code. Such repositories are part of the Spring JPA Data features.

To implement a customized user and corresponding persistence, see the example available here.

More Spring related posts here.