| Browse the App Server Filesystem via iScript |
|
| Tuesday, 02 January 2007 | |||||
|
Debugging that SQL-intensive page is easier when you can get to your trace files quickly. And sometimes it’s easier to look in the Process Scheduler config file to see where it’s pulling SQR’s from than to ask your over-worked DBA. But the reality is that you don’t always have access to the servers filesystems to pull this valuable information. Well no worries, here’s a technique to browse your Application Server’s filesystem using nothing but a little PeopleCode. It’s actually pretty easy to write a set of PeopleCode functions to display a directory structure and view a file. The functions you’ll need are all well documented in PeopleBooks, with sample code and everything. If you combine them into an iScript with a few parameters on the query line, you can create a nice application server browsing utility. Here’s a screen shot: ![]() It works pretty much like you’d expect. By default it will start in the LOGS directory ($PS_HOME/appserv/<domain>/LOGS). Clicking “..” takes you up one directory, clicking on a directory takes you into that directory, and clicking a file attempts to display it in the browser window. It doesn’t do any checking for file content before it sends it to your browser, so be careful what file you click on! Here’s the code that I used: /******** This iScript lists and views files in a directory ********/ Function IScript_ListDirectory() &Domain = "PS89FNSB"; %Response.WriteLine("<p align=""center""><b>" | "Directory Listing" | "</b></p>"); &PS_HOME = GetEnv("PS_HOME"); Local string &pwd; &pwd = %Request.GetParameter("curdir"); If None(&pwd) Then &pwd = &PS_HOME | "/appserv/" | &Domain; End-If; If Right(&pwd, 2) = ".." Then &pwd = Left(&pwd, Len(&pwd) - 3); For &i = Len(&pwd) To 1 Step - 1 If Right(&pwd, 1) <> "/" Then &pwd = Left(&pwd, Len(&pwd) - 1); Else &pwd = Left(&pwd, Len(&pwd) - 1); &i = 0; End-If; End-For; %Response.RedirectURL(EncodeURL(%Request.ContentURI | "/EMPLOYEE/EMPL/s/WEBLIB_BM_XX.USER1.FieldFormula.IScript_ListDirectory?&disconnect=y&type=public&curdir=" | &pwd)); End-If; Local array of string &FNAMES; Local File &MYFILE; Local string &CurrFile; &FNAMES = FindFiles(&pwd | "/*", %FilePath_Absolute); If &FNAMES.Len = 0 Then /* No files in this directory - this must be a file instead */ %Response.RedirectURL(EncodeURL(%Request.ContentURI | "/EMPLOYEE/EMPL/s/WEBLIB_BM_XX.USER1.FieldFormula.IScript_ViewFile?&disconnect=y&type=public&Filename=" | &pwd)); Else %Response.WriteLine("Directory: " | &pwd | "<br><br>"); While &FNAMES.Len > 0 &CurrFile = &FNAMES.Shift(); rem &FileURL = %Request.ContentURI | "/EMPLOYEE/EMPL/s/WEBLIB_BM_XX.USER1.FieldFormula.IScript_ViewFile?&disconnect=y&type=public&Filename=" | &CurrFile; &FileURL = %Request.ContentURI | "/EMPLOYEE/EMPL/s/WEBLIB_BM_XX.USER1.FieldFormula.IScript_ListDirectory?&disconnect=y&type=public&curdir=" | &CurrFile; %Response.WriteLine("<a href=""" | &FileURL | """> " | Substring(&CurrFile, Len(&pwd) + 2, Len(&CurrFile) - Len(&pwd) + 2) | "</a></br>"); End-While; End-If; End-Function; Function IScript_ViewFile() &Filename = %Request.GetParameter("FILENAME"); Local File &ServerFile; Local string &ln, &RelativePath; %Response.Write("<p align=""center""><b>" | &Filename | "</b></p>"); /* Note: To restrict access to a particular directory or subdirectory, set the environment variable PS_FILEDIR on your application server to point to that directory. Otherwise the user will be restricted to the PS_SERVDIR environment variable. To disable security altogether, comment out the relitive path logic and replace %FilePath_Relative in the GetFile method to %FilePath_Absolute */ rem Get the relative path name; If GetEnv("PS_FILEDIR") <> "" Then &RelativePath = GetEnv("PS_FILEDIR"); Else &RelativePath = GetEnv("PS_SERVDIR") | "/files"; End-If; If Left(&Filename, Len(&RelativePath)) = &RelativePath Then &Filename = Right(&Filename, Len(&Filename) - Len(&RelativePath)); End-If; rem Open the file; &ServerFile = GetFile(&Filename, "R", "A", %FilePath_Relative); %Response.Write("<p>"); While &ServerFile.ReadLine(&ln); %Response.WriteLine(&ln | "<br>"); End-While; %Response.WriteLine("</p>"); &ServerFile.Close(); End-Function; There are two basic functions: IScript_ListDirectory() and IScript_ViewFile. The ListDirectory function first sets some basic values. You’ll want to change the “DOMAIN” value to be your application server domain, so that it will be able to change to the default directory correctly. (You can get the domain from a CTRL-J in your browser). Then it takes the directory passed in the “curdir” query parameter (or the default directory) and sends them to the browser as a hyperlink back to this same ListDirectory function. If for some reason the first entry isn’t a “.”, this isn’t a directory so it must be a file and it redirects you to the ViewFile iScript with the filename passed as a query parameter. The ViewFile function uses the GetFile function to open the file passed on the URL in the Filename query parameter. It uses the %FilePath_Relative option to keep users from opening files above the directory you specify in the PS_FILEDIR or PS_SERVDIR environment variables to keep things somewhat secure. If security isn't an issue in your environment, you can follow the instructions in the comment to disable security. Once the file is open, the function reads it one line at a time and sends it to your browser, adding a <br> tag at the end of each line. Security Warning This code opens up some security issues. While you can restrict users to a top-level directory for viewing the contents of files, users can browse the directory structure of the entire system with the authority of the Application Server user ID. As a result, use some common sense before deploying this code. Make sure your systems administrator and management approves. Be sure that your app server user doesn't have access to view directories that you don't want them to view. Use the PS_FILEDIR environment variable to lock users down to a specific directory. Making it work Here’s how to put the code into a web library, set security, and hit it with a URL:
|
|||||
| Last Updated ( Saturday, 13 January 2007 ) | |||||
| < Prev | Next > |
|---|

