/*
* Copyright 2010 Jared Hatfield
* SecureMessageReplay Java Client Library
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.unitvectory.securemessagerelay.client.model;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.unitvectory.securemessagerelay.client.SMRException;
import com.unitvectory.securemessagerelay.client.SMRGuid;
import com.unitvectory.securemessagerelay.client.encryption.AESDecryptor;
import com.unitvectory.securemessagerelay.client.encryption.AESEncryptor;
public class SMRMessage {
/**
*
*/
private String mid;
/**
*
*/
private String cid;
/**
*
*/
private String descriptor;
/**
*
*/
private String body;
/**
*
* @param descriptor
* @param body
* @param password
*/
public SMRMessage(String descriptor, String body, String password) {
this.mid = SMRGuid.NewGuid();
this.cid = SMRGuid.NewPassword();
this.descriptor = descriptor;
AESEncryptor encryptor = new AESEncryptor(password);
this.body = encryptor.encrypt(body);
}
public SMRMessage(String xmlcontent) throws SMRException {
try {
InputStream is = new ByteArrayInputStream(
xmlcontent.getBytes("UTF-8"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("message");
Node fstNode = nodeLst.item(0);
this.mid = ParseXMLValueFromElement(fstNode, "mid");
// NOTE: the message may not have a CID if it is retrieved from
// the server
try {
this.cid = ParseXMLValueFromElement(fstNode, "cid");
} catch (Exception e) {
this.cid = null;
}
this.descriptor = ParseXMLValueFromElement(fstNode, "descriptor");
this.body = ParseXMLValueFromElement(fstNode, "body");
} catch (Exception e) {
throw new SMRException();
}
}
private static String ParseXMLValueFromElement(Node fstNode, String name) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(name);
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
return (fstNm.item(0)).getNodeValue();
}
/**
* @return the mid
*/
public String getMid() {
return this.mid;
}
/**
* @return the cid
*/
public String getCid() {
return this.cid;
}
/**
* @return the descriptor
*/
public String getDescriptor() {
return this.descriptor;
}
/**
* @return the body
*/
public String getBody() {
return this.body;
}
/**
*
* @param password
* @return
*/
public String getDecryptedBody(String password) {
AESDecryptor decryptor = new AESDecryptor(password);
return decryptor.decrypt(this.getBody());
}
public String toXML() {
String xmlString;
try {
// Create the document Document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
// Generate the XML content
org.w3c.dom.Element root = doc.createElement("smr");
doc.appendChild(root);
org.w3c.dom.Element message = doc.createElement("message");
root.appendChild(message);
// Add all of the fields to the message
org.w3c.dom.Element midElement = doc.createElement("mid");
message.appendChild(midElement);
org.w3c.dom.Text midXML = doc.createTextNode(this.getMid());
midElement.appendChild(midXML);
if (this.cid != null) {
org.w3c.dom.Element cidElement = doc.createElement("cid");
message.appendChild(cidElement);
org.w3c.dom.Text cidXML = doc.createTextNode(this.getCid());
cidElement.appendChild(cidXML);
}
org.w3c.dom.Element descriptorElement = doc
.createElement("descriptor");
message.appendChild(descriptorElement);
org.w3c.dom.Text descriptorXML = doc.createTextNode(this
.getDescriptor());
descriptorElement.appendChild(descriptorXML);
org.w3c.dom.Element bodyElement = doc.createElement("body");
message.appendChild(bodyElement);
org.w3c.dom.Text bodyXML = doc.createTextNode(this.getBody());
bodyElement.appendChild(bodyXML);
// Output the XML
TransformerFactory tfac = TransformerFactory.newInstance();
Transformer trans = tfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
// Generate the string that will be returned
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
xmlString = sw.toString();
} catch (Exception e) {
xmlString = "<message>Fail</message>";
}
return xmlString;
}
}
|