| 1 | | See [http://delight.opendfki.de/wiki/ExampleUsingOwnConceteTypes#UsingowntypesinCollectionsandMaps How to use own typed in Collections an Maps]. |
| | 1 | == Using Third Party Types in your API == |
| | 2 | |
| | 3 | Eventually, you want to use third party types in your remote calls. Unfortunately the nice annotation features we saw in [wiki:ExampleUsingOwnConceteTypes] and [wiki:ExampleUsingOwnInterfaceTypes] are not applicable here because you are neither allowed to modify the classes nor makes it sense to add an additional dependency to that type.[[BR]] |
| | 4 | The Delight XML-RPC framework offers a solution to this problem by offering conversion mappings to be declared with the API (of course you can add as many mapping you want): |
| | 5 | |
| | 6 | {{{ |
| | 7 | @ConverterMappings( |
| | 8 | @Mapping(type=URL.class,converter=URLConverter.class) |
| | 9 | ) |
| | 10 | public interface Api |
| | 11 | { |
| | 12 | URL getHomepageLocation(); |
| | 13 | void addSite( URL url ); |
| | 14 | } |
| | 15 | }}} |
| | 16 | |
| | 17 | We have to define a standalone converter to transfer instances of type URL to a XML-RPC representation and vice versa. |
| | 18 | Therefor, the converter has to implemente the interface ''ParameterConverter'' to define three methods: |
| | 19 | |
| | 20 | - getXmlRpcRepresentationType(): states what XML-RPC type will be useed as transport representation |
| | 21 | |
| | 22 | - createFrom(): takes a XML-RPC representation and creates a corresponding instance of type ''URL''. |
| | 23 | |
| | 24 | - toXmlRpc(): takes an instance of type ''URL'' and converts it into it's XML-RPC representation. |
| | 25 | |
| | 26 | See the complete class here: |
| | 27 | {{{ |
| | 28 | public class URLConverter implements ParameterConverter<URL, String> |
| | 29 | { |
| | 30 | /** Converter uses XML-RPC type {@value XmlRpc.Type.STRING} as XML-RPC representation.*/ |
| | 31 | public Type getXmlRpcRepresentationType() |
| | 32 | { |
| | 33 | return( XmlRpc.Type.STRING ); |
| | 34 | } |
| | 35 | |
| | 36 | public URL createFrom( String xmlRepresentation ) |
| | 37 | throws TypeConversionException |
| | 38 | { |
| | 39 | URL url = null; |
| | 40 | try |
| | 41 | { |
| | 42 | url = new URL( xmlRepresentation ); |
| | 43 | } |
| | 44 | catch( MalformedURLException e ) |
| | 45 | { |
| | 46 | throw( new TypeConversionException( e ) ); |
| | 47 | } |
| | 48 | return( url ); |
| | 49 | } |
| | 50 | |
| | 51 | public String toXmlRpc( URL param ) |
| | 52 | throws TypeConversionException |
| | 53 | { |
| | 54 | return( param.toExternalForm() ); |
| | 55 | } |
| | 56 | } |
| | 57 | }}} |
| | 58 | |
| | 59 | Now, without having to modify it, the type ''URL'' can be used in XML-RPC calls. |
| | 60 | {{{ |
| | 61 | Api remote_api = XmlRpc.createClient( Api.class, "handlerId", host, port ); |
| | 62 | |
| | 63 | URL homepageUrl = remote_api.getHomepageLocation(); |
| | 64 | InputStream is = homepageUrl.openStream(); |
| | 65 | ... |
| | 66 | |
| | 67 | remote_api.addSite( new URL( "http://middle.of.nowhere" ) ); |
| | 68 | |
| | 69 | ... |
| | 70 | }}} |
| | 71 | |
| | 72 | See also: [http://delight.opendfki.de/wiki/ExampleUsingOwnConceteTypes#UsingowntypesinCollectionsandMaps How to use own types contained in Collections an Maps]. |
| | 73 | |