Připomeňme si, že jsme vytvořili WeHandler webh1 nad transportní vrstvou WEB reprezentovaný třídou OOABL webh1Handler.cls a pracující s daty z business entity customerBE.
V tomto článku si ukážeme, jak WebHandler pracuje se dvěma business entitami na základě zadaných vstupních parametrů.
Podobně jako v předchozím příkladu vytvoříme projekt webh2, typ serveru PASOE a transportní vrstva WEB. Projekt je připojen k databázi sports2000. Pokud nevíte, jak na to, podívejte se na prosím na první článek.
Entitu customerBE pro tabulku Customer jsme vytvořili v předchozím příkladu. Nyní analogicky vytvoříme business entitu orderBE pro tabulku Order. Tím jsou vytvořeny dvě třídy reprezentované soubory customerBE.cls a orderBE.cls a také dva .i soubory, customerBE.i a orderBE.i.
Nyní klikneme pravým tlačítkem myši na název služby webh2Service, vybereme možnost Edit a tlačítko Add.
Do položky Resource URI zadáme /Customer a opakovaným postupem přidáme URI /Orders. Výsledek bude vypadat takto.
Vidíme, že k implicitnímu URI webh2 byla přidána dvě další, Customer a Orders. Tato URI budou vstupními parametry třídy. Nyní budeme modifikovat soubor (třídu) webh2Handler.cls. Metodu HandleGet upravíme tak, aby vyhodnocovala vstupní parametr - URI a podle toho zpracovala požadavek. Nezapomeňte do souboru vložit dva soubory include, customerbe.i a orderbe.i. Obsahují definice struktur (např. dataset), které jsou nutné pro volání metod těchto objektů.
USING Progress.Lang.*. USING OpenEdge.Web.WebResponseWriter. USING OpenEdge.Net.HTTP.StatusCodeEnum. USING OpenEdge.Web.WebHandler. USING Progress.Json.ObjectModel.*. USING customerBE.*. USING orderBE.*. BLOCK-LEVEL ON ERROR UNDO, THROW. CLASS webh2Handler INHERITS WebHandler: {"customerbe.i"} {"orderbe.i"} /*------------------------------------------------------------------------------ Purpose: Handler for unsupported methods. The request being serviced and an optional status code is returned. A zero or null value means this method will deal with all errors. Notes: ------------------------------------------------------------------------------*/ METHOD OVERRIDE PROTECTED INTEGER HandleNotAllowedMethod( INPUT poRequest AS OpenEdge.Web.IWebRequest ): /* Throwing an error from this method results in a 500/Internal Server Error response. The web handler will attempt to log this exception. See the HandleGet method's comments on choosing a value to return from this method. */ UNDO, THROW NEW Progress.Lang.AppError("METHOD NOT IMPLEMENTED"). END METHOD. /*------------------------------------------------------------------------------ Purpose: Handler for unknown methods. The request being serviced and an optional status code is returned. A zero or null value means this method will deal with all errors. Notes: ------------------------------------------------------------------------------*/ METHOD OVERRIDE PROTECTED INTEGER HandleNotImplemented( INPUT poRequest AS OpenEdge.Web.IWebRequest ): /* Throwing an error from this method results in a 500/Internal Server Error response. The web handler will attempt to log this exception. See the HandleGet method's comments on choosing a value to return from this method. */ UNDO, THROW NEW Progress.Lang.AppError("METHOD NOT IMPLEMENTED"). END METHOD. /*------------------------------------------------------------------------------ Purpose: Default handler for the HTTP GET method. The request being serviced and an optional status code is returned. A zero or null value means this method will deal with all errors. Notes: ------------------------------------------------------------------------------*/ METHOD OVERRIDE PROTECTED INTEGER HandleGet( INPUT poRequest AS OpenEdge.Web.IWebRequest ): DEFINE VARIABLE oResponse AS OpenEdge.Net.HTTP.IHttpResponse NO-UNDO. DEFINE VARIABLE oWriter AS OpenEdge.Web.WebResponseWriter NO-UNDO. DEFINE VARIABLE oBody AS OpenEdge.Core.String NO-UNDO. DEFINE VARIABLE jsonObj AS JsonObject NO-UNDO. DEFINE VARIABLE lcJSON AS LONGCHAR NO-UNDO. ASSIGN oResponse = NEW OpenEdge.Web.WebResponse() oResponse:StatusCode = INTEGER(StatusCodeEnum:OK). jsonObj = NEW JsonObject(). IF ENTRY(2,poRequest:PathInfo,"/") = "Customer" THEN DO: DEFINE VARIABLE beCustomer AS customerBE NO-UNDO. DEFINE VARIABLE hTTCustomer AS HANDLE NO-UNDO. beCustomer = NEW customerBE(). beCustomer:ReadcustomerBE("",OUTPUT DATASET dsCustomer). hTTCustomer = TEMP-TABLE ttCustomer:HANDLE. hTTCustomer:WRITE-JSON ("JsonObject",jsonObj). lcJSON= jsonObj:GetJsonText(). oBody = NEW OpenEdge.Core.String(lcJSON). ASSIGN oResponse:Entity = oBody oResponse:ContentType = 'application/json':u oResponse:ContentLength = oBody:Size. END. ELSE IF ENTRY(2,poRequest:PathInfo,"/") = "Orders" THEN DO: DEFINE VARIABLE beOrder AS orderBE NO-UNDO. DEFINE VARIABLE hTTOrder AS HANDLE NO-UNDO. beOrder = NEW orderBE(). beOrder:ReadOrderBE("",OUTPUT DATASET dsOrder). hTTOrder = TEMP-TABLE ttOrder:HANDLE. hTTOrder:WRITE-JSON ("JsonObject",jsonObj). lcJSON= jsonObj:GetJsonText(). oBody = NEW OpenEdge.Core.String(lcJSON). ASSIGN oResponse:Entity = oBody oResponse:ContentType = 'application/json':u oResponse:ContentLength = oBody:Size. END. ELSE DO: ASSIGN oBody = NEW OpenEdge.Core.String( 'Hello ' + '~r~n':u /*CRLF */ + 'This is the default message by th HandleGet in webh2Handler.' ). ASSIGN oResponse:Entity = oBody oResponse:ContentType = 'text/plain':u oResponse:ContentLength = oBody:Size. END. ASSIGN oWriter = NEW WebResponseWriter(oResponse). oWriter:Open(). oWriter:Close(). RETURN 0. END METHOD. END CLASS. |
Nyní nezbývá než spustit PASOE a handler otestovat. Nejprve otestujeme službu - URI Customer. Do prohlížeče zadáme https://localhost:8810/webh2/web/Customer.
Nyní zadáme https://localhost:8810/webh2/web/Orders.
A nakonec zadáme implicitní URI https://localhost:8810/webh2/web/webh2.
Konfiguraci Webhandleru, kterou jsme si v příkladu ukázali, můžeme dělat i jinými způsoby. Například přímou editací konfiguračního souboru PASOE.Tento postup je určen pouze pro zkušené administrátory.
Lepším způsobem je webové rozhraní nástroje OpenEdge Explorer. Vybereme Progress Application Server -> oepas1 -> ABL Application: oepas1 -> ABL WebApp: webh2 -> WEB Transport Configuration.
Autoři: Michal Džmuráň volně dle Piotr Tucholski.