HttpClient.java

/*
** Module   : HttpClient.java
** Abstract : Implementation of the builtin class.
**
** Copyright (c) 2019-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 IAS 20190923 First version.
** 002 IAS 20191108 Added HTTP Redirect support.
** 003 ME  20200410 Added setter method for logger.
** 004 MP  20200602 Added static method instance and static constructor
** 005 MP  20200611 Added missing methods as stub.
** 006 ME  20201103 Added default logger, cookies state management. 
**         20210128 Update assert messages, add user agent and retry support.
**     CA  20210221 Fixed parameters at the LegacySignature annotation.
** 007 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy signature.
** 008 ME  20210524 Added support for retries, destroy library and fix variable scope on execute.
**     CA  20220120 Do not used TypeFactory.object when the OO reference must not be tracked or registered.
**                  Do not use TypeFactory.object for internal usages, use ObjectVar if the reference must 
**                  be tracked.
**                  All TypeFactory.object variable definitions must be done outside of the top-level block.
**     CA  20220923 Variable definitions (including associated with parameters) must be done always outside of 
**                  the BlockManager API
** 009 CA  20230817 Removed ObjectOps.register, as this registration is handled by runtime.
** 010 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.logging.*;
import com.goldencode.p2j.oo.net.http.filter.writer.*;
import com.goldencode.p2j.ui.LogicalTerminal;
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.*;

/**
 * 
 * Public HttpClient. All client code should be written against this class.
 *
 */
@LegacyResource(resource = "OpenEdge.Net.HTTP.HttpClient")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_PARTIAL)
public class HttpClient
extends com.goldencode.p2j.oo.lang.BaseObject
implements IhttpClient, ISupportInitialize, ISupportLogging
{
   protected static final String ICLIENTLIBRARY = "openedge.net.http.ihttpclientlibrary";

   /** Client name. */
   @LegacySignature(type = Type.PROPERTY, name = "ClientName")
   private final character clientName = UndoableFactory.character();
   
   /** Client version. */
   @LegacySignature(type = Type.PROPERTY, name = "ClientVersion")
   private final character clientVersion = UndoableFactory.character();
   
   /** Client options. */
   @LegacySignature(type = Type.PROPERTY, name = "Options")
   private final object<? extends ClientOptions> options = 
         UndoableFactory.object(ClientOptions.class);
   
   /** Client library. */
   private final object<? extends IHttpClientLibrary> library = 
         UndoableFactory.object(IHttpClientLibrary.class);
   
   /** Logger **/
   @LegacySignature(type = Type.PROPERTY, name = "Logger")
   private final object<? extends IlogWriter> logger = TypeFactory.object(IlogWriter.class);
   
   /**
    * Destroy.
    */
   @LegacySignature(type = Type.METHOD, name = "Destroy")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   @Override
   public void destroy()
   {
      internalProcedure(HttpClient.class, this, "Destroy", new Block((Body) () -> 
      {
         if (ObjectOps.typeOf(library, ISupportInitialize.class).booleanValue())
            ObjectOps.cast(library, ISupportInitialize.class).ref().destroy();
      }));
   }

   /**
    * Initialize 
    */
   @LegacySignature(type = Type.METHOD, name = "Initialize")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void initialize()
   {
      internalProcedure(HttpClient.class, this, "Initialize", new Block((Body) () -> 
      {
         // no-op (oe12.2)
      }));
   }

   /**
    * Constructor.
    * 
    * @param _pcClientName client name.
    * @param _pcClientVersion client version.
    * @param _poLibrary client library.
    */
   @LegacySignature(type = Type.CONSTRUCTOR, parameters = 
   {
      @LegacyParameter(name = "pcClientName", type = "CHARACTER", mode = "INPUT"),
      @LegacyParameter(name = "pcClientVersion", type = "CHARACTER", mode = "INPUT"),
      @LegacyParameter(name = "poLibrary", type = "object", qualified = ICLIENTLIBRARY, mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void __net_http_HttpClient_constructor__(final character _pcClientName,
         final character _pcClientVersion,
         final object<? extends IHttpClientLibrary> _poLibrary)
   {
      character pcClientName = TypeFactory.initInput(_pcClientName);
      character pcClientVersion = TypeFactory.initInput(_pcClientVersion);
      object<? extends IHttpClientLibrary> poLibrary = TypeFactory.initInput(_poLibrary);

      internalProcedure(this, "__net_http_RequestBuilder_constructor__", new Block((Body) () -> 
      {
         __lang_BaseObject_constructor__();
         Assert.notNullOrEmpty(_pcClientName, new character("Client name"));
         Assert.notNullOrEmpty(_pcClientVersion, new character("Client version"));
         Assert.notNull(_poLibrary, new character("Http library"));
         this.clientName.assign(pcClientName);
         this.clientVersion.assign(pcClientVersion);
         this.library.assign(poLibrary);
         this.options.assign(ObjectOps.newInstance(ClientOptions.class));
      }));
   }
   
   @LegacySignature(type = Type.CONSTRUCTOR)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public static void __net_http_HttpClient_constructor__static__()
   {
      externalProcedure(HttpClient.class, new Block((Body) () ->
      {
         onBlockLevel(Condition.ERROR, Action.THROW);
      }));
   }
   
   @LegacySignature(type = Type.DESTRUCTOR)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_STUB)
   public void __net_http_HttpClient_destructor__()
   {
      internalProcedure(HttpClient.class, this, "__net_http_HttpClient_destructor__", new Block());
   }

   /**
    * Execute method.
    */
   @LegacySignature(type = Type.EXECUTE)
   public void __net_http_HttpClient_execute__()
   {
      onBlockLevel(Condition.ERROR, Action.THROW);
   }
   
   @LegacySignature(type = Type.METHOD, name = "AddUserAgent", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.net.http.ihttprequest", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_STUB)
   protected void addUserAgent(
            final object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> _p1)
   {
      object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> p1 = TypeFactory
               .initInput(_p1);

      internalProcedure(HttpClient.class, this, "AddUserAgent", new Block((Body) () -> {
         p1.ref().setHeader(HttpHeaderBuilder.build(new character("User-Agent")).ref()
                  .value(TextOps.substitute("&1/&2 (&3/&4) OpenEdge/&5", clientName,
                           clientVersion, EnvironmentOps.getOperatingSystem(),
                           EnvironmentOps.getProcessArchitecture(), EnvironmentOps.getVersion()))
                  .ref().getHeader());
      }));
   }   
   
   /**
    * Get logger.
    * 
    * @return logger.
    */
   @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Logging.ILogWriter", type = Type.GETTER, name = "Logger")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_PARTIAL)
   @Override
   public object<? extends IlogWriter> getLogger()
   {
      return function(this, "Logger", object.class, new Block((Body) () ->
      {
         if (!logger._isValid())
            logger.assign(ObjectOps.newInstance(VoidLogger.class));
         
         returnNormal(logger);
      }));
   }
   
   /**
    * Set logger.
    * 
    * @param _logger The logger instance to be used by the client.
    */
   @LegacySignature(type = Type.SETTER, name = "Logger", parameters = 
   {
      @LegacyParameter(name = "logger", type = "OBJECT", mode = "INPUT", qualified = "OpenEdge.Logging.ILogWriter")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   @Override
   public void setLogger(object<? extends IlogWriter> _logger)
   {
      object<? extends IlogWriter> logger = TypeFactory.initInput(_logger);
      
      internalProcedure(this, "Logger", new Block((Body) () ->
      {
         this.logger.assign(logger);
      }));

   }

   /**
    * Execute HTTP request.
    * 
    * @param _poRequest HTTP request.
    * 
    * @return HTTP Response.
    */
   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Execute", qualified = IHTTPRESPONSE, parameters = {
            @LegacyParameter(name = "poRequest", type = "OBJECT", qualified = IHTTPREQUEST, mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   @Override
   public object<? extends IhttpResponse> execute(object<? extends IhttpRequest> _poRequest)
   {
      object<? extends IhttpRequest> poRequest = TypeFactory.initInput(_poRequest);
      object<? extends IhttpResponse> poResponse = TypeFactory.object(IhttpResponse.class);

      return function(this, "execute", object.class, new Block((Body) () -> 
      {
         poResponse.assign(ResponseBuilder.build().ref().getResponse());
         execute(poRequest, poResponse);
         returnNormal(poResponse);
      }));
   }
   
   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Instance", qualified = "openedge.net.http.httpclient")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
   public static object<? extends com.goldencode.p2j.oo.net.http.HttpClient> instance()
   {
      object<? extends HttpClient> httpClient = TypeFactory.object(HttpClient.class);

      return function(HttpClient.class, "Instance", object.class, new Block((Body) () ->
      {
         httpClient.assign(ClientBuilder.build().ref().getClient());
         
         returnNormal(httpClient);
         
      }));
   }

   /**
    * Execute HTTP request.
    * 
    * @param _poRequest HTTP request.
    * @param _poResponse HTTP Response.
    */
   @LegacySignature(type = Type.METHOD, name = "Execute", parameters = 
   {
      @LegacyParameter(name = "poRequest", type = "OBJECT", qualified = IHTTPREQUEST, mode = "INPUT"),
      @LegacyParameter(name = "poResponse", type = "OBJECT", qualified = IHTTPRESPONSE, mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   @Override
   public void execute(object<? extends IhttpRequest> _poRequest, 
         object<? extends IhttpResponse> _poResponse)
   {
      object<? extends IhttpRequest> poRequest = TypeFactory.initInput(_poRequest);
      object<? extends IhttpResponse> poResponse = TypeFactory.initInput(_poResponse);
      object<? extends IAuthenticatedRequest> authRequest = TypeFactory.object(IAuthenticatedRequest.class);
      raw reqHash = TypeFactory.raw();

      internalProcedure(this, "execute", new Block((Body) () -> 
      {
         
         Assert.notNull(poRequest, new character("Http request"));
         Assert.notNull(poResponse, new character("Http response"));
         
         int retries = Math.max(options.ref().getNumRetries().intValue(), 0);
         int retry = 0;
         double retryPause = retries > 0 ? Math.max(options.ref().getPauseBetweenRetry().doubleValue(), 0) : 0;
         
         if (retries > 0)
            getLogger().ref().debug(TextOps.substitute("Initial request for &1 &2; max retries=&3", 
                     poRequest.ref().getMethod(), 
                     poRequest.ref().getUri().ref().toLegacyString(),
                     retries));
         
         do
         {
            if (retry > 0)
               getLogger().ref().debug(TextOps.substitute("Retry attempt &1 of &2 for &3 &4",
                        retry,
                        retries,
                        poRequest.ref().getMethod(), 
                        poRequest.ref().getUri().ref().toLegacyString()));
               
            addUserAgent(poRequest);
            
            _addAuthentication(poRequest, authRequest);
            
            reqHash.assign(poRequest.ref().getContentMd5());
            
            library.ref().execute(poRequest, poResponse);
            
            if (processStatusAction(poRequest, poResponse).booleanValue())
               break;
            
            if (retries == 0 || (retries > 0 && CompareOps._isEqual(reqHash, poRequest.ref().getContentMd5())))
            {
               retry++;
               
               if (retryPause > 0)
                  LogicalTerminal.pause(retryPause, (String) null);
            }
         }
         while(retry <= retries);
      }));
   }

   private void _addAuthentication(object<? extends IhttpRequest> _poRequest, object<? extends IAuthenticatedRequest> authRequest)
   {
      if (ObjectOps.typeOf(_poRequest, IAdaptable.class).booleanValue())
      {
         authRequest.assign(ObjectOps.cast(_poRequest, IAdaptable.class).ref()
                  .getAdapter(ObjectOps.getLegacyClass(IAuthenticatedRequest.class)));
      }
      
      if (!authRequest._isValid() && ObjectOps.typeOf(_poRequest, IAuthenticatedRequest.class).booleanValue())
      {
         authRequest.assign(ObjectOps.cast(_poRequest, IAuthenticatedRequest.class));
      }
      
      if (authRequest._isValid())
         authRequest.ref().addAuthentication();
      
   }
   
   /**
    * Process HttpResponse status code.
    * 
    * This basically handle redirects (301, 302, not 307 yet) and authorization required (401). 
    * 
    * @param poRequest HTTP request.
    * @param poResponse HTTP Response.
    */
   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "ProcessStatusAction", parameters = 
   {
      @LegacyParameter(name = "poRequest", type = "OBJECT", qualified = IHTTPREQUEST, mode = "INPUT"),
      @LegacyParameter(name = "poResponse", type = "OBJECT", qualified = IHTTPRESPONSE, mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   protected logical processStatusAction(object<? extends IhttpRequest> poRequest, 
         object<? extends IhttpResponse> poResponse)
   {
      object<? extends Cookie> oCookies[][] = new object[][] { TypeFactory.objectExtent(Cookie.class, false) };

      return function(this, "ProcessStatusAction", logical.class, new Block((Body) () -> 
      {
         object<? extends IHttpMessageWriter> statusCodeWriter = StatusCodeWriterBuilder.build(poRequest, poResponse);

         if (statusCodeWriter._isValid())
         {
            // maybe there is no point to retry if authorization is required but 
            // not an authenticated request, still seems not to be the case in 4GL
            // if (ObjectOps.typeOf(statusCodeWriter, AuthorizationStatusFilter.class).booleanValue() &&
            //         ObjectOps.typeOf(poRequest, AuthenticatedRequest.class).booleanValue())
            //   returnNormal(true);
            
            // copy session management info from initial response, if any
            _copyCookies(poResponse.ref(), poRequest.ref(), oCookies);
            
            statusCodeWriter.ref().open();
            statusCodeWriter.ref().write(poResponse);
            statusCodeWriter.ref().close();

            returnNormal(false);
         }
         
         returnNormal(true);
      }));
   }
   
   /**
    * Get client name.
    * 
    * @return client name.
    */
   @LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "ClientName")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   @Override
   public character getClientName()
   {
      return function(this, "ClientName", character.class, new Block((Body) () -> 
      {
         returnNormal(this.clientName);
      }));
   }


   /**
    * Get client version.
    * @return client version.
    */
   @LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "ClientVersion")
   @Override
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public character getClientVersion()
   {
      return function(this, "ClientVersion", character.class, new Block((Body) () -> 
      {
         returnNormal(this.clientVersion);
      }));
   }

   /**
    * Get client options.
    * 
    * @return client options.
    */
   @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.ClientOptions", type = Type.GETTER, name = "Options")
   @Override
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public object<? extends ClientOptions> getOptions()
   {
      return function(this, "Options", object.class, new Block((Body) () -> 
      {
         returnNormal(this.options);
      }));
   }
   
   /**
    * Helper method to copy/transfer cookies from one message to another (usually from response to request)
    * 
    * @param source the message to copy cookies from
    * @param target the message to copy cookies into
    */
   private void _copyCookies(IhttpMessage source, IhttpMessage target, object<? extends Cookie> oCookies[][])
   {
      if (source != null & target != null)
      {
         source.getCookies(new OutputExtentParameter<object<? extends Cookie>>()
         {

            @Override
            public object<? extends Cookie>[] getVariable()
            {
               return oCookies[0];
            }

            @Override
            public void setVariable(object<? extends Cookie>[] reference)
            {
               oCookies[0] = reference;
            }
         });

         // if there are any set those on request
         if (oCookies[0].length > 0)
            target.setCookies(oCookies[0]);
      }
   }
}