net.paslavsky.springrest.SpringRestClientMethodInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for net.paslavsky.springrest.SpringRestClientMethodInterceptor.java

Source

/*
 * Copyright (c) 2014 Andrey Paslavsky.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.paslavsky.springrest;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Method;
import java.net.URI;

/**
 * This method interceptor used to execute REST web-service methods. Data required to execute a web-service are going
 * by the annotations.
 *
 * @author Andrey Paslavsky
 * @version 1.0
 */
public class SpringRestClientMethodInterceptor implements MethodInterceptor, InitializingBean {
    private final RestTemplate restTemplate;
    private HttpHeadersHelper helper;
    private AnnotationPreprocessor annotationPreprocessor;
    private ConversionService conversionService;
    private URI baseUrl;
    private AuthenticationManager authenticationManager;

    public SpringRestClientMethodInterceptor() {
        this(new RestTemplate());
    }

    public SpringRestClientMethodInterceptor(RestTemplate restTemplate) {
        Assert.notNull(restTemplate);
        this.restTemplate = restTemplate;
    }

    public void setAnnotationPreprocessor(AnnotationPreprocessor annotationPreprocessor) {
        this.annotationPreprocessor = annotationPreprocessor;
    }

    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    public void setBaseUrl(URI baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public void afterPropertiesSet() {
        Assert.notNull(annotationPreprocessor);
        Assert.notNull(conversionService);
        helper = new HttpHeadersHelper(conversionService);
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Method method = invocation.getMethod();
        RestMethodMetadata metadata = annotationPreprocessor.parse(method.getDeclaringClass(), method);

        Object[] arguments = invocation.getArguments();
        URI uri = new UriBuilder(baseUrl, arguments).build(metadata);

        Object body = getRequestBody(metadata, arguments);
        HttpHeaders headers = helper.getHttpHeaders(metadata.getRequestHeaderParameters(), arguments);

        if (authenticationManager != null) {
            headers.putAll(authenticationManager.getAuthenticationHeaders());
        }

        HttpEntity<?> httpEntity = new HttpEntity<Object>(body, headers);

        ResponseEntity<?> responseEntity = restTemplate.exchange(uri, metadata.getHttpMethod(), httpEntity,
                metadata.getResponseClass());

        if (metadata.getMethodReturnType() == ResponseEntity.class) {
            return responseEntity;
        } else {
            return responseEntity.getBody();
        }
    }

    private Object getRequestBody(RestMethodMetadata metadata, Object[] arguments) {
        Object body = null;
        if (metadata.getRequestParameter() != null) {
            body = arguments[metadata.getRequestParameter()];
        }
        return body;
    }
}