Spring by Example Utils Module

David Winterfeldt

2008


The Spring by Example Utils module currently has the HttpClientTemplate and HttpClientOxmTemplate which are light wrappers on top of Apache's HttpClient providing Spring style code based templating. The latter template provides marshaling and unmarshalling of XML based requests.

The Logger BeanPostProcessor provides logger creation and injection based on reflection, interfaces, or annotations.

The ImageProcessor is a utility to process images and currently can help scale and image from one size to another either using a java.io.File or a java.io.InputStream to a java.io.OututStream.

1. HttpClientTemplate

HttpClientTemplate which uses Apache's HttpClient to process HTTP requests and receive the data as a String, InputStream, or byte[].

Spring Configuration

Basic Configuration

Set a default URI for the HttpClientTemplate.

HttpClientTemplateTest-context.xml
                        
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="httpClient" class="org.springbyexample.httpclient.HttpClientTemplate">
        <property name="defaultUri">
            <value><![CDATA[http://localhost:8093/test]]></value>
        </property>
    </bean>

</beans>
                        
                    

HTTP Authorization Configuration

The HttpClient instance used by HttpClientTemplate could be set during configuration with authorization, but all of the default authorization classes must be set using constructors. The Spring by Example authorization beans allow the use of setters so are a little more Spring friendly. The example below will use the userName and password when challenged for authentication by a request to localhost:8093.

When accessing a site that will challenge any request, authenticationPreemptive can be set to true. Then the credentials will be sent along with every request instead of a challenging needed to be issues before the credentials are sent.

HttpClientTemplateAuthTest-context.xml
                        
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="httpClient" class="org.springbyexample.httpclient.HttpClientTemplate">
        <property name="defaultUri">
            <value><![CDATA[http://localhost:8093/admin/test]]></value>
        </property>
        <property name="credentials">
            <list>
                <bean class="org.springbyexample.httpclient.auth.Credentials">
                    <property name="authScopeHost" value="localhost" />
                    <property name="authScopePort" value="8093" />
                    <property name="userName" value="jsmith" />
                    <property name="password" value="password" />
                </bean>
            </list>
        </property>
    </bean>

</beans>
                        
                    

Code Example

Excerpts from HttpClientTemplateTest.

Example 1. HTTP Get Response with a String

                    
template.executeGetMethod(new ResponseStringCallback() {
    public void doWithResponse(String response) throws IOException {
        ...
        
        logger.debug("HTTP Get string response. '{}'", response);
    }
});
                    
                

Example 2. HTTP Get Response with an InputStream

                    
template.executeGetMethod(new ResponseStreamCallback() {
    public void doWithResponse(InputStream in) throws IOException {
        String response = IOUtils.toString(in);
        
        ...
        
        logger.debug("HTTP Get stream response. '{}'", response);
    }
});
                    
                

Example 3. HTTP Get Response with a Byte Array

                    
template.executeGetMethod(new ResponseByteCallback() {
    public void doWithResponse(byte[] byteResponse) throws IOException {
        String response = new String(byteResponse);

        ...
        
        logger.debug("HTTP Get byte response. '{}'", response);
    }
);
                    
                

Example 4. HTTP Get with Parameters & Response with a String

                    
Map<String, String> hParams = new HashMap<String, String>();
hParams.put(GET_PARAM_INPUT_KEY, GET_PARAM_INPUT_VALUE);

template.executeGetMethod(hParams, 
    new ResponseStringCallback() {
        public void doWithResponse(String response) throws IOException {
            ...
            
            logger.debug("HTTP Get with params string response. '{}'", response);
        }
});
                    
                

Example 5. HTTP Post with Parameters & Response with a String

                    
Map<String, String> hParams = new HashMap<String, String>();
hParams.put(LOWER_PARAM, POST_NAME);

template.executePostMethod(hParams, 
    new ResponseStringCallback() {
        public void doWithResponse(String response) throws IOException {
            ...

            logger.debug("HTTP Post string response. '{}'", response);
        }
});
                    
                

Example 6. HTTP Post with Parameters & Response with an InputStream

                    
Map<String, String> hParams = new HashMap<String, String>();
hParams.put(LOWER_PARAM, POST_NAME);

template.executePostMethod(hParams,
    new ResponseStreamCallback() {
        public void doWithResponse(InputStream in) throws IOException {
            String response = IOUtils.toString(in);
            
            ...

            logger.debug("HTTP Post stream response. '{}'", response);
        }
});
                    
                

Example 7. HTTP Post with Parameters & Response with a Byte Array

                    
Map<String, String> hParams = new HashMap<String, String>();
hParams.put(LOWER_PARAM, POST_NAME);

template.executePostMethod(hParams,
    new ResponseByteCallback() {
        public void doWithResponse(byte[] byteResponse) throws IOException {
            String response = new String(byteResponse);
            
            ...

            logger.debug("HTTP Post byte response. '{}'", response);
        }
});
                    
                

Example 8. HTTP Post with Data & Response with a String

A post sending data, like XML, in the request for the server to process and return results.

                    
protected final static String POST_DATA_INPUT = "<message>Greetings</message>";

...

template.executePostMethod(POST_DATA_INPUT,
    new ResponseStringCallback() {
        public void doWithResponse(String response) throws IOException {
            ...

            logger.debug("HTTP string data post string response. '{}'", response);
        }
});