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

com.jianggujin.http.request.JDefaultRequestBodyResolver Maven / Gradle / Ivy

package com.jianggujin.http.request;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.jianggujin.http.core.JRequest;
import com.jianggujin.http.core.JRequestBodyResolver;
import com.jianggujin.http.util.JDataUtils;
import com.jianggujin.http.util.JOutputStream;

/**
 * 默认请求体解析器
 * 
 * @author jianggujin
 *
 */
public class JDefaultRequestBodyResolver implements JRequestBodyResolver {

   @Override
   public void write(JRequest req, Object requestBody, JOutputStream w) throws IOException {
      if (requestBody instanceof CharSequence) {
         w.write((CharSequence) requestBody);
      } else if (requestBody instanceof byte[]) {
         w.write((byte[]) requestBody);
      } else if (requestBody instanceof char[]) {
         w.write((char[]) requestBody);
      } else if (requestBody instanceof Map) {
         Map map = (Map) requestBody;
         Iterator iterator = map.entrySet().iterator();
         boolean first = true;
         while (iterator.hasNext()) {
            Entry entry = (Entry) iterator.next();
            String key = JDataUtils.toString(entry.getKey());
            String value = JDataUtils.toString(entry.getValue());
            if (JDataUtils.isEmpty(key)) {
               continue;
            }
            if (!first) {
               w.write("&");
            } else {
               first = false;
            }
            w.write(key);
            w.write("=");
            if (!JDataUtils.isEmpty(value)) {
               w.write(URLEncoder.encode(value, req.charset()));
            }
         }
      }
   }

   @Override
   public boolean support(Class clazz) {
      return CharSequence.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz)
            || byte[].class.equals(clazz) || char[].class.equals(clazz);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy