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

com.ibasco.agql.core.AbstractWebRequest Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
/*
 * Copyright (c) 2022 Asynchronous Game Query Library
 *
 * 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.ibasco.agql.core;

import io.netty.handler.codec.http.HttpMethod;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 

Abstract AbstractWebRequest class.

* * @author Rafael Luis Ibasco */ @SuppressWarnings("all") abstract public class AbstractWebRequest extends AbstractRequest { private static final Logger log = LoggerFactory.getLogger(AbstractWebRequest.class); private RequestBuilder requestBuilder; /** *

Constructor for AbstractWebRequest.

*/ public AbstractWebRequest() { //super(null); requestBuilder = new RequestBuilder(); } /** *

baseUrl.

* * @param url * a {@link java.lang.String} object */ protected void baseUrl(String url) { request().setUrl(url); } /** *

request.

* * @return a {@link org.asynchttpclient.RequestBuilder} object */ public final RequestBuilder request() { return requestBuilder; } /** *

header.

* * @param header * a {@link java.lang.CharSequence} object * @param value * a {@link java.lang.String} object */ protected void header(CharSequence header, String value) { request().addHeader(header, value); } /** *

method.

* * @param method * a {@link io.netty.handler.codec.http.HttpMethod} object */ protected void method(HttpMethod method) { request().setMethod(method.name()); } /** *

urlParam.

* * @param name * a {@link java.lang.String} object * @param value * a {@link java.lang.Object} object */ protected void urlParam(String name, Object value) { RequestBuilder builder = request(); if (value == null) return; if (value instanceof Collection) { Collection values = (Collection) value; int idx = 0; for (Object v : values) { String paramName = String.format("%s[%d]", name, idx++); builder.addQueryParam(paramName, String.valueOf(v)); } } else { builder.addQueryParam(name, String.valueOf(value)); } } /** *

encode.

* * @param element * a {@link java.lang.String} object * * @return a {@link java.lang.String} object */ protected String encode(String element) { return encode(element, "UTF-8"); } /** *

encode.

* * @param element * a {@link java.lang.String} object * @param encoding * a {@link java.lang.String} object * * @return a {@link java.lang.String} object */ protected String encode(String element, String encoding) { try { return URLEncoder.encode(element, encoding); } catch (UnsupportedEncodingException ignored) { } return null; } /** *

getMessage.

* * @return a {@link org.asynchttpclient.Request} object */ public Request getMessage() { build(requestBuilder); Request webRequest = requestBuilder.build(); log.debug("Request URL: {}, PARAMS: {}", webRequest.getUrl(), webRequest.getQueryParams()); return requestBuilder.build(); } //@Override /** *

build.

* * @param requestBuilder * a {@link org.asynchttpclient.RequestBuilder} object */ protected void build(RequestBuilder requestBuilder) { //no implementation } }