Example usage for org.jdom2 Element getNamespace

List of usage examples for org.jdom2 Element getNamespace

Introduction

In this page you can find the example usage for org.jdom2 Element getNamespace.

Prototype

public Namespace getNamespace() 

Source Link

Document

Returns the element's Namespace .

Usage

From source file:net.instantcom.mm7.Address.java

License:Open Source License

@Override
public void load(Element e) {
    {//from   w  ww.  j  av  a  2s .  c o m
        String value = e.getAttributeValue("displayOnly", e.getNamespace());
        String valueNoNS = e.getAttributeValue("displayOnly");
        if (value != null) {
            displayOnly = Boolean.parseBoolean(value);
        } else if (valueNoNS != null) {
            displayOnly = Boolean.parseBoolean(valueNoNS);
        }
    }
    {
        String value = e.getAttributeValue("addressCoding", e.getNamespace());
        if (value != null) {
            addressCoding = AddressCoding.map.get(value);
        }
    }
    addressType = AddressType.map.get(e.getName());
    id = e.getAttributeValue("id");
    address = e.getText();
}

From source file:net.instantcom.mm7.Address.java

License:Open Source License

@Override
public Element save(Element parent) {
    Element e = new Element(addressType.toString(), parent.getNamespace());
    if (displayOnly) {
        e.setAttribute("displayOnly", Boolean.toString(displayOnly), parent.getNamespace());
    }/*from   w  w w .j a v  a 2s .  c om*/
    if (addressCoding != null) {
        e.setAttribute("addressCoding", addressCoding.toString(), parent.getNamespace());
    }
    if (id != null) {
        e.setAttribute("id", id, parent.getNamespace());
    }
    e.setText(address);
    return e;
}

From source file:net.instantcom.mm7.DeliverReq.java

License:Open Source License

@Override
public Element save(Element parent) {
    Element e = super.save(parent);
    if (sender != null) {
        Element r = new Element("Sender", e.getNamespace());
        r.addContent(sender.save(e));//from  w ww. ja  v a 2s.c  o  m
        e.addContent(r);
    }
    if (!recipients.isEmpty()) {
        Element r = new Element("Recipients", e.getNamespace());
        addRecipients(r, Address.RecipientType.TO);
        addRecipients(r, Address.RecipientType.CC);
        addRecipients(r, Address.RecipientType.BCC);
        if (r.getContentSize() > 0) {
            e.addContent(r);
        }
    }
    if (linkedId != null) {
        e.addContent(new Element("LinkedID", e.getNamespace()).setText(linkedId));
    }
    if (senderSPI != null) {
        e.addContent(new Element("SenderSPI", e.getNamespace()).setText(senderSPI));
    }
    if (recipientSPI != null) {
        e.addContent(new Element("RecipientSPI", e.getNamespace()).setText(recipientSPI));
    }
    if (timeStamp != null) {
        e.addContent(new Element("TimeStamp", e.getNamespace()).setText(timeStamp.toString()));
    }
    if (replyChargingId != null) {
        e.addContent(new Element("ReplyChargingID", e.getNamespace()).setText(replyChargingId));
    }
    if (priority != null) {
        e.addContent(new Element("Priority", e.getNamespace()).setText(priority.toString()));
    }
    if (Subject != null) {
        e.addContent(new Element("Subject", e.getNamespace()).setText(Subject));
    }
    if (applicId != null) {
        e.addContent(new Element("ApplicID", e.getNamespace()).setText(applicId));
    }
    if (replyApplicId != null) {
        e.addContent(new Element("ReplyApplicID", e.getNamespace()).setText(replyApplicId));
    }
    if (auxApplicInfo != null) {
        e.addContent(new Element("AuxApplicInfo", e.getNamespace()).setText(auxApplicInfo));
    }
    if (content != null) {
        Element c = new Element("Content", e.getNamespace());
        String href = c.getAttributeValue("href");
        if (href != null) {
            c.setAttribute("href", href);
        }
        e.addContent(c);
    }

    return e;
}

From source file:net.instantcom.mm7.DeliverReq.java

License:Open Source License

private void addRecipients(Element e, Address.RecipientType recipientType) {
    Element r = new Element(recipientType.toString(), e.getNamespace());
    for (Address a : recipients) {
        if (a.getRecipientType().equals(recipientType)) {
            r.addContent(a.save(e));/*from w  ww .  ja va 2s . co m*/
        }
    }
    if (r.getContentSize() > 0) {
        e.addContent(r);
    }
}

From source file:net.instantcom.mm7.DeliverRsp.java

License:Open Source License

@Override
public Element save(Element parent) {
    Element e = super.save(parent);
    if (serviceCode != null) {
        e.addContent(new Element("ServiceCode", e.getNamespace()).setText(serviceCode));
    }//from   www  .  j  a  v  a2s .  c om
    return e;
}

From source file:net.instantcom.mm7.MM7Message.java

License:Open Source License

/**
 * Loads a correct subclass of a MM7 message by looking at a SOAP request
 * and instantiating an object of related class. Handles SOAP with
 * attachments./* w ww .  j av a2 s .c  o m*/
 *
 * @param in
 *            input stream to load message from
 * @param contentType
 *            of a request. Can be an multipart or text/xml
 * @param ctx
 *            configuration for loading of message
 *
 * @return an MM7Message instance
 * @throws IOException
 *             if can't deserialize SOAP message or some other IO problem
 *             occurs
 * @throws MM7Error
 *             if SOAP fault is received.
 */
public static MM7Message load(InputStream in, String contentType, MM7Context ctx) throws IOException, MM7Error {
    ContentType ct = new ContentType(contentType);
    BasicContent content = fromStream(in, ct);
    SoapContent soap = null;
    if (content.getParts() != null) {
        String start = (String) ct.getParameter("start");
        if (start != null) {
            if (start.length() == 0) {
                throw new MM7Error("invalid content type, start parameter is empty: " + contentType);
            }
            if (start.charAt(0) == '<') {
                start = start.substring(1, start.length() - 1);
            }
            for (Content c : content.getParts()) {
                if (start.equals(c.getContentId())) {
                    soap = (SoapContent) c;
                    break;
                }
            }
        } else {
            for (Content c : content.getParts()) {
                if (c instanceof SoapContent) {
                    soap = (SoapContent) c;
                    break;
                }
            }
        }
    } else {
        soap = (SoapContent) content;
    }

    // Parse SOAP message to JDOM
    if (soap == null) {
        throw new MM7Error("can't find SOAP parts");
    }
    Document doc = soap.getDoc();

    Element body = doc.getRootElement().getChild("Body", ENVELOPE);
    Element e = (Element) body.getChildren().get(0);
    String message = e.getName();

    // Check if we've got a SOAP fault
    if ("Fault".equals(message)) {
        MM7Error mm7error = new MM7Error();
        mm7error.load(doc.getRootElement());
        throw mm7error;
    }

    // Instantiate a correct message class
    try {
        Class<?> clazz = Class.forName("net.instantcom.mm7." + message);
        MM7Message mm7 = (MM7Message) clazz.newInstance();

        // Load response
        mm7.load(doc.getRootElement());

        // Set content if any
        if (content.getParts() != null && mm7 instanceof HasContent) {
            Element contentElement = e.getChild("Content", e.getNamespace());
            String href = contentElement.getAttributeValue("href", contentElement.getNamespace());
            if (href == null) {
                href = contentElement.getAttributeValue("href");
            }

            // Loop over content, try to match content-location or
            // content-id
            Content payload = null;
            if (href.startsWith("cid:")) {
                // Match by content-id
                String cid = href.substring(4).trim();
                for (Content c : content.getParts()) {
                    if (cid.equals(c.getContentId())) {
                        payload = c;
                        break;
                    }
                }
            } else {
                // Match by content-location
                for (Content c : content.getParts()) {
                    if (href.equals(c.getContentLocation())) {
                        payload = c;
                        break;
                    }
                }
            }
            // We've got a junk message here... try to be a smart cookie and
            // use first non-SOAP part
            if (payload == null) {
                for (Content c : content.getParts()) {
                    if (!(c instanceof SoapContent)) {
                        payload = c;
                        break;
                    }
                }
            }
            if (payload != null) {
                ((HasContent) mm7).setContent(payload);
            }
        }
        return mm7;
    } catch (Throwable t) {
        throw new MM7Error("failed to instantiate message " + message, t);
    }
}

From source file:net.instantcom.mm7.MM7Message.java

License:Open Source License

@Override
public void load(Element element) {
    Element body = element.getChild("Body", element.getNamespace());

    // Extract MM7 namespace from SOAP body
    Iterator<?> i = body.getDescendants(new ElementFilter());
    while (i.hasNext()) {
        Element e = (Element) i.next();
        Namespace ns = e.getNamespace();
        if (ns != null && ns.getURI().contains("MM7")) {
            this.namespace = ns;
            break;
        }/*from www .ja  va  2s. c  o  m*/
    }

    if (this.namespace == null) {
        throw new IllegalStateException("can't autodetect MM7 namespace: " + body.toString());
    }

    Element header = element.getChild("Header", element.getNamespace());
    setTransactionId(header.getChildTextTrim("TransactionID", namespace));
}

From source file:net.instantcom.mm7.MM7Message.java

License:Open Source License

@Override
public Element save(Element parent) {
    Element e = new Element(getClass().getSimpleName(), namespace);
    final String mm7Version = getMm7Version();
    if (mm7Version != null) {
        e.addContent(new Element("MM7Version", e.getNamespace()).setText(mm7Version));
    }//from  ww w.j  av a 2 s .  c  o  m
    return e;
}

From source file:net.instantcom.mm7.MM7Request.java

License:Open Source License

public Element save(Element parent) {
    Element e = super.save(parent);
    if (vaspId != null || vasId != null) {
        Element si = new Element("SenderIdentification", e.getNamespace());
        if (relayServerId != null) {
            si.addContent(new Element("MMSRelayServerID", e.getNamespace()).setText(relayServerId));
        }//w  ww  .j  a  va2  s  . c  o  m
        if (vaspId != null) {
            si.addContent(new Element("VASPID", e.getNamespace()).setText(vaspId));
        }
        if (vasId != null) {
            si.addContent(new Element("VASID", e.getNamespace()).setText(vasId));
        }
        if (senderAddress != null) {
            Element sa = new Element("SenderAddress", e.getNamespace());
            si.addContent(sa);
            if (senderAddress.getAddressType() != null) {
                sa.addContent(senderAddress.save(sa));
            } else {
                sa.addContent(senderAddress.getAddress());
            }
        }
        e.addContent(si);
    }
    return e;
}

From source file:net.instantcom.mm7.MM7Request.java

License:Open Source License

@Override
public void load(Element element) {
    super.load(element);

    Element body = element.getChild("Body", MM7Message.ENVELOPE);
    Element req = (Element) body.getChildren().get(0);
    setVasId(req.getChildTextTrim("VASID", req.getNamespace()));
    setVaspId(req.getChildTextTrim("VASPID", req.getNamespace()));
    setRelayServerId(req.getChildTextTrim("MMSRelayServerID", req.getNamespace()));
}