Feed aggregator

Securing URLs Using Spring Security

Javalobby Syndicated Feed - Fri, 18-Nov-16 22:31

Typically when securing a URL you are looking to do one of the following:

  • Allow access to everyone to a given URL
  • Secure URL based on roles.
  • Secure URL based on multiple roles.
  • Secure URL based on IP Address.

This post shows how to do this using spring security

Categories: Java

A short history in Version Control Systems – RCS, ClearCase, SVN, Git

codecentric Blog - Fri, 18-Nov-16 07:08

Let’s start this post with a question: Have you worked productively with RCS in any software development project? If the answer is “yes”, then you are either old – so am I

Categories: Agile, Java, TDD & BDD

The Internal Cache of Integers

Javalobby Syndicated Feed - Fri, 18-Nov-16 03:31

The Internal Cache of Integers

There is an IntegerCache in java.lang.Integer which stores instances for values -128 though 127. This means that Integer.valueOf(17) always will return the very same instance, while Integer.of(200) will not. While this clearly has the advantage of reuse of commonly used Integer values, thus relieving the GC from some work, it also has implications for autoboxing and identity comparisons.

The Cache

An easy way to see the cache in action is to investigate identity for two small integers compared to numbers outside the scope of the cache. The following two statements both hold true.

Categories: Java

Fixed-Point and Floating-Point: Two Things That Don't Go Well Together

Javalobby Syndicated Feed - Fri, 18-Nov-16 02:01

One of the more challenging aspects of software development can be dealing with floating-point numbers. David Goldberg's 1991 Computing Surveys paper What Every Computer Scientist Should Know About Floating-Point Arithmetic is a recognized classic treatise on this subject. This paper not only provides an in-depth look at how floating-point arithmetic is implemented in most programming languages and computer systems, but also, through its length and detail, provides evidence of the nuances and difficulties of this subject. The nuances of dealing with floating-point numbers in Java and tactics to overcome these challenges are well documented in sources such as JavaWorld's Floating-point Arithmetic, IBM DeveloperWorks's Java's new math, Part 2: Floating-point numbers and Java theory and practice: Where's your point?, Dr. Dobb's Java's Floating-Point (Im)Precision and Fixed, Floating, and Exact Computation with Java's Bigdecimal, Java Glossary's Floating Point, Java Tutorial's Primitive Data Types, and NUM04-J. Do not use floating-point numbers if precise computation is required.

Most of the issues encountered and discussed in Java related to floating-point representation and arithmetic are caused by the inability to precisely represent (usually) decimal (base ten) floating point numbers with an underlying binary (base two) representation. In this post, I focus on similar consequences that can result from mixing fixed-point numbers (as stored in a database) with floating-point numbers (as represented in Java).

Categories: Java

Three Methods to Automatically Validate PDF Data

Javalobby Syndicated Feed - Fri, 18-Nov-16 01:01

An insurance customer delivery team wanted to (as part of regression testing) automate the validation of data present in PDF documents. After going through the requirements, we explored multiple options and suggested three solutions, each with its own set of unique features. Two of the options involve a two-step process, where the first step converts the PDF document into a text document, while text is extracted in the second step. In this article, we elaborate on the problem and also share an overview of each option.

Introduction

Recently, we were in a discussion with a project delivery team that was dealing with PDF documents. The delivery team works for an insurance customer, where one of their activities is to generate customer policies as PDF documents. As a standard process, the PDF documents generated are verified for content and structure and then sent to the customer. After each functionality change, the team needs to perform a regression test using various data sets and multiple templates. Today, the team has to go through each generated PDF document and validate information like name, address, policy number, policy start date, and the like, manually. As the number of tests is expected to grow along with the number of PDF templates, the team wanted a solution that would reduce the manual efforts involved and work across a large volume of documents.

Categories: Java

Tracing in Microservices With Spring Cloud Sleuth

Javalobby Syndicated Feed - Thu, 17-Nov-16 23:31

One of the problems developers encounter as their microservice apps grow is tracing requests that propagate from one microservice to the next. It can quite daunting to try and figure out how a requests travels through the app, especially when you may not have any insight into the implementation of the microservice you are calling.

Spring Cloud Sleuth is meant to help with this exact problem. It introduces unique IDs to your logging which are consistent between microservice calls which makes it possible to find how a single request travels from one microservice to the next.

Categories: Java

The Beauty of Quasi-Enumerations

Javalobby Syndicated Feed - Thu, 17-Nov-16 14:37

Everybody knows at least some HTTP Status Codes, even if they don’t realize it. Most people did at one point or another in their web browsing career see the famous 404 – Not Found, or 200 – OK codes.

How could these codes be represented in Java code, however, when writing HTTP client or server code?

Categories: Java

Small Scale Stream Processing Kata (Part 1): Thread Pools

Javalobby Syndicated Feed - Thu, 17-Nov-16 03:41

Once again I prepared a programming contest on GeeCON 2016 for my company. This time the assignment required designing and optionally implementing a system given the following requirements:

A system delivers around one thousand events per second. Each event has at least two attributes:

Categories: Java

Supporting Your Unsupported Class Version Error [Code Snippet]

Javalobby Syndicated Feed - Thu, 17-Nov-16 02:01

This article covers how to fix Java errors akin to UnsupportedClassVersionError. Please feel free to comment/suggest if I missed something.

How to Reproduce the Error

  • Set JAVA_HOME with one version of Java. Include a “bin” of different Java versions in the PATH variable.
  • Compile the class, for example HelloWorld.java, with “javac”.
  • Start the JVM with following class: “java HelloWorld”.

With the steps above, you might see an error like:

Categories: Java

Gradle Goodness: Validate Model In Rule Based Model Configuration

Javalobby Syndicated Feed - Thu, 17-Nov-16 01:01

Rule based model configuration gives Gradle more knowledge about the objects and their dependencies. This information can be used by Gradle to optimise the build process. We define rules on how we want Gradle to create objects and how we want to mutate objects in a class that extends RuleSource. We can also add rules to validate objects available in the Gradle model space. We use the @Validate annotation on methods that have validation logic. The first argument of the method is of the type of the object we want to validate. This type must be managed by Gradle.

In the following example we use the sample from a previous post. In this sample, we have a VersionFile class that is managed by Gradle. The class has a version and outputFile property. The version must be set and must start with a v. The outputFile property is also required.

Categories: Java

This Week in Spring: SpringOne Talks, REST APIs, and Spring 5's Approach

Javalobby Syndicated Feed - Wed, 16-Nov-16 23:31

Welcome to another installment of This Week in Spring! Time sure is flying! We’ve got so much to get into this week. Can you believe we’re staring down 2017? We’ve got so much to cover so let’s get to it!

Categories: Java

Getting Started with Java and Couchbase [Video]

Javalobby Syndicated Feed - Wed, 16-Nov-16 22:31

In this quickstart video tutorial, you'll learn how to set up and start building a Java application that communicates with Couchbase Server to save NoSQL JSON data as well as query for it numerous ways.

For more information on how to use the Couchbase Java SDK, visit the Couchbase developer portal. For help getting Couchbase Server set up on your machine, check out the Ubuntu, Windows, Mac, or Docker tutorials.

Categories: Java

Define-Datatype and Cases in Clojure

Javalobby Syndicated Feed - Wed, 16-Nov-16 03:31

I'm going through the Essentials of Programming Languages (3rd ed.) book and it's been pretty good so far. In chapter 2, the authors use a pair of macros — define-datatype and cases — to make it easy to define data-driven programs, where objects belong to types, each of which has several "variants" with custom fields.

The canonical example used chapter 2 is the "Lambda calculus expression":

Categories: Java

Learn Drools: Part III (Filter Facts)

Javalobby Syndicated Feed - Wed, 16-Nov-16 02:01

In the previous article, we saw how Drools creates a Cartesian product between Facts if there is a HAS-A relationship between Domain Object/Facts. In this article we will learn how to filter the facts. You can think it as of using Where clause in SQL to filter the resultset.

Let's understand it with the same problem we have used in the previous article, where we created the following Drools Rule.

Categories: Java

Gradle Goodness: Set Default Values with Rule-Based Model Configuration

Javalobby Syndicated Feed - Wed, 16-Nov-16 01:01

When we use rule-based model configuration in our Gradle project, we can give Gradle rules on how to manage objects from the model space. These rules are defined in a class that extends RuleSource. When we want to set some default values for properties of a model object (in Gradle terms this is a subject) we can use the @Defaults annotation. Rules annotated with @Defaults are invoked right after the object is created and before any other methods that can mutate the state of the object.

The method, to set the default values, must have the type of the object as the first parameter. Other parameters are considered input parameters and can be used to set a default value based on other model objects.

Categories: Java

Continuous Delivery Patterns: Building your application inside a Docker container

codecentric Blog - Wed, 16-Nov-16 00:43

Let me be clear: This post is not about building a Docker container for your application – it is about building your application inside a container specially designed for exactly doing that – building your application – and nothing else. It helped us a lot to deal with different environments, technologies, versions etc. without polluting our continuous delivery infrastructure.

Where we came from

Our continuous delivery pipeline was perfect, everything’s automated! We generated our jobs with the Jenkins Job DSL Plugin upon creation of a new repository, nothing to do manually inside Jenkins. All jobs looked the same and were 100% reproducible within seconds. What more do you want?

What bothered us

In the beginning we had one type of job – Maven 3 with JDK 7, every project was built that way. But soon things started to fragment – there was that JavaScript web project that needed to be built with NPM, there was that legacy project built with ant that needed to be integrated into the pipeline as well, and soon projects started to upgrade to Java 8.

What we did to solve it

We started out with tagging build types to the repositories and having a lot of different Job DSL definitions for the different build types. It was okay. Still, everything was maintained by the continuous delivery team, and we wanted to do more devops – more power to the people! Technology choices – and therefore build tool choices – became more decentralized. Everybody building up their own continuous delivery platform was an option, but we didn’t like it. It might be not as hard as it was, but you still need knowledge and time for doing that.
So – one platform to rule them all. But how?
We decided that every project manages on its own how exactly it is built by providing a Dockerfile for it. Its name has to be Dockerfile-build to distinguish it from a Dockerfile meant for running the application. Now we have the same job definition for everybody – again. And it goes like this, triggered by a commit:

  1. Clone the repository
  2. Take the Dockerfile-build and build a docker image for it
  3. Take the image and run it, thereby mounting the Jenkins workspace into the container
  4. Take the Dockerfile (meant for running the application) and build it, copying the built artifacts from step 3 into the image
  5. Push the image to a docker repository

Here is an example for a Dockerfile-build for an Angular app:

FROM node:6.8.0

RUN npm install -g angular-cli

CMD npm install && ng build

And the according Dockerfile for running the application:

FROM nginx

COPY dist /usr/share/nginx/html

Why we like it

Since all the build specific technologies are hidden in the docker image we have very little maintenance costs for the Jenkins itself. No installing and upgrading of Maven, NPM, ant, Java, etc. No different Job DSL definitions for different build types. You can check out any old version, and you can still build it, even if it’s built with far outdated build tools. There are pre-built docker images for every build tool you need.

Caveats

Nearly all the build tool docker images run per default as root, so for avoiding file permission problems we let Jenkins run in a docker container as well, under the user root, and mount both the Jenkins and the build container to a shared volume. Since both are root there’s no problem.
You cannot do that if Jenkins is running directly on the host. One option would be then to make the build images run under the Jenkins user by changing file permissions in the image.

The post Continuous Delivery Patterns: Building your application inside a Docker container appeared first on codecentric AG Blog.

Categories: Agile, Java, TDD & BDD

Pippo and Jersey (JAX-RS): A Match Made in Heaven

Javalobby Syndicated Feed - Tue, 15-Nov-16 23:31

For the last few, days I have been playing with Pippo and Jersey (JAX-RS) and I wanted to share my experience with you. The result (a very good one) and the details of my experiment are materialized in this article — along with a demo application.

To begin with, I would like to say a few words about the main actors:

Categories: Java

Toward More Functional Java: Digging Into Nested Data Structures

Javalobby Syndicated Feed - Tue, 15-Nov-16 22:31

In the last post, we saw an example that used a generator combined with a filter to find the first available port in a specific range. It returned an Optional to model the case when no open ports are found, as opposed to returning null. In this example, we'll look at how to use Java 8 streams to dig into a nested data structure and find objects of a specific type. We'll use map and filter operations on the stream, and also introduce a new concept, the flat-map.

In the original, pre-Java 8 code that I was working on in a project, the data structure was a three-level nested structure that was marshaled into Java objects from an XML file based on a schema from an external web service. The method needed to find objects of a specific type at the bottom level. For this article, to keep things simple we will work with a simple class structure in which class A contains a collection of class B, and B contains a collection of class C. The C class is a base class, and there are several subclasses C1, C2, and C3. In pseudo-code the classes look like:

Categories: Java

Gradle Goodness: Adding Tasks With Rule Based Model Configuration

Javalobby Syndicated Feed - Tue, 15-Nov-16 03:31

Gradle has an incubating feature Rule based model configuration. This is a new way to configure Gradle projects where Gradle has more control of the configuration and the dependencies between configuration objects. This allows Gradle to resolve configuration values before they are used, because Gradle knows there is a dependency. With this new model, we don't need any lazy evaluation "tricks" we had to use. For example, there was an internal convention mapping mechanism for tasks to assign values to a task configuration after the task was already created. Also, the project.afterEvalute is a mechanism to have late binding for task properties. With the new rule based model, Gradle can do without these options, we can rely on Gradle resolving all dependent configuration values when we create a task.

In Gradle, we already know about the "project space" where the Project object is the root of the object graph. For example repositories are part of the project space. Gradle can get some useful information from the project space, but it is mostly a graph of objects that Gradle only partially can reason about. Then we have the "model space". This is part of a project and we can use it in our build script with the model configuration block. The model space is separate from the project space and contains objects that are managed by Gradle. Gradle knows dependencies between the objects and how to create and change them. This helps Gradle to optimize build logic. To help Gradle we must define rules to work with objects in the model space. Each rule is like a recipe for Gradle on how to work with the model. Gradle can build a graph of models and know about dependencies between models. This way Gradle guarantees that model objects are completely configured before being used. For example, if a rule needs a VersionFile model configuration object then Gradle makes sure that the VersionFile is created and all properties are set. So we don't need any lazy or late binding anymore, because the properties will be set (Gradle makes sure) when we want to use them. The rules are defined by a class that extends RuleSource. Such a class is stateless and only contains methods to work with the model objects. Gradle has some specific annotations that can be used on methods to indicate what a method should do.

Categories: Java

The Way of the Lambda and removeIf()

Javalobby Syndicated Feed - Tue, 15-Nov-16 02:01

The Way of the Lambda: Elegant and Efficient

In Java 8, you can do:

items.removeIf(i -> predicate(i));

Where you used to do:

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content