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

org.onetwo.common.spring.rest.RestUtils Maven / Gradle / Ivy

package org.onetwo.common.spring.rest;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiFunction;

import org.onetwo.common.reflect.BeanToMapConvertor;
import org.onetwo.common.spring.utils.EnhanceBeanToMapConvertor.EnhanceBeanToMapBuilder;
import org.onetwo.common.utils.CharsetUtils;
import org.onetwo.common.utils.ParamUtils;
import org.onetwo.common.utils.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.collect.Sets;

public final class RestUtils {
	private static Collection BODY_SUPPORT_METHODS = Sets.newHashSet(RequestMethod.POST, RequestMethod.PATCH, RequestMethod.PUT);

	public static final String CLASS_OK_HTTP_CLIENT = "okhttp3.OkHttpClient";
	
	public static final String LIST_LEFT = "[";
	public static final String LIST_RIGHT = "]";
	public static final String DOT_ACCESOR = ".";
	
	public static final String ACCEPT_CHARSET = "Accept-Charset";

	public static final HttpHeaders FORM_HEADER;
	public static final HttpHeaders XML_HEADER;
	public static final HttpHeaders JSON_HEADER;
	public static final HttpHeaders TEXT_HEADER;

//	private static final BeanToMapConvertor BEAN_TO_MAP_CONVERTOR = BeanToMapBuilder.newBuilder().build();
	/*private static final BeanToMapConvertor STRING_VALUE_CONVERTOR = BeanToMapBuilder.newBuilder()
																						.propertyAcceptor((p, v)->v!=null)
																						.valueConvertor((p, v)->v.toString())
																						.build();*/
	private static final BeanToMapConvertor BEAN_TO_MAP_CONVERTOR = EnhanceBeanToMapBuilder.enhanceBuilder()
																							.enableJsonPropertyAnnotation()
																							.enableFieldNameAnnotation()
//																							.enableUnderLineStyle()
																							.propertyAcceptor((p, v)->v!=null)
//																							.valueConvertor((p, v)->v.toString())
																							.build();
	
	static{
		FORM_HEADER = createHeader(MediaType.APPLICATION_FORM_URLENCODED);
		
		XML_HEADER = new HttpHeaders();
		XML_HEADER.setContentType(MediaType.APPLICATION_ATOM_XML);
		XML_HEADER.set("Accept-Charset", CharsetUtils.UTF_8);

		JSON_HEADER = new HttpHeaders();
		JSON_HEADER.setContentType(MediaType.APPLICATION_JSON);
		JSON_HEADER.set(ACCEPT_CHARSET, CharsetUtils.UTF_8);

		TEXT_HEADER = new HttpHeaders();
		TEXT_HEADER.setContentType(MediaType.TEXT_PLAIN);
		TEXT_HEADER.set(ACCEPT_CHARSET, CharsetUtils.UTF_8);
		TEXT_HEADER.setAccept(Arrays.asList(MediaType.TEXT_PLAIN));
		
	}
	
	public static BeanToMapConvertor getBeanToMapConvertor() {
		return BEAN_TO_MAP_CONVERTOR;
	}
	public static boolean isRequestBodySupportedMethod(HttpMethod method){
		return isRequestBodySupportedMethod(RequestMethod.valueOf(method.name()));
	}
	public static boolean isRequestBodySupportedMethod(RequestMethod method){
		return BODY_SUPPORT_METHODS.contains(method);
	}
	
	public static boolean isOkHttp3Present(){
		return ClassUtils.isPresent(CLASS_OK_HTTP_CLIENT, null);
	}
	
	public static boolean isHttpComponentPresent(){
		return ClassUtils.isPresent("org.apache.http.impl.client.AbstractHttpClient", null);
	}
	

//	@SuppressWarnings("unchecked")
	public static HttpEntity createFormEntity(final Object obj){
		return createFormEntity(obj, BEAN_TO_MAP_CONVERTOR);
	}
	public static HttpEntity createFormEntity(final Object obj, BeanToMapConvertor convertor){
//		Assert.notNull(obj);
		if (obj==null) {
			return new HttpEntity>(FORM_HEADER);
		}
		if(HttpEntity.class.isInstance(obj)){
			return (HttpEntity)obj;
		}
		
		HttpHeaders formHeader = FORM_HEADER;//createHeader(MediaType.APPLICATION_FORM_URLENCODED);
		
		final MultiValueMap params = toMultiValueStringMap(obj, convertor);
		return new HttpEntity>(params, formHeader);
	}
	
	public static MultiValueMap toMultiValueMap(final Object obj){
		return toMultiValueMap(obj, BEAN_TO_MAP_CONVERTOR);
	}
	
	public static MultiValueMap toMultiValueStringMap(final Object obj){
		return toMultiValueStringMap(obj, BEAN_TO_MAP_CONVERTOR);
	}
	
	public static MultiValueMap toMultiValueStringMap(final Object obj, BeanToMapConvertor convertor){
		final MultiValueMap params = new LinkedMultiValueMap<>();
		convertor.flatObject("", obj, (key, value, ctx)->params.set(key, value==null?"":value.toString()));
		return params;
	}
	/*public static MultiValueMap toMultiValueStringMap(final Object obj, BeanToMapConvertor convertor){
		final MultiValueMap params = new LinkedMultiValueMap<>();
		convertor.flatObject("", obj, (key, value, ctx)->{
			if(value==null){
				return ;
			}
			params.add(key, value.toString());
		});
		return params;
	}*/
	@SuppressWarnings("unchecked")
	public static  MultiValueMap toMultiValueMap(final Object obj, BeanToMapConvertor convertor){
		final MultiValueMap params = new LinkedMultiValueMap<>();
//		appendMultiValueMap(params, "", obj);
		convertor.flatObject("", obj, (key, value, ctx)->params.set(key, (V)value));
		return params;
	}
	
	public static String propertiesToParamString(Object request){
		return keysToParamString(toMultiValueMap(request));
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String keysToParamString(Map params){
		return ParamUtils.toParamString(params, (BiFunction)(k, v)->{
			return k+"={"+k+"}";
		});
	}
	
	public static HttpHeaders createHeader(MediaType mediaType){
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(mediaType);
		headers.set(ACCEPT_CHARSET, CharsetUtils.UTF_8);
		return headers;
	}
	
	public static  HttpEntity createHttpEntity(T obj, MediaType mediaType){
		HttpHeaders headers = createHeader(mediaType);
		
		HttpEntity entity = new HttpEntity(obj, headers);
		return entity;
	}
	
	public static  HttpEntity createHttpEntity(T obj, HttpHeaders headers){
		HttpEntity entity = new HttpEntity(obj, headers);
		return entity;
	}
	
	/****
	 * 解释pathvariable参数,并且把所有queryParameters转化为queryString参数
	 * @author wayshall
	 * @param url
	 * @param invokeMethod
	 * @param context
	 * @return
	 */
	public static String appendQueryParametersToURL(final String url, Map queryParameters){
		String actualUrl = url;
		/*if(LangUtils.isNotEmpty(context.getPathVariables())){
			actualUrl = ExpressionFacotry.newStrSubstitutor("{", "}", context.getPathVariables()).replace(actualUrl);
		}*/
		if(!queryParameters.isEmpty()){
			String paramString = RestUtils.keysToParamString(queryParameters);
			actualUrl = ParamUtils.appendParamString(actualUrl, paramString);
		}
		return actualUrl;
	}
	
	/***
	 * 把路径串联起来
	 * @author weishao zeng
	 * @param url
	 * @param path
	 * @param requestPath
	 * @return
	 */
	public static String concatPath(String url, String path, String requestPath){
		StringBuilder fullPath = new StringBuilder();
		if(StringUtils.isNotBlank(url)){
			fullPath.append(StringUtils.trimEndWith(url, "/"));
		}
		if(StringUtils.isNotBlank(path)){
			if(!path.startsWith("/")){
				fullPath.append("/");
			}
			fullPath.append(StringUtils.trimEndWith(path, "/"));
		}
		if(StringUtils.isNotBlank(requestPath)){
			if(!requestPath.startsWith("/")){
				fullPath.append("/");
			}
			fullPath.append(requestPath);
		}
		return fullPath.toString();
	}

	private RestUtils(){}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy