StatusCodeHelper.java

/*
** Module   : StatusCodeHelper.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  20201218 Implement as of OE12.2.
** 003 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
**                  signature.
**     CA  20220923 Variable definitions (including associated with parameters) must be done always outside of 
**                  the BlockManager API
** 004 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 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.lang.*;

import static com.goldencode.p2j.report.ReportConstants.CVT_LVL_FULL;
import static com.goldencode.p2j.report.ReportConstants.RT_LVL_FULL;
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/StatusCodeHelper.cls).
 */
@LegacyResource(resource = "OpenEdge.Net.HTTP.StatusCodeHelper")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public class StatusCodeHelper
extends BaseObject
{
   @LegacySignature(type = Type.EXECUTE)
   public void __net_http_StatusCodeHelper_execute__()
   {
      onBlockLevel(Condition.ERROR, Action.THROW);
   }

   @LegacySignature(type = Type.CONSTRUCTOR)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public void __net_http_StatusCodeHelper_constructor__()
   {
      internalProcedure(StatusCodeHelper.class, this, "__net_http_StatusCodeHelper_constructor__", new Block((Body) () -> 
      {
         __lang_BaseObject_constructor__();
      }));
   }
   
   @LegacySignature(type = Type.CONSTRUCTOR)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public static void __net_http_StatusCodeHelper_constructor__static__()
   {
      externalProcedure(StatusCodeHelper.class, new Block((Body) () ->
      {
         onBlockLevel(Condition.ERROR, Action.THROW);
      }));
   }

   @LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetMessage", parameters = 
   {
      @LegacyParameter(name = "piStatusCode", type = "INTEGER", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public static character getMessage(final integer _piStatusCode)
   {
      integer piStatusCode = TypeFactory.initInput(_piStatusCode);
      
      return function(StatusCodeHelper.class, "GetMessage", character.class, new Block((Body) () -> 
      {
         
         if (piStatusCode.isUnknown() || piStatusCode.intValue() < 0)
         {
            returnNormal(StatusCodeEnum.unknown.ref().toLegacyString());
         }
         
         switch(piStatusCode.intValue())
         {
            case(0):
               returnNormal(StatusCodeEnum.none.ref().toLegacyString());
               break;
            case(101):
               returnNormal(new character("Switching Protocols"));
               break;
            case(102):
               returnNormal(new character("Processing"));
               break;
            case(200):
               returnNormal(new character("OK"));
               break;
            case(201):
               returnNormal(new character("Created"));
               break;
            case(202):
               returnNormal(new character("Accepted"));
               break;
            case(203):
               returnNormal(new character("Non Authoritative Information"));
               break;
            case(204):
               returnNormal(new character("No Content"));
               break;
            case(205):
               returnNormal(new character("Reset Content"));
               break;
            case(206):
               returnNormal(new character("Partial Content"));
               break;
            case(207):
               returnNormal(new character("Multi-Status"));
               break;
            case(208):
               returnNormal(new character("Already Reported"));
               break;
            case(226):
               returnNormal(new character("IM Used"));
               break;
            case(300):
               returnNormal(new character("Multiple Choices"));
               break;
            case(301):
               returnNormal(new character("Moved Permanently"));
               break;
            case(302):
               returnNormal(new character("Found"));
               break;
            case(303):
               returnNormal(new character("See Other"));
               break;
            case(304):
               returnNormal(new character("Not Modified"));
               break;
            case(305):
               returnNormal(new character("Use Proxy"));
               break;
            case(307):
               returnNormal(new character("Temporary Redirect"));
               break;
            case(308):
               returnNormal(new character("Permanent Redirect"));
               break;
            case(400):
               returnNormal(new character("Bad Request"));
               break;
            case(401):
               returnNormal(new character("Unauthorized"));
               break;
            case(402):
               returnNormal(new character("Payment Required"));
               break;
            case(403):
               returnNormal(new character("Forbidden"));
               break;
            case(404):
               returnNormal(new character("Not Found"));
               break;
            case(405):
               returnNormal(new character("Method Not Allowed"));
               break;
            case(406):
               returnNormal(new character("Not Acceptable"));
               break;
            case(407):
               returnNormal(new character("Proxy Authentication Required"));
               break;
            case(408):
               returnNormal(new character("Request Timeout"));
               break;
            case(409):
               returnNormal(new character("Conflict"));
               break;
            case(410):
               returnNormal(new character("Gone"));
               break;
            case(411):
               returnNormal(new character("Length Required"));
               break;
            case(412):
               returnNormal(new character("Precondition Failed"));
               break;
            case(413):
               returnNormal(new character("Request Entity Too Large"));
               break;
            case(414):
               returnNormal(new character("Request URI Too Large"));
               break;
            case(415):
               returnNormal(new character("Unsupported Media Type"));
               break;
            case(416):
               returnNormal(new character("Requested Range Not Satisfiable"));
               break;
            case(417):
               returnNormal(new character("Expectation Failed"));
               break;
            case(421):
               returnNormal(new character("Misdirected Request"));
               break;
            case(422):
               returnNormal(new character("Unprocessable Entity"));
               break;
            case(423):
               returnNormal(new character("Locked"));
               break;
            case(424):
               returnNormal(new character("Failed Dependency"));
               break;
            case(426):
               returnNormal(new character("Upgrade Required"));
               break;
            case(428):
               returnNormal(new character("Precondition Required"));
               break;
            case(429):
               returnNormal(new character("Too Many Requests"));
               break;
            case(431):
               returnNormal(new character("Request Header Fields Too Large"));
               break;
            case(500):
               returnNormal(new character("Internal Server Error"));
               break;
            case(501):
               returnNormal(new character("Not Implemented"));
               break;
            case(502):
               returnNormal(new character("Bad Gateway"));
               break;
            case(503):
               returnNormal(new character("Service Unavailable"));
               break;
            case(504):
               returnNormal(new character("Gateway Timeout"));
               break;
            case(505):
               returnNormal(new character("HTTP Version Not Supported"));
               break;
            case(506):
               returnNormal(new character("Variant Also Negotiates"));
               break;
            case(507):
               returnNormal(new character("Insufficient Storage"));
               break;
            case(508):
               returnNormal(new character("Loop Detected"));
               break;
            case(510):
               returnNormal(new character("Not Extended"));
               break;
            case(511):
               returnNormal(new character("Network AuthenticationRequired"));
               break;   
            default:
               returnNormal(StatusCodeEnum.unknown.ref().toLegacyString());
         } 
      }));
   }

   @LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetMessage", parameters = 
   {
      @LegacyParameter(name = "poStatusCode", type = "OBJECT", qualified = "openedge.net.http.statuscodeenum", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public static character getMessage(final object<? extends com.goldencode.p2j.oo.net.http.StatusCodeEnum> _poStatusCode)
   {
      object<? extends com.goldencode.p2j.oo.net.http.StatusCodeEnum> poStatusCode = TypeFactory.initInput(_poStatusCode);
      
      return function(StatusCodeHelper.class, "GetMessage", character.class, new Block((Body) () -> 
      {
         if (!poStatusCode._isValid())
         {
            returnNormal(StatusCodeEnum.unknown.ref().toLegacyString());
         }
         
         returnNormal(getMessage(new integer(poStatusCode.ref().getValue())));
         
      }));
   }

   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "GetCode", qualified = "openedge.net.http.statuscodeenum", parameters = 
   {
      @LegacyParameter(name = "pcStatusMessage", type = "CHARACTER", mode = "INPUT")
   })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
   public static object<? extends com.goldencode.p2j.oo.net.http.StatusCodeEnum> getCode(final character _pcStatusMessage)
   {
      character pcStatusMessage = TypeFactory.initInput(_pcStatusMessage);
      character enumValues = TypeFactory.character();
      
      return function(StatusCodeHelper.class, "GetCode", object.class, new Block((Body) () -> 
      {
         Assert.notNull(pcStatusMessage, new character("Status message"));
                  
         if (TextOps.isEmpty(pcStatusMessage))
         {
            returnNormal(StatusCodeEnum.none);
         }
         
         enumValues.assign(StatusCodeEnum.getEnumNames(StatusCodeEnum.class));
         
         if (TextOps.lookup(TextOps.toLowerCase(pcStatusMessage), TextOps.toLowerCase(enumValues)).intValue() == 0)
         {
            returnNormal(StatusCodeEnum.unknown);
         }
         
         returnNormal(StatusCodeEnum.getEnum(pcStatusMessage));

      }));
   }
}