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

me.saro.commons.web.Web Maven / Gradle / Ivy

The newest version!
package me.saro.commons.web;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import me.saro.commons.Converter;
import me.saro.commons.Files;
import me.saro.commons.function.ThrowableConsumer;
import me.saro.commons.function.ThrowableFunction;
import me.saro.commons.json.JsonReader;

/**
 * Web Client
 * @author      PARK Yong Seo
 * @since       0.1
 */
public interface Web {
    /**
     * create get method Web
     * @param url
     * @return
     */
    public static Web get(String url) {
        return new BasicWeb(url, "GET");
    }

    /**
     * create post method Web
     * @param url
     * @return
     */
    public static Web post(String url) {
        return new BasicWeb(url, "POST");
    }

    /**
     * create put method Web
     * @param url
     * @return
     */
    public static Web put(String url) {
        return new BasicWeb(url, "PUT");
    }

    /**
     * create patch method Web
     * @param url
     * @return
     */
    public static Web patch(String url) {
        return new BasicWeb(url, "PATCH");
    }

    /**
     * create delete method Web
     * @param url
     * @return
     */
    public static Web delete(String url) {
        return new BasicWeb(url, "DELETE");
    }
    
    /**
     * request charset
     * @return
     */
    public String getRequestCharset();
    
    /**
     * response charset
     * @return
     */
    public String getResponseCharset();

    /**
     * create custom method Web
     * @param url
     * @return
     */
    public static Web custom(String url, String method) {
        return new BasicWeb(url, method);
    }
    
    /**
     * Connect Timeout
     * @param connectTimeout
     * @return
     */
    public Web setConnectTimeout(int connectTimeout);
    
    /**
     * Read Timeout
     * @param readTimeout
     * @return
     */
    public Web setReadTimeout(int readTimeout);
    
    /**
     * set request Charset
     * @param charset
     * @return
     */
    public Web setRequestCharset(String charset);
    
    /**
     * set response charset
     * @param charset
     * @return
     */
    public Web setResponseCharset(String charset);
    
    /**
     * ignore https certificate
     * 
* this method not recommend *
* ignore certificate is defenseless the MITM(man-in-the-middle attack) * @param ignoreCertificate * @return */ public Web setIgnoreCertificate(boolean ignoreCertificate); /** * add url parameter *
* always append url parameter even post method *
* is not body write * @param name * @param value * @return */ public Web addUrlParameter(String name, String value); /** * set header * @param name * @param value * @return */ public Web setHeader(String name, String value); /** * write body binary * @param bytes * @return */ public Web writeBody(byte[] bytes); /** * writeBodyParameter *
* WARNING : is not json type *
*
* web *
* .writeBodyParameter("aa", "11") *
* .writeBodyParameter("bb", "22"); *
* equals *
* aa=11&bb=22 * @param name * @param value * @return */ public Web writeBodyParameter(String name, String value); /** * to Custom result * @param result * @param function * @return */ public WebResult toCustom(WebResult result, ThrowableFunction function); /** * to Custom result * @param function * @return */ default public WebResult toCustom(ThrowableFunction function) { return toCustom(new WebResult(), function); } /** * to Map result by JsonObject * @return */ default public WebResult> toMapByJsonObject() { return toJsonTypeReference(new TypeReference>(){}); } /** * to Map List result by JsonArray * @return */ default public WebResult>> toMapListByJsonArray() { return toJsonTypeReference(new TypeReference>>(){}); } /** * to JsonReader * @return */ default public WebResult toJsonReader() { return toCustom(is -> new JsonReader(Converter.toStringNotClose(is, getResponseCharset()))); } /** * to Json result by TypeReference * @param typeReference * @return */ default public WebResult toJsonTypeReference(TypeReference typeReference) { return toCustom(is -> new ObjectMapper().readValue(is, typeReference)); } /** * to text result * @return */ default public WebResult toPlainText() { return toCustom(is -> Converter.toStringNotClose(is, getResponseCharset())); } /** * save file and return WebResult * @return WebResult[WebResult] */ default public WebResult saveFile(File file, boolean overwrite) { return toCustom(is -> { Files.createFile(file, overwrite, is); return file; }); } /** * readRawResultStream * @param reader * @return it has Body */ default public WebResult readRawResultStream(ThrowableConsumer reader) { return toCustom(is -> { reader.accept(is); return "OK"; }); } /** * set header ContentType * @param value * @return */ default public Web setContentType(String value) { return setHeader("Content-Type", value); } /** * ContentType application/json * @return */ default public Web setContentTypeApplicationJson() { return setHeader("Content-Type", "application/json"); } /** * write Body text * @param text * @return */ default public Web writeBody(String text) { try { return writeBody(text.getBytes(getRequestCharset())); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * write json class *
* use jackson lib * @param toJsonObject * @return * @see * com.fasterxml.jackson.databind.ObjectMapper */ default public Web writeJsonByClass(Object toJsonObject) { return writeBody(Converter.toJson(toJsonObject)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy