org.openschedule.api.impl.AbstractOpenSchedulApiBinding.java Source code

Java tutorial

Introduction

Here is the source code for org.openschedule.api.impl.AbstractOpenSchedulApiBinding.java

Source

/**
 *  This file is part of OpenSchedule for Android
 * 
 *  OpenSchedule for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenSchedule for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSchedule for Android.  If not, see <http://www.gnu.org/licenses/>.
 *   
 * @author Daniel Frey <dmfrey at gmail dot com>
 * 
 * This software can be found at <http://code.google.com/p/open-schedule-android/>
 *
 */
package org.openschedule.api.impl;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.web.client.RestTemplate;

/**
 * @author Daniel Frey
 *
 */
public abstract class AbstractOpenSchedulApiBinding {

    private final RestTemplate restTemplate;

    /**
     * Constructs the API template without user authorization. This is useful for accessing operations on a provider's API that do not require user authorization.
     */
    protected AbstractOpenSchedulApiBinding() {
        restTemplate = new RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
        restTemplate.setMessageConverters(getMessageConverters());
        configureRestTemplate(restTemplate);
    }

    /**
     * Set the ClientHttpRequestFactory. This is useful when custom configuration of the request factory is required, such as configuring custom SSL details.
     * @param requestFactory the request factory
     */
    public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
        restTemplate.setRequestFactory(requestFactory);
    }

    // public implementation operations

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    // subclassing hooks

    protected void configureRestTemplate(RestTemplate restTemplate) {
    }

    protected List<HttpMessageConverter<?>> getMessageConverters() {
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new StringHttpMessageConverter());
        messageConverters.add(getFormMessageConverter());
        messageConverters.add(getJsonMessageConverter());
        messageConverters.add(getByteArrayMessageConverter());

        return messageConverters;
    }

    /**
     * Returns an {@link FormHttpMessageConverter} to be used by the internal {@link RestTemplate}.
     * By default, the message converter is set to use "UTF-8" character encoding.
     * Override to customize the message converter (for example, to set supported media types or message converters for the parts of a multipart message). 
     * To remove/replace this or any of the other message converters that are registered by default, override the getMessageConverters() method instead.
     */
    protected FormHttpMessageConverter getFormMessageConverter() {
        FormHttpMessageConverter converter = new FormHttpMessageConverter();
        converter.setCharset(Charset.forName("UTF-8"));
        return converter;
    }

    /**
     * Returns a {@link MappingJacksonHttpMessageConverter} to be used by the internal {@link RestTemplate}.
     * Override to customize the message converter (for example, to set a custom object mapper or supported media types).
     * To remove/replace this or any of the other message converters that are registered by default, override the getMessageConverters() method instead.
     */
    protected MappingJacksonHttpMessageConverter getJsonMessageConverter() {
        return new MappingJacksonHttpMessageConverter();
    }

    /**
     * Returns a {@link ByteArrayHttpMessageConverter} to be used by the internal {@link RestTemplate} when consuming image or other binary resources.
     * By default, the message converter supports "image/jpeg", "image/gif", and "image/png" media types.
     * Override to customize the message converter (for example, to set supported media types).
     * To remove/replace this or any of the other message converters that are registered by default, override the getMessageConverters() method instead.    
     */
    protected ByteArrayHttpMessageConverter getByteArrayMessageConverter() {
        ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
        converter.setSupportedMediaTypes(
                Arrays.asList(MediaType.IMAGE_JPEG, MediaType.IMAGE_GIF, MediaType.IMAGE_PNG));
        return converter;
    }

}