HttpRequest.java
/*
** Module : HttpRequest.java
** Abstract : Implementation of the builtin class.
**
** Copyright (c) 2019-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 IAS 20190923 First version.
** 002 MP 20200707 Add code to setHost method
** 003 ME 20201111 Update support level, no-undo variables.
** 20201118 Fix cookies header update on cookies related operations.
** 004 CA 20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
** signature.
** 005 CA 20230215 Sanitized the LegacySignature parameters, so their type match their Java method definition.
** 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;
import static com.goldencode.p2j.util.BlockManager.*;
import static com.goldencode.p2j.report.ReportConstants.*;
import com.goldencode.p2j.oo.core.*;
import com.goldencode.p2j.oo.net.*;
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.*;
/**
*
* Contains an HTTP Request per RFC2616 and friends
*
*/
@LegacyResource(resource = "OpenEdge.Net.HTTP.HttpRequest")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public class HttpRequest
extends com.goldencode.p2j.oo.net.http.HttpMessage
implements com.goldencode.p2j.oo.net.http.IhttpRequest
{
protected static final String CHAR = "CHARACTER";
protected static final String OBJECT = "OBJECT";
protected static final String INPUT = "INPUT";
protected static final String NET_URI = "openedge.net.uri";
protected static final String NET_COOKIE = "openedge.net.cookie";
protected static final String NET_HTTP_METHOD_ENUM = "OpenEdge.Net.HTTP.MethodEnum";
/** Request method. */
private final character method = TypeFactory.character();
/** Request URI */
private final object<? extends Uri> uri = TypeFactory.object(Uri.class);
private character mcMethod = TypeFactory.character();
private object<? extends Uri> moUri = TypeFactory.object(Uri.class);
/**
* Get the HTTP method.
* @return the HTTP method.
*/
@LegacySignature(returns = "CHARACTER", type = Type.GETTER, name = "Method")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
@Override
public character getMethod()
{
return function(this, "Method", character.class, new Block((Body) () ->
{
returnNormal(method);
}));
}
/**
* Set the HTTP method.
* @param _method the HTTP method.
*/
@LegacySignature(type = Type.SETTER, name = "Method", parameters =
{
@LegacyParameter(name = "method", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
@Override
public void setMethod(character _method)
{
character method = TypeFactory.initInput(_method);
internalProcedure(this, "Method", new Block((Body) () ->
{
this.method.assign(method);
}));
}
/**
* Get the HTTP URI.
*
* @return the HTTP URI.
*/
@LegacySignature(returns = "OBJECT", qualified = "OpenEdge.Net.URI", type = Type.GETTER, name = "URI")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
@Override
public object<? extends Uri> getUri()
{
return function(this, "URI", object.class, new Block((Body) () ->
{
returnNormal(uri);
}));
}
/**
* Set the HTTP URI.
*
* @param _uri the HTTP URI.
*/
@LegacySignature(type = Type.SETTER, name = "URI", parameters =
{
@LegacyParameter(name = "uri", type = "OBJECT",
qualified = "OpenEdge.Net.URI", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
@Override
public void setUri(object<? extends Uri> _uri)
{
object<? extends Uri> uri = TypeFactory.initInput(_uri);
internalProcedure(this, "URI", new Block((Body) () ->
{
Assert.notNull(uri, new character("URI"));
this.uri.assign(uri);
setHost(uri);
}));
}
/**
* Removes all cookies from this request.
*/
@Override
@LegacySignature(type = Type.METHOD, name = "ClearCookies")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void clearCookies()
{
internalProcedure(HttpRequest.this, "ClearCookies", new Block((Body) () ->
{
super.clearCookies();
stripCookieHeader(null);
}));
}
/**
* Removes cookies from this message for a given name
*
* @param _pcName The name of the cookie(s) to remove.
*/
@Override
@LegacySignature(type = Type.METHOD, name = "RemoveCookie", parameters =
{
@LegacyParameter(name = "pcName", type = CHAR, mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void removeCookie(character _pcName)
{
character pcName = TypeFactory.initInput(_pcName);
internalProcedure(HttpRequest.this, "RemoveCookie", new Block((Body) () ->
{
boolean hasCookie = !pcName.isUnknown() && hasCookie(pcName).booleanValue();
super.removeCookie(pcName);
if (hasCookie)
{
stripCookieHeader(pcName);;
}
}));
}
/**
* Sets the Host header based on the request's URI.
*
* @param _poUri the request's URI.
*/
@LegacySignature(type = Type.METHOD, name = "SetHost", parameters =
{
@LegacyParameter(name = "poCookie", type = OBJECT, qualified = NET_URI, mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
protected void setHost(object<? extends Uri> _poUri)
{
object<? extends Uri> poUri = TypeFactory.initInput(_poUri);
internalProcedure(HttpRequest.this, "SetHost", new Block((Body) () ->
{
String cHost = "";
if (poUri._isValid())
{
cHost = poUri.ref().getHost().getValue();
if (poUri.ref().getPort().isUnknown())
{
cHost = cHost + "";
}
else
{
cHost = cHost + ":" + poUri.ref().getPort().intValue();
}
}
this.setHeader(HttpHeaderBuilder.build(new character("Host")).ref().value(new character(cHost)).ref().getHeader());
}));
}
/**
* Adds a cookie to this request.
*
* @param _poCookie The cookie to add.
*
*/
@Override
@LegacySignature(type = Type.METHOD, name = "SetCookie", parameters =
{
@LegacyParameter(qualified = "OpenEdge.Net.HTTP.Cookie", name = "poCookie", type = OBJECT, mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void setCookie(object<? extends Cookie> _poCookie)
{
object<? extends Cookie> poCookie = TypeFactory.initInput(_poCookie);
internalProcedure(HttpRequest.this, "SetCookie", new Block((Body) () ->
{
super.setCookie(poCookie);
character headerName = new character("Cookie");
if (hasHeader(headerName).booleanValue())
{
getHeader(headerName).ref().setParameterValue(poCookie.ref().getName(), poCookie.ref().getValue());
}
else
{
this.setHeader(HttpHeaderBuilder.build(headerName).ref()
.value(TextOps.substitute("&1=&2", poCookie.ref().getName(), poCookie.ref().getValue()))
.ref()
.parametersDelimitedBy(new character(";"))
.ref()
.getHeader());
}
}));
}
/**
* Initialize
*/
@Override
@LegacySignature(type = Type.METHOD, name = "Initialize")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void initialize()
{
internalProcedure(HttpRequest.this, "Initialize", new Block((Body) () ->
{
super.initialize();
if(moUri._isValid())
{
setUri(this.moUri);
this.method.assign(this.mcMethod);
this.moUri.setUnknown();
this.mcMethod.setUnknown();
}
}));
}
/**
* Execute method
*/
@LegacySignature(type = Type.EXECUTE)
public void __net_http_HttpRequest_execute__()
{
onBlockLevel(Condition.ERROR, Action.THROW);
}
/**
* Constructor.
*/
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __net_http_HttpRequest_constructor__()
{
internalProcedure(this, "__net_http_HttpRequest_constructor__", new Block((Body) () ->
{
}));
}
/**
* Constructor.
*
* @param _pcMethod request method.
* @param _poURI request URI.
*/
@LegacySignature(type = Type.CONSTRUCTOR, parameters =
{
@LegacyParameter(name = "pcMethod", type = CHAR, mode = INPUT),
@LegacyParameter(name = "poUri", type = OBJECT, qualified = NET_URI, mode = INPUT)
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __net_http_HttpRequest_constructor__(final character _pcMethod,
final object<? extends Uri> _poURI)
{
character pcMethod = TypeFactory.initInput(_pcMethod);
object<? extends com.goldencode.p2j.oo.net.Uri> poURI = TypeFactory.initInput(_poURI);
internalProcedure(this, "__net_http_HttpRequest_constructor__", new Block((Body) () ->
{
__net_http_HttpMessage_constructor__();
Assert.notNullOrEmpty(pcMethod, new character("Method"));
Assert.notNull(poURI, new character("URI"));
this.moUri.assign(poURI);
this.mcMethod.assign(pcMethod);
}));
}
/**
* Constructor.
*
* @param _poMethod request MethodEnum.
* @param _poURI request URI.
*/
@LegacySignature(type = Type.CONSTRUCTOR, parameters =
{
@LegacyParameter(name = "poMethod", type = OBJECT, qualified = NET_HTTP_METHOD_ENUM, mode = INPUT),
@LegacyParameter(name = "poUri", type = OBJECT, qualified = NET_URI, mode = INPUT)
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __net_http_HttpRequest_constructor__(final object<? extends MethodEnum> _poMethod,
final object<? extends Uri> _poURI)
{
object<? extends MethodEnum> poMethod = TypeFactory.initInput(_poMethod);
internalProcedure(this, "__net_http_HttpRequest_constructor__", new Block((Body) () ->
{
Assert.notNull(poMethod, new character("Method"));
this.__net_http_HttpRequest_constructor__(poMethod.ref().toLegacyString(), _poURI);
}));
}
private void stripCookieHeader (character pcName) {
character headerName = new character("Cookie");
if (pcName == null)
{
this.removeHeader(headerName);
}
else
{
object<? extends HttpHeader> header = this.getHeader(headerName);
if (header._isValid())
{
if (header.ref().hasParameter(pcName).booleanValue())
{
header.ref().removeParameters(pcName);
}
else
{
// must be header value
header.ref().setValue(new character(""));
}
}
}
}
}