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

top.jfunc.http.interfacing.AbstractParameterHandler Maven / Gradle / Ivy

package top.jfunc.http.interfacing;

import top.jfunc.http.annotation.*;
import top.jfunc.http.base.FormFile;
import top.jfunc.http.request.FormRequest;
import top.jfunc.http.request.HttpRequest;
import top.jfunc.http.request.MutableStringBodyRequest;
import top.jfunc.http.request.UploadRequest;
import top.jfunc.common.utils.MultiValueMap;

import java.lang.reflect.Array;
import java.util.Map;
import java.util.Objects;

/**
 * 参数处理器定义及对应注解的实现
 * @see Header
 * @see HeaderMap
 * @see Query
 * @see QueryMap
 * @see Route
 * @see RouteMap
 * @see Part
 * @see Field
 * @see FieldMap
 * @see Body
 * @see Url
 *
 * @author xiongshiyan at 2019/5/24 , contact me with email [email protected] or phone 15208384257
 */
abstract class AbstractParameterHandler

{ /** * 对HttpRequest应用实际参数 * @param r HttpRequest * @param p 参数值 */ abstract void apply(HttpRequest r, P p); /** * 什么都不干的处理器,比如处理 HttpRequest * @see HttpRequest */ static final class DoNothing extends AbstractParameterHandler { private DoNothing(){} static final DoNothing INSTANCE = new DoNothing(); @Override public void apply(HttpRequest httpRequest, Object value) { //doNothing } } /** * 处理header的 * @see top.jfunc.http.annotation.Header */ static final class Header extends AbstractParameterHandler { private final String name; Header(String name) { this.name = Objects.requireNonNull(name); } @Override public void apply(HttpRequest httpRequest, String value) { if (value == null) { return; // Skip null values. } httpRequest.addHeader(name, value); } } /** * 处理headerMap的 * @see top.jfunc.http.annotation.HeaderMap */ static final class HeaderMap extends AbstractParameterHandler> { @Override public void apply(HttpRequest httpRequest, Map headers) { if (headers == null || headers.isEmpty()) { return; // Skip null values. } if(headers instanceof MultiValueMap){ MultiValueMap multiValueMap = (MultiValueMap) headers; multiValueMap.forEachKeyValue(httpRequest::addHeader); }else { headers.forEach(httpRequest::addHeader); } } } /** * 处理查询参数的 * @see top.jfunc.http.annotation.Query */ static final class Query extends AbstractParameterHandler { private final String name; Query(String name) { this.name = Objects.requireNonNull(name); } @Override public void apply(HttpRequest httpRequest, Object value) { if (value == null) { return; // Skip null values. } httpRequest.addQueryParam(name, value.toString()); } } /** * 处理headerMap的 * @see top.jfunc.http.annotation.QueryMap */ static final class QueryMap extends AbstractParameterHandler> { @Override public void apply(HttpRequest httpRequest, Map querys) { if (querys == null || querys.isEmpty()) { return; // Skip null values. } if(querys instanceof MultiValueMap){ MultiValueMap multiValueMap = (MultiValueMap) querys; multiValueMap.forEachKeyValue(httpRequest::addQueryParam); } querys.forEach(httpRequest::addQueryParam); } } /** * 处理路径参数的 * @see Path */ static final class Route extends AbstractParameterHandler { private final String name; Route(String name) { this.name = Objects.requireNonNull(name); } @Override public void apply(HttpRequest httpRequest, Object value) { if (value == null) { return; // Skip null values. } httpRequest.addRouteParam(name, value.toString()); } } /** * 处理headerMap的 * @see PathMap */ static final class RouteMap extends AbstractParameterHandler> { @Override public void apply(HttpRequest httpRequest, Map routes) { if (routes == null || routes.isEmpty()) { return; // Skip null values. } routes.forEach(httpRequest::addRouteParam); } } /** * 处理Part * @see top.jfunc.http.annotation.Part */ static final class Part extends AbstractParameterHandler { private final String name; Part(String name) { this.name = Objects.requireNonNull(name); } @Override public void apply(HttpRequest httpRequest, Object value) { if (value == null) { return; // Skip null values. } UploadRequest uploadRequest = (UploadRequest)httpRequest; if(value instanceof FormFile){ uploadRequest.addFormFile((FormFile)value); }else if(value.getClass().isArray() && Array.get(value , 0) instanceof FormFile){ int length = Array.getLength(value); FormFile[] formFiles = new FormFile[length]; System.arraycopy(value , 0 , formFiles , 0 , length); uploadRequest.addFormFile(formFiles); }else if(value instanceof Iterable){ Iterable formFiles = (Iterable) value; formFiles.forEach(uploadRequest::addFormFile); }else { uploadRequest.addFormParam(name, value.toString()); } } } /** * 处理field的 * @see top.jfunc.http.annotation.Field */ static final class Field extends AbstractParameterHandler { private final String name; Field(String name) { this.name = Objects.requireNonNull(name); } @Override public void apply(HttpRequest httpRequest, Object value) { if (value == null) { return; // Skip null values. } ((FormRequest) httpRequest).addFormParam(name, value.toString()); } } /** * 处理fieldMap的 * @see top.jfunc.http.annotation.FieldMap */ static final class FieldMap extends AbstractParameterHandler> { @Override public void apply(HttpRequest httpRequest, Map fields) { if (fields == null || fields.isEmpty()) { return; // Skip null values. } FormRequest formRequest = (FormRequest) httpRequest; if(fields instanceof MultiValueMap){ MultiValueMap multiValueMap = (MultiValueMap) fields; multiValueMap.forEachKeyValue(formRequest::addFormParam); } fields.forEach(formRequest::addFormParam); } } /** * 处理Body的 * @see top.jfunc.http.annotation.Body */ static final class Body extends AbstractParameterHandler { @Override public void apply(HttpRequest httpRequest, String value) { if (value == null) { return; // Skip null values. } ((MutableStringBodyRequest)httpRequest).setBody(value); } } /** * 处理请求URL的 * @see top.jfunc.http.annotation.Url */ static final class Url extends AbstractParameterHandler { @Override public void apply(HttpRequest httpRequest, Object value) { if (value == null) { return; // Skip null values. } //支持URL和String httpRequest.setUrl(value.toString()); } } }