Title: XML Technology in E-Commerce
1XML Technology in E-Commerce
Lecture 4 Case Study XmlMessenger
2Lecture Outline
- Instant Messaging
- Case Study XMLMessenger
- Communication between client and server
- Application classes
- Sending and receiving XML through network
- User login
- Sending and receiving messages
- Summary
3OverviewXML Technologies and Applications
XML Docs Logical Structure Physical Structure
DTD XML Schema Meta-Modeling
XML Parser Well-formedness Validity
SAX
DOM
Applications E-Commerce
4Instant MessagingAn E-Commerce Application
- One of the most popular communication services
- 175 million users are expected by 2002
- Deployment on new client types is expected
mobiles, PDAs, TVs - Vendors
- AOL
- Yahoo
- Infoseek
- ICQ
5Case StudyXMLMessenger
- Simple Client/Server E-Commerce application
- Demonstrates some of XML capabilities utilized by
the current eCommerce systems - Platform and language independent data
representation - Standardized data exchange between involved
parties - Communication protocol implementation
6XML Messenger
Message to User 3
User 4
User 3
Message to User 1
Message to User 3
Messenger Server
Message to User 1
User 2
User 1
7XML MessengerLogin
ltusersgt ltusergtUser 2lt/usergt ltusergtUser
3lt/usergt lt/usersgt
User 3
ltupdate typelogingt ltusergtUser
1lt/usergt lt/updategt
ltusergtUser 1lt/usergt
ltnameInUse/gt
ltusersgt ltusergtUser 2lt/usergt ltusergtUser
3lt/usergt lt/usersgt
User 1
User 2
8XML MessengerMessages
ltmessage toUser 2 fromUser
1gt Hello! lt/messagegt
User 3
ltmessage toUser 2 fromUser
1gt Hello! lt/messagegt
Messenger Server
User 1
User 2
9XML MessengerLogout
ltupdate typelogoutgt ltusergtUser
1lt/usergt lt/updategt
User 3
ltupdate typelogoutgt ltusergtUser
1lt/usergt lt/updategt
ltdisconnect/gt
Messenger Server
User 1
User 2
10XML MessengerClass Diagram
11Class Responsibilities
- MessengerServer
- Listens for new client connections
- Maintains an in-memory XML document with online
users - Manages a set of threads (one thread per client)
- UserThread
- Communicates with a particular client
- MessengerClient
- Communicates with the corresponding thread on the
server - Manages client-side GUI
12Class Responsibilities
- ClientStatus
- Displays a list of online users
- Manages conversations with other users
- Conversation
- Displays the messages exchanged with a particular
user - Provides interface for entering a new message
13XML MessengerDemo
- Demo - Deitel 10, page 263
- Tools
- Java 1.2.2
- Java API for XML Processing (JAXP) 1.0.1
- Classes jaxp.jar and parser.jar
- Demo files
- MessengerServer.java
- UserThread.java
- MessengerClient.java
- ClientStatus.java
- Conversation.java
14ImplementationSending XML through network
- OutputStream output
- Document message
- try
- ((XmlDocument)message).write(output)
-
- catch (IOException e)
- e.printStackTrace()
-
- This fragment is used in
- MessengerClient, method send
- UserThread, method send
15ImplementationReceiving XML from network
InputStream input int bufferSize
0 bufferSize input.available() if
(bufferSize gt 0) byte buf new
bytebufferSize input.read(buf)
InputSource source new InputSource(
new ByteArrayInputStream(buf)) Document
message builder.parse(source) if (message !
null) messageReceived(messa
ge) This fragment is used in UserThread.run and
MessengerClient.runMessengerClient.
16ImplementationUser Login
17ImplementationUser Login
18User Login
- Login message creation (MessengerClient.java)
- factoryDocumentBuilderFactory.newInstance()
- builderfactory.newDocumentBuilder()
-
- public void loginUser()
- Document submitName builder.newDocument()
- Element root submitName.createElement("use
r") - submitName.appendChild(root)
- root.appendChild(
submitName.createTextNode(name.getText())) - send(submitName)
-
19User Login
- Getting output and input streams
(MessengerClient.java) - public void runMessengerClient()
- ...
- output clientSocket.getOutputStream()
- input clientSocket.getInputStream()
- Sending an XML message through the network
(method send) - try
- ((XmlDocument)message).write(output)
-
- catch (IOException e)
- e.printStackTrace()
20User Login
- Reading the XML message from the network stream
(UserThread.java) - public void run()
- ..
- int bufferSize 0
- bufferSize input.available()
- if ( bufferSize gt 0 )
- byte buf new bytebufferSize
- input.read(buf)
- InputSource source new InputSource(
- new ByteArrayInputStream(buf))
- Document message builder.parse(source)
- if ( message ! null )
- messageReceived(message)
- Class InputSource in package org.xml.sax
21User Login
- Analyzing the message (UserThread.java)
- public void messageReceived(Document
message) - Element root message.getDocumentElement()
- if (root.getTagName().equals("user"))
- String enteredName
root.getFirstChild().getNodeValue() - if (server.findUserIndex(enteredName)!
-1) - nameInUse()
- else
- send(server.getUsers())
- username enteredName
- server.addUser(this)
-
-
- .
22User Login
- Initialization of the document with user names
(MessengerServer.java) -
- private Document initUsers()
-
- Document init builder.newDocument()
- init.appendChild(init.createElement("users")
) - return init
-
23User Login
- Adding new user (MessengerServer.java)
- public void addUser(UserThread newUserThread)
- String userName newUserThread.getUsername(
) - updateGUI( "Received new user " userName
) - // notify all users of user's login
- updateUsers( userName, "login" )
- // add new user element to Document users
- Element usersRoot users.getDocumentElement
() - Element newUser users.createElement(
"user" ) - newUser.appendChild(
- users.createTextNode( userName ) )
- usersRoot.appendChild( newUser )
- updateGUI( "Added user " userName )
- // add to Vector onlineUsers
- onlineUsers.addElement( newUserThread )
24User Login
- Notification for new user login
(MessengerServer.java) - public void updateUsers(String userName,
- String type)
- Document doc builder.newDocument()
- Element root doc.createElement( "update" )
- Element userElt doc.createElement( "user" )
- doc.appendChild( root )
- root.setAttribute( "type", type )
- root.appendChild( userElt )
- userElt.appendChild(doc.createTextNode(userName
)) - // send to all users
- for (int i 0 i lt onlineUsers.size() i)
- UserThread receiver
(UserThread)onlineUsers.elementAt( i ) - receiver.send( doc )
25Message Sending
26Message Sending
27Message Sending
- Construction of a message in the conversation
window (Conversation.java) - public void submitMessage()
-
- Document sendMessage
-
- sendMessage builder.newDocument()
- Element root sendMessage.createElement("mess
age") - root.setAttribute( "to", target )
- root.setAttribute( "from", clientStatus.getUse
r() ) - root.appendChild(
- sendMessage.createTextNode(
messageToSend )) - sendMessage.appendChild( root )
- client.send( sendMessage )
-
28Message Sending
- Message handling in MessengerServer
-
- public void sendMessage(Document message)
- Element root message.getDocumentElement()
- String from root.getAttribute("from")
- String to root.getAttribute("to")
- int index findUserIndex(to)
-
- UserThread receiver
- (UserThread) onlineUsers.elementAt(index)
- receiver.send(message)
-
29Message Sending
- Message handling on the recipient side
(MessengerClient.java) - public void messageReceived(Document message)
- Element root message.getDocumentElement()
-
- if(root.getTagName().equals("message"))
- String from root.getAttribute( "from" )
- String messageText
root.getFirstChild().getNodeValue() -
- int index findConversationIndex( from )
-
- Conversation receiver
- (Conversation) conversations.elementAt(i
ndex) - receiver.updateGUI(from" "messageText)
30Summary
- Demonstration of XML technology for a simple
E-Commerce application - Implementation of a communication protocol with
XML messages - Handling of in-memory XML documents through DOM
calls - Sending and receiving XML through network
connection
- Read Deitel 10
- Assignment
- Modify the login message to include information
about login time. Update the user interface
accordingly - DeitelEx 10.4
- For more detailed explanation and some hints see
the course site.