In object-oriented languages, like Java, this refers to the instance of the class where you run the method.
In javascript this is often also the case, but not always.
In this post we'll explore some situations.
And I give some tips on how to deal with them. The normal case. The function eat is defined on carrot.
And we simply run it. this will refer to the enclosing object, which is carrot, with name "carrot".
var carrot = {
name: "carrot",
eat: function () {
console.log(this);
console.log("eating " + this.name);
}
};
carrot.eat(); //result: eating carrot
Continue reading →
Securing an application is difficult.
Securing an entire application landscape is even more difficult! In this modern era of blazing fast microservices we do not want the additional complexity of having to secure it all manually.
This is where Spring Cloud Security comes in.
By combining proven technologies, it helps us achieve performant, configurable end-to-end security across multiple applications.
So what technologies are being combined? Well, a lot...
We will not mention them all here, but the foundation relies on Spring Boot and Spring Security OAuth.
OAuth, or, in our case, OAuth2 is basically an authorization delegation protocol.
To quote Wikipedia, OAuth:
[...] specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials.
Continue reading →
We have a microservice architecture with one of the microservices having a Spring Batch job which processes a CSV file and calling another microservice.
Both microservices are OAuth2 protected ResourceServers.
When we call the first microservice, a batch job is started and we want the Authorization header to be passed to the second microservice.
The solution can be defined as: In a Feign RequestInterceptor, grab the current OAuth access_token and pass it on the the RequestTemplate with Hystrix running in SEMAPHORE execution isolation strategy
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableResourceServer
@EnableOAuth2Client
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
Continue reading →
The new version IntelliJ IDEA 2016.2 is out!
It includes a new feature to support font ligatures.
It looks very promising, so I downloaded and installed the font FiraCode.
But I noticed rough edges in the font.
I enabled the feature in Settings → Editor → Colors & Fonts → Font and selected FiraCode as Font.
Continue reading →
For several of my projects I required a list where input items could be dynamically added and removed.
Because i saw uses for this over and over i created a component which i'm sharing with you here.
The component ended up like the code below.
Where i used a template generation function in order to create the ul and li elements.
I'm using the angular.element function to create nodes as a method of preference.
function mpExpandableListController(){
var vm = this;
vm.add = add;
vm.manipulatable = true;
vm.remove = remove;
// If items was not declared, create new array.
if(!vm.items) {
vm.items = [];
}
// If the items array is empty, create first item.
if(vm.items.length === 0) {
add();
}
/**
* Add an item to the item list.
*/
function add(){
vm.items.push({});
setManipulatable();
}
/**
* Remove an item from the item list.
* @param item The item to remove
*/
function remove(item){
vm.items.splice(vm.items.indexOf(item), 1);
setManipulatable();
}
/**
* Set the flag which designates that the list may be manipulated.
*/
function setManipulatable() {
vm.manipulatable = vm.items.length > 1;
}
}
/**
*
* @param $element The element as declared in the HTML
* @param $attrs The attributes for said element
* @returns {string} A template for this component.
*/
function template($element, $attrs){
var container = angular.element('<ul>'),
item = angular.element('<li ng-repeat="item in list.items">'),
footer = angular.element('<li>');
container.addClass($attrs.universalClass);
item.addClass($attrs.itemClass);
footer.addClass($attrs.footerClass);
item.html($element.find('item').html());
footer.html($element.find('footer').html());
container.append(item);
container.append(footer);
return container[0].outerHTML;
}
var mpExpandableList = {
bindings: {
footerClass: '@',
itemClass: '@',
items: '=',
universalClass: '@'
},
controller: mpExpandableListController,
controllerAs: 'list',
template: template
};
angular
.module('my.project')
.component('mpExpandableList', mpExpandableList);
Continue reading →
Suppose we want to support partial JSON responses in our Ratpack application.
The user must send a request parameter with a list of fields that need to be part of the response.
In our code we must use the value of the request parameter and output only the given properties of an object.
We implement this logic using a custom renderer in Ratpack.
Inside the renderer we can get access to the request parameters of the original request.
In our example Ratpack application we have a Course class, which is a simple class withs some properties:
Continue reading →
We can use the environment variable SPRING_APPLICATION_JSON with a JSON value as configuration source for our Grails 3 application.
The JSON value is parsed and merged with the configuration.
Instead of the environment variable we can also use the Java system property spring.application.json.
Let's create a simple controller that reads the configuration property app.message:
Continue reading →
Ratpack has a lot of options to add configuration data to our application.
We can use for example YAML and JSON files, properties, environment variables and Java system properties.
Groovy has the ConfigSlurper class to parse Groovy script with configuration data.
It even supports an environments block to set configuration value for a specific environment.
If we want to support Groovy scripts as configuration definition we write a class that implements the ratpack.config.ConfigSource interface.
We create a new class ConfigSlurperConfigSource and implement the ConfigSource interface.
We must implement the loadConfigData method in which we read the Groovy configuration and transform it to a ObjectNode so Ratpack can use it:
Continue reading →
We have many ways to provide configuration properties to a Spring (Boot) application.
We can add our own custom configuration properties format.
For example we can use Groovy's ConfigObject object to set configuration properties.
We need to read a configuration file using ConfigSlurper and make it available as a property source for Spring.
We need to implement two classes and add configuration file to support a Groovy configuration file in a Spring application.
First we need to write a class that extends the PropertySource in the package org.springframework.core.env.
This class has methods to get property values based on a given key.
There are already some subclasses for specific property sources.
There is for example also a MapPropertySource class.
We will extend that class for our implementation, because we can pass our flattened ConfigObject and rely on all existing functionality of the MapPropertySource class:
Continue reading →
To define configuration sources for our Ratpack application we have several options.
We can set default properties, look at environment variables or Java system properties, load JSON or YAML formatted configuration files or implement our own configuration source.
When something goes wrong using one of these methods we want to be able to handle that situation.
For example if an optional configuration file is not found, we want to inform the user, but the application must still start.
The default exception handling will throw the exception and the application is stopped.
We want to customise this so we have more flexibility on how to handle exceptions.
We provide the configuration source in the serverConfig configuration block of our Ratpack application.
We must add the onError method and provide an error handler implementation before we load any configuration source.
This error handler will be passed to each configuration source and is execute when an exception occurs when the configuration source is invoked.
The error handler implements the Action interface with the type Throwable.
In our implementation we can for example check for the type of Throwable and show a correct status message to the user.
Continue reading →