Announcement Announcement Module
Collapse
No announcement yet.
Endpoint methods matched by type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Endpoint methods matched by type

    Hello,

    I have made a simple extension to AbstractMarshallingPayloadEndpoint so that the incoming objects are routed to methods that take the incoming object as input. In this way I get endpoint methods matched by type.

    So I have all the objects sent to invokeInteral, and I initially did:

    public class PropertyServiceEndpoint extends AbstractMarshallingPayloadEndpoint {

    protected Object invokeInternal(Object request) throws Exception {

    if (request instanceof GetBuildingsRequestDocument) {
    }else if (request instanceof GetContractsRequestDocument) {
    }
    }
    }

    But this is quite tiresome when you have many endpoints so now I have:
    public class InvoiceServiceEndpoint extends TypedMarshallingPayloadEndpoint {
    public Object someMethod(GetBuildingsRequestDocument req){
    }

    public Object someMetod2(GetContractsRequestDocument req){
    }

    And thus avoid the annoying instanceof code.


    Here is the class:

    public class TypedMarshallingPayloadEndpoint extends AbstractMarshallingPayloadEndpoint {

    protected final Log logger = LogFactory.getLog(getClass());

    public TypedMarshallingPayloadEndpoint(Marshaller marshaller) {
    super(marshaller);
    }

    protected Object invokeInternal(Object request) throws Exception {

    Method[] m = this.getClass().getDeclaredMethods();

    for (int i = 0; i < m.length; i++) {
    Class[] methodTypes = m[i].getParameterTypes();
    if (methodTypes.length == 1) {
    if (methodTypes[0].isInstance(request)) {
    logger.debug("Invoking method: " + m[i].getName());
    return m[i].invoke(this, new Object[] { request });
    }
    }
    }

    String s = "Could not invoke method with parameter type: " + request.getClass();
    logger.error(s);

    throw (new Exception(s));
    }
    }

    and example usage and unit test:
    public class TypedEndpointImpl extends TypedMarshallingPayloadEndpoint {

    public TypedEndpointImpl(Marshaller marshaller) {
    super(marshaller);
    }


    public String method1(String name){
    return "Method1:" + name;
    }

    public String method2(Integer number){
    return "Method2:" + number;
    }
    }

    @RunWith(JUnit4ClassRunner.class)
    public class TypeTest {

    @Test
    public void test() throws Exception{
    TypedEndpointImpl ep = new TypedEndpointImpl(new XmlBeansMarshaller());
    assertEquals("Method1:hello",ep.invokeInternal(new String("hello")));
    assertEquals("Method2:123",ep.invokeInternal(Integ er.valueOf(123)));
    }

    @Test(expected=Exception.class)
    public void testException() throws Exception{

    TypedEndpointImpl ep = new TypedEndpointImpl(new XmlBeansMarshaller());

    assertEquals("Method1:hei",ep.invokeInternal(new BigInteger("123")));

    }
    }


    I hope this can be useful for someone. I think also it would have been a good idea to have support for this in the already great spring ws. Feel free to include the code, rewrite or do anything.

    Best regards,
    Tor
Working...
X