The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: Hiding java exceptions from clients. (JAX-WS/SOAP)

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 4 - Last Post: May 15, 2008 5:36 AM by: uprooter
uprooter

Posts: 25
Hiding java exceptions from clients. (JAX-WS/SOAP)
Posted: May 11, 2008 9:27 AM
  Click to reply to this thread Reply

Hi.
I have a simple web service using jax-ws 2.1 on top of tomcat-6.
When I have some kind of java exceptions (like database connection error, missing file, IO error) java throws an exception and pass it to the SOAP client.
I'd like to hide my internal errors from the SOAP client and provide other generic exception that would just say something like "internal error" and nothing more. (but still have that exception details in my log files).
I can wrap each web method I have with try/catch but I'm looking for a generic solution that would catch exceptions in all of the web methods I've got and also catch all kinds of exceptions.

Any idea?

jamesw

Posts: 26
Re: Hiding java exceptions from clients. (JAX-WS/SOAP)
Posted: May 13, 2008 12:24 PM   in response to: uprooter
  Click to reply to this thread Reply

One idea is to use the approach I've incorporated with my Web Service. On the server side you split the implementation across two classes. The first class is the one JAX-WS calls when dispatching a web service method and the second one contains the real implementation code. The first class delegates the web service call to the second through a Java proxy that you write. Since all the method calls go through the single invoke method on the proxy, this provides one place to trap exceptions and convert them to any SOAP fault that you wish.

uprooter

Posts: 25
Re: Hiding java exceptions from clients. (JAX-WS/SOAP)
Posted: May 14, 2008 2:16 AM   in response to: jamesw
  Click to reply to this thread Reply

Hi jamesw.
I understand the idea of splitting the implementation. but I don't know enough about java proxy and class delegations.
Can you please post an example of your web service ? It would be very helpful an focus me on what I have to learn.
Thanks.

jamesw

Posts: 26
Re: Hiding java exceptions from clients. (JAX-WS/SOAP)
Posted: May 14, 2008 12:52 PM   in response to: uprooter
  Click to reply to this thread Reply

Proxies can be found in the java.lang.reflect package and you can read more about them in the JDK javadocs.

To illustrate how all this fits together, we have an interface called ServicePort that models the Web Service. It only has one method called service. The class PortImpl is the one you supply to JAX-WS for the Web Service implementation class. It contains an annotated method, init, that JAX-WS calls after it creates an instance of PortImpl. Within this method we set up a delegate that is a proxy to our real implementation class called ServiceImpl. The tricky part is in the proxy class called ServiceProxy that implements the InvocationHandler interface. All method calls go through the invoke method and inside it we look up the corresponding method on the implementation class and call the implementation. Any exceptions that come back are wrapped in the InvocationTargetException and you can unwrap them and handle them however you like. Note that any checked exceptions thrown by the proxy need to match a declared exception in the ServicePort interface.


interface ServicePort {
void service();
}

class PortImpl {

ServicePort delegate;

@PostConstruct
private void init(){

ServiceImpl impl = new ServiceImpl();
ServiceProxy proxy = new ServiceProxy(impl);
delegate = (ServicePort)Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class[] {ServicePort.class},proxy);
}

//for all the web service methods, call the proxy to the implementation
public void service(){
delegate.service();
}

}

class ServiceImpl {

public void service(){
//real implementation here
}
}

class ServiceProxy implements InvocationHandler {

ServiceImpl impl;

public ServiceProxy(ServiceImpl impl){
this.impl = impl;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try{
Method implMethod = ServiceImpl.class.getMethod(
method.getName(),method.getParameterTypes());

implMethod.invoke(impl,args);
}
catch(InvocationTargetException e){
Throwable realException = e.getCause();

//handle realException however you want
}

}

}


uprooter

Posts: 25
Re: Hiding java exceptions from clients. (JAX-WS/SOAP)
Posted: May 15, 2008 5:36 AM   in response to: jamesw
  Click to reply to this thread Reply

I'll get to it soon.

Many thanks !!




 XML java.net RSS