December 19, 2014
Hot Topics:

Enterprise Web Services and Model-View-Controller

  • March 15, 2005
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Remote Web Service Client: View

Clients invoking Web service methods can decide what action to perform, based on the response from the server. In this case, a server returns a key that is also a method name. Clients can dynamically invoke the next method based on the result value from the server and continue with their operation execution.

For instance, Java clients can get a Web service access point and set data as before, but the response will tell the client what API to call next. In case of method1_SetName, the call return value is the "method2_UpperCaseName" string, which can be instantiated as a next method to invoke dynamically on the Web service.

The return value from method2_upperCaseName is a"method3_getName" string that is also called dynamically.

//True MVC test
name = "Kofman";
String nextActionKey = port.method1_SetName(name);
System.out.println("Setting new name to Server model: " + name);

Method method = port.getClass().getMethod(nextActionKey, new Class[] {});
String nextKey = (String) method.invoke(port, new Object[] {});

Method method2 = port.getClass().getMethod(nextKey, new Class[] {});
String result = (String) method2.invoke(port, new Object[] {});

System.out.println("Result from Server Model - name is: " + result); 

For a complete listing, please see the accompanying source code. The result is the same as before, but the client did not explicitly call the methods; the calling was guided by the controller on the server.

Setting new name to Server model: Kofman
Result from Server Model - name is: KOFMAN

Of course, this architecture can be expanded and applied to any scenario where MVC Web service architecture can be beneficial. Here is the complete client listing:

package test;

import beanexport.generated.*;
import java.lang.reflect.Method;

public class Test {
   public static void main(String[] args) {
      try {
         String wsdlUrl = "http://localhost:9501/web-services/
                                  Bean1?WSDL";
         Bean1 service = new Bean1_Impl(wsdlUrl);
         Bean1Port port = service.getBean1Port();

         //Simplified Remote View test
         String name = "Vlad";
         System.out.println("Setting Name to: " + name);
         port.setName(name);
         port.upperCaseName();

         String resultStr = port.getName();
         System.out.println("Modified name is: " + resultStr);

         //True MVC test
         name = "Kofman";
         String nextActionKey = port.method1_SetName(name);
         System.out.println("Setting new name to Server model: "
                            + name);
         Method method = port.getClass().getMethod(nextActionKey,
                                                   new Class[] {});
         String nextKey = (String) method.invoke(port, new Object[] {});

         Method method2 = port.getClass().getMethod(nextKey,
                                                    new Class[] {});
         String result = (String) method2.invoke(port, new Object[] {});

         System.out.println("Result from Server Model - name is: "
                            + result);

      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Conclusion

In this article, I've discussed how to expand MVC to include a Web service and relocate the View layer to remote machines. Given that the communication protocol will be standardized via XML schema and can be SOAP over HTTP, the potential for a Client Interfaces is quite large, encompassing other platforms and languages different from whatever language the Controller and Model on the server are written in. Different user interfaces such as desktop GUI, text mode, Web-based pages, or non-interactive autonomous applications all can serve as clients in this type of architecture. If you are designing your application for the Web and choose to use MVC as an underlying framework, it may make sense to add a Web service component early on in your design process. Eventually, offering a remote presentation with standard protocols for Web service-centric clients can greatly increase your customer base.

Source Code

Download the accompanying source code here.

Articles Written by the Author on Similar Topics

Applying MVC to Web-Based Applications with Generic Views

Creating Interactive GUIs with Swing's MVC Architecture

Creating Dynamic Swing Views with Reflection by Extending MVC

Additional References

Advanced MVC: http://www.ifi.unizh.ch/richter/Classes/oose2/03_mvc/02_mvc_java/02_mvc_java.html#4%20Models%20as%20Proxies

Web Services Developer: http://info.borland.com/techpubs/jbuilder/

Developing Web Services: http://info.borland.com/techpubs/jbuilder/jbuilder2005/websvcs/contents.html

About the Author

Vlad Kofman is a System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel