DOC PREVIEW
CMU ISM 95702 - Homework

This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lab 3 Due in class Thursday October 28, 2004Understanding UDP, TCP, SOAP and RMI/** Figure 3.3* Title: <p>* Description: <p>* Copyright: Copyright (c) Daniel Nehren<p>* Company: Openport Technology Inc.<p>* @author Daniel Nehren* @version 1.0*/import java.io.*;import org.w3c.dom.*;import org.apache.xerces.dom.*;import org.apache.xml.serialize.*;/*** identifies the XML response* here is an example for a correct XML response* <http-response>* <responseMessage>[the response Messasge. 'OK' ,'Error' or 'Exception']</responseCode>* <responseCode>[application specific return code. A text Node with a numeric value ]</responseCode>* <response>* [ application specific response can be anElement or a text Node ]* [ in case of an Exception or Error it will be a text node with the error message]* </response>* </http-response>**/public class ServiceResponse{public final static String RESPONSE_MESSAGE_TAG_NAME = "responseMessage";public final static String RESPONSE_CODE_TAG_NAME = "responseCode";public final static String RESPONSE_TAG_NAME = "response";public final static String ROOT_TAG_NAME = "http-response";protected Document dom;public ServiceResponse(Document response){dom = response;}public ServiceResponse(){dom = new DocumentImpl();initializeResponse();}//initializes an empty responseprivate void initializeResponse(){Element root = dom.createElement(ROOT_TAG_NAME);dom.appendChild(root);Element eResponseMessage = dom.createElement(RESPONSE_MESSAGE_TAG_NAME);eResponseMessage.appendChild(dom.createTextNode(""));Element eResponseCode = dom.createElement(RESPONSE_CODE_TAG_NAME);eResponseCode.appendChild(dom.createTextNode("0"));root.appendChild(eResponseMessage);root.appendChild(eResponseCode);Element eResponse = dom.createElement(RESPONSE_TAG_NAME);root.appendChild(eResponse);}public String getResponseMessage() throws HttpServiceException{try{return getTextAttribute(RESPONSE_MESSAGE_TAG_NAME);}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public void setResponseMessage(String responseMessage) throws HttpServiceException{try{setTextAttribute(RESPONSE_MESSAGE_TAG_NAME,responseMessage);}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public int getResponseCode() throws HttpServiceException{try{Integer value =new Integer(getTextAttribute(RESPONSE_CODE_TAG_NAME));return value.intValue();}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public void setResponseCode(int responseCode) throws HttpServiceException{try{setTextAttribute(RESPONSE_CODE_TAG_NAME, String.valueOf(responseCode));}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public Node getResponse() throws HttpServiceException{try{Node response =((NodeList)dom.getElementsByTagName(RESPONSE_TAG_NAME)).item(0);return response.getFirstChild().cloneNode(true);}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public Element createElementNode(String elementName){return dom.createElement(elementName);}public Text createTextNode(String value){return dom.createTextNode(value);}public void setResponse(Node response) throws HttpServiceException{try{//just in case they create the element from somewhere elseNode reponseElement =((NodeList)dom.getElementsByTagName(RESPONSE_TAG_NAME)).item(0);Node oldResponse = reponseElement.getFirstChild();if(oldResponse != null)reponseElement.removeChild(oldResponse);reponseElement.appendChild(response);}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Invalid Response Format.");}}public byte[] serializeResponseToByteArray(String encoding)throws HttpServiceException{try{return serializeDOM(encoding).toByteArray();}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Error during serialization");}}public String serializeResponseToString(String encoding)throws HttpServiceException{try{return serializeDOM(encoding).toString();}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Error during serialization");}}private ByteArrayOutputStream serializeDOM(String encoding)throws HttpServiceException{try{ByteArrayOutputStream bytes = new ByteArrayOutputStream (4096) ;PrintWriter out = new PrintWriter (new OutputStreamWriter (bytes,encoding),true) ;OutputFormat of = new OutputFormat(dom,encoding,true);XMLSerializer serializer = new XMLSerializer(out,of);serializer.serialize(dom);out.close();return bytes;}catch(Exception ex){ex.printStackTrace(System.err);throw new HttpServiceException("Error during serialization");}}protected String getTextAttribute(String name){Node textAttributeNode =((NodeList)dom.getElementsByTagName(name)).item(0);Node textAttribute = textAttributeNode.getFirstChild();if(textAttribute.getNodeType() == Node.TEXT_NODE)return textAttribute.getNodeValue();elsereturn null;}protected void setTextAttribute(String name, String value){if(value == null)value = "";Node textAttributeNode =((NodeList)dom.getElementsByTagName(name)).item(0);Node textAttribute = textAttributeNode.getFirstChild();textAttribute.setNodeValue(value);}}Figure 3.3import java.util.*; // Figure 3.5public class DBServiceClient{private Vector resultSet;private String serviceURL;public static void main(String[] args){String serviceURL = "http://localhost:8080/SOAPDemo/HttpService";try{DBServiceClient serviceClient = new DBServiceClient(serviceURL);serviceClient.executeSQLStatement("SELECT * FROM TABLE");System.out.println(serviceClient.getResultSet());}catch(Exception ex){System.out.println("something went wrong: " + ex.getMessage());ex.printStackTrace(System.err);}}public DBServiceClient(String serviceURL){this.serviceURL = serviceURL;}public void executeSQLStatement(String statement) throws Exception{HttpServiceClient client = new HttpServiceClient(serviceURL);DBServiceRequest request = new DBServiceRequest();request.setSqlStatement(statement);DBServiceResponse response =new DBServiceResponse(client.executeRequest(request));resultSet = response.getResultSet();}public Vector getResultSet(){return resultSet;}} Figure 3.5/** Figure 3.7* Title: <p>* Description: <p>* Copyright: Copyright (c) Daniel Nehren<p>* Company: Openport Technology Inc.<p>* @author Daniel Nehren* @version 1.0*//*** The main exception for the HttpService*/public class


View Full Document

CMU ISM 95702 - Homework

Documents in this Course
Homework

Homework

12 pages

Lecture

Lecture

25 pages

Lecture

Lecture

21 pages

Lecture

Lecture

24 pages

Exam

Exam

11 pages

Homework

Homework

16 pages

lecture

lecture

38 pages

review

review

7 pages

lecture

lecture

18 pages

review

review

8 pages

Chapter2

Chapter2

32 pages

Lecture 4

Lecture 4

47 pages

Lecture

Lecture

22 pages

Naming

Naming

26 pages

lecture

lecture

34 pages

lecture

lecture

42 pages

lecture

lecture

112 pages

Lecture

Lecture

33 pages

Axis

Axis

43 pages

lecture

lecture

32 pages

review

review

17 pages

Lecture

Lecture

53 pages

Lecture

Lecture

80 pages

Lab

Lab

14 pages

Load more
Download Homework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?