AuthorizationStatusFilter.java

/*
** Module   : AuthorizationStatusFilter.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 MP  20200611 Add missing methods as stubs taken by converting the skeleton using FWD.
**     CA  20210219 OO input parameter definitions for functions/procedures must not register with  ObjectOps 
**                  - TypeFactory.initInput will take care of this.
** 003 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy signature.
** 004 ME  20210331 Implement as per OE12.2.
**     VVT 20210917 Javadoc fixed, missing @Override annotations added.
**     CA  20220923 Variable definitions (including associated with parameters) must be done always outside of 
**                  the BlockManager API
** 005 CA  20230817 Removed ObjectOps.register, as this registration is handled by runtime.
** 006 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.filter.status;

import com.goldencode.p2j.oo.core.Assert;
import com.goldencode.p2j.oo.core.IAdaptable;
import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.oo.net.http.*;
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.*;

import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.BlockManager.externalProcedure;
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;

/**
 * 
 * Processes the 401/Unauthorized status code
 *
 */
@LegacyResource(resource = "OpenEdge.Net.HTTP.Filter.Status.AuthorizationStatusFilter")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public class AuthorizationStatusFilter
extends BaseObject
implements IHttpMessageWriter
{
   @LegacySignature(type = Type.PROPERTY, name = "Message")
   private object<? extends com.goldencode.p2j.oo.net.http.IhttpMessage> message =
      TypeFactory.object(com.goldencode.p2j.oo.net.http.IhttpMessage.class);

   /** Authorization challenge */
   private final character challenge = TypeFactory.character();
   
   /**
    * 
    */
   @LegacySignature(type = Type.EXECUTE)
   public void __net_http_filter_status_AuthorizationStatusFilter_execute__()
   {
      onBlockLevel(Condition.ERROR, Action.THROW);
   }
   
   @Override
   @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.IHttpMessage", type = Type.GETTER, name = "Message")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public object<? extends com.goldencode.p2j.oo.net.http.IhttpMessage> getMessage()
   {
      return function(AuthorizationStatusFilter.class, this, "Message", object.class, new Block((Body) () -> 
      {
         returnNormal(message);
      }));
   }
   
   /**
    * @param _poMessage
    */
   @LegacySignature(type = Type.CONSTRUCTOR, parameters = 
   {
      @LegacyParameter(name = "poMessage", type = "OBJECT", qualified = "openedge.net.http.ihttprequest", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void __net_http_filter_status_AuthorizationStatusFilter_constructor__(final object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> _poMessage)
   {
      object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> poMessage = TypeFactory.initInput(_poMessage);
      
      internalProcedure(AuthorizationStatusFilter.class, this, "__net_http_filter_status_AuthorizationStatusFilter_constructor__", new Block((Body) () -> 
      {
         __lang_BaseObject_constructor__();
         Assert.notNull(poMessage, new character("Http request"));
         this.message.assign(poMessage);
      }));
   }

   /** 
    * Opens the writer for output. Open/Close allows us to write the same message multiple
    * times without requiring a new writer each time. 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Open")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void open()
   {
      internalProcedure(AuthorizationStatusFilter.class, this, "Open", new Block((Body) () -> 
      {
         this.challenge.assign("");
      }));      
   }

   /** 
   * Write data to/from the message
   *   
   * @param _poData Data to write 
   */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Write", parameters = 
   {
         @LegacyParameter(name="poData", type = "OBJECT", 
               qualified = "Progress.Lang.Object", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void write(object<? extends _BaseObject_> _poData)
   {
      object<? extends _BaseObject_> poData = TypeFactory.initInput(_poData);
      
      internalProcedure(this, "Write", new Block((Body) () -> {
         Assert.isType(poData, ObjectOps.getLegacyClass(IhttpResponse.class));

         object<? extends IhttpResponse> response = ObjectOps.cast(poData, IhttpResponse.class);

         if (response.ref().hasHeader(new character("WWW-Authenticate")).booleanValue())
         {
            this.challenge.assign(response.ref().getHeader(new character("WWW-Authenticate")).ref().getValue());
         }
      }));     
   }

   /** 
    * Write data to/from the message 
    *
    * @param _pcData Data to write 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Write", parameters = 
   {
         @LegacyParameter(name="pcData", type = "LONGCHAR", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void write(longchar _pcData)
   {
      longchar pcData = TypeFactory.initInput(_pcData);
      
      internalProcedure(AuthorizationStatusFilter.class, this, "Write", new Block((Body) () -> 
      {
         Assert.notNullOrEmpty(pcData, new character("Challenge"));
         this.challenge.assign(pcData);
      }));     
   }

   /** 
    * Write data to/from the message
    *
    * @param _phData Data to write 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Write", parameters = 
   {
         @LegacyParameter(name="phData", type = "HANDLE", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void write(handle _phData)
   {
      // no-op    
   }

   /** 
    * Write data to/from the message
    *
    * @param _pcData Data to write 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Write", parameters = 
   {
         @LegacyParameter(name="pcData", type = "CHARACTER", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void write(character _pcData)
   {
      character pcData = TypeFactory.initInput(_pcData);
      
      internalProcedure(AuthorizationStatusFilter.class, this, "Write", new Block((Body) () -> 
      {
         Assert.notNullOrEmpty(pcData, new character("Challenge"));
         this.challenge.assign(pcData);
      }));      
   }

   /** 
    * Write data to/from the message 
    *
    * @param _pmData Data to write 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Write", parameters = 
   {
         @LegacyParameter(name="pmData", type = "MEMPTR", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void write(memptr _pmData)
   {
      memptr pmData = TypeFactory.initInput(_pmData);
      
      internalProcedure(AuthorizationStatusFilter.class, this, "Write", new Block((Body) () -> 
      {
         Assert.isPositive(pmData.length(), new character("Data size"));
         this.challenge.assign(pmData.getString(1));
      }));   
   }

   /**  
    * Flushes data to the output location. What data is flushed depends on the implementation 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Flush")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void flush()
   {
      object<? extends IAuthenticatedRequest> authReq = TypeFactory.object(IAuthenticatedRequest.class);
      
      internalProcedure(AuthorizationStatusFilter.class, this, "Flush", new Block((Body) () -> {

         if (challenge.isUnknown())
            challenge.assign("");

         character authMethod = TextOps.entry(1, challenge, " ");

         if (TextOps.isEmpty(authMethod))
            authMethod.assign(AuthenticationMethodEnum.none);

         if (ObjectOps.typeOf(this.message, IAdaptable.class).booleanValue())
         {
            authReq.assign(ObjectOps.cast(this.message, IAdaptable.class).ref()
                     .getAdapter(ObjectOps.getLegacyClass(IAuthenticatedRequest.class)));
         }
         
         if (!authReq._isValid())
         {
            if (!ObjectOps.typeOf(this.message, IAuthenticatedRequest.class).booleanValue()) 
            {
               this.message.assign(RequestBuilder.decorateRequest(ObjectOps.getLegacyClass(IAuthenticatedRequest.class), 
                        ObjectOps.cast(this.message, IhttpRequest.class)));
            }
            authReq.assign(ObjectOps.cast(this.message, IAuthenticatedRequest.class));
         }

         authReq.ref().setChallenge(authMethod, challenge);
      }));
   }

   /**  
    * Closes the output. See Open() 
    */
   @Override
   @LegacySignature(type = Type.METHOD, name = "Close")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void close()
   {
      internalProcedure(AuthorizationStatusFilter.class, this, "Close", new Block((Body) () -> 
      {
         flush();
      }));      
   }


}