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

org.wisdom.test.http.BaseRequest Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.
 * #L%
 */
package org.wisdom.test.http;

import com.fasterxml.jackson.databind.JsonNode;
import org.jsoup.nodes.Document;

import java.io.InputStream;
import java.util.concurrent.Future;

/**
 * The default code for all requests.
 */
public abstract class BaseRequest {

    /**
     * The UTF-8 charset.
     */
    protected static final String UTF_8 = "UTF-8";

    /**
     * The underlying HTTP Request.
     */
    protected HttpRequest httpRequest;

    /**
     * Creates a base request form the given HTTP Request.
     *
     * @param httpRequest the request
     */
    protected BaseRequest(HttpRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    /**
     * Creates a base request.
     */
    protected BaseRequest() {
        super();
    }

    /**
     * Gets the request results as String.
     *
     * @return the result
     * @throws Exception if the request failed
     */
    public HttpResponse asString() throws Exception {
        return HttpClientHelper.request(httpRequest, String.class);
    }


    /**
     * Gets the request results as String. The invocation is made asynchronously.
     *
     * @return future to retrieve the result
     */
    public Future> asStringAsync() {
        return HttpClientHelper.requestAsync(httpRequest, String.class, null);
    }

    /**
     * Gets the request results as String. The invocation is made asynchronously.
     *
     * @param callback the callback invoked with the resulting String.
     * @return future to retrieve the result
     */
    public Future> asStringAsync(Callback callback) {
        return HttpClientHelper.requestAsync(httpRequest, String.class, callback);
    }

    /**
     * Gets the request results as JSON.
     *
     * @return the result
     * @throws Exception if the request failed
     */
    public HttpResponse asJson() throws Exception {
        return HttpClientHelper.request(httpRequest, JsonNode.class);
    }

    /**
     * Gets the request results as HTML.
     *
     * @return the result
     * @throws Exception if the request failed
     */
    public HttpResponse asHtml() throws Exception {
        return HttpClientHelper.request(httpRequest, Document.class);
    }

    /**
     * Gets the request results as JSON. The invocation is made asynchronously.
     *
     * @return future to retrieve the result
     */
    public Future> asJsonAsync() {
        return HttpClientHelper.requestAsync(httpRequest, JsonNode.class, null);
    }

    /**
     * Gets the request results as String. The invocation is made asynchronously.
     *
     * @param callback the callback invoked with the resulting String.
     * @return future to retrieve the result
     */
    public Future> asJsonAsync(Callback callback) {
        return HttpClientHelper.requestAsync(httpRequest, JsonNode.class, callback);
    }

    /**
     * Gets the request results as binary. The invocation is made asynchronously.
     *
     * @return future to retrieve the result
     */
    public HttpResponse asBinary() throws Exception {
        return HttpClientHelper.request(httpRequest, InputStream.class);
    }

    /**
     * Gets the request results as binary. The invocation is made asynchronously.
     *
     * @return future to retrieve the result
     */
    public Future> asBinaryAsync() {
        return HttpClientHelper.requestAsync(httpRequest, InputStream.class, null);
    }

    /**
     * Gets the request results as binary. The invocation is made asynchronously.
     *
     * @param callback the callback invoked with the resulting String.
     * @return future to retrieve the result
     */
    public Future> asBinaryAsync(Callback callback) {
        return HttpClientHelper.requestAsync(httpRequest, InputStream.class, callback);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy