Checking Document Existence in Platform Server via Java REST API

Hi,

is it just me or is it nearly impossible to check the existence of a document via the com.mgmtp.a12.services.client.document.rest.RestDocumentClient? I try to read a document via com.mgmtp.a12.services.client.document.DocumentClient#loadDocumentAsJson. But unfortunately I just get:

Caused by: java.lang.RuntimeException: null
	at com.mgmtp.a12.services.client.config.ExceptionHandler.fromResponse(ExceptionHandler.java:17) ~[rest-client-23.1.0.jar:23.1.0]
	at org.apache.cxf.jaxrs.client.ClientProxyImpl.checkResponse(ClientProxyImpl.java:313) ~[cxf-rt-rs-client-3.2.2.jar:3.2.2]
	at org.apache.cxf.jaxrs.client.ClientProxyImpl.handleResponse(ClientProxyImpl.java:875) ~[cxf-rt-rs-client-3.2.2.jar:3.2.2]
	at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:788) ~[cxf-rt-rs-client-3.2.2.jar:3.2.2]
	at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:235) ~[cxf-rt-rs-client-3.2.2.jar:3.2.2]
	at com.sun.proxy.$Proxy48.loadDocumentAsJson(Unknown Source) ~[na:na]
	at com.mgmtp.a12.services.client.document.rest.RestDocumentClient.loadDocumentAsJson(RestDocumentClient.java:113) ~[rest-client-23.1.0.jar:23.1.0]
	at com.mgmtp.a12.services.client.document.DocumentClient$loadDocumentAsJson.call(Unknown Source) ~[na:na]
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) [groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) [groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136) [groovy-2.4.15.jar:2.4.15]
	at com.mgmtp.cosmo.claims.contentloader.loaders.DemoContentLoader.uploadDocument(DemoContentLoader.groovy:57) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98) ~[groovy-2.4.15.jar:2.4.15]
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:352) ~[groovy-2.4.15.jar:2.4.15]
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034) ~[groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68) ~[groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) [groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157) [groovy-2.4.15.jar:2.4.15]
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185) [groovy-2.4.15.jar:2.4.15]

Digging deeper it shows that the server returned completely reasonably 404.

My expectation to the API was to get null or some ‘NoSuchDocument’ Exception or so. Did I configure something wrong?
Because catching the RuntimEception does cut it for now - but is so strongly wrong that I do not live with it for a long time.

Thanks

Marcus

Ok,

i was wrong. The url was configured wrongly (forgot the /api/ at the end) - even though would again be happy if the rest client would do this by itself so that i just have to provide the url to the Platform Server but anyway.

But the reaction is from my point of view only slightly better:

Caused by: java.lang.RuntimeException: Document [claim1] not found
	at com.mgmtp.a12.services.client.config.ExceptionHandler.fromResponse(ExceptionHandler.java:17) ~[rest-client-23.1.0.jar:23.1.0]
	at org.apache.cxf.jaxrs.client.ClientProxyImpl.checkResponse(ClientProxyImpl.java:313) ~[cxf-rt-rs-client-3.2.2.jar:3.2.2]

mitigation code is:

private boolean existsDocument(String modelName, String documentId) {
        try {
            client.loadDocumentAsJson(modelName, documentId); 
        } catch (RuntimeException e) {
            if (e.getMessage() == "Document [${documentId}] not found") {
                return false
            } else {
                throw e;
            }
        }
        return true;
    }

The error codes get lost in translation. All specialized exceptions end up as Runtime exceptions where the users of the implementation must check exception message itself. It is not a good approach and I have created a ticket for it <INTERNAL_LINK>

Excellent, thanks!
(Funnily enough I have to write 20 letters, so this text)