📄 message.java
字号:
/*
* Message.java
*
* Created on Antradienis, 2007, Lapkri鑙o 6, 19.04
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
*/
package org.openacs;
import java.io.*;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;
import javax.xml.soap.*;
/**
*
* @author Administrator
*/
abstract public class Message implements Serializable {
/** Creates a new instance of Message */
public Message () {
//id = "intrnl.unset.id."+((name!=null) ? name : "") +(Calendar.getInstance().getTimeInMillis()+3600*1000);
}
abstract protected void createBody (SOAPBodyElement body, SOAPFactory spf) throws SOAPException;
abstract protected void parseBody (SOAPBodyElement body, SOAPFactory f) throws SOAPException;
static public SOAPBodyElement getRequest (SOAPMessage msg) throws SOAPException
{
SOAPBodyElement request = null;
Iterator i1 = msg.getSOAPBody().getChildElements ();
while (i1.hasNext()) {
Node n = (Node)i1.next();
if (n.getNodeType() == Node.ELEMENT_NODE) {
request = (SOAPBodyElement)n;
}
}
return request;
}
static public String getRequestName (SOAPMessage msg) throws SOAPException
{
if (msg.getSOAPBody().hasFault()) return "Fault";
String name = getRequest (msg).getNodeName();
if (name.startsWith("cwmp:")) name = name.substring(5);
return name;
}
public void parse (SOAPMessage msg)
{
try {
SOAPFactory spf = SOAPFactory.newInstance();
SOAPBodyElement soaprequest = getRequest (msg);
SOAPHeader hdr = msg.getSOAPHeader();
id = "device_did_not_send_id"; // or make it null?...
noMore = false;
if (hdr != null) {
try {
id = getHeaderElement (spf, hdr, "ID");
} catch (NoSuchElementException e) {}
try {
noMore = getHeaderElement (spf, hdr, "NoMoreRequests").equals("1");
} catch (NoSuchElementException e) {}
}
name = getRequestName(msg);
if (soaprequest != null) {
parseBody (soaprequest, spf);
}
} catch (NoSuchElementException e) {
System.out.println ("Message parse: No such element: "+e.getMessage());
} catch (SOAPException e) {
System.out.println ("Message parse: SOAPException: "+e.getMessage());
}
}
public void writeTo (OutputStream out)
{
try {
MessageFactory mf = MessageFactory.newInstance();
SOAPFactory spf = SOAPFactory.newInstance();
String s = "<SOAP-ENV:Envelope";
s += " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"";
s += " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"";
s += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
s += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
s += " xmlns:cwmp=\"urn:dslforum-org:cwmp-1-0\"><SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>";
ByteArrayInputStream in = new ByteArrayInputStream (s.getBytes());
SOAPMessage msg = mf.createMessage(null, in);
/*
SOAPMessage msg = mf.createMessage();
SOAPEnvelope env = msg.getSOAPPart().getEnvelope ();
env.addNamespaceDeclaration("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
env.addNamespaceDeclaration("SOAP-ENC","http://schemas.xmlsoap.org/soap/encoding/");
env.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
env.addNamespaceDeclaration(CWMP,URN_CWMP);
*/
//env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
msg.getSOAPHeader().addHeaderElement(spf.createName("ID",CWMP,URN_CWMP)).setValue(id);
msg.getSOAPHeader().addHeaderElement(spf.createName("NoMoreRequests",CWMP,URN_CWMP)).setValue((noMore) ? "1" : "0");
SOAPBodyElement bd = msg.getSOAPBody().addBodyElement(spf.createName(name,CWMP,URN_CWMP));
if (name == null || name.equals("")) name = this.getClass().getSimpleName();
createBody (bd, spf);
//msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, true);
msg.writeTo(out);
} catch (SOAPException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected SOAPElement getRequestChildElement(SOAPFactory f, SOAPElement req, String name) throws SOAPException {
Name n = f.createName(name);
Iterator i = req.getChildElements (n);
boolean b = i.hasNext();
Object o = i.next ();
String c = o.getClass().getName();
return (SOAPElement)o;
// return (SOAPElement)req.getChildElements(f.createName(name)).next();
}
protected SOAPElement getRequestChildElement2 (SOAPFactory f, SOAPElement req, String name) throws SOAPException {
return (SOAPElement)req.getChildElements(f.createName(name, CWMP, URN_CWMP)).next();
}
protected String getRequestElement(SOAPFactory f, SOAPElement req, String name) throws SOAPException {
return getRequestChildElement(f,req,name).getValue();
}
protected SOAPElement getRequestChildElement(SOAPElement req, Name name) throws SOAPException {
return (SOAPElement)req.getChildElements(name).next();
}
protected String getRequestElement(SOAPElement req, Name name) throws SOAPException {
return getRequestChildElement(req,name).getValue();
}
protected String getHeaderElement(SOAPFactory f, SOAPHeader hdr, String name) throws SOAPException {
return ((SOAPHeaderElement)hdr.getChildElements(f.createName(name,CWMP,URN_CWMP)).next()).getValue();
}
protected Hashtable<String,String> parseParamList (SOAPElement body, SOAPFactory spf) throws SOAPException
{
return parseParamList (body, spf, "ParameterValueStruct", "Value");
}
protected Hashtable<String,String> parseParamList (SOAPElement body, SOAPFactory spf, String sn, String vn) throws SOAPException
{
//Hashtable pl = new Hashtable();
Iterator pi = getRequestChildElement (spf, body, "ParameterList").getChildElements(spf.createName(sn));
Name nameKey = spf.createName("Name");
Name nameValue = spf.createName(vn);
Hashtable<String,String> pl = new Hashtable<String,String> ();
while (pi.hasNext()) {
SOAPElement param = (SOAPElement)pi.next();
String key = getRequestElement (param, nameKey);
String value = getRequestElement (param, nameValue);
if (value == null) value = "";
pl.put(key, value);
}
return pl;
}
protected int getArrayCount (SOAPFactory spf, SOAPElement e) throws SOAPException
{
Name nameArray = spf.createName("arrayType","soap-enc","http://schemas.xmlsoap.org/soap/encoding/");
String attr = e.getAttributeValue(nameArray).replaceAll(" ", "");
String c = attr.substring(attr.indexOf('[')+1, attr.length() -1);
return Integer.parseInt(c);
}
public boolean isFault () {
return name.equals("Fault");
}
protected String b2s (boolean b) {
return (b) ? "1" : "0";
}
public String name;
protected String id;
public String getId () {
if (id == null) {
id = "intrnl.unset.id."+((name!=null) ? name : "") +(Calendar.getInstance().getTimeInMillis()+3600*1000)+"."+hashCode();
}
return id;
}
public boolean noMore;
private static final String URN_CWMP = "urn:dslforum-org:cwmp-1-0";
private static final String CWMP = "cwmp";
protected static final String PARAMETER_KEY="ParameterKey";
protected static final String COMMAND_KEY="CommandKey";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -