Distributed Application Platform Based on Web Service basics Summary

1, Web Service's profile
1, What is Web Service
Web services is to build interoperable distributed applications for the new platform.
Web service platform is a standard that defines how applications to achieve interoperability in the Web. You can use any language you like in any platform you like to write Web service, as long as we can through standard Web service to query and access those services.
Web service platform needs a protocol to achieve the creation of distributed applications. Any platform has its own data representation and types of systems. To achieve interoperability, Web service platform must provide a standard type system for communication between different platforms, programming languages and component models of different types of systems.
Based Web Services platform is XML + HTTP.
HTTP protocol is the most common Internet protocols.
XML provides a platform for different programming languages and between languages.
Web services platform is a simple interoperable messaging framework. It still lacks many important, such as security and routing features. However, once SOAP becomes more advanced, these issues will be solved.
Web services is expected to make applications easier to communicate.
★ Web services to Web applications to upgrade to another level through the use of Web services, your application can function or message to the world.
Web services use XML for encoding and decoding data, and by using the SOAP protocol to transfer from the open data.
Through Web services, your accounting department Win 2k server with IT suppliers to connect UNIX server.
★ Web services application, there are two types of reusable application components that some features are often different applications used. So why should cycle to develop them?
Web services application components can be provided as a service, such as currency conversion, weather, or even language translation and so on.
Ideal situation yes, each application component is only one of the most excellent of the versions, so Renhe were all OK in their application Zhong use it.
Connect existing software for different applications by providing a way to link their data, Web services help solve the interoperability problem.
By using Web services, you can in different applications and platforms to exchange data.
★ Web Services has two basic elements.
They are: SOAP and WSDL
(1) What is SOAP?
 SOAP Simple Object Access Protocol refers to  SOAP is a protocol  SOAP for communication between applications  SOAP is a format used to send messages  SOAP is designed to communicate via the Internet  SOAP platform-independent  SOAP is independent of XML-based language  SOAP
 SOAP is simple and can be extended to allow you to bypass the firewall  SOAP  SOAP as the development of W3C standards
(2) What is WSDL?
WSDL is based on XML for describing Web Services and how to access Web Services language.
 WSDL means Web Service Description Language  WSDL using the XML write  WSDL is an XML document  WSDL for describing network services  WSDL Web services can also be used to locate is not W3C standards  WSDL
2, What is JWS
JWS (Java Web Service) is a Java application platform specifically for the development of Web services, systems and service-oriented system of products, and its latest version is 2.0, Java EE 5 and Java SE 6 are on the JWS 2.0 support.
In JWS 2.0, Java defines a series of new standards, JMS itself contains a number of tools, such as JAX-WS 2.0, JAXB 2.0, JAXP 1.4, SAAJ 1.3 and WS-Metadata and so on.
Service-oriented systems are often by multiple sub-functions of different independent components, through their mutual cooperation between the good, the demand can be complex. Service-oriented system, the characteristics and requirements of the independent formation of the interface between the public, the public interface for exchanging data with good definition. Due to data communication between units, which have well-defined interfaces to other components must be identified and properly understood, can work together.
In the definition of a public interface, there are specific data exchange problem, that both sides need to follow a common data exchange standards, the data exchange standard known as protocols. To communicate between separate components, requires a series of stringent requirements on data communications standard format and rules.
The emergence of Web Service to solve the above problem, the definition of a unified interface, using WSDL format, with a SOAP message unified input / output parameters of the unified format. SOAP messages can be delivered by a variety of ways, for example, through HTTP, SMTP and JMS protocol transmission. To HTTP, for example, in the service use client, WSDL interface definitions can request access through HTTP-GET, and SOAP response message and reply message can be transmitted to implement HTTP-POST request. Thus, based on WSDL and SOAP message mechanism can meet the service-oriented application development needs.
Web Service platform architecture consists of three parts: serialization and deserialization subsystem, call subsystem and release subsystem. The three subsystems are not subject to specific restrictions on language, platform and framework are not subject to the restrictions, using Java language, or both. NET language, regardless of platform or Axis JWS to develop web services, will involve three basic functions module.
(1) serialization and deserialization
JMS will be in a Java object into an XML element of the process, known as serialization. Instead an XML element into the corresponding Java
The process object is called deserialization. Serialization and deserialization process depends on the Java classes and XML-Scheme mapping between, JWS independent serialization and deserialization subsystem to complete the mapping and conversion charge.
In the web service client, serialization process parameters into a xml node, and then packaged into a Soap request message, sent to the server-side web service endpoint. Return value obtained when the deserialization starts, it will return value specified in the messages from the SOAP node in the xml out, and then turn into a Kehu Duanxiangduiying the Java Object .
Transformation rule is defined in the JWS comment by JAXB accomplished. The new version of JAXB simplifies the description of binding rules, which allows rules to directly write notes in the form of Java classes.
For example:
@ Entity
@ Name ("hotel")
@ XmlAccessorType (XmlAccessType.PUBLIC_MEMBER)
@ XmlType (name = "", propOrder = (
"Id",
"Img",
"Name",
"Address",
"City",
"State",
"Zip",
"Country",
"Price",
"IpAddress"
))
@ XmlRootElement (name = "Hotel")
public class Hotel implements Serializable
(
private Long id;
private String img;
private String name;
private String address;
private String city;
private String state;
private String zip;
private String country;
private BigDecimal price;
private String ipAddress = "192.168.1.112";

@ Id @ GeneratedValue
public Long getId ()
(
return id;
)
public void setId (Long id)
(
this.id = id;
)

@ Length (max = 50)
@ NotNull
public String getImg () (
return img;
)

public void setImg (String img) (
this.img = img;
)

@ Length (max = 50) @ NotNull
public String getName ()
(
return name;
)
public void setName (String name)
(
this.name = name;
)

@ Length (max = 100) @ NotNull
public String getAddress ()
(
return address;
)
public void setAddress (String address)
(
this.address = address;
)

@ Length (max = 40) @ NotNull
public String getCity ()
(
return city;
)
public void setCity (String city)
(
this.city = city;
)

@ Length (min = 4, max = 6) @ NotNull
public String getZip ()
(
return zip;
)
public void setZip (String zip)
(
this.zip = zip;
)

@ Length (min = 2, max = 10) @ NotNull
public String getState ()
(
return state;
)
public void setState (String state)
(
this.state = state;
)

@ Length (min = 2, max = 40) @ NotNull
public String getCountry ()
(
return country;
)
public void setCountry (String country)
(
this.country = country;
)

@ Column (precision = 6, scale = 2)
public BigDecimal getPrice ()
(
return price;
)
public void setPrice (BigDecimal price)
(
this.price = price;
)
@ Transient
public String getIpAddress () (
return ipAddress;
)
public void setIpAddress (String ipAddress) (
this.ipAddress = ipAddress;
)
@ Override
public String toString ()
(
return "Hotel (" + name + "," + address + "," + city + "," + zip + ")";
)
)
(2) service call process
In a service-oriented distributed system, the general framework of the traditional client-server Client as a client in the consumer services, while the server side as a service provider.
According to current web service standards, a service is called, in the service delivery end of a rough process is as follows:
① accepted and pre SOAP request message, such as efficacy, packet header processing SOAP messages.
② to get the message from the message interface and hope that the specific operation is called.
③ using web services support, to find concrete realization of the object, and call the object's interface. This object can be a different language. JWS support from WSDL to Java mapping, you can find it through the WSDL corresponding to the Java service endpoint implementation class.
④ The serialization deserialization tools will be SOAP request message to the service request parameters out, passed to step 3 in the corresponding target function.
⑤ target Java object for the appropriate action, the results of the calculation to return the object form.
⑥ sequence using the sequence of process tools, according to the definition in the wsdl will be the result of step 5 in the sequence of objects into XML elements, and packages to the SOAP response message.
⑦ to Step 6 in the SOAP response message sent back to service calls end.
Corresponding, in the service process using the end of the call are as follows:
① First, create a service endpoint interface object SEI (Service Endpoint Interface), the Web service client will normally have completed SEI corresponding factory class to instantiate the object. In the JWS in, SEI object is normally provided by Java agents to achieve.
② client package which is called by SEI web service interface.
③ sequence using sequence-based tools to process, according to the definition of WSDL, the client calls the interface parameters into XML elements, and then package the elements in the SOAP request message.
④ In the synchronous mode, the SOAP request message is issued, the client will wait for the SOAP response message; asynchronous mode, the client code for sequential follow-up, until received by listeners in SOAP request message.
⑤ Analysis obtained from the server SOAP response message, use the tool in serialization deserialization process, the SOAP response message in the data into the client object, the object's value is called service return value.
(3) web services, publishing
For example JMS, web services, the main functions of distribution system
① The URL to be released in the form of public WEB service WSDL file, and the binding target SOAP request message and Java classes.
② Release JAVA object file (such as Java Object files, WAR files, JAR files and associated configuration files, etc.).
③ configuration serialization and deserialization subsystem.
④ configure web services and SOAP endpoint message listener pretreatment process.
Second, web service development and call several ways (1) with the EJB container model development Web Service, use the wsimport tool to generate the auxiliary class called.
Examples are as follows (examples of the application EJB container as Jboss, all of the code is based on the JBoss Seam framework written):
① service provider side <service-side code>:
Service provider side of the task is to expose a web service for external use, in this case the web service is available through the JavaEE platform annotation @ java.jws.WebService achieved.
The following code path is the VSS folder on the preliminary investigation SOA \ web service \ EJB container model development using Web Service, use the wsimport tool to generate the auxiliary class called \ tele \ book \ src \ org \ jboss \ seam \ example \ booking \ ws \ impl, with the EJB container model development Web Service, use the wsimport tool to generate the auxiliary class called folder contains two folders: local, tele. tele folder decentralized service provider side of the project and to lay the EAR package. decentralization of services to local consumers in the folder end engineering and to lay the EAR package.
package org.jboss.seam.example.booking.ws.impl;
import static org.jboss.seam.ScopeType.SESSION;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Out;
import org.jboss.seam.example.booking.Hotel;
import org.jboss.seam.example.booking.User;
import org.jboss.seam.example.booking.ws.HotelObject;

@ Stateless
/ ** Instance of the use of two JBoss application server, the code is the service provider side of the code, so use the @ Remote annotation, remote interfaces. . * /
@ Remote (HotelObject.class)
@ WebService
public class HotelObjectImpl implements HotelObject (
@ PersistenceContext
private EntityManager em;

public List <Hotel> getHotelObject (String hotelName) (
List <Hotel> results = em.createQuery ("select h from Hotel h where h.name like" + "'%" + hotelName +"%'")
. GetResultList ();
if (results.size ()> 0) (
return results;
)
return null;
)

public String updateHotelObject (Hotel h) (
try (
System.out.println (h);
System.out.println (h.getName ());
em.merge (h);
) Catch (Exception e) (
e.printStackTrace ();
return "error";
)
return "success";
)
)
The above class inherits an interface, interface class path is: VSS folder on the preliminary investigation SOA \ web service \ EJB container model development using Web Service, use the wsimport tool to generate the auxiliary class called \ tele \ book \ src \ org \ jboss \ seam \ example \ booking \ ws.
package org.jboss.seam.example.booking.ws;

import java.util.List;
import org.jboss.seam.example.booking.Hotel;

public interface HotelObject (
public List <Hotel> getHotelObject (String hotelName);
public String updateHotelObject (Hotel h);
)
@ WebService annotation named above is a Web Service. Open the Ant file, perform desploy business. After deployment, JBoss management platform through the deployment of view just Web Service, enter http://localhost:8080/jbossws/ the view into the JBoss Web Service interface, you can see View a list of deployed services link, click View a list of deployed services link, you can view the already published Web Service's URL address, click the address you can view the WSDL generated text.
This is the service provider side of the main code, first annotation defines a web service, the web service provides two methods, both methods can receive parameters from a remote pass over. Services consumed by the above URL address to access the service.
(2) services to consumers side "Using server-side code>
To call the web service, we can better the way in which a way is through wsimport Gongju (Zai JDK1.6 In , Sun users already have a wsimport Gongju) Sheng Cheng auxiliary class to call the web Service .
In the VSS folder on the web Service has a document called the jaxws-ri package which contains the wsimport tool, in this case the helper class is to use it directly generated. Use the wsimport tool to generate the helper class, we must first open a dos environment where wsimport tool directory, such as wsimport tool of the local path:

Dos wsimport tool to open the path where the.

Wsimport path input in the following:
wsimport.bat-keep-d .. / build / classes / client http://192.168.1.106:8080/WsHelloWorld/HelloWorldBean?wsdl
Including .. / build / classes / client that generated the path stored in the auxiliary class, http://192.168.1.106:8080/WsHelloWorld/HelloWorldBean?wsdl said to call the web service WSDL text addresses, which said the service 192.168.1.106 provide end IP address.
Generated auxiliary class as shown below:

Java class to generate the secondary copy to our project and then instantiate an object of helper classes generated by the object to call services provided by the method of <or getHotelObjec ()>, to retrieve the records meet the criteria.
Such as in the VSS on the path;
SOA \ Web Servic \ EJB container model development using Web Service, use the wsimport tool to generate the auxiliary class called \ local \ book \ src \ org \ jboss \ seam \ example \ booking
package org.jboss.seam.example.booking;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.example.booking.ws.impl.Hotel;
import org.jboss.seam.example.booking.ws.impl.HotelObjectImpl;
import org.jboss.seam.example.booking.ws.impl.HotelObjectImplService;

@ Stateful
@ Name ("teledataHotelSearch")
@ Scope (ScopeType.SESSION)
@ Restrict ("# (identity.loggedIn)")
public class TeledateHotelSearchingAction implements TeledateHotelSearching
(
@ PersistenceContext
private EntityManager em;

private String searchString;

@ DataModel
private List <Hotel> telehotels;

public void find ()
(
queryHotels ();
)

private void queryHotels () (
/ ** Helper class to instantiate an object generated by the object calls a remote web service. * /
HotelObjectImplService service;
try
(
service = new HotelObjectImplService ();
HotelObjectImpl hotelObjectImpl = service.getHotelObjectImplPort ();
List <Hotel> result = hotelObjectImpl.getHotelObject (this.getSearchString ());
System.out.println (result.size ());
if (result.size ()> 0) (
telehotels = result;
) Else (
telehotels = null;
)
) Catch (Exception e) (
e.printStackTrace ();
)
)
public String getSearchString ()
(
return searchString;
)

public void setSearchString (String searchString)
(
this.searchString = searchString;
)

@ Remove
public void destroy () ()

@ Override
public void cancel () (
/ / TODO Auto generated method stub

)
)
There is also a class is modified to save the service provider (ie a remote Web Service) to return the Hotel object, you must call the service provider to provide methods of preservation Hotel object <that updateHotelObject ()>.
package org.jboss.seam.example.booking;

import static javax.persistence.PersistenceContextType.EXTENDED;

import java.util.Calendar;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Events;
import org.jboss.seam.example.booking.ws.impl.HotelObjectImpl;
import org.jboss.seam.example.booking.ws.impl.HotelObjectImplService;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

@ Stateful
@ Name ("saveTeteDataHotel")
@ Restrict ("# (identity.loggedIn)")
public class SaveTeteDataHotelAction implements SaveTeteDataHotel
(

@ Begin
public void seveHotel (Hotel hotel)
(
HotelObjectImplService service;
try
(
service = new HotelObjectImplService ();
HotelObjectImpl hotelObjectImpl = service.getHotelObjectImplPort ();
String result = hotelObjectImpl.updateHotelObject (this.conversionHotel (hotel));
System.out.println (result);
) Catch (Exception e) (
e.printStackTrace ();
)
)

public org.jboss.seam.example.booking.ws.impl.Hotel conversionHotel (Hotel hotel) (
org.jboss.seam.example.booking.ws.impl.Hotel h = new org.jboss.seam.example.booking.ws.impl.Hotel ();
h.setAddress (hotel.getAddress ());
h.setCity (hotel.getCity ());
h.setCountry (hotel.getCountry ());
h.setId (hotel.getId ());
h.setImg (hotel.getImg ());
h.setName (hotel.getName ());
h.setPrice (hotel.getPrice ());
h.setState (hotel.getState ());
h.setZip (hotel.getZip ());
return h;
)

@ End
public void cancel () ()

@ Remove
public void destroy () ()
)
It is noteworthy in the above category, that is, service consumers in the Hotel object can not be directly used for preservation, must be converted, into the generated auxiliary class org.jboss.seam.example.booking.ws.impl.Hotel class in order to save the above conversionHotel (Hotel hotel) method is to carry out the conversion.
Above is the kind of web service to use several important points, through the above steps to access remote web services published, and complete the process, please refer to Vss Dome works on .
(2) the use of dynamically generated SAAJ SOAP Message Access Protocol service provider side of the web service.
The first way is to use wsimpot tool to generate helper classes to implement the service provider side of the web service access, particularly due to the formation of auxiliary classes and more, and inflexible. Here we introduced the use of dynamically generated SAAJ SOAP message protocol to access service provider side of the web service.
① service provider side of the VSS on the class code:
EHR \ SOA \ SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ ws \ impl under
package org.jboss.seam.example.booking.ws.impl;
import static org.jboss.seam.ScopeType.SESSION;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Out;
import org.jboss.seam.example.booking.Hotel;
import org.jboss.seam.example.booking.User;
import org.jboss.seam.example.booking.ws.HotelObject;

@ Stateless
@ Remote (HotelObject.class)
@ WebService (name = "HotelObject", targetNamespace = "http://tower/ehr_DEV")
public class HotelObjectImpl implements HotelObject (
@ PersistenceContext
private EntityManager em;
@ WebResult (name = "Hotel")
public List <Hotel> getHotelObject (String hotelName) (
List <Hotel> results = em.createQuery ("select h from Hotel h where h.name like" + "'%" + hotelName +"%'")
. GetResultList ();
if (results.size ()> 0) (
return results;
)
return null;
)

public String updateHotelObject (Hotel h) (
try (
System.out.println (h);
System.out.println (h.getName ());
em.merge (h);
) Catch (Exception e) (
e.printStackTrace ();
return "error";
)
return "success";
)
)
The web service provides two methods, getHotelObject (String hotelName) method receives the parameters of the remote, through the parameter to find qualified Hotel object; updateHotelObject (Hotel h) method to save the modified object.
② services such caller-side code in VSS on:
EHR \ SOA \ SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ soa \ esb \ impl following this example uses Jboss ESB, in fact call the client service First send the request to Jboss ESB server, Jboss ESB Server also defines a number of web services, to accept the request, receives the request message, the message for further processing or directly to the message transmitted to the real news provider side.
The following code shows how the service consumer side ESB server to call the service provider side services.
package org.jboss.seam.example.booking.soa.esb.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.SOAPMessage;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.example.booking.Hotel;
import org.jboss.seam.example.booking.commom.collectionbinder.ListHotel;
import org.jboss.seam.example.booking.commom.esb.BuildSoapMessage;
import org.jboss.seam.example.booking.commom.esb.SendSoapMessageToEsb;
import org.jboss.seam.example.booking.commom.esb.impl.BuildSoapMessageImpl;
import org.jboss.seam.example.booking.commom.esb.impl.SendSoapMessageToEsbImpl;
import org.jboss.seam.example.booking.commom.formattransform.TransformMessageFormat;
import org.jboss.seam.example.booking.commom.formattransform.TransformString;
import org.jboss.seam.example.booking.commom.formattransform.impl.TransformMessageFormatImpl;
import org.jboss.seam.example.booking.commom.formattransform.impl.TransformStringImpl;
import org.jboss.seam.example.booking.soa.esb.TeledateHotelSearching;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

@ Stateful
@ Name ("teledataHotelSearch")
@ Scope (ScopeType.SESSION)
@ Restrict ("# (identity.loggedIn)")
public class TeledateHotelSearchingImpl implements TeledateHotelSearching
(
@ PersistenceContext
private EntityManager em;

private String searchString;

@ DataModel
private List <Hotel> telehotels;

public void find ()
(
getHotelObject (this.getSearchString ());
)

public void getHotelObject (String searchString) (
try (
BuildSoapMessage bsm = new BuildSoapMessageImpl ();
SOAPMessage sm = bsm.getMessage ("sayGoodbye", "http://webservice_producer/goodbyeworld", searchString);
TransformMessageFormat tmf = new TransformMessageFormatImpl ();
String stringSOAPMessage = tmf.transformToString (sm);
SendSoapMessageToEsb smtEsb = new SendSoapMessageToEsbImpl ();
Object o = smtEsb.sendMessageToJBRListener ("http", "8765", stringSOAPMessage);

System.out.println ("wo de ma ya !!!"+ o.toString ());

TransformString tfs = new TransformStringImpl ();
Document doc = tfs.transformStringToDocument (o.toString ());
NodeList nodeList = doc.getElementsByTagName ("ListHotel");
Element element = (Element) nodeList.item (0);
String returnContent = element.getTextContent ();
if (returnContent.length ()> 0) (
String returnContentAnd = "<ListHotel>" + returnContent + "</ ListHotel>";
InputStream inputStream = new ByteArrayInputStream (returnContentAnd.getBytes ());
JAXBContext context = JAXBContext.newInstance (ListHotel.class);
try (
Unmarshaller um = context.createUnmarshaller ();
ListHotel hl = (ListHotel) um.unmarshal (inputStream);
System.out.println ("I'm King!" + Hl.getElements ());
telehotels = hl.getElements ();
) Catch (JAXBException e) (
e.printStackTrace ();
)
) Else (
telehotels = null;
)
) Catch (Exception e) (
e.printStackTrace ();
telehotels = null;
) Catch (Throwable e) (
e.printStackTrace ();
)

)

public String getSearchString ()
(
return searchString;
)

public void setSearchString (String searchString)
(
this.searchString = searchString;
)

@ Remove
public void destroy () ()

@ Override
public void cancel () (
searchString = "";
telehotels = null;
)
)
In getHotelObject (String searchString) method, first class provided by BuildSoapMessage getMessage () method to generate the SOAP message to send. (Calling the code is as follows:)
BuildSoapMessage bsm = new BuildSoapMessageImpl ();
SOAPMessage sm = bsm.getMessage ("sayGoodbye", "http://webservice_producer/goodbyeworld", searchString);
BuildSoapMessage class and code as follows:
Such a path in the vss on the following:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ commom \ esb \ impl
package org.jboss.seam.example.booking.commom.esb.impl;

import java.util.HashMap;
import java.util.Map;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.jboss.seam.example.booking.commom.esb.BuildSoapMessage;

public class BuildSoapMessageImpl implements BuildSoapMessage (

public static final SOAPConnection getSOAPConnection () (
SOAPConnectionFactory soapConnFactory;
SOAPConnection connection;
try (
soapConnFactory = SOAPConnectionFactory.newInstance ();
connection = soapConnFactory.createConnection ();
return connection;
) Catch (UnsupportedOperationException e) (
e.printStackTrace ();
return null;
) Catch (SOAPException e) (
e.printStackTrace ();
return null;
)
)

public static final SOAPMessage getSOAPMessage () (
MessageFactory messageFactory;
SOAPMessage message;
try (
messageFactory = MessageFactory.newInstance ();
message = messageFactory.createMessage ();
return message;
) Catch (SOAPException e) (
e.printStackTrace ();
return null;
)
)

public Name getName (String param) (
SOAPFactory soapFactory;
try (
soapFactory = SOAPFactory.newInstance ();
Name name = soapFactory.createName (param); / / "arg0"
return name;
) Catch (SOAPException e) (
e.printStackTrace ();
return null;
)
)

public SOAPElement getBodyElement (SOAPMessage message, String param1, String param2, String param3) (
SOAPPart soapPart = message.getSOAPPart ();
SOAPEnvelope envelope;
SOAPBody body;
SOAPElement bodyElement;
try (
envelope = soapPart.getEnvelope ();
body = envelope.getBody ();
bodyElement = body.addChildElement (envelope.createName (param1, / / "sayHello",
param1, / / "ns1",
param1 ));//" http://webservice_consumer1/helloworld "));
bodyElement.setEncodingStyle (SOAPConstants.URI_NS_SOAP_ENCODING);
return bodyElement;
) Catch (SOAPException e) (
e.printStackTrace ();
return null;
)
)

public SOAPElement addTextNode (SOAPMessage message, String param1, String param2, String param3) (
SOAPPart soapPart = message.getSOAPPart ();
SOAPEnvelope envelope;
SOAPBody body;
SOAPElement bodyElement;
try (
envelope = soapPart.getEnvelope ();
body = envelope.getBody ();
bodyElement = body.addChildElement (envelope.createName (param1, / / "sayHello",
param1, / / "ns1",
param1 ));//" http://webservice_consumer1/helloworld "));
bodyElement.setEncodingStyle (SOAPConstants.URI_NS_SOAP_ENCODING);
return bodyElement;
) Catch (SOAPException e) (
e.printStackTrace ();
return null;
)
)

public SOAPMessage getMessage (String methodName, String targetNamespace, String messageContext) throws SOAPException, Exception (

/ / Send parameters need to create a Name
SOAPFactory soapFactory = SOAPFactory.newInstance ();
/ / Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance ();
SOAPMessage message = messageFactory.createMessage ();
/ / Get a SOAPPart object
SOAPPart soapPart = message.getSOAPPart ();
SOAPEnvelope envelope = soapPart.getEnvelope ();
SOAPBody body = envelope.getBody ();
/ / Create the main element and namespace
SOAPElement bodyElement =
body.addChildElement (envelope.createName (methodName,
"Tower",
targetNamespace));
bodyElement.setEncodingStyle (SOAPConstants.URI_NS_SOAP_ENCODING);

/ / Send parameter Create a Name object
Name name = soapFactory.createName ("message");
SOAPElement symbol = bodyElement.addChildElement (name);
symbol.addTextNode (messageContext);
message.saveChanges ();
return message;
)

public SOAPMessage getMessage (String methodName, String targetNamespace, HashMap <String,String> hm) throws SOAPException, Exception (
/ / Send parameters need to create a Name
SOAPFactory soapFactory = SOAPFactory.newInstance ();
/ / Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance ();
SOAPMessage message = messageFactory.createMessage ();
/ / Get a SOAPPart object
SOAPPart soapPart = message.getSOAPPart ();
SOAPEnvelope envelope = soapPart.getEnvelope ();
SOAPBody body = envelope.getBody ();
/ / Create the main element and namespace
SOAPElement bodyElement =
body.addChildElement (envelope.createName (methodName,
"Ns1",
targetNamespace));
bodyElement.setEncodingStyle (SOAPConstants.URI_NS_SOAP_ENCODING);

/ / Send parameter Create a Name object
Name name = soapFactory.createName ("arg0");
SOAPElement symbol = bodyElement.addChildElement (name);
for (Map.Entry <String, String> m: hm.entrySet ()) (
System.out.println ("HashMap" + m.getKey () + ":" + m.getValue ());
Name symbolName = soapFactory.createName (m.getKey ());
SOAPElement symbolNameSOAPElemen = symbol.addChildElement (symbolName);
symbolNameSOAPElemen.addTextNode (m.getValue ());
)
message.saveChanges ();
return message;

)

)
In this case, Jboss esb server a web service can only accept arguments of type String, so it is a soap message format conversion. The consumption side in the service code by the following method call format:
TransformMessageFormat tmf = new TransformMessageFormatImpl ();
String stringSOAPMessage = tmf.transformToString (sm);
Format conversion classes and methods are as follows:
Such a path in:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ commom \ formattransform \ impl

package org.jboss.seam.example.booking.commom.formattransform.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.jboss.internal.soa.esb.util.StreamUtils;
import org.jboss.seam.example.booking.commom.formattransform.TransformMessageFormat;

public class TransformMessageFormatImpl implements TransformMessageFormat (

public String transformToString (SOAPMessage sm) throws SOAPException, IOException (
ByteArrayOutputStream s = new ByteArrayOutputStream ();
sm.writeTo (s);
byte [] buf = s.toByteArray ();
ByteArrayInputStream bin = new ByteArrayInputStream (buf);
String msg = new String (StreamUtils.readStream (bin));
return msg;
)

)
Soap the converted message to the Jboss ESB Server, the service consumer side of the call to process the code is as follows:
SendSoapMessageToEsb smtEsb = new SendSoapMessageToEsbImpl ();
Object o = smtEsb.sendMessageToJBRListener ("http", "8765", stringSOAPMessage);
Send a message to the Jboss ESB server classes and methods are as follows:
VSS on such a path in the following:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ commom \ esb \ impl

package org.jboss.seam.example.booking.commom.esb.impl;

import org.jboss.remoting.Client;
import org.jboss.remoting.InvokerLocator;
import org.jboss.seam.example.booking.commom.esb.SendSoapMessageToEsb;

public class SendSoapMessageToEsbImpl implements SendSoapMessageToEsb (

public static String ESBSERVICEIP = "192.168.1.101";
public Object sendMessageToJBRListener (String protocol, String port, String message) throws Throwable (
String locatorURI = protocol + "://"+ ESBSERVICEIP + ":" + port;
InvokerLocator locator = new InvokerLocator (locatorURI);
Client remotingClient = null;
try (
remotingClient = new Client (locator);
remotingClient.connect ();
Object response = remotingClient.invoke (message);
return response;
) Finally (
if (remotingClient! = null) (
remotingClient.disconnect ();
)
)
)

)
public static String ESBSERVICEIP = "192.168.1.101"; the IP address of that server's IP address Jboss ESB.
String locatorURI = protocol + "://"+ ESBSERVICEIP + ":" + port; this line of code makes up access Jboss ESB published on the Web service URL address.
We then look at call-side code, from Jboss ESB's return value (in fact, Jboss ESB server published Web services and through the forwarding call service provider side of Web services to get the return value, the service provider side of the return value to first value transfer Back to the Jboss ESB Server, Jboss ESB server then returns it to the service call side.) is also an Object object, Object object we want to convert this application in our real objects (such as in the example ListHotel object), this conversion I Use the JAXB standard, the standard used. xml and Mutual Convert between Java objects, because the return value is the SOAP message is. xml form, with over JAXB standard can easily be converted into what we want java class (eg ListHotel), in the call-side code is as follows:
TransformString tfs = new TransformStringImpl ();
Document doc = tfs.transformStringToDocument (o.toString ());
NodeList nodeList = doc.getElementsByTagName ("ListHotel");
Element element = (Element) nodeList.item (0);
String returnContent = element.getTextContent ();
if (returnContent.length ()> 0) (
String returnContentAnd = "<ListHotel>" + returnContent + "</ ListHotel>";
InputStream inputStream = new ByteArrayInputStream (returnContentAnd.getBytes ());
JAXBContext context = JAXBContext.newInstance (ListHotel.class);
try (
Unmarshaller um = context.createUnmarshaller ();
ListHotel hl = (ListHotel) um.unmarshal (inputStream);
System.out.println ("I'm King!" + Hl.getElements ());
telehotels = hl.getElements ();
) Catch (JAXBException e) (
e.printStackTrace ();
)
) Else (
telehotels = null;
)
) Catch (Exception e) (
e.printStackTrace ();
telehotels = null;
) Catch (Throwable e) (
e.printStackTrace ();
)

Jboss ESB server from the first through the return value of conversion, Document type it into the code as follows:
TransformString tfs = new TransformStringImpl ();
Document doc = tfs.transformStringToDocument (o.toString ());
Conversion of classes and methods are as follows:
VSS on such a path in the following:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ commom \ formattransform \ impl
package org.jboss.seam.example.booking.commom.formattransform.impl;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jboss.seam.example.booking.commom.formattransform.TransformString;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class TransformStringImpl implements TransformString (

public Document transformStringToDocument (String s) (
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
DocumentBuilder builder;
try (
builder = factory.newDocumentBuilder ();
Document doc = builder.parse (new InputSource (new StringReader (s)));
return doc;
) Catch (ParserConfigurationException e) (
e.printStackTrace ();
) Catch (SAXException e) (
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)
return null;
)

public String [] getObjectArrayByESBReturnContent (String s) (
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
DocumentBuilder builder;
try (
builder = factory.newDocumentBuilder ();
Document doc = builder.parse (new InputSource (new StringReader (s)));
NodeList nodeList = doc.getElementsByTagName ("return");
Element element = (Element) nodeList.item (0);
String returnContent = element.getTextContent ();
if (returnContent.length ()> 0) (
String [] getObjectContest = returnContent.split ("</ O>");
return getObjectContest;
) Else (
return null;
)
) Catch (ParserConfigurationException e) (
e.printStackTrace ();
) Catch (SAXException e) (
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)
return null;
)

public String [] getPropertyArrayByStringObject (String returnContent) (
if (returnContent.length ()> 0) (
String [] getObjectContest = returnContent.split ("</ A & V>");
return getObjectContest;
) Else (
return null;
)
)
)
Document object after conversion to be about the interception and re-assembled into a form suitable for JAXB XML conversion, the process is as follows:
NodeList nodeList = doc.getElementsByTagName ("ListHotel");
Element element = (Element) nodeList.item (0);
String returnContent = element.getTextContent ();
String returnContentAnd = "<ListHotel>" + returnContent + "</ ListHotel>";
InputStream inputStream = new ByteArrayInputStream (returnContentAnd.getBytes ());
The return value is converted to first convert the InputStream object InputStream object that will satisfy the conversion of form, and can be converted.
To use JAXB specification for such a conversion, to conduct two steps:
① marked with the annotation @ XmlRootElement want to convert the Java class.
Java class is as follows:
VSS on the Java class path is:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ commom \ collectionbinder
package org.jboss.seam.example.booking.commom.collectionbinder;

import java.util.List;

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

import org.jboss.seam.example.booking.Hotel;
@ XmlRootElement (name = "ListHotel")
public class ListHotel (
private List <Hotel> hotel;

public ListHotel () (
)

public ListHotel (List <Hotel> hotel) (
this.hotel = hotel;
)

@ XmlElementRef
public List <Hotel> getElements () (
return hotel;
)

public void setElements (List <Hotel> hotel) (
this.hotel = hotel;
)

)
② grouping mechanisms provided by JAXB, the Java object into xml character. Code:
JAXBContext context = JAXBContext.newInstance (ListHotel.class);
try (
Unmarshaller um = context.createUnmarshaller ();
ListHotel hl = (ListHotel) um.unmarshal (inputStream);
System.out.println ("I'm King!" + Hl.getElements ());
telehotels = hl.getElements ();
) Catch (JAXBException e) (
e.printStackTrace ();
)
That is all I use SAAJ implementation details of Web Service development process, the document is only a reference to the role of flexibility in this way to other information to climb, serious thought.
This instance involves the JBoss ESB Server, I have not conducted a detailed introduction, I will discuss in detail the next document in the part. Good understanding of the instance to refer to the documentation of my Jboss ESB and Vss on the relevant code.
These are by Jboss ESB server as middleware forwarding messages from the message consumer-ended use of the process of the news side, SOA (V1.2) works under this folder also includes a service consumer-side direct access to the service provider side instance. Namely:
On the class path in the VSS:
SOA (V1.2) \ Request endpoint (192.168.1.112) \ booking \ src \ org \ jboss \ seam \ example \ booking \ soa \ esb \ impl

package org.jboss.seam.example.booking.soa.esb.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.example.booking.Hotel;
import org.jboss.seam.example.booking.commom.esb.BuildSoapMessage;
import org.jboss.seam.example.booking.commom.esb.SendSoapMessage;
import org.jboss.seam.example.booking.commom.esb.impl.BuildSoapMessageImpl;
import org.jboss.seam.example.booking.commom.esb.impl.SendSoapMessageImpl;
import org.jboss.seam.example.booking.commom.formattransform.TransformMethodName;
import org.jboss.seam.example.booking.commom.formattransform.impl.TransformMethodNameImpl;
import org.jboss.seam.example.booking.soa.esb.TeteDataHotelSave;

@ Stateful
@ Name ("saveTeteDataHotel")
@ Restrict ("# (identity.loggedIn)")
public class TeteDataHotelSaveImpl implements TeteDataHotelSave
(
@ In (required = false)
@ Out (required = false)
private Hotel hotel;

@ Begin
public void selectHotel (Hotel selectedHotel)
(
/ / Hotel h = new Hotel ();
hotel = selectedHotel;
)

public void seveHotel (Hotel hotel)
(
String ipAddress = hotel.getIpAddress ();
System.out.println ("yuexiangcheng, test !!!!!========"+ ipAddress);
TransformMethodName tmn = new TransformMethodNameImpl ();
Field [] f = hotel.getClass (). GetDeclaredFields ();
HashMap <String,String> hm = new HashMap ();
for (int i = 0; i <f.length; i + +) (
try (
String attributeName = f [i]. GetName ();
System.out.println ("attributeNameattributeNameattributeNameattributeNameattributeNameattributeName" + attributeName);
String motherName = tmn.getGetMethodByAttribute (attributeName);
Method method = hotel.getClass (). GetDeclaredMethod (motherName, null);
String attributeValue = method.invoke (hotel, null). ToString ();
System.out.println ("attributeValueattributeValueattributeValueattributeValueattributeValueattributeValue" + attributeValue);
hm.put (attributeName, attributeValue);
) Catch (SecurityException e) (
e.printStackTrace ();
) Catch (NoSuchMethodException e) (
e.printStackTrace ();
) Catch (IllegalArgumentException e) (
e.printStackTrace ();
) Catch (IllegalAccessException e) (
e.printStackTrace ();
) Catch (InvocationTargetException e) (
e.printStackTrace ();
)
)
BuildSoapMessage bsm = new BuildSoapMessageImpl ();
SendSoapMessage ssm = new SendSoapMessageImpl ();
try (
SOAPMessage sm = bsm.getMessage ("updateHotelObject", "http://tower/ehr_DEV", hm);
SOAPMessage result = ssm.send (sm, "http://" + ipAddress + ": 8080/jboss-seam-booking-jboss-seam-booking/HotelObjectImpl");
TransformerFactory transformerFactory =
TransformerFactory.newInstance ();
Transformer transformer =
transformerFactory.newTransformer ();
Source sourceContent = result.getSOAPPart (). GetContent ();
StreamResult resultPrint = new StreamResult (System.out);
transformer.transform (sourceContent, resultPrint);
System.out.println ();
) Catch (SOAPException e) (
/ / TODO automatically generated catch block
e.printStackTrace ();
) Catch (Exception e) (
/ / TODO automatically generated catch block
e.printStackTrace ();
)

)

@ End
public void cancel () ()

@ Remove
public void destroy () ()
)
Service consumer side of the process is modified from the service provider side to retrieve the data, and its been modified to remain to the service provider side. Specific process and check on cases of small, please study the code in VSS. It is noteworthy that in the case of the solution used in the JAXB set of functions.

I realize these are the two Web Service method, because capacity is limited, so badly written, for reference only. We want to help, thank you.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Distributed Application Platform Based on Web Service basics Summary

  • JS code management mechanism

    Issue With the AJAX pop, JS in web applications, increasing over time. Attendant on the JS code maintenance and reuse on the need for a mechanism to manage. In addition to Java objects, classes of these concepts, there is also a flexible mechanism for the

  • Let Ruby On Rails into the Embedded Development

    In the embedded Linux development, and C are a pair of golden partner, almost to occupy the vast majority of the domestic market and become the mainstream model of embedded development. At present, for some equipment to do configuration interface, many of

  • ajax schematic

    Ajax by the HTML, JavaScript ™ technology, DHTML and DOM composition, this method can be distinguished clumsy Web interface will be transformed into interactive Ajax applications. The author of this article is a Ajax experts, he demonstrated how these tec

  • Let Ruby On Rails into the enterprise development

    ROR developed by B / S has been 3 years, and early exposure to ROR in early 2006, when there are no Chinese data point, but saying "10 times higher than the Java development efficiency" Let me go look for ROR 4 English information A month later

  • ROR troubled long Axis2 web service calls to be initially resolved

    Problem: ROR how Axis2 call the web service Currently experiencing this problem, Google and Baidu found at numerous times, both at home and abroad have seen virtually no valuable information, the only help is a call ROR about. Net Services article (http:

  • Using Ruby on Rails and Action Web Service to create Web Services

    Action Web Service module will be at Ruby on Rails implementation of Web services. Action Web Service will create the SOAP and XML-RPC Web Service Protocol server-side support. You can use the Declaration and the Action Web Service released the API. ...

  • Buffalo has PHPRPC and those differences

    zhou7707 wrote Asked about Comet support on how to? Chen Jinzhou of Buffalo and has those differences ? 1, PHPRPC with Buffalo positioning different, Buffalo only positioning in a Javascript to Java's remote procedure call. PHPRPC not only suppor ...

  • J2EEer Learn Ruby

    Ruby has long heard of the development of efficient and accurate is to use Ruby on Rails to develop WEB applications very convenient. While J2EE has a lot of very good Opensource framework, automatic code generation tools, can greatly enhance the developm

  • Hibernate performance tuning (the focus of understanding)

    1. Object Cache: fine-grained, in view of the table Record Level, transparent access, without changing the procedure code can greatly enhance the performance of web applications. ORM object cache is a magic weapon for the victory. 2. The merits of th ...

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries