All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restler.spring.mvc.spring.SpringUtils Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package org.restler.spring.mvc.spring;

import com.google.common.collect.ImmutableMultimap;
import org.restler.http.HttpCall;
import org.restler.http.HttpForm;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;

public class SpringUtils {

    private static volatile Boolean isSpringAvailable = null;

    public static boolean isSpringAvailable() {
        if (isSpringAvailable == null) {
            isSpringAvailable = checkSpring();
        }

        return isSpringAvailable;
    }

    public static  HttpCall prepareForSpringMvc(HttpCall call) {
        if (call.getRequestBody() instanceof HttpForm) {
            return new HttpCall(call.getUrl(), call.getHttpMethod(), ((HttpForm) call.getRequestBody()).getFields(), call.getHeaders(), call.getReturnType());
        } else {
            return call;
        }
    }

    private static MultiValueMap toMultiValueMap(ImmutableMultimap guavaMap) {
        MultiValueMap res = new LinkedMultiValueMap<>();
        for (Map.Entry field : guavaMap.entries()) {
            res.add(field.getKey(), field.getValue());
        }
        return res;
    }

    public static ImmutableMultimap toGuavaMultimap(MultiValueMap headers) {
        ImmutableMultimap.Builder headersBuilder = new ImmutableMultimap.Builder<>();
        for (Map.Entry> header : headers.entrySet()) {
            headersBuilder.putAll(header.getKey(), header.getValue());
        }
        return headersBuilder.build();
    }

    private static boolean checkSpring() {
        try {
            Class.forName("org.springframework.web.context.request.async.DeferredResult");
            Class.forName("org.springframework.web.client.RestTemplate");
            Class.forName("org.springframework.http.converter.json.MappingJackson2HttpMessageConverter");

            Class.forName("org.springframework.util.LinkedMultiValueMap");
            Class.forName("org.springframework.util.MultiValueMap");

            Class.forName("org.springframework.http.HttpHeaders");
            Class.forName("org.springframework.http.HttpMethod");
            Class.forName("org.springframework.http.RequestEntity");
            Class.forName("org.springframework.http.ResponseEntity");
            Class.forName("org.springframework.http.converter.HttpMessageNotWritableException");
            Class.forName("org.springframework.util.MultiValueMap");
            Class.forName("org.springframework.web.client.HttpStatusCodeException");
            Class.forName("org.springframework.web.client.RestTemplate");

            Class.forName("org.springframework.core.ParameterizedTypeReference");
        } catch (ClassNotFoundException e) {
            return false;
        }

        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy