aspose file tools*
The moose likes BEA/Weblogic and the fly likes Application level post-processor for Weblogic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "Application level post-processor for Weblogic " Watch "Application level post-processor for Weblogic " New topic
Author

Application level post-processor for Weblogic

Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi All,
We have a third party application that runs in Weblogic 5.1 Whenever a request is made (from a client browser), we want to be able to grab the output produced by the application BEFORE it is sent out to the client. We will modify the output (parsing and inline includes) and then send it to the client. Is there any way to do this in Weblogic?
Thanks in advance,
Sharad


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
What you are looking for is Servlet Chaining. Unfortunately WebLogic never supported Servlet Chaining since it wasn't covered by the specification.
You will need to rethink your design. There is nothing that can be done with Filters that can't be done without them. It is just that Filters (and Servlet Chaining) are easier and more elegant solutions.
For example you could write all of your output to a StringBuffer (instead of directly to the response) and then pass that StringBuffer to your "post-processor" before writing it to the response.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi Chris,
Thanks for the response. I totally agree with you.
One problem I should have mentioned is that we do not own the third party application and cannot modify it's source code. I am now thinking along the lines of an http proxy or implementing my own 'filter'.
The proxy solution involved having another webserver listen on port 80. This server forwards requests to Weblogic. When Weblogic responds, the webserver intercepts and modifies the response before sending it to the client. Does anyone have comments on this? Performance issues (both Weblogic and a web server on the same box, web server's only purpose is to re-direct requests to Weblogic)? Suggestions for second web server (IIS, iPlanet, Weblogic 7.0, Apache)? I know that in IIS filters it is strongly discouraged to post-process.
My own filter would be to wrap the response before passing it to the main servlet. When the response comes back from the app, unwrap it, modify and send to client. I have been told that this can get to be messy.
Warm regards,
Sharad
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Instead of messing with IIS Filters, you could do everything within a Servlet using basically the same idea. You could write a Servlet that creates a URLConnection to your third-party application and retrieves the response. Take the response and modify its contents as necessary, then write it out to the HttpResponse.
I have had to resort to this once to integrate with a reporting tool and I know it is not the most elegant solution. It is however a solution that works and is both effective and portable between servers.
Good Luck.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi All/Chris,
I tried to follow the solution proposed by Chris. I wrote a servlet (say myServlet) which will receive all requests. This servlet opens a URL connection to the main application's entry servlet (say appEntryServlet).
The problem is: How will I set all the request headers? The servlet API does not seem to provide any way to get the complete Request Object. There is a way to get the complete Request Body (getReader() or getInputStream()), but the Request Headers will need to be extracted one by one. What that means is that I will have to consider all possible header properties, extract them and set them manually (using setRequestProperty() of URLConnection).
This seems to be a wasteful and error-prone strategy. Any ideas?
Warm regards,
Sharad
PS: I am not keen to go through the complete HTTP spec (http://www.ietf.org/rfc/rfc2068.txt) to find out all possible headers, there formats, handling, etc.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Why not just write a thin wrapper on top of the other servlet? In your wrapper's init method you could initialize the other servlet. In your wrapper's service method you could delegate to the other servlet's service method, after you do your necessary preprocessing of course. Instead of passing the actual HttpServletRequest and HttpServletResponse you could pass your own custom version.
Sorry I didn't mention this earlier, I was comparing your problem with one of my own. The main difference was that my third-party application was not Servlet-based so I needed to jump thru more hoops.
[ December 27, 2002: Message edited by: Chris Mathews ]
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi All/Chris,
Wrapping of HttpServletRequest and HttpServletResponse was the other solution I was considering. Folks in the weblogic servlets Usenet group have told me that it can be a messy solution. It seems that Weblogic tries to cast these objects to it's internal implementations; not being aware of the wrapping it gives a ClassCastException. I haven't tried it yet, so I am not sure why that would happen. Further, I am not particularly keen on implementing the whole interface. Note that I am stuck with Servlets 2.2 (WebLogic 5.1) and hence cannot use the classes HttpServletRequestWrapper and HttpServletResponseWrapper which are meant specifically for this kind of use (maybe I could just take the code from these classes and use them for my 'custom' wrappers).
Right now I have managed to implement the HttpURLConnection proxy. Most of the request header and body is being passed across as is. Some manipulation is being done to make the appEntryServlet get the session Id from the web client (rather than create a new one for my servlet). It seems to be working as expected.
Thanks for all the ideas. Any further thoughts/ideas are welcome.
Warm regards,
Sharad Agarwal
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by Sharad Agarwal:
It seems that Weblogic tries to cast these objects to it's internal implementations; not being aware of the wrapping it gives a ClassCastException.

I didn't know that.
I guess you could solve that problem by extending from WebLogic's implementation classes, if they are not marked final, and putting your classes on the System Classpath. Does seem like it would get messy and I would probably be inclined to keep with the approach you are currently taking.
The good news is, all of this is simple when your organization decides to upgrade to a newer version of WebLogic.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi All/Chris,
Originally posted by Chris Mathews:

The good news is, all of this is simple when your organization decides to upgrade to a newer version of WebLogic.

Yes sir, indeed! Its been some time since we have been trying to get the vendor to upgrade.
This brings us to my next problem/query.
Quick Question: How to parse and modify an HTML document?
Detailed Question: Now, I am able to get the Response Object in my proxy servlet and can modify it to my liking. I wanted to know the best way to parse an HTML document - I will need to both READ (find various elements and their values) and WRITE (insert HTML elements at various places into the original HTML). The first thing that comes to mind is to use an XML parser (Xerces); but since the generated page will not conform strictly to the spec, the parser will reject the document. Some search on the Weblogic site reveals Kona (http://www.weblogic.com/docs51/classdocs/API_html.html) - this seems to be of use when generating a fresh HTML page. I cannot find any constructors which will take an existing HTML as input.
I find javax.swing.text.html.parser.Parser to be a possible class. Another site I found was http://www.quiotix.com/downloads/html-parser/ I was wondering if there was a standard way to achieve this. Any ideas?
Warm regards,
Sharad
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Do you just need simple search and replace functionality? If so, then I suggest you take a look at Jakarta Regexp. This functionality is built into J2SE 1.4 but obviously you are stuck with J2SE 1.3.
[ December 30, 2002: Message edited by: Chris Mathews ]
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Simple search/replace will not be adequate. It is a bit more involved. For example, I need to be able to get the list of all TD elements that have their class attribute set to "classIntricate" (say). For these elements, I will need the outertext. The insert of data into the HTML will be after the above-said TD elements (perhaps another TD element).
But my requirements are not very intricate either - so I am a little reticent to research all the free packages available on the net.
I am experimenting with http://java.sun.com/j2se/1.3/docs/api/javax/swing/text/html/parser/Parser.html - will post my findings.
Thanks for your continued time and support. Suggestions/advice/pointers of any sort are welcome,
Sharad
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Hi,
Just wanted to quickly post an update. I narrowed down the possibilities to three:
  • Swing HTML Parser (http://java.sun.com/j2se/1.3/docs/api/javax/swing/text/html/parser/Parser.html)
  • JavaCC (http://www.quiotix.com/downloads/html-parser/)
  • Regular Expressions
  • [list=A]
  • Jakarta RegExp (http://jakarta.apache.org/regexp/index.html)
  • Jakarta ORO (http://jakarta.apache.org/oro/index.html)


  • [/list]
    Dropped the idea of using the Swing HTML Parser because I encountered an Exception in using this (which others on the net have found too - http://www.eos.dk/pipermail/advanced-swing/2001-March/000331.html).
    Decided not to use JavaCC - if something similar to the Swing HTML Parser exception was thrown, there would be no elegant way to handle it. Also, my requirements were not intricate enough to warrant a complete parse tree overhead.
    Between the two Regular Expression options, I am going with Jakarta ORO. I really don't know why. Perhaps I like the name better - it seemed more comprehensive and offers a bigger API.
    Great prescience Chris - I ended up using RE's as you had suggested!
    Warm regards,
    Sharad Agarwal
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Application level post-processor for Weblogic