|
|
## [Home](https://git.opendfki.de/delight/delight/wikis/Home) | [Documentation](https://git.opendfki.de/delight/delight/wikis/Documentation) | [People / Contact](https://git.opendfki.de/delight/delight/wikis/people)
|
|
|
---
|
|
|
|
|
|
### Using Third Party Types in your API
|
|
|
|
|
|
Eventually, you want to use third party types in your remote calls. Unfortunately the nice annotation features we saw in [Using own concrete types in your API](ExampleUsingOwnConceteTypes) and [Using interface or abstract types in your API](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.
|
|
|
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 mappings as you want):
|
|
|
|
|
|
```java
|
|
|
@ConverterMappings(
|
|
|
@Mapping(type=URL.class,converter=URLConverter.class)
|
|
|
)
|
|
|
public interface Api
|
|
|
{
|
|
|
URL getHomepageLocation();
|
|
|
void addSite( URL url );
|
|
|
}
|
|
|
```
|
|
|
|
|
|
We have to define a standalone converter to transfer instances of type URL to a XML-RPC representation and vice versa.
|
|
|
Therefor, the converter has to implemente the interface ''ParameterConverter'' to define three methods:
|
|
|
|
|
|
- getXmlRpcRepresentationType(): states what XML-RPC type will be used as transport representation
|
|
|
|
|
|
- createFrom(): takes a XML-RPC representation and creates a corresponding instance of type ''URL''.
|
|
|
|
|
|
- toXmlRpc(): takes an instance of type ''URL'' and converts it into it's XML-RPC representation.
|
|
|
|
|
|
See the complete class here:
|
|
|
```java
|
|
|
public class URLConverter implements ParameterConverter<URL, String>
|
|
|
{
|
|
|
public Type getXmlRpcRepresentationType() { return( XmlRpc.Type.STRING ); }
|
|
|
|
|
|
public URL createFrom( String xmlRepresentation )
|
|
|
throws TypeConversionException
|
|
|
{ ...
|
|
|
}
|
|
|
|
|
|
public String toXmlRpc( URL param )
|
|
|
throws TypeConversionException
|
|
|
{
|
|
|
return( param.toExternalForm() );
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Now, without having to modify it, the type ''URL'' can be used in XML-RPC calls.
|
|
|
|
|
|
```java
|
|
|
Api remote_api = XmlRpc.createClient( Api.class, "handlerId", host, port );
|
|
|
|
|
|
URL homepageUrl = remote_api.getHomepageLocation();
|
|
|
InputStream is = homepageUrl.openStream();
|
|
|
...
|
|
|
|
|
|
remote_api.addSite( new URL( "http://middle.of.nowhere" ) );
|
|
|
|
|
|
...
|
|
|
```
|
|
|
|
|
|
See also: [Using own concrete types in your API](ExampleUsingOwnConceteTypes) how to use own types contained in Collections an Maps].
|
|
|
|
|
|
Examples in source code: [here](https://git.opendfki.de/delight/delight/tree/master/example-delight-webapp) |
|
|
\ No newline at end of file |