RTSP/RTP Torrent

news Is a small and fast RTSP/RTP client Java library used to get streaming data from IP cameras or streaming servers. RTSP client is RFC 2326 compliant and RTP client is RFC 3550 compliant.

1
2
3
4
5
6
    RtpVideoPlayer videoPlayer = new RtpVideoPlayer("0.0.0.0", 9000);
    RtpAudioPlayer audioPlayer = new RtpAudioPlayer("0.0.0.0", 9002);
    RtspSession session = new RtspSession(videoPlayer, audioPlayer);
    session.setUsername("username");
    session.setPassword("password");
    session.play("rtsp://hostname:554/stream");
Available on GitHub...

POJO-SP

news If you have to call stored procedures on your Java applications you have to use the standard JDBC API working with CallableStatement objects. But now you have a more simple and efficient solution using annotated POJO classes and a ProcedureManager instance. Designed from scratch the pojo-sp library offers the best object oriented solution to use stored procedures on your applications by hiding the JDBC SQL programming artefacts.

Available on GitHub...

Class method call protection.

Sometime is useful to protect a class method in such a way that could be called only from a list of trusted locations. The solution is to inspect the stack trace for allowed callers whenever the protected method is called. Below is a snippet of code which check the stack trace for allowed callers list provided as a parameter.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Protector {

    /**
     * Check if the caller of a method is on the list of allowed callers. The
     * allowed callers MUST be among the stack trace elements.
     * 
     * @param callers
     *            A list of callers which are allowed to call the method.
     */
    public static void checkCaller(String[] callers) {
        // get stack trace
        StackTraceElement[] elements = new Throwable().getStackTrace();
        for (StackTraceElement element : elements) {
            // caller
            String caller = element.getClassName() + "." + element.getMethodName();
            // check allowed callers
            for (String item : callers) {
                if (caller.equals(item)) {
                    return;
                }
            }
        }
        // if allowed callers aren't in the stack trace throw exception
        throw new RuntimeException("Caller not allowed.");
    }
}

Below is an example of how to use the Protector.checkCaller(String[]) helper in order to protect a class method allowing only the callers from a list to call the function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package ws.gmax.protect;

public class Test {

    public void test() {
        Protector.checkCaller(new String[] { "ws.gmax.protect.Test.main" });
        System.out.println("OK.");
    }

    public static void main(String[] args) {
        new Test().test();
    }
}

Using JMS on JBoss AS 7.1.1 remotely

news This is a short paper which show you how to use the JMS on JBoss AS 7.1.1 remotely. For more details please download the example.

Download...

JBoss RESTEasy 3.0.4 Client

news JAX-RS 2.0 introduces a new client API so that you can make http requests to your remote RESTful web services. RESTEasy 3.0.4 provide also a new client API. However the userguide does not contains any example on how to access a resource using @POST method and @FormParam parameters. Bellow is a possible solution related to this topic. Another example show you how to access a resource using @GET method and @QueryParam parameters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Server side code */
@GZIP
@Path("/rest")
public class Service extends SpringBeanAutowiringSupport {

    @Autowired
    UserServices userServices;

    @POST
    @Path("/isbn2ean")
    @Produces(MediaType.TEXT_PLAIN)
    public Response isbn2ean(@FormParam("isbn") String isbn) {
        String ean = Isbn.isbnToEan(isbn);
        return Response.ok(ean).build();
    }
    
    @GET
    @Path("/address")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAddress(@QueryParam("id") int id) {
        return Response.ok(userServices.getContactAddress(id)).build();
    }
    
}

/* Client side code */
public class TestRestService {

    String uri = "http://localhost:8080/services/rest";
    ResteasyClient client = new ResteasyClientBuilder().build();

    public String isbn2ean(String isbn) throws Exception {
            
        // set target URI
        ResteasyWebTarget target = client.target(uri).path("isbn2ean");

        // set form parameters
        Form form = new Form("isbn", isbn);
        Entity<Form> entity = Entity.form(form);
        
        // do a post request
        Response response = target.request().post(entity);
        String value = response.readEntity(String.class);
        int status = response.getStatus();
        response.close();  // You should close connections!

        // test response status
        if (status != HttpResponseCodes.SC_OK) {
            throw new RuntimeException("Failed with HTTP error code: " + 
                status); 
        }
        
        return value;
    }

    public String getAddress(int id) throws Exception {
    
        // set target URI
        ResteasyWebTarget target = client.target(uri).path("address")
            .queryParam("id", id);
                
        // do a get request
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        int status = response.getStatus();
        response.close();  // You should close connections!

        // test response status
        if (status != HttpResponseCodes.SC_OK) {
            throw new RuntimeException("Failed with HTTP error code: " + 
                status); 
        }
        
        return value;
    }
}

Functional Programming Design Pattern in Java

news An example of how to implement Functional Programming in your Java applications.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Functional Programming Design Pattern in Java 5, 6 or 7
 * 
 * @author Marius Gligor
 */
public class FunctionalProgramming {

    /**
     * Define the generic function signature
     *
     * @param <T> type
     */
    public interface Foo<T> {
        // foo
        T foo(T p1, T p2);
    }

    /**
     * Iterate over a population using a function parameter which take 
     * two parameters, an initial value and the current item value.
     *
     * @param <T> type
     * @param initialValue initial value
     * @param population a collection containing items of type T
     * @param f function parameter
     * @return a value of type T the result of operations 
     *         implemented by the function parameter
     */
    public <T> T fold(T initialValue, Collection<T> population, Foo<T> f) {
        T result = initialValue;
        for (T item : population) {
            result = f.foo(result, item);
        }
        return result;
    }

    /**
     * Test functional programming design pattern
     *
     * @param population a list of integers
     */
    private void test(List<Integer> population) {        
        // sum
        Integer sum = fold(0, population, new Foo<Integer>() {
            @Override
            public Integer foo(Integer p1, Integer p2) {
                return p1 + p2;
            }
        });
        System.out.println("Sum = " + sum);
        
        // product
        Integer prod = fold(1, population, new Foo<Integer>() {
            @Override
            public Integer foo(Integer p1, Integer p2) {
                return p1 * p2;
            }
        });
        System.out.println("Product = " + prod);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Integer[] population = {1, 6, 15, 45, 23, 39};
        new FunctionalProgramming().test(Arrays.asList(population));
    }
}

news The same example using Java 8 lambda expressions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Functional Programming Design Pattern in Java 8
 * 
 * @author Marius Gligor
 */
public class FunctionalProgramming {

    /**
     * Define the generic function signature
     *
     * @param <T> type
     */
    @FunctionalInterface 
    public interface Foo<T> {
        // foo
        T foo(T p1, T p2);
    }

    /**
     * Iterate over a population using a function parameter which take 
     * two parameters, an initial value and the current item value.
     *
     * @param <T> type
     * @param initialValue initial value
     * @param population a collection containing items of type T
     * @param f function parameter
     * @return a value of type T the result of operations 
     *         implemented by the function parameter
     */
    public <T> T fold(T initialValue, Collection<T> population, Foo<T> f) {
        T result = initialValue;
        for (T item : population) {
            result = f.foo(result, item);
        }
        return result;
    }

    /**
     * Test functional programming design pattern
     *
     * @param population a list of integers
     */
    private void test(List<Integer> population) {        
        // sum
        Integer sum = fold(0, population, (x, y) -> x + y);
	System.out.println("Sum = " + sum);
        
        // product
        Integer prod = fold(1, population, (x, y) -> x * y);       
	System.out.println("Product = " + prod);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Integer[] population = {1, 6, 15, 45, 23, 39};
        new FunctionalProgramming().test(Arrays.asList(population));
    }
}

Useful generic functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * Compare two objects to be equal
 *
 * @param m first object
 * @param o second object
 * @return true if objects are equals false otherwise
 *
 */
public static <T> boolean equals(T m, T o) {
    return (m != null) ? m.equals(o) : (o != null) ? o.equals(m) : true;
}

/**
 * Implements ORACLE NVL function
 *
 * @param obj1 First argument tested for null value
 * @param obj2 Second argument returned if the first argument is null
 * @return obj1 if obj1 is not null or obj2 if obj1 is null
 *
 */
public static <T> T nvl(T obj1, T obj2) {
    return (obj1 == null) ? obj2 : obj1;
}

/**
 * Implements ORACLE IN function
 *
 * @param obj Object to search on list
 * @param list List of objects to search for
 * @return true if object is in the list false otherwise
 */
public static <T> boolean in(T obj, T... list) {
    if (obj != null && list != null) {
        for (T t : list) {
            if (obj.equals(t)) {
                return true;
            }
        }
    }
    return false;
} 

Blocks

It's the next generation of Database Objects, a collection of Java SWING custom components allowing you to design professional database desktop GUI applications in a minute.

blocks
Download...

J2ME Motion JPEG Player

This Motion JPEG Player has been designed to play Motion JPEG streams from IP cameras on PDAs and mobile phones J2ME enabled. It is small, compact and fast. The player is developed under the Sun Java(TM) Wireless Toolkit 2.5.2 for CLDC framework. Requirements: MIDP 2.0, CLDC 1.0 It support HTTP Basic and Digest access authentication RFC2617 compliant.

Mobile Player
Download...


Flag Counter