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

com.jd.httpservice.agent.RequestBodyResolvers Maven / Gradle / Ivy

There is a newer version: 2.1.4.RELEASE
Show newest version
package com.jd.httpservice.agent;

import java.io.IOException;
import java.io.OutputStream;

import com.jd.httpservice.HttpServiceException;

import utils.io.RuntimeIOException;

class RequestBodyResolvers {

	public static final RequestBodyResolver NULL_BODY_RESOLVER = new NullBodyResolver();

	/**
	 * 创建基于参数列表的解析器;
	 * 
	 * @param argIndex
	 *            要作为 body 输出的参数的位置;
	 * @param converter
	 *            参数值转换器;
	 * @return
	 */
	public static RequestBodyResolver createArgumentResolver(ArgDefEntry defEntry) {
		return new ArgurmentResolver(defEntry);
	}
	
	private static final class ArgurmentResolver implements RequestBodyResolver {

		private ArgDefEntry defEntry;

		public ArgurmentResolver(ArgDefEntry defEntry) {
			this.defEntry = defEntry;
		}

		@Override
		public void resolve(Object[] args, OutputStream out) {
			Object arg = args[defEntry.getIndex()];
			if (arg == null && defEntry.getDefinition().isRequired()) {
				throw new HttpServiceException("The required body argument is null!");
			}
			try {
				defEntry.getDefinition().getConverter().write(arg, out);
			} catch (IOException e) {
				throw new RuntimeIOException(e.getMessage(), e);
			}
		}

	}

	/**
	 * 空的 body 解析器;
	 * 
	 * @author haiq
	 *
	 */
	private static final class NullBodyResolver implements RequestBodyResolver {

		@Override
		public void resolve(Object[] args, OutputStream out) {
			//Do nothing;
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy