<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>java on Moritz Halbritter's personal blog</title><link>https://www.mkammerer.de/tags/java/</link><description>Recent content in java on Moritz Halbritter's personal blog</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Tue, 25 Aug 2020 12:57:04 +0200</lastBuildDate><atom:link href="https://www.mkammerer.de/tags/java/index.xml" rel="self" type="application/rss+xml"/><item><title>RSA on the JVM</title><link>https://www.mkammerer.de/blog/rsa-on-the-jvm/</link><pubDate>Tue, 25 Aug 2020 12:57:04 +0200</pubDate><guid>https://www.mkammerer.de/blog/rsa-on-the-jvm/</guid><description>This post shows how to work with RSA on the JVM with the Java language. We&amp;rsquo;re gonna:
Generate two keypairs (one for alice, one for bob) Save these keypairs to a file and load them again Send a message from alice to bob using a hybrid encryption scheme with confidentiality and authenticity Disclaimer: This post is intended to show the concepts underpinning a hybrid encryption scheme. This is not production ready code!</description></item><item><title>Routes with dynamic parameters in Micronaut</title><link>https://www.mkammerer.de/blog/routes-with-dynamic-parameters-in-micronaut/</link><pubDate>Fri, 01 May 2020 09:51:43 +0200</pubDate><guid>https://www.mkammerer.de/blog/routes-with-dynamic-parameters-in-micronaut/</guid><description>If you want to create a REST controller in Micronaut, which accepts a dynamic amount of parameters, e.g.
curl http://localhost:8080/?foo=1&amp;amp;bar=2&amp;amp;baz=3 where the stuff after the ? is varying, here&amp;rsquo;s the way to go:
@Controller class FooController { @Get(&amp;#34;/{?values*}&amp;#34;) public void takeAll(@Nullable @QueryValue(&amp;#34;values&amp;#34;) Map&amp;lt;String, String&amp;gt; values) { System.out.println(&amp;#34;Received: &amp;#34; + values); } } Micronaut will map the query parameters to the values map. When running the curl from above, the map contains [foo: 1, bar: 2, baz: 3].</description></item><item><title>AES-CTR encryption with Java</title><link>https://www.mkammerer.de/blog/aes-ctr-encryption-with-java/</link><pubDate>Wed, 22 Apr 2020 17:27:58 +0200</pubDate><guid>https://www.mkammerer.de/blog/aes-ctr-encryption-with-java/</guid><description>And another post about my pet peeve &amp;ldquo;crypto stuff&amp;rdquo;. This time I want to encrypt something with the AES cipher in CTR mode.
The advantages of CTR are, quote wikipedia:
CTR mode &amp;hellip; also allows a random access property during decryption. CTR mode is well suited to operate on a multi-processor machine where blocks can be encrypted in parallel
But keep in mind that CTR isn&amp;rsquo;t an AEAD mode, meaning you have to secure the ciphertext with additional tamper proofing, e.</description></item><item><title>Fun with Alexa skills and TLS</title><link>https://www.mkammerer.de/blog/fun-with-alexa-skills-and-tls/</link><pubDate>Wed, 11 Mar 2020 17:38:49 +0100</pubDate><guid>https://www.mkammerer.de/blog/fun-with-alexa-skills-and-tls/</guid><description>I recently updated my Alexa skill, the &amp;ldquo;Mächtiger Aluhut&amp;rdquo; (mighty tinfoil hat), which provides you with your daily dose of conspiracy theories. The technical base for the skill is an embedded Jetty server, using the Alexa SDK with servlets.
I developed the skill (source code is here) and deployed it on my server. Then I opened up the Alexa ASK console (where you can test your skill) and tried to invoke it.</description></item><item><title>Jackson's Polymorphic Deserialization</title><link>https://www.mkammerer.de/blog/jacksons-polymorphic-deserialization/</link><pubDate>Sat, 21 Dec 2019 17:57:54 +0100</pubDate><guid>https://www.mkammerer.de/blog/jacksons-polymorphic-deserialization/</guid><description>Say you have this class hierarchy:
abstract class Animal { String name; } class Cat extends Animal { boolean canMeow; } class Dog extends Animal { boolean canBark; } and you have a class with a List&amp;lt;Animal&amp;gt;, which can contain Dog and Cat instances at runtime:
class Zoo { List&amp;lt;Animal&amp;gt; animals; } and you want to serialize it as JSON with Jackson:
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); Zoo animals = new Zoo(List.</description></item><item><title>Async logging with Log4j2</title><link>https://www.mkammerer.de/blog/async-logging-with-log4j2/</link><pubDate>Mon, 28 Oct 2019 17:04:03 +0100</pubDate><guid>https://www.mkammerer.de/blog/async-logging-with-log4j2/</guid><description>Finally I got some time to play around with Log4j2 and the async logging support. Async logging promises high throughput and minimal logging latency, the numbers on this page are impressive.
So, how to get async logging running?
First, add the LMAX disruptor library on your classpath:
&amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;com.lmax&amp;lt;/groupId&amp;gt; &amp;lt;artifactId&amp;gt;disruptor&amp;lt;/artifactId&amp;gt; &amp;lt;version&amp;gt;3.4.2&amp;lt;/version&amp;gt; &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt; &amp;lt;/dependency&amp;gt; Now you have to switch all the loggers to async by setting a JVM property:
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector and that&amp;rsquo;s it.</description></item><item><title>Customize Micronaut JSON serialization</title><link>https://www.mkammerer.de/blog/customize-micronaut-json-serialization/</link><pubDate>Fri, 04 Oct 2019 12:58:50 +0200</pubDate><guid>https://www.mkammerer.de/blog/customize-micronaut-json-serialization/</guid><description>or: why won&amp;rsquo;t Micronaut serialize my empty lists?
Micronaut omits fields which are null or empty in its JSON output.
For example, this code
class Dto { public List&amp;lt;String&amp;gt; foo; public Optional&amp;lt;String&amp;gt; bar; public String baz; } @Controller class HelloController { @Get public Dto test() { Dto result = new Dto(); result.foo = Collections.emptyList(); result.bar = Optional.empty(); result.baz = null; return result; } } results in this JSON:
{} Micronaut not also omits null fields and empty Optional, but also empty collections.</description></item><item><title>Disable Spring Caching per request</title><link>https://www.mkammerer.de/blog/disable-spring-caching-per-request/</link><pubDate>Tue, 18 Sep 2018 19:26:45 +0200</pubDate><guid>https://www.mkammerer.de/blog/disable-spring-caching-per-request/</guid><description>Another round of Spring problems! This time, I have the need to disable Spring Caching per request.
The methods which should be cached looks like this:
@Cacheable(&amp;#34;name-of-the-cache&amp;#34;) public String doSomething(String input) { // Costly operation here } I don&amp;rsquo;t want to write a second version of these methods without the @Cacheable annotation, but instead introduce some Spring black magic to disable the cache per request.
First, we need a @RequestScopedBean:</description></item><item><title>Visualizing the dependencies between Maven modules</title><link>https://www.mkammerer.de/blog/visualizing-the-dependencies-between-maven-modules/</link><pubDate>Sat, 31 Mar 2018 15:57:44 +0200</pubDate><guid>https://www.mkammerer.de/blog/visualizing-the-dependencies-between-maven-modules/</guid><description>I was looking for (and found) a way to visualize the dependencies between the Maven modules in a multi-module Maven project. In other words: draw a graph with the modules as nodes and the dependencies between them as edges.
I found this plugin. By default it also shows the 3rd party dependencies, so I had to tweak the configuration a little bit.
Running
mvn com.github.ferstl:depgraph-maven-plugin:3.0.1:aggregate -DcreateImage=true -DreduceEdges=false -Dscope=compile &amp;quot;-Dincludes=your.group.id*:*&amp;quot; will create the file dependency-graph.</description></item><item><title>Spring Boot Locale Resolver</title><link>https://www.mkammerer.de/blog/spring-boot-locale-resolver/</link><pubDate>Wed, 20 Dec 2017 09:33:29 +0100</pubDate><guid>https://www.mkammerer.de/blog/spring-boot-locale-resolver/</guid><description>I had to implement locale awareness in a Spring Boot application. The client would send its preferred languages in the Accept-Language header and the application should respect that.
You can inject a Locale in your Spring MVC controller, which then should contain the locale the client has chosen.
To get Spring Boot to inject the locale from the Accept-Language header in the Locale controller parameter, install the following bean:
// Do not rename that bean, otherwise Spring MVC won&amp;#39;t pick it up!</description></item><item><title>JavaFX on Fedora 27</title><link>https://www.mkammerer.de/blog/javafx-on-fedora-27/</link><pubDate>Fri, 17 Nov 2017 15:10:35 +0100</pubDate><guid>https://www.mkammerer.de/blog/javafx-on-fedora-27/</guid><description>The standard java-1.8.0-openjdk package on Fedora doesn&amp;rsquo;t include JavaFX (which is needed for some GUI applications). You can get it on Fedora 27 by installing the java-1.8.0-openjdk-openjfx package.</description></item><item><title>Spring Cloud and Wiremock</title><link>https://www.mkammerer.de/blog/spring-cloud-and-wiremock/</link><pubDate>Fri, 23 Dec 2016 13:16:54 +0100</pubDate><guid>https://www.mkammerer.de/blog/spring-cloud-and-wiremock/</guid><description>I tried to write a test using Wiremock 2.4.1 in a Spring Boot 1.4.3 with Spring Cloud Camden.SR3 project.
When using the WireMockRule you get exceptions like:
java.lang.NoClassDefFoundError: org/apache/http/HttpRequest at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:124) at com.github.tomakehurst.wiremock.WireMockServer.&amp;lt;init&amp;gt;(WireMockServer.java:71) at com.github.tomakehurst.wiremock.junit.WireMockRule.&amp;lt;init&amp;gt;(WireMockRule.java:42) at com.github.tomakehurst.wiremock.junit.WireMockRule.&amp;lt;init&amp;gt;(WireMockRule.java:38) at com.github.tomakehurst.wiremock.junit.WireMockRule.&amp;lt;init&amp;gt;(WireMockRule.java:47) at com.example.Foo.&amp;lt;init&amp;gt;(Foo.java:8) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.</description></item><item><title>JNA and Unsigned Integers</title><link>https://www.mkammerer.de/blog/jna-and-unsigned-integers/</link><pubDate>Sat, 09 Jan 2016 21:16:08 +0100</pubDate><guid>https://www.mkammerer.de/blog/jna-and-unsigned-integers/</guid><description>I&amp;rsquo;ve written an Argon2 binding for the JVM (in case you&amp;rsquo;re interested, you can find it here). This binding uses JNA to call the Argon2 C library. The C library uses the uint32_t datatype, that&amp;rsquo;s an unsigned 32-bit integer. The JNA documentation states:
Unsigned values may be passed by assigning the corresponding two&amp;rsquo;s-complement representation to the signed type of the same size.
Haha, you expect me to bitshift my integers around to get rid of the two&amp;rsquo;s-complement representation?</description></item><item><title>A Java Keyczar Tutorial</title><link>https://www.mkammerer.de/blog/a-keyczar-tutorial/</link><pubDate>Fri, 06 Nov 2015 21:23:50 +0100</pubDate><guid>https://www.mkammerer.de/blog/a-keyczar-tutorial/</guid><description>In this post I&amp;rsquo;ll show you how to use Keyczar and Java to exchange messages between two parties in a secure way. Keyczar is a great library, but it lacks some documentation.
In this tutorial we use a SignedSession, that means: the sender and the receiver each has a key pair consisting of private and public &amp;ldquo;crypt&amp;rdquo; keys and a key pair of private and public &amp;ldquo;sign&amp;rdquo; keys. The crypt keys are used to encrypt/decrypt the message, the sign keys are used to sign/verify the message.</description></item><item><title>Hibernate, Java 8 and Dates</title><link>https://www.mkammerer.de/blog/hibernate-java-8-and-dates/</link><pubDate>Sun, 01 Nov 2015 12:07:29 +0100</pubDate><guid>https://www.mkammerer.de/blog/hibernate-java-8-and-dates/</guid><description>Java 8 introduced a new date and time framework (thank god, the old one is a piece of crap). But unfortunately, Hibernate doesn&amp;rsquo;t support those types not out of the box. The Jadira usertypes project adds support for those to hibernate.
Here&amp;rsquo;s what you have to do when using Hibernate and the Java 8 date types:
Include the correct Jadira type You have to include the extended Jadira version, not the core one.</description></item><item><title>Main-Class Manifest Entry With Gradle</title><link>https://www.mkammerer.de/blog/main-class-manifest-entry-with-gradle/</link><pubDate>Wed, 07 Oct 2015 19:15:30 +0200</pubDate><guid>https://www.mkammerer.de/blog/main-class-manifest-entry-with-gradle/</guid><description>Use Gradle? Want to add the Main-Class attribute to your manifest in a JAR file? Here you go:
mainClassName = &amp;#39;foo.bar.Program&amp;#39; jar { manifest { attributes &amp;#39;Main-Class&amp;#39;: mainClassName } }</description></item><item><title>Using Google Keyczar With Gradle</title><link>https://www.mkammerer.de/blog/using-google-keyczar-with-gradle/</link><pubDate>Thu, 04 Jun 2015 20:11:18 +0200</pubDate><guid>https://www.mkammerer.de/blog/using-google-keyczar-with-gradle/</guid><description>Google Keyczar is a framework for cryptography in Java and other languages. Unfortunately they don&amp;rsquo;t upload their artefacts to Maven Central and don&amp;rsquo;t provide documentation on how to add Keyczar to your Maven or Gradle project.
To add Keyczar to Gradle, add the following in your build.gradle:
repositories { // ... maven { url &amp;#39;https://raw.githubusercontent.com/google/keyczar/master/java/maven/&amp;#39; } } dependencies { compile &amp;#39;org.keyczar:keyczar:0.66&amp;#39; } This adds the Keyczar Maven repository, which is hosted on GitHub and adds a dependency on Keyczar 0.</description></item><item><title>Downloading SceneBuilder</title><link>https://www.mkammerer.de/blog/downloading-scenebuilder/</link><pubDate>Sun, 12 Apr 2015 11:39:39 +0200</pubDate><guid>https://www.mkammerer.de/blog/downloading-scenebuilder/</guid><description>I&amp;rsquo;ve tried to get the newest version of SceneBuilder to create FXML files for a JavaFX project. But Oracle has decided that precompiled binaries are a relict of the past and no longer provides up-to-date binaries. Thank you very much. You can still download the older versions here.
I searched the internet a little bit, the source code for the SceneBuilder is here. I&amp;rsquo;m not motivated to build this from source.</description></item><item><title>Dropwizard 0.80 breaks your transaction handling</title><link>https://www.mkammerer.de/blog/dropwizard-0-80-breaks-your-transaction-handling/</link><pubDate>Sat, 07 Mar 2015 11:27:01 +0100</pubDate><guid>https://www.mkammerer.de/blog/dropwizard-0-80-breaks-your-transaction-handling/</guid><description>Dropwizard 0.8 has shipped. Unfortunately with broken transactions when using the hibernate module. I found this bug while migrating RESTwars to Dropwizard 0.80.
The behaviour should be:
Start a transaction before a request is handled Commit the transaction if the request has been successfully handled Abort the transaction if the request failed with an exception What happens instead is:
Start a transaction before a request is handled Commit the transaction after the request has been handled, even if an exception has been thrown This could have dire consequences, as your transaction will be committed even when an exception is thrown.</description></item><item><title>Dropwizard and Atmosphere</title><link>https://www.mkammerer.de/blog/dropwizard-and-atmosphere/</link><pubDate>Fri, 20 Feb 2015 16:51:14 +0100</pubDate><guid>https://www.mkammerer.de/blog/dropwizard-and-atmosphere/</guid><description>In this blog post I&amp;rsquo;m going to write about combining Dropwizard and Atmosphere. Atmosphere is a framework for handling websockets. If you take a look at the samples, it seems like the integration is a no-brainer. I thought so too. Fool.
Atmosphere provides a Jersey integration, Dropwizard also uses Jersey, so everything will be alright. Hah.
First, add org.atmosphere:atmosphere-jersey and org.eclipse.jetty.websocket:websocket-server:9.0.7.v20131107 (use the same Jetty version as in Dropwizard) to your dependencies.</description></item></channel></rss>