Friday, May 29, 2009

Directory Listing - A Simple WebLogic Application

In this article I will show how to create a simple WebLogic application that lets you navigate directories and files on a server. This can be one of the handiest utilities. I use it all the time to give developers access to logs in test and pre-production environments.


But first, a warning... Be careful what files you make available to others. Some files, especially within a WebLogic domain directory, contain information that MUST be kept secret if the domain is to remain secure. NEVER provide read-access to all files in a WebLogic domain directory.

Like any WebLogic application, you must create a WAR file with the following components inside:
  • WEB-INF (directory)
  • WEB-INF\web.xml
  • WEB-INF\weblogic.xml
The first step is to create a new directory called 'WEB-INF'.

The web.xml descriptor

Inside the WEB-INF directory, create a new file called web.xml, and paste the following XML code:


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>


I won't explain this XML code, but it is essentially an empty deployment descriptor.

The weblogic.xml descriptor

Create another file in the WEB-INF directory called weblogic.xml, and paste the following into it:


<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN" "http://www.bea.com/servers/wls810/dtd/we
blogic810-web-jar.dtd">

<weblogic-web-app>

<container-descriptor>
<index-directory-enabled>true</index-directory-enabled>
</container-descriptor>

<virtual-directory-mapping>
<local-path>../../../logs/</local-path>
<url-pattern>*</url-pattern>
</virtual-directory-mapping>

<context-root>/getlogs</context-root>

</weblogic-web-app>


XML Elements Explained

<index-directory-enabled> If true, WebLogic will display a list of directories and files. Unfortunately, you cannot filter this list. WebLogic does not provide much in the way of directory/file listing functionality - either you list them or don't.

<local-path> The path component of a URL where you will find the application. For example, the XML code above will make the application accessible at http://localhost:7001/getlogs/

<url-pattern> Specifies the local directory that will be displayed when a user navigates to the context-root.

<context-root> A pattern that URLs must match in order for them to be mapped to the local directory.

Sample Applications (update)

The sample apps from this article can be downloaded here. These sample applications are NOT SAFE for a production environment as they could potentially expose sensitive information.

Deployment Notes:
  • For WebLogic 8.1 or older, when deploying you must select "Copy this Web Application module onto every target for me".
  • For WebLogic 9 or newer, deploy with default options.

12 comments:

  1. Thanks, do you have like a sample war file which you can send me at ravi_tiwari_786@yahoo.com, i really need this kind of features for the dvelopers and the testing team.

    ReplyDelete
  2. Hi Ravi, the sample war files are now available from the links above.

    ReplyDelete
  3. It worked. I made the WAR file with WinRAR as a Zip, and just changed the extension to WAR. I had to deploy the application (Prepared), then start the deployment (Active). Thank you.

    ReplyDelete
  4. hello, I was looking for your sample war file and the link was not working at all. Will you be kind enough to send the sample war file to me chua.mykel@gmail.com

    Thanks.

    ReplyDelete
  5. Hello, I would like to do the same; publish log files to developers. My issue is, that the directory listing shows the files, but if I want to download files, which are not small, they are getting truncated.
    Is there a configuration, which has to be changed. Interesting is also, that the size seems to be different between broswers.

    Thanks

    ReplyDelete
  6. after deployment...i got 403 requesting getlogs. why?

    ReplyDelete
  7. hello, I was looking for your sample war file and the link was not working at all. Will you be kind enough to send the sample war file to me yu.randy@gmail.com

    Thanks.

    ReplyDelete
  8. hello, I was looking for your sample war file and the link was not working at all. Will you be kind enough to send the sample war file to me fongtsz@gmail.com

    ReplyDelete
  9. hello, I was as well looking for your sample war file and the link was not working, will you be so kind enough to send me the sample war file @ gmail addess srinivas.bnglr@gmail.com

    ReplyDelete
  10. Nice post, I have used the same approach to retrieve logs from a remote WLS, a war file can be retrieved from:
    https://github.com/devwebcl/wls_contraption

    ReplyDelete
  11. Thanks for the article. Its pretty simple

    Sample download link doesn't works. Anyways easy to create the 2 xml files.

    ReplyDelete
  12. Is there any way to change the file date modified format? for my it is listing as DD-MM-YYYY, but wanted to change to YYYY-MM-DD?

    ReplyDelete