Archive For The “Java” Category

Grouping Items with Java 8 Streams

By |

Grouping Items with Java 8 Streams

This small example demonstrates the usage of Collectors.groupingBy to group a list of Persons by their age. The mapped result is a map of list with all Persons of the same age. public class Person { private Integer age; private String name; public Person(Integer age, String name) { this.age = age; this.name = name; }…

Read more »

Freemarker: Transform lists

By |

Freemarker: Transform lists

The following are the key points described in this article: What is Freemarker? How does Freemarker works? How to transform Lists? What is Freemarker? Freemarker is a Java Template Engine, it replaces marker strings by dynamical content. The most common use case is generating html in web applications, but Freemarker is also used to generate…

Read more »

Custom Jackson Annotations

By |

Custom Jackson Annotations

Jackson provides a feature to aggregate specific annotations together into one annotation. This becomes useful if the same annotations are copied over and over again for the same attribute along multiple classes: public class Message { @JsonProperty(“id”) @JsonDeserialize(using=IdTypeDeserializer.class) private String identifier; public String getIdentifier() { return identifier; } } The identifier attribute is mapped to…

Read more »

JavaMail API – Reading emails from googlemail

By |

JavaMail API – Reading emails from googlemail

This example shows you how to use the javax.mail API to read emails from a googlemail account: Note: To run the example change the values for user and password. import javax.mail.*; import java.util.Properties; import java.util.stream.Stream; public class MailReceiver { public static void main(String[] args) throws Exception { String host = “imap.gmail.com”; String user = “your.name@gmail.com”;…

Read more »

Merging pdf documents with java

By |

A Java 8 code snippet to merge to pdf documents into a single file: import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; import java.io.*; public class PdfMerger { public static void main(String[] args) throws Exception { File[] inFiles = new File[] { new File(“1.pdf”), new File(“2.pdf”) }; File outFile = new File(“out.pdf”); Document document = new…

Read more »

Deserializing a piece of json into a string

By |

Deserializing a piece of json into a string

Jackson provides a functionallity with com.fasterxml.jackson.annotation.JsonRawValue  annotation to serialize a raw string to the output json. The opposite way is currently not supported directly, but a simple custom Deserializer can add the required functionallity: public class JsonRawValueDeserializer extends JsonDeserializer<String> { @Override public String deserialize(JsonParser jp, DeserializationContext context) throws IOException { return jp.readValueAsTree().toString(); } } The…

Read more »

Bypassing geoblocking with a few lines of code on AWS

By |

Geoblocking is a really annoying and mainly hated internet technology to block us users from media content. Streaming providers are forced by the content industry to block their services depending from IP address, irrespective of whether the user is a legal customer resident in the same country as the streaming provider. This is really annoying…

Read more »

LocalDateTime and JPA

By |

Today I saw some SQL warnings in our developement server logs: 10:09:23,432 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EE-ManagedExecutorService-default-Thread-10) SQL Warning Code: 1292, SQLState: 22007 10:09:23,432 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EE-ManagedExecutorService-default-Thread-10) Incorrect datetime value: ‘\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B”H\xB2\x0C\x00\x00xpw\x0E \x05\x00\x00\x07\xE0\x04\x04\x0A\x09\x17\x19s\x’ for column ‘valid_time’ at row 1 In our case this warning was caused by the missing support of the new java 8 java.time API in JPA…

Read more »

Capturing images from raspberry pi cam with java

By |

Capturing images from the raspberry pi cam is done by the command line tool raspistill. It’s possible to perform the command by invoking java’s Runtime.exec(..) method: Runtime rt = Runtime.getRuntime(); Process p = rt.exec(“raspistill -n –nopreview -t 250 -w 400 -h 300 -e jpg -o -“); p.waitFor(5000, TimeUnit.MILLISECONDS); InputStream is = p.getInputStream(); byte[] currentImage =…

Read more »

Discover ejb3.x transactions programmatically

By |

Sometimes it’s necessary to optimize the transaction bounds in existing ejb systems. Especially if the system provides long running services or a large composition of service calls. In this case avoiding transactions may improve the service performance. If there are a large number of services implemented by other developers than logging the current transaction status…

Read more »