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