HttpHeader.java
/*
** Module : HttpHeader.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 IAS 20190527 Added multipart support.
** 003 CA 20191024 Added method support levels and updated the class support level.
** 004 ME 20200413 Fix parameters and to string methods.
** ME 20201204 Fix strip quotes for value.
** ME 20210111 Fix toString (null header).
** 005 CA 20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
** signature.
** 006 ME 20210519 Fix NULL header lazy loading.
** ME 20210929 Fix function block for getNullHeader.
** 007 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.*;
import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.security.*;
import static com.goldencode.p2j.util.BlockManager.*;
import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.InternalEntry.Type;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.*;
/**
* Business logic (converted to Java from the 4GL source code
* in OpenEdge/Net/HTTP/HttpHeader.cls).
*/
@LegacyResource(resource = "OpenEdge.Net.HTTP.HttpHeader")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public class HttpHeader
extends BaseObject
{
private static final String QUOTE = "\"";
private static Pattern specialPattern = Pattern.compile(".*[\\s|\"|\\\\|;|(|)|<|>|@|,|=|:|\\/|\\[|\\]|?].*");
/** Header name. */
@LegacySignature(type = Type.PROPERTY, name = "Name")
protected character name = new character();
/** Header value. */
@LegacySignature(type = Type.PROPERTY, name = "Value")
protected character value = TypeFactory.character();
/** NULL header instance. */
@LegacySignature(type = Type.PROPERTY, name = "NullHeader")
protected static ContextLocal<object<? extends NullHeader>> NULL =
new ContextLocal<object<? extends NullHeader>>()
{
/**
* Initial value.
*/
@Override
protected object<? extends NullHeader> initialValue()
{
return TypeFactory.object(NullHeader.class);
}
};
/** Parameters' delimiter. */
@LegacySignature(type = Type.PROPERTY, name = "ParamDelimiter")
protected character paramDelimiter = TypeFactory.character();
/** Header parameters. */
private LinkedHashMap<character, character> params = new LinkedHashMap<character, character>();
/**
* Execute method.
*/
@LegacySignature(type = Type.EXECUTE)
public void __net_http_HttpHeader_execute__()
{
onBlockLevel(Condition.ERROR, Action.THROW);
}
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public static void __net_http_HttpHeader_constructor__static__()
{
externalProcedure(HttpHeader.class, new Block((Body) () ->
{
onBlockLevel(Condition.ERROR, Action.THROW);
}));
}
protected void __net_http_HttpHeader_constructor__()
{
internalProcedure(this, "__net_http_HttpHeader_constructor__", new Block((Body) () ->
{
__lang_BaseObject_constructor__();
}));
}
/**
* Constructor.
*
* @param _name - header name.
*/
@LegacySignature(type = Type.CONSTRUCTOR, parameters =
{
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __net_http_HttpHeader_constructor__(final character _name)
{
character name = TypeFactory.initInput(_name);
internalProcedure(this, "__net_http_HttpHeader_constructor__", new Block((Body) () ->
{
__net_http_HttpHeader_constructor__();
Assert.notNullOrEmpty(name, new character("Header name"));
this.name.assign(name);
}));
}
/**
* Constructor.
*
* @param _p1 header name.
* @param _p2 header value.
*/
@LegacySignature(type = Type.CONSTRUCTOR, parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "p2", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __net_http_HttpHeader_constructor__(final character _p1, final character _p2)
{
character p1 = TypeFactory.initInput(_p1);
character p2 = TypeFactory.initInput(_p2);
internalProcedure(this, "__net_http_HttpHeader_constructor__", new Block((Body) () ->
{
__net_http_HttpHeader_constructor__(p1);
setValue(p2);
}));
}
/**
* Get header name.
*
* @return header name.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "Name")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getName()
{
return function(this, "Name", character.class, new Block((Body) () ->
{
returnNormal(name);
}));
}
/**
* Set header name.
* @param _p1 header name.
*/
@LegacySignature(type = Type.SETTER, name = "Name", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void setName(character _p1)
{
character p1 = TypeFactory.initInput(_p1);
internalProcedure(this, "Name", new Block((Body) () ->
{
this.name.assign(p1);
}));
}
/**
* Get header value.
*
* @return header value.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "Value")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getValue()
{
return function(this, "Value", character.class, new Block((Body) () ->
{
returnNormal(getValue_1());
}));
}
/**
* Set header value.
*
* @param _p1 header value.
*/
@LegacySignature(type = Type.SETTER, name = "Value", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void setValue(character _p1)
{
character p1 = TypeFactory.initInput(_p1);
internalProcedure(this, "Value", new Block((Body) () ->
{
setValue_1(p1);
}));
}
/**
* Get parameters' delimiter.
*
* @return parameters' delimiter.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "ParamDelimiter")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getParamDelimiter()
{
return function(this, "ParamDelimiter", character.class, new Block((Body) () ->
{
returnNormal(paramDelimiter);
}));
}
/**
* Set parameters' delimiter.
* @param _p1 parameters' delimiter.
*/
@LegacySignature(type = Type.SETTER, name = "ParamDelimiter", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void setParamDelimiter(character _p1)
{
character p1 = TypeFactory.initInput(_p1);
internalProcedure(this, "ParamDelimiter", new Block((Body) () ->
{
Assert.notNull(p1, new character("Parameter delimiter"));
this.paramDelimiter.assign(p1);
}));
}
/**
* Get NULL header.
*
* @return NULL header.
*/
@LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.HTTP.HttpHeader", type = Type.GETTER, name = "NullHeader")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public static object<? extends HttpHeader> getNullHeader()
{
return function(HttpHeader.class, "NullHeader", object.class, new Block((Body) () ->
{
if (!NULL.get()._isValid())
{
NULL.get().assign(ObjectOps.newInstance(NullHeader.class));
}
returnNormal(NULL.get());
}));
}
@LegacySignature(type = Type.SETTER, name = "NullHeader", parameters =
{
@LegacyParameter(qualified = "OpenEdge.Net.HTTP.HttpHeader", name = "var", type = "OBJECT", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_STUB)
public static void setNullHeader(final object<? extends HttpHeader> _var)
{
ObjectOps.load(HttpHeader.class);
object<? extends HttpHeader> var = TypeFactory.initInput(_var);
internalProcedure(HttpHeader.class, "NullHeader", new Block((Body) () ->
{
NULL.get().assign(var);
}));
}
/**
* Removes any parameter records for this header.
*/
@LegacySignature(type = Type.METHOD, name = "ClearParams")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void clearParams()
{
internalProcedure(this, "ClearParams", new Block((Body)() ->
{
if (params != null)
params.clear();
}));
}
/**
* Extracts/separates any parameters from the header's value, and stores the 'clean' value.
*/
@LegacySignature(type = Type.METHOD, name = "ExtractParameters")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void extractParameters()
{
internalProcedure(this, "ExtractParameters", new Block((Body)() ->
{
Assert.notNullOrEmpty(this.paramDelimiter, new character("Param delimiter"));
this._extractParameters();
}));
}
private void _extractParameters () {
if (value.isUnknown() || paramDelimiter.getValue().trim().isEmpty())
return;
String[] params = TextOps.entries(value.getValue(), paramDelimiter.getValue());
// skip first entry, base value
for (int i = 1; i < params.length; i++)
{
String param = params[i].replaceFirst("^\\s+", "");
if (!param.isEmpty()) {
int p = param.indexOf('=');
if (p < 0)
{
param = param.trim();
setParameterValue(new character(param), new character(param));
}
else
{
setParameterValue(new character(param.substring(0, p).trim()), new character(param.substring(p + 1)));
}
}
}
}
/**
* Returns the unparameterised value for this header.
*
* @return the value.
*/
@LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetBaseValue")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getBaseValue()
{
return function(this, "GetBaseValue", character.class, new Block((Body)() ->
{
if (TextOps.isEmpty(paramDelimiter))
returnNormal(value);
returnNormal(TextOps.entry(1, value, paramDelimiter));
}));
}
/**
* Returns a parameter from a header value
*
* @param _name The name of the parameter value to retrieve
*
* @return The parameter value; blank/empty if not defined
*/
@LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetParameterValue", parameters =
{
@LegacyParameter(name = "name", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getParameterValue(final character _name)
{
character name = TypeFactory.initInput(_name);
return function(this, "GetParameterValue", character.class, new Block((Body)() ->
{
Assert.notNullOrEmpty(name, new character("Param name"));
if (params == null || params.isEmpty())
{
returnNormal(new character(""));
}
returnNormal(params.getOrDefault(name, new character("")));
}));
}
/**
* Returns the unparameterised value for this header.
*
* @return The value.
*/
@LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetParameterValues")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character getParameterValues()
{
return function(this, "GetParameterValues", character.class, new Block((Body)() ->
{
if (params == null || params.isEmpty() || paramDelimiter.getValue().trim().isEmpty())
{
returnNormal(new character(""));
}
String d = this.paramDelimiter.getValue();
String ret = params.entrySet().stream().
map(p -> String.format(" %s=%s", p.getKey().getValue(),
quote(p.getValue().getValue()))).
collect(Collectors.joining(d));
returnNormal(new character(value.isUnknown() || value.getValue().trim().isEmpty() ? ret : d + ret));
}));
}
/**
* Indicates whether a parameter exists in the header.
*
* @param _p1 paramater name.
*
* @return <code>true</code> if the parameter exists.
*/
@LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "HasParameter", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public logical hasParameter(final character _p1)
{
character p1 = TypeFactory.initInput(_p1);
return function(this, "HasParameter", logical.class, new Block((Body)() ->
{
Assert.notNull(p1, new character("Param name"));
returnNormal(new logical(this.params.containsKey(p1)));
}));
}
/**
* Removes all parameters in this header with a name.
*
* @param _p1 the parameter name
*
* @return <code>true</code> if at least one parameter was removed.
*/
@LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "RemoveParameters", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public logical removeParameters(final character _p1)
{
character name = TypeFactory.initInput(_p1);
return function(this, "RemoveParameters", logical.class, new Block((Body)() ->
{
Assert.notNullOrEmpty(name, new character("Param name"));
returnNormal(new logical(params.remove(name) != null));
}));
}
/**
* Set the header parameter value.
*
* @param _p1 parameter name.
* @param _p2 paramater value.
*
* @return <code>true</code> if the value was replaced.
*/
@LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetParameterValue", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "p2", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public logical setParameterValue(final character _p1, final character _p2)
{
character name = TypeFactory.initInput(_p1);
character val = TypeFactory.initInput(_p2);
return function(this, "SetParameterValue", logical.class, new Block((Body)() ->
{
Assert.notNullOrEmpty(this.paramDelimiter, new character("Param delimiter"));
Assert.notNullOrEmpty(name, new character("Param name"));
Assert.notNull(val, new character("Param value"));
returnNormal(new logical(params.put(name, val) != null));
}));
}
/**
* Overridable mutator method
*
* @param _p1 the header value.
*/
@LegacySignature(type = Type.METHOD, name = "SetValue", parameters = {
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
protected void setValue_1(final character _p1)
{
character p1 = TypeFactory.initInput(_p1);
internalProcedure(this, "SetValue", new Block((Body) () -> {
validate(p1);
if (p1.isUnknown())
{
this.value.setUnknown();
}
else
{
if (!TextOps.isEmpty(paramDelimiter))
{
this.value.assign(p1);
_extractParameters();
}
else
{
String p = p1.getValue();
this.value.assign(p.length() > 1 && p.startsWith(QUOTE) && p.endsWith(QUOTE)
? p.substring(1, p.length() - 1)
: p);
}
}
}));
}
/**
* Overridable accessor method
*
* @return the header value.
*/
@LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetValue")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
protected character getValue_1()
{
return function(this, "GetValue", character.class, new Block((Body) () ->
{
returnNormal(value);
}));
}
/**
* Returns a stringified version of this header object.
*
* @return A stringified representation of this object.
*/
@LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "ToString")
@Override
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public character toLegacyString()
{
return function(this, "ToString", character.class, new Block((Body) () ->
{
returnNormal(TextOps.substitute("&1: &2&3", name, getBaseValue(), getParameterValues()));
}));
}
/**
* Validates a header value.
*
* @param _p1 a header value.
*/
@LegacySignature(type = Type.METHOD, name = "Validate", parameters =
{
@LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
protected void validate(final character _p1)
{
character p1 = TypeFactory.initInput(_p1);
internalProcedure(this, "Validate", new Block((Body)() ->
{
Assert.notNull(p1, new character("Header value"));
}));
}
/**
* Quite string
*
* @param s string to be quoted.
*
* @return quoted string.
*/
private String quote(String s)
{
if (s.startsWith(QUOTE) && s.endsWith(QUOTE) && s.length() > 1)
{
return s;
}
if (specialPattern.matcher(s).matches())
{
return QUOTE + s + QUOTE;
}
return s;
}
}