PT_METADATA and Web Server Cache

A while back I presented a solution to manage configuration data on the web server. This solution involved a servlet-based cache managing utility that was responsible for the storage and caching of data to the PeopleSoft web server. I have been experimenting with ways that I can get custom data cached to the PeopleSoft web server without being so intrusive to the web-tier. I recently came across the PT_METADATA app package which can be used to create object types such as HTML, Style Sheet, and Images. These object types are nice because they are PeopleTools-managed and they have the flexibility to store custom data expressed in any form. Another great perk of these object types is that they can be cached to the web server using the %Response class. I am going to demonstrate a technique that I use to get PeopleTools to manage and cache my custom data.

This solution is going to involve using the PT_METADATA app package to create new image objects with PeopleCode.  For demonstration purposes, I created a custom page to gather the required fields to create an Image object.

Required_Fields

The four required fields to create an image object are the object name, object description, object file extension, and the content to store in the object. The object’s file extension is limited to three characters, but can be set to any three characters. This allows us to have non-image-type extensions such as htm, js, css, txt, dat, or log for example. Also, the data that can be stored in the image object can be of any form. We can store HTML, XML, or JSON for example.  Most importantly however, the type of data that an image object can hold is arbitrary. Image objects do not actually have to hold images/image data just like HTML objects don’t have to hold actual HTML. This is why these object types are so great because they are a Tools objects that have the flexibility to store custom data.

As you see in the picture above, I am storing some JSON data in an image object with a “dat” file extension. When I click the OK button, a small piece of PeopleCode fires to create a new image object and cache the object to the web server cache directory.

Create_Image_Object

/* Create Image Object */
import PT_METADATA:MetaDataAPI:ImageDefn;

Local string &sIMGObjName = PSM_WRK0.CONTNAME.Value;
Local string &sFileExtension = PSM_WRK0.EXTENSION.Value;
Local string &sIMGObjDescr = PSM_WRK0.DESCR.Value;
Local Field &fIMGObjContent = GetLevel0().GetRow(1).GetRecord(Record.PSM_WRK0).CONTDATA;

Local PT_METADATA:MetaDataAPI:ImageDefn &oIMGMetaData;
&oIMGMetaData = create PT_METADATA:MetaDataAPI:ImageDefn(&sIMGObjName);

If (&oIMGMetaData.Open()) Then
 &oIMGMetaData.SetContentData(&sFileExtension, &fIMGObjContent);
 &oIMGMetaData.Description = &sIMGObjDescr;
 If (&oIMGMetaData.Save()) Then
 MessageBox(0, "", 0, 0, "Saved");
 Else
 MessageBox(0, "", 0, 0, "Save failed");
 End-If;
Else
 Error MsgGetText(0, 0, "Open failed");
End-If;

/* Cache the file */
Local string &sIMGName = "IMAGE." | &sIMGObjName;
Local string &sCachedURL = %Response.GetImageURL(@&sIMGName);

/* Only show the non-versioned filename if ENABLENOVERSION is checked on the web profile */
Local string &sCachedURLNoVersion = Substring(&sCachedURL, 1, Find(&sIMGObjName, &sCachedURL) - 1) | &sIMGObjName | "." | &sFileExtension;
PSM_WRK0.HTMLAREA.Value = "<br><div class=""PSEDITBOXLABEL""><b>Cached URL: </b><a href=""" | &sCachedURL | """target=""_blank"">" | &sCachedURL | "</a></div>";
PSM_WRK0.HTMLAREA.Value = PSM_WRK0.HTMLAREA.Value | "<br><div class=""PSEDITBOXLABEL""><b>Cached URL (No Version): </b><a href=""" | &sCachedURLNoVersion | """target=""_blank"">" | &sCachedURLNoVersion | "</a></div>";

The code uses the ImageDefn class to create an image object based on the provided input values. After the image object is created, the GetImageURL method of the %Response class is invoked to get the newly created data stored to the web server’s cache directory. The cached URLs are presented to the user at this point.

Cache_URLs

The GetImageURL method will create two cached URLs for the data file.  The first one is the versioned one and the second is the non-versioned one.  Here is a look at the web server’s cache directory.

Cached_Files

The reason that there were two URLs that got generated is due to a setting on the web profile that allows for a non-versioned copy of cached Image and Style objects to be generated in the web server’s cached directory.  This setting can be toggled by checking the checkbox labeled “Copy Image/CSS (no Versioning)”under the caching tab of the web profile.

Enable_No_Version

What this setting does is it forces the web server to maintain two separate versions of cached image and css files. This allows for one version of the file to keep a static filename, while the other copy has a version number appended to the end of it. This is a great feature because the non-versioned version of the cached file is always reference-able since its filename stays the same, but its content will always be up to date. This, along with the file extension flexibility is the main reasons that I am using Image objects to house my custom data verses HTML or Style objects. Side Note: The “Copy Image/CSS (no Versioning)” functionality on the web profile did not work for me when I would cache Style objects. This is another reason that I am using Image objects with this solution.

So this is how one can go about generating data on the fly and immediately getting it cached to the web server.  I like this solution because it allows for the storage and caching to be managed completely by PeopleTools. One thing to keep in mind is the sensitivity of data that you are caching. I am using this solution to cache non-sensitive configuration data. I advise to not cache any sensitive data with this solution as the cache directory is available to any authenticated user (or non-authenticated user if the public user is enabled).

Leave a Reply

Your email address will not be published.