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

com.landawn.abacus.http.AbstractHttpRequest Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 2.1.12
Show newest version
/*
 * Copyright (C) 2019 HaiYang Li
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.landawn.abacus.http;

import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;

import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.util.N;

/**
 *
 * @author Haiyang Li
 * @param 
 * @since 1.3
 */
abstract class AbstractHttpRequest> {

    final AbstractHttpClient httpClient;

    HttpMethod httpMethod;

    HttpSettings settings;

    Object request;

    AbstractHttpRequest(AbstractHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * 
     * @param user
     * @param password
     * @return
     */
    public S basicAuth(String user, Object password) {
        checkSettings();

        settings.basicAuth(user, password);

        return (S) this;
    }

    /**
     *
     * @param name
     * @param value
     * @return
     */
    public S header(String name, Object value) {
        checkSettings();

        settings.header(name, value);

        return (S) this;
    }

    /**
     *
     * @param name1
     * @param value1
     * @param name2
     * @param value2
     * @return
     */
    public S headers(String name1, Object value1, String name2, Object value2) {
        checkSettings();

        settings.headers(name1, value1, name2, value2);

        return (S) this;
    }

    /**
     *
     * @param name1
     * @param value1
     * @param name2
     * @param value2
     * @param name3
     * @param value3
     * @return
     */
    public S headers(String name1, Object value1, String name2, Object value2, String name3, Object value3) {
        checkSettings();

        settings.headers(name1, value1, name2, value2, name3, value3);

        return (S) this;
    }

    /**
     *
     * @param headers
     * @return
     */
    public S headers(Map headers) {
        checkSettings();

        settings.headers(headers);

        return (S) this;
    }

    /**
     *
     * @param headers
     * @return
     */
    public S headers(HttpHeaders headers) {
        checkSettings();

        settings.headers(headers);

        return (S) this;
    }

    /**
     *
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public String get() throws UncheckedIOException {
        return get(String.class);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T get(final Class resultClass) throws UncheckedIOException {
        return get(resultClass, null);
    }

    /**
     *
     * @param query
     * @return
     */
    public String get(Object query) {
        return get(String.class, query);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @param query
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T get(final Class resultClass, final Object query) throws UncheckedIOException {
        this.httpMethod = HttpMethod.GET;
        this.request = query;

        return execute(resultClass);
    }

    /**
     *
     * @param body
     * @return
     */
    public String post(Object body) {
        return post(String.class, body);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @param body
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T post(final Class resultClass, final Object body) throws UncheckedIOException {
        this.httpMethod = HttpMethod.POST;
        this.request = body;

        return execute(resultClass);
    }

    /**
     *
     * @param body
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public String put(Object body) throws UncheckedIOException {
        return put(String.class, body);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @param body
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T put(final Class resultClass, final Object body) throws UncheckedIOException {
        this.httpMethod = HttpMethod.PUT;
        this.request = body;

        return execute(resultClass);
    }

    //    /**
    //     *
    //     * @param body
    //     * @return
    //     * @throws UncheckedIOException the unchecked IO exception
    //     */
    //    public String patch(Object body) throws UncheckedIOException {
    //        return patch(String.class, body);
    //    }
    //
    //    /**
    //     *
    //     * @param 
    //     * @param resultClass
    //     * @param body
    //     * @return
    //     * @throws UncheckedIOException the unchecked IO exception
    //     */
    //    public  T patch(final Class resultClass, final Object body) throws UncheckedIOException {
    //        this.httpMethod = HttpMethod.PATCH;
    //        this.request = body;
    //
    //        return execute(resultClass);
    //    }

    /**
     *
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public String delete() throws UncheckedIOException {
        return delete(String.class);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T delete(final Class resultClass) throws UncheckedIOException {
        return delete(resultClass, null);
    }

    /**
     *
     * @param query
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public String delete(Object query) throws UncheckedIOException {
        return delete(String.class, query);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @param query
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T delete(final Class resultClass, final Object query) throws UncheckedIOException {
        this.httpMethod = HttpMethod.DELETE;
        this.request = query;

        return execute(resultClass);
    }

    /**
     * 
     * @param httpMethod
     * @return
     * @throws UncheckedIOException
     */
    public String execute(final HttpMethod httpMethod) throws UncheckedIOException {
        return execute(String.class, httpMethod);
    }

    /**
     * 
     * @param 
     * @param resultClass
     * @param httpMethod
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T execute(final Class resultClass, final HttpMethod httpMethod) throws UncheckedIOException {
        return execute(resultClass, httpMethod, null);
    }

    /**
     * 
     * @param httpMethod
     * @param body
     * @return
     * @throws UncheckedIOException
     */
    public String execute(final HttpMethod httpMethod, final Object body) throws UncheckedIOException {
        return execute(String.class, httpMethod, body);
    }

    /**
     *
     * @param 
     * @param resultClass
     * @param httpMethod
     * @param body
     * @return
     * @throws UncheckedIOException the unchecked IO exception
     */
    public  T execute(final Class resultClass, final HttpMethod httpMethod, final Object body) throws UncheckedIOException {
        N.checkArgNotNull(httpMethod, "httpMethod");

        this.httpMethod = httpMethod;
        this.request = body;

        return execute(resultClass);
    }

    public void execute(final File output, final HttpMethod httpMethod, final Object body) throws UncheckedIOException {
        N.checkArgNotNull(httpMethod, "httpMethod");

        this.httpMethod = httpMethod;
        this.request = body;

        httpClient.execute(output, this.httpMethod, this.request, settings);
    }

    public void execute(final OutputStream output, final HttpMethod httpMethod, final Object body) throws UncheckedIOException {
        N.checkArgNotNull(httpMethod, "httpMethod");

        this.httpMethod = httpMethod;
        this.request = body;

        httpClient.execute(output, this.httpMethod, this.request, settings);
    }

    public void execute(final Writer output, final HttpMethod httpMethod, final Object body) throws UncheckedIOException {
        N.checkArgNotNull(httpMethod, "httpMethod");

        this.httpMethod = httpMethod;
        this.request = body;

        httpClient.execute(output, this.httpMethod, this.request, settings);
    }

    /**
     * 
     * @param 
     * @param resultClass
     * @return
     */
    protected  T execute(final Class resultClass) {
        if (httpMethod == null) {
            throw new RuntimeException("HTTP method is not set");
        }

        return httpClient.execute(resultClass, httpMethod, request, settings);
    }

    /**
     * Check settings.
     */
    protected void checkSettings() {
        if (settings == null) {
            settings = new HttpSettings();
        }
    }
}