ClientBuilder.java
/*
** Module : ClientBuilder.java
** Abstract : Implementation of the builtin class.
**
** Copyright (c) 2019-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 CA 20190526 First version, stubs taken by converting the skeleton using FWD.
** 002 CA 20191024 Added method support levels and updated the class support level.
** 003 MP 20200402 Modify methods and properties access level and adapt code for it.
** 004 ME 20200410 Complete implementation as of OE 11.7.4.
** 005 CA 20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
** signature.
** 006 ME 20210519 Fix registry lazy loading.
** ME 20210929 Fix return object on build.
** ME 20220918 Use local variable on buildLibrary (returnNormal doesn't work).
** 007 CA 20231019 All BlockManager calls must have a non-null referent.
** 008 CA 20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
** dropped if is a no-op.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.oo.net.http;
import static com.goldencode.p2j.util.BlockManager.externalProcedure;
import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.BlockManager.function;
import static com.goldencode.p2j.util.BlockManager.internalProcedure;
import static com.goldencode.p2j.util.BlockManager.onBlockLevel;
import static com.goldencode.p2j.util.BlockManager.returnNormal;
import com.goldencode.p2j.oo.core.*;
import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.oo.logging.IlogWriter;
import com.goldencode.p2j.oo.net.http.lib.ClientLibraryBuilder;
import com.goldencode.p2j.security.*;
import com.goldencode.p2j.util.*;
import com.goldencode.p2j.util.BlockManager.Action;
import com.goldencode.p2j.util.BlockManager.Condition;
import com.goldencode.p2j.util.InternalEntry.*;
/**
* Business logic (converted to Java from the 4GL source code
* in OpenEdge/Net/HTTP/ClientBuilder.cls).
*/
@LegacyResource(resource = "OpenEdge.Net.HTTP.ClientBuilder")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public abstract class ClientBuilder extends ConfigBuilder
{
protected static final String LANG_CLASS = "Progress.Lang.Class";
protected static final String NET_URI = "OpenEdge.Net.URI";
protected static final String ILOGWRITER = "OpenEdge.Logging.ILogWriter";
protected static final String IHTTPCLIENT = "OpenEdge.Net.HTTP.IHttpClient";
protected static final String CLIENTBUILDER = "OpenEdge.Net.HTTP.ClientBuilder";
protected static final String ICOOKIEJAR = "OpenEdge.Net.HTTP.ICookieJar";
protected static final String IHTTPCLIENTLIBRARY = "OpenEdge.Net.HTTP.IHttpClientLibrary";
/**
*
* Properties
* */
private character propClientName = TypeFactory.character("clientName");
private character propClientVersion = TypeFactory.character("clientVersion");
/**
* Registry property*/
private static ContextLocal<object<? extends BuilderRegistry>> registry = new ContextLocal<object<? extends BuilderRegistry>>()
{
/**
* Initial value
*/
@Override
protected object<? extends BuilderRegistry> initialValue()
{
object<? extends BuilderRegistry> registry = TypeFactory
.object(BuilderRegistry.class);
return registry;
}
};
/**
* Execute method
*/
@LegacySignature(type = Type.EXECUTE)
public void __net_http_ClientBuilder_execute__()
{
onBlockLevel(Condition.ERROR, Action.THROW);
}
/**
* Constructor
*/
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void __net_http_ClientBuilder_constructor__()
{
internalProcedure(this, "__net_http_ClientBuilder_constructor__", new Block((Body) () ->
{
__net_http_ConfigBuilder_constructor__();
}));
}
/**
* Static Constructor
*/
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void __net_http_ClientBuilder_constructor__static__()
{
externalProcedure(ClientBuilder.class, new Block((Body) () ->
{
onBlockLevel(Condition.ERROR, Action.THROW);
}));
}
/**
* Builder director method.
*
* @return A builder for the requested type.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Build", qualified = CLIENTBUILDER)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> build()
{
object<? extends ClientBuilder> builder = TypeFactory.object(ClientBuilder.class);
return function(ClientBuilder.class, "Build", object.class, new Block((Body) () -> {
object<? extends LegacyClass> builderType = getRegistry().ref()
.get(new character(ObjectOps.getLegacyName(ClientBuilder.class)));
Assert.isType(builderType, ObjectOps.getLegacyClass(ClientBuilder.class));
builder.assign(ObjectOps.newInstance(builderType.ref().getType()));
if (!builder.isUnknown() && builder.ref() instanceof ISupportInitialize)
((ISupportInitialize) builder.ref()).initialize();
returnNormal(builder);
}));
}
/**
* Helper method to build a http client library
*
* @return A builder for the requested type.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "BuildLibrary", qualified = IHTTPCLIENTLIBRARY)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
protected object<? extends com.goldencode.p2j.oo.net.http.IHttpClientLibrary> buildLibrary()
{
object<? extends com.goldencode.p2j.oo.net.http.IHttpClientLibrary> lib = TypeFactory.object(com.goldencode.p2j.oo.net.http.IHttpClientLibrary.class);
return function(ClientBuilder.class, this, "BuildLibrary", object.class, new Block((Body) () ->
{
character libTypeName = new character(ObjectOps.getLegacyName(IHttpClientLibrary.class));
character logTypeName = new character(ObjectOps.getLegacyName(IlogWriter.class));
if (super.hasOption(libTypeName).booleanValue())
{
lib.assign(getOptionObjectValue(libTypeName));
}
else
{
object<? extends ClientLibraryBuilder> libBuilder = ClientLibraryBuilder.build();
if (super.hasOption(new character("requestTimeout")).booleanValue())
{
libBuilder.ref().setRequestTimeout(super.getOptionNumericValue(new character("requestTimeout")));
}
if (super.hasOption(logTypeName).booleanValue())
{
libBuilder.ref().usingLogger((object<? extends IlogWriter>) getOptionObjectValue(logTypeName));
}
lib.assign(libBuilder.ref().getLibrary());
if (lib._isValid())
{
setOption(libTypeName, lib);
}
}
returnNormal(lib);
}));
}
/**
* Decorate the client with additional behaviour.
*
* @param p1 The type of decorator to use.
* @param p2 The client/instance we're decorating.
*
* @return The new, decorating client.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "DecorateClient", qualified = IHTTPCLIENT, parameters = {
@LegacyParameter(name = "p1", type = "OBJECT", qualified = LANG_CLASS, mode = "INPUT"),
@LegacyParameter(name = "p2", type = "OBJECT", qualified = IHTTPCLIENT, mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static object<? extends com.goldencode.p2j.oo.net.http.IhttpClient> decorateClient(
final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> p1,
final object<? extends com.goldencode.p2j.oo.net.http.IhttpClient> p2)
{
object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _p1 = TypeFactory.initInput(p1);
object<? extends IhttpClient> client = TypeFactory.object(IhttpClient.class);
return function(ClientBuilder.class, "DecorateClient", object.class, new Block((Body) () -> {
object<? extends LegacyClass> decoratorType = ObjectOps.newInstance(LegacyClass.class);
character dd = _p1.ref().getTypeName();
decoratorType.assign(getRegistry().ref().get(dd));
if (decoratorType.isUnknown())
{
returnNormal(p2);
}
else {
Assert.isType(decoratorType, ObjectOps.getLegacyClass(HttpClientDecorator.class));
client.assign(ObjectOps.newInstance(decoratorType.ref().getType(), "I", p2));
if (!client.isUnknown() && client.ref() instanceof ISupportInitialize)
((ISupportInitialize) client.ref()).initialize();
returnNormal(client);
}
}));
}
/**
* Initialize registry.
*
* @param _poRegistry the registry to be initialized.
*/
private static void initializeRegistry(final object<? extends BuilderRegistry> _poRegistry)
{
internalProcedure(ClientBuilder.class, "InitializeRegistry", new Block((Body) () ->
{
_poRegistry.ref().put(new character(ObjectOps.getLegacyName(ClientBuilder.class)),
ObjectOps.getLegacyClass(DefaultHttpClientBuilder.class));
_poRegistry.ref().put(new character(ObjectOps.getLegacyName(IhttpClient.class)),
ObjectOps.getLegacyClass(HttpClient.class));
_poRegistry.ref().put(new character(ObjectOps.getLegacyName(ISupportCookies.class)),
ObjectOps.getLegacyClass(StatefulHttpClient.class));
_poRegistry.ref().put(new character(ObjectOps.getLegacyName(ISupportProxy.class)),
ObjectOps.getLegacyClass(ProxyHttpClient.class));
}));
}
/**
* Get registry.
*
* @return registry.
*/
@LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.BuilderRegistry", type = Type.GETTER, name = "Registry")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static object<? extends BuilderRegistry> getRegistry()
{
return function(ClientBuilder.class, "Registry", object.class, new Block((Body) () ->
{
if (!registry.get()._isValid()) {
registry.get().assign(ObjectOps.newInstance(BuilderRegistry.class));
initializeRegistry(registry.get());
}
returnNormal(registry.get());
}));
}
/**
* Get client name property.
*
* @return client name property.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "PROP_CLIENT_NAME")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
protected character getPropClientName()
{
return function(ClientBuilder.class, this, "PROP_CLIENT_NAME", character.class,
new Block((Body) () ->
{
returnNormal(propClientName);
}));
}
/**
* Get client version property.
*
* @return client version property.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "PROP_CLIENT_VERSION")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
protected character getPropClientVersion()
{
return function(ClientBuilder.class, this, "PROP_CLIENT_VERSION", character.class,
new Block((Body) () ->
{
returnNormal(propClientVersion);
}));
}
/**
* Return a configured client
*
* @return a configured client.
*/
@LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.IHttpClient", type = Type.GETTER, name = "Client")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public abstract object<? extends IhttpClient> getClient();
/**
* Uses a default cookie for managing cookies.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "KeepCookies", qualified = CLIENTBUILDER)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> keepCookies()
{
return function(ClientBuilder.class, this, "KeepCookies", object.class,
new Block((Body) () ->
{
object<? extends CookieJarBuilder> oCJB = CookieJarBuilder.build();
object<? extends ICookieJar> oICJ = oCJB.ref().getCookieJar();
returnNormal(keepCookies(oICJ));
}));
}
/**
* Uses a default cookie for managing cookies.
*
* @param _p1 ICookieJar The cookie jar which manages cookies for this client
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "KeepCookies", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "OBJECT", qualified = ICOOKIEJAR, mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> keepCookies(
final object<? extends com.goldencode.p2j.oo.net.http.ICookieJar> _p1)
{
object<? extends com.goldencode.p2j.oo.net.http.ICookieJar> p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "KeepCookies", object.class,
new Block((Body) () ->
{
Assert.notNull(p1, new character("Cookie jar"));
super.setOption(ObjectOps.getLegacyClass(ICookieJar.class).ref().getTypeName(), p1);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Sets the client name and version.
*
* @param _p1 The client name.
* @param _p2 The client version (semantic version: major.minor.patch).
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Named", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "p2", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> named(
final character _p1, final character _p2)
{
character p1 = TypeFactory.initInput(_p1);
character p2 = TypeFactory.initInput(_p2);
return function(ClientBuilder.class, this, "Named", object.class, new Block((Body) () ->
{
Assert.notNullOrEmpty(p1, new character("Client name"));
Assert.notNullOrEmpty(p2, new character("Client version"));
setOption(propClientName, p1);
setOption(propClientVersion, p2);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Sets the value of the NumRetries option.
*
* @param _p1 The value of the NumRetries option.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "SetNumRetries", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "INTEGER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> setNumRetries(
final integer _p1)
{
integer p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "SetNumRetries", object.class,
new Block((Body) () ->
{
Assert.isZeroOrPositive(p1, new character("Retry count"));
setOption(new character("numRetries"), new decimal(p1));
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Sets the value of the RequestTimeout option.
*
* @param _p1 The value of the RequestTimeout option.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "SetRequestTimeout", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "DECIMAL", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> setRequestTimeout(
final decimal _p1)
{
decimal p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "SetRequestTimeout", object.class,
new Block((Body) () ->
{
Assert.isZeroOrPositive(p1, new character("Request Timeout"));
setOption(new character("requestTimeout"), p1);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Sets the value of the PauseBetweenRetry option.
*
* @param _p1 The value of the PauseBetweenRetry option.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "SetRetryPause", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "DECIMAL", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> setRetryPause(
final decimal _p1)
{
decimal p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "SetRetryPause", object.class,
new Block((Body) () ->
{
Assert.isZeroOrPositive(p1, new character("Retry pause"));
setOption(new character("pauseBetweenRetry"), p1);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Override the default client library to be used with this http client.
*
* @param _p1 IHttpClientLibrary The library to be used
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "UsingLibrary", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "OBJECT", qualified = IHTTPCLIENTLIBRARY, mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> usingLibrary(
final object<? extends com.goldencode.p2j.oo.net.http.IHttpClientLibrary> _p1)
{
object<? extends com.goldencode.p2j.oo.net.http.IHttpClientLibrary> p1 = TypeFactory
.initInput(_p1);
return function(ClientBuilder.class, this, "UsingLibrary", object.class,
new Block((Body) () ->
{
Assert.notNull(p1, new character("Http library"));
setOption(new character(ObjectOps.getLegacyName(IHttpClientLibrary.class)), p1);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Override the default log writer to be used with this http client.
*
* @param _p1 The log writer to be used
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "UsingLogger", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "OBJECT", qualified = ILOGWRITER, mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> usingLogger(
final object<? extends com.goldencode.p2j.oo.logging.IlogWriter> _p1)
{
object<? extends com.goldencode.p2j.oo.logging.IlogWriter> p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "UsingLogger", object.class,
new Block((Body) () ->
{
Assert.notNull(p1, new character("Log writer"));
setOption(new character(ObjectOps.getLegacyName(IlogWriter.class)), p1);
returnNormal(ObjectOps.thisObject());
}));
}
/**
* Sets the URI to use as a proxy server for all requests.
*
* @param _p1 The proxy server URI.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "ViaProxy", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> viaProxy(
final character _p1)
{
character p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "ViaProxy", object.class, new Block((Body) () ->
{
Assert.notNullOrEmpty(p1, new character("Proxy server URI"));
returnNormal(viaProxy(com.goldencode.p2j.oo.net.Uri.parse(p1)));
}));
}
/**
* Sets the URI to use as a proxy server for all requests.
*
* @param _p1 The proxy server URI.
*
* @return This builder instance.
*/
@LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "ViaProxy", qualified = CLIENTBUILDER, parameters = {
@LegacyParameter(name = "p1", type = "OBJECT", qualified = NET_URI, mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends com.goldencode.p2j.oo.net.http.ClientBuilder> viaProxy(
final object<? extends com.goldencode.p2j.oo.net.Uri> _p1)
{
object<? extends com.goldencode.p2j.oo.net.Uri> p1 = TypeFactory.initInput(_p1);
return function(ClientBuilder.class, this, "ViaProxy", object.class, new Block((Body) () ->
{
Assert.notNull(p1, new character("Proxy server URI"));
setOption(new character(ObjectOps.getLegacyName(ISupportProxy.class)), p1);
returnNormal(ObjectOps.thisObject());
}));
}
}