# ClientSocketResponseWriter.java

    /*
    ** Module   : ClientSocketResponseWriter.java
    ** Abstract : Implementation of the builtin class.
    **
    ** Copyright (c) 2020-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
    ** 001 MP  20200603 First version, stubs taken by converting the skeleton using FWD.
    ** 002 MP  20200707 Add code in methods to run like legacy class
    ** 003 CA  20210113 Renamed clear() to clear_(), to follow NameConverter's rules.
    **     CA  20210219 OO input parameter definitions for functions/procedures must not register with  ObjectOps
    **                  - TypeFactory.initInput will take care of this.
    ** 004 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy signature.
    **     CA  20210609 Updated INPUT/INPUT-OUTPUT parameters to the new approach, where they are explicitly
    **                  initialized at the method's execution, and not at the caller's arguments.
    ** 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.payload;

    import com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.BlockManager.Action;
    import com.goldencode.p2j.util.BlockManager.Condition;
    import com.goldencode.p2j.oo.core.Assert;
    import com.goldencode.p2j.oo.core.ByteBucket;
    import com.goldencode.p2j.oo.core.Memptr;
    import com.goldencode.p2j.oo.lang.*;
    import com.goldencode.p2j.oo.net.http.IhttpRequest;
    import com.goldencode.p2j.oo.net.serverconnection.SocketReadEventArgs;

    import static com.goldencode.p2j.report.ReportConstants.CVT_LVL_FULL;
    import static com.goldencode.p2j.report.ReportConstants.RT_LVL_STUB;
    import static com.goldencode.p2j.util.BlockManager.*;
    import static com.goldencode.p2j.util.InternalEntry.Type;

    /**
     * Business logic (converted to Java from the 4GL source code
     * in OpenEdge/Net/HTTP/Filter/Payload/ClientSocketResponseWriter.cls).
     */
    @LegacyResource(resource = "OpenEdge.Net.HTTP.Filter.Payload.ClientSocketResponseWriter")
    @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
    public class ClientSocketResponseWriter
    extends BaseObject
    implements com.goldencode.p2j.oo.net.http.IHttpMessageWriter
    {
       private object<? extends com.goldencode.p2j.oo.core.ByteBucket> moChunk =
                TypeFactory.object(com.goldencode.p2j.oo.core.ByteBucket.class);
       private boolean mlStatusLineRead;
       private character mcCurrentLine = TypeFactory.character();

       @LegacySignature(type = Type.PROPERTY, name = "MessageBody")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       private object<? extends com.goldencode.p2j.oo.core.ByteBucket> messageBody =
          TypeFactory.object(com.goldencode.p2j.oo.core.ByteBucket.class);

       @LegacySignature(type = Type.PROPERTY, name = "Response")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       private object<? extends com.goldencode.p2j.oo.net.http.IhttpResponse> response =
          TypeFactory.object(com.goldencode.p2j.oo.net.http.IhttpResponse.class);

       @LegacySignature(type = Type.PROPERTY, name = "Request")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       private object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> request =
          TypeFactory.object(com.goldencode.p2j.oo.net.http.IhttpRequest.class);

       @LegacySignature(type = Type.PROPERTY, name = "Message")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       private object<? extends com.goldencode.p2j.oo.net.http.IhttpMessage> message =
          TypeFactory.object(com.goldencode.p2j.oo.net.http.IhttpMessage.class);

       @LegacySignature(type = Type.EXECUTE)
       public void __net_http_filter_payload_ClientSocketResponseWriter_execute__()
       {
          onBlockLevel(Condition.ERROR, Action.THROW);
       }

       @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Core.ByteBucket", type = Type.GETTER, name = "MessageBody")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       protected object<? extends com.goldencode.p2j.oo.core.ByteBucket> getMessageBody()
       {
          return function(ClientSocketResponseWriter.class, this, "MessageBody", object.class, new Block((Body) () ->
          {
             returnNormal(messageBody);
          }));
       }

       @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.IHttpResponse", type = Type.GETTER, name = "Response")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       protected object<? extends com.goldencode.p2j.oo.net.http.IhttpResponse> getResponse()
       {
          return function(ClientSocketResponseWriter.class, this, "Response", object.class, new Block((Body) () ->
          {
             returnNormal(response);
          }));
       }

       @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.IHttpRequest", type = Type.GETTER, name = "Request")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       protected object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> getRequest()
       {
          return function(ClientSocketResponseWriter.class, this, "Request", object.class, new Block((Body) () ->
          {
             returnNormal(request);
          }));
       }

       @LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.IHttpMessage", type = Type.GETTER, name = "Message")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public object<? extends com.goldencode.p2j.oo.net.http.IhttpMessage> getMessage()
       {
          return function(ClientSocketResponseWriter.class, this, "Message", object.class, new Block((Body) () ->
          {
             returnNormal(this.response);
          }));
       }

       @LegacySignature(type = Type.CONSTRUCTOR, parameters =
       {
          @LegacyParameter(name = "poMessage", type = "OBJECT", qualified = "openedge.net.http.ihttpresponse", mode = "INPUT"),
          @LegacyParameter(name = "poMessageBody", type = "OBJECT", qualified = "openedge.core.bytebucket", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void __net_http_filter_payload_ClientSocketResponseWriter_constructor__(
                final object<? extends com.goldencode.p2j.oo.net.http.IhttpResponse> _poMessage,
                final object<? extends com.goldencode.p2j.oo.core.ByteBucket> _poMessageBody)
       {
          object<? extends com.goldencode.p2j.oo.net.http.IhttpResponse> poMessage = TypeFactory.initInput(_poMessage);
          object<? extends com.goldencode.p2j.oo.core.ByteBucket> poMessageBody = TypeFactory.initInput(_poMessageBody);

          internalProcedure(ClientSocketResponseWriter.class, this, "__net_http_filter_payload_ClientSocketResponseWriter_constructor__", new Block((Body) () ->
          {
             __lang_BaseObject_constructor__();
             Assert.notNull(poMessage, new character("Http Response"));
             Assert.notNull(poMessageBody, new character("Message body"));
             this.response.assign(poMessage);
             this.messageBody.assign(poMessageBody);
             this.moChunk.assign(ByteBucket.instance());
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Close")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void close()
       {
          internalProcedure(ClientSocketResponseWriter.class, this, "Close", new Block((Body) () ->
          {
             this.moChunk.ref().clear_();
             this.request.setUnknown();
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Open")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void open()
       {
          internalProcedure(ClientSocketResponseWriter.class, this, "Open", new Block((Body) () ->
          {
             this.moChunk.ref().clear_();
             this.messageBody.ref().clear_();
             this.mlStatusLineRead = false;
             this.mcCurrentLine.assign("");
          }));
       }

       @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_STUB)
       public void write(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _poData)
       {
          object<? extends com.goldencode.p2j.oo.lang._BaseObject_> poData = TypeFactory.initInput(_poData);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             Assert.notNull(poData);

             if (poData.ref().getLegacyClass().ref()._isA(ObjectOps.getLegacyClass(SocketReadEventArgs.class)))
             {
                this.write_2(ObjectOps.cast(poData, SocketReadEventArgs.class));
             }

             if (poData.ref().getLegacyClass().ref()._isA(ObjectOps.getLegacyClass(IhttpRequest.class)))
             {
                this.write_1(ObjectOps.cast(poData, IhttpRequest.class));
             }

          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "poRequest", type = "OBJECT", qualified = "openedge.net.http.ihttprequest", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       protected void write_1(final object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> _poRequest)
       {
          object<? extends com.goldencode.p2j.oo.net.http.IhttpRequest> poRequest = TypeFactory.initInput(_poRequest);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             Assert.notNull(poRequest, new character("Http request"));
             this.request.assign(poRequest);
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "poSocketArgs", type = "OBJECT", qualified = "openedge.net.serverconnection.socketreadeventargs", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       protected void write_2(final object<? extends com.goldencode.p2j.oo.net.serverconnection.SocketReadEventArgs> _poSocketArgs)
       {
          object<? extends com.goldencode.p2j.oo.net.serverconnection.SocketReadEventArgs> poSocketArgs = TypeFactory.initInput(_poSocketArgs);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             Assert.notNull(poSocketArgs, new character("Socket read argument"));

             this.statusLineWorkout(poSocketArgs.ref().getData(), poSocketArgs.ref().getBytesRead());

          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "pcData", type = "LONGCHAR", mode = "INPUT-OUTPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void write(final longchar _pcData)
       {
          longchar pcData = TypeFactory.initInputOutput(_pcData);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             //NO-OP
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "phData", type = "HANDLE", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void write(final handle _phData)
       {
          handle phData = TypeFactory.initInput(_phData);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             //NO-OP
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "pcData", type = "CHARACTER", mode = "INPUT-OUTPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void write(final character _pcData)
       {
          character pcData = TypeFactory.initInputOutput(_pcData);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             //NO-OP
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Write", parameters =
       {
          @LegacyParameter(name = "pmData", type = "MEMPTR", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void write(final memptr _pmData)
       {
          memptr pmData = TypeFactory.initInput(_pmData);

          internalProcedure(ClientSocketResponseWriter.class, this, "Write", new Block((Body) () ->
          {
             //NO-OP
          }));
       }

       @LegacySignature(type = Type.METHOD, name = "Flush")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
       public void flush()
       {
          internalProcedure(ClientSocketResponseWriter.class, this, "Flush", new Block((Body) () ->
          {
             //NO-OP
          }));
       }

       private void statusLineWorkout(object<? extends Memptr> data, int64 bytesRead)
       {
          int64 iBytesRead = bytesRead;
          int64 iStartByte = new int64(1);
          int64 iLoop = TypeFactory.int64();

          while(!mlStatusLineRead)
          {

          }

          character currentLine = TypeFactory.character();
          integer prevByte = TypeFactory.integer();
          integer currentByte = TypeFactory.integer();
          for (int i = 0; i < bytesRead.intValue(); i++)
          {

             currentByte.assign(data.ref().getByte(new int64(i + 1)));

             if((prevByte.intValue() == 13 && currentByte.intValue() == 10) || (i + 1) == bytesRead.intValue())
             {
                int gg = 6;
             }

             prevByte.assign(currentByte);

          }

       }
    }