‘Git Happens’ by Jessica Kerr

10 10 2015

A perfect git introduction video for git beginners:

The most important git concepts are explained. That will help you to understand the way how merge, rebase or branching works.

Definetely a must watch – even if you are already a git user not understanding what actually is happening under the hood. That’s not a magic – it is well defined flow and idea behind it





SOAP request in Spring 4: Add Header to SOAP message

27 09 2014

Spring ‘Convention over configuration‘ routine makes it easy to implement basic use cases. But when it comes to advanced configuration, sometimes it can be difficult (see post about authenticating SOAP requests).

Another case is when you need to add SOAP Header to the SOAP message. This time it is however much easier than authenticating requests.

Sending the basic request

Developer has access to the request after it is marshalled, but before it is send. This is the moment when you can modify message by adding header. Following the spring.io site tutorial, you probably know that sending request is done via:

getWebServiceTemplate().marshalSendAndReceive(
	request,
	someSoapActionCallback);

Create class that will modify the request

The someSoapActionCallback will be called after the request is marshalled, and before it is send, allowing you to modify it before sending. So this is a hook where you can modify step of an sending algorythm. This is an instance of a class implementing the WebServiceMessageCallback and overriding its method: doWithMessage(WebServiceMessage message). This is how my class looks like:

public class SoapRequestHeaderModifier implements WebServiceMessageCallback{
	
	@Override
	public void doWithMessage(WebServiceMessage message) throws IOException,
			TransformerException {
		
		SoapHeader header = ((SoapMessage)message).getSoapHeader();
		
		StringSource headerSource = new StringSource(prepareSecurityHeader());
        
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(headerSource, header.getResult());
	}
	
	private String prepareSecurityHeader() {
		String result = "";
		StringWriter sw = new StringWriter();
        try {
            JAXBContext carContext = JAXBContext.newInstance(Security.class);
            Marshaller carMarshaller = carContext.createMarshaller();
            carMarshaller.marshal(SECURITY_HEADER, sw);
            result = sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
		return result;
	}
}

Where SECURITY_HEADER is an authentication string required by SOAP server. The Security.class is a data class generated from JAX2B (marshalled from WSDL). And Security class is in fact being added to the SOAP header.

Use the Header Modifier while sending request

I annotated the Header Modifier as a Component and Atowired its instance in a constructor. Thanks to that I can add its instance to the marshall request as folows:

getWebServiceTemplate().marshalSendAndReceive(
       request, 
       soapRequestHeaderModifier);

Now you can ensure that your request header is modified using your favorite SOAP mock client (SoapUI for instance).

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!






Spring website layouts SSI with thymeleaf templates

28 06 2014

Based on application from this post, I will show how to make the MVC app containing two pages, both with the same header and footer, but different contents. I will reuse the header and footer HTML code, to avoid code duplication and follow DRY principle.

What I am going to build

The goal is to have two pages differecing only in body, the header and footer are the same, reused by SSI (Server Side Include)

layout

Get the source code

The source code for this tutorial you can find here. on the GitHub

Build basic MVC Spring app

You can base it, or copy the code from the tutorial in this post

Create Header and Footer contents

Create two HTML files with header and footer content. Mine will be basic ones:

header.html:

<div th:fragment="head" style="background-color: red">
	<h3><i>Spring tutorial header</i></h3>
</div>

footer.html:

<footer th:fragment="foot" style="background-color: yellow;">
  &copy; 2014 Jacek Milewski from <a href="http://www.looksok.wordpress.com">Looks OK!</a>
</footer>

Include fragments in your main pages
Add header and footer divs in your page, and include head and foot fragment from header.html and footer.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:th="http://www.thymeleaf.org" >
    <head>
        <title>Spring MVC Example</title>
    </head>
    <body>
       	<div th:include="header::head"></div>
    
        <h1>Welcome!</h1>
        <p>Click <a th:href="@{/greeting}">here</a> to advance to the next page.</p>

        <div th:include="footer::foot"></div>

    </body>
</html>

You can do it at any other page you need.

Include or replace?

Here I used th:include, so the header.html was inserted into div element. The other option is to replace that div entirely with fragment content. You can do it with th:replace instead of th:include.

Get the source code

The source code for this tutorial you can find here. on the GitHub

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: