Example usage for org.dom4j QName QName

List of usage examples for org.dom4j QName QName

Introduction

In this page you can find the example usage for org.dom4j QName QName.

Prototype

public QName(String name, Namespace namespace) 

Source Link

Usage

From source file:com.zimbra.common.soap.Element.java

License:Open Source License

public static void main(String[] args) throws ContainerException, SoapParseException {
    System.out.println(Element.parseJSON("{ 'a':'b'}").getAttribute("a", null));
    System.out.println(Element.parseJSON("{ '_attrs' : {'a':'b'}}").getAttribute("a", null));
    System.out.println(Element.parseJSON("{foo:' bar'}").getAttribute("foo", null));
    System.out.println(Element.parseJSON("{foo:'bar'}").getAttribute("foo", null));
    System.out.println(Element.parseJSON("{foo:''}").getAttribute("foo", null));
    System.out.println(Element.parseJSON("{ \"items\" : [ ] }"));
    System.out.println(Element.parseJSON("{ '_attrs' : {'a':[]}}").getAttribute("a", null));

    org.dom4j.Namespace bogusNS = org.dom4j.Namespace.get("bogus", "");
    QName qm = new QName("m", bogusNS);

    SoapProtocol proto = SoapProtocol.SoapJS;
    Element ctxt = new JSONElement(proto.getHeaderQName()).addUniqueElement(HeaderConstants.E_CONTEXT);
    ctxt.addNonUniqueElement(HeaderConstants.E_SESSION).setText("3").addAttribute(HeaderConstants.A_ID, 3);
    System.out.println(ctxt.getAttribute(HeaderConstants.E_SESSION, null));

    Element env = testMessage(new JSONElement(proto.getEnvelopeQName()), proto, qm);
    System.out.println(env);/*from www. j  a  v  a  2s  .  com*/
    System.out.println(Element.parseJSON(env.toString()));

    proto = SoapProtocol.Soap12;
    env = testMessage(new XMLElement(proto.getEnvelopeQName()), proto, qm);
    System.out.println(env.prettyPrint());
    System.out.println("             name: " + env.getName());
    System.out.println("   qualified name: " + env.getQualifiedName());
    System.out.println("            qname: " + env.getQName());

    Element e = testContacts(new JSONElement(MailConstants.GET_CONTACTS_RESPONSE));
    System.out.println(e);
    System.out.println(e.prettyPrint());
    testKeyValuePairs(e);

    System.out.println(Element.parseJSON(e.toString()));
    testKeyValuePairs(Element.parseJSON(e.toString()));
    System.out.println(Element.parseJSON(e.toString(), XMLElement.mFactory).prettyPrint());

    e = testContacts(new XMLElement(MailConstants.GET_CONTACTS_RESPONSE));
    System.out.println(e.prettyPrint());
    for (Element elt : e.listElements())
        System.out.println("  found: id=" + elt.getAttribute("ID", null));
    testKeyValuePairs(e);

    //        System.out.println(com.zimbra.common.soap.SoapProtocol.toString(e.toXML(), true));
    System.out.println(new XMLElement("test").setText("  this\t    is\nthe\rway ").getTextTrim() + "|");
    System.out.println(Element.parseJSON(
            "{part:\"TEXT\",t:null,h:true,i:\"false\",\"ct\":\"\\x25multipart\\u0025\\/mixed\",\\u0073:3718}")
            .toString());
    try {
        Element.parseJSON("{\"wkday\":{\"day\":\"TU\"},\"wkday\":{\"day\":\"WE\"},\"wkday\":{\"day\":\"FR\"}}");
    } catch (SoapParseException spe) {
        System.out.println("caught exception (expected): " + spe.getMessage());
    }

    System.out.println(
            new XMLElement("test").addAttribute("x", (String) null).addAttribute("x", "", Disposition.CONTENT)
                    .addAttribute("x", "bar").addAttribute("x", (String) null));
    System.out.println(new JSONElement("test").addAttribute("x", (String) null)
            .addAttribute("x", "foo", Disposition.CONTENT).addAttribute("x", "bar")
            .addAttribute("x", (String) null));

    try {
        System.out.println("foo: |" + Element.parseXML("<test><foo/></test>").getAttribute("foo") + "|");
    } catch (Exception x) {
        System.out.println("error parsing XML element: " + x);
    }
}

From source file:com.zimbra.common.soap.SoapProtocol.java

License:Open Source License

/** Package-Private constructor to prevent anyone else from
extending *///w w w.j a  v a  2 s.co  m
SoapProtocol() {
    mFaultQName = new QName("Fault", getNamespace());
    mEnvelopeQName = new QName("Envelope", getNamespace());
    mBodyQName = new QName("Body", getNamespace());
    mHeaderQName = new QName("Header", getNamespace());
    mFactory = getFactory();
}

From source file:com.zimbra.examples.extns.samlprovider.SamlAuthProvider.java

License:Open Source License

private static Element getSamlAssertionRequest(String samlAssertionId) {
    Element envelope = new Element.XMLElement(new QName("Envelope", SoapProtocol.Soap11.getNamespace()));
    Element body = envelope.addElement(new QName("Body", SoapProtocol.Soap11.getNamespace()));
    Element requestElt = body.addElement(new QName("AssertionIDRequest", SAML_PROTOCOL_NS));
    Date now = new Date();
    requestElt.addAttribute("ID", "id-" + now.getTime());
    requestElt.addAttribute("Version", "2.0");
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    requestElt.addAttribute("IssueInstant", dateFormat.format(now));
    Element idRefElt = requestElt.addElement(new QName("AssertionIDRef", SAML_PROTOCOL_NS));
    idRefElt.addText(samlAssertionId);//  w  ww  .  j a  v  a2s  .c o  m
    return envelope;
}

From source file:com.zimbra.examples.extns.samlprovider.SamlRequestHandler.java

License:Open Source License

/**
 * Processes HTTP POST requests./*from   ww w  .j  av  a2  s. c om*/
 *
 * @param req request message
 * @param resp response message
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    Element respEnv;
    try {
        Element reqEnv = Element.parseXML(req.getInputStream());
        Element reqBody = reqEnv.getElement("Body");
        Element requestElt = reqBody.getElement("AssertionIDRequest");
        Element idRefElt = requestElt.getElement("AssertionIDRef");

        respEnv = new Element.XMLElement(new QName("Envelope", SoapProtocol.Soap11.getNamespace()));
        Element respBody = respEnv.addElement(new QName("Body", SoapProtocol.Soap11.getNamespace()));
        Element respElt = respBody.addElement(new QName("Response", SAML_PROTOCOL_NS));
        Date now = new Date();
        respElt.addAttribute("ID", "id-" + now.getTime());
        respElt.addAttribute("Version", "2.0");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        respElt.addAttribute("IssueInstant", dateFormat.format(now));
        respElt.addAttribute("InResponseTo", requestElt.getAttribute("ID"));
        Element statusElt = respElt.addElement(new QName("Status", SAML_PROTOCOL_NS));
        Element statusCodeElt = statusElt.addElement(new QName("StatusCode", SAML_PROTOCOL_NS));

        if (requestElt != null) {
            String assertionId = idRefElt.getTextTrim();
            Element assertion = samlAssertionsMap.get(assertionId);
            if (assertion == null) {
                statusCodeElt.addAttribute("Value", "urn:oasis:names:tc:SAML:2.0:status:Requester");
                Element statusMsgElt = statusElt.addElement(new QName("StatusMessage", SAML_PROTOCOL_NS));
                statusMsgElt.addText("No assertion found corresponding to the id: " + assertionId);
            } else {
                statusCodeElt.addAttribute("Value", "urn:oasis:names:tc:SAML:2.0:status:Success");
                respElt.addElement(assertion.clone());
            }
        }
    } catch (Exception e) {
        ZimbraLog.extensions.error(SystemUtil.getStackTrace(e));
        throw new IOException(e);
    }

    String respEnvStr = respEnv.toString();
    if (ZimbraLog.extensions.isDebugEnabled())
        ZimbraLog.extensions.debug("SAML response: " + respEnvStr);
    resp.getOutputStream().print(respEnvStr);
}

From source file:com.zimbra.soap.DocumentDispatcher.java

License:Open Source License

public void registerHandler(QName qname, DocumentHandler handler) {
    if (handler instanceof AdminDocumentHandler) {
        if (!(includeList == null) && !includeList.isEmpty() && !includeList
                .contains(String.format("%s::%s", qname.getNamespaceURI(), qname.getQualifiedName()))) {
            ZimbraLog.soap.debug("skipping %s::%s", qname.getNamespaceURI(), qname.getQualifiedName());
            return;
        }/* ww  w.j  a  va 2 s . co m*/
        ZimbraLog.soap.debug("Registering %s::%s", qname.getNamespaceURI(), qname.getQualifiedName());
    }

    handlers.put(qname, handler);
    QName respQName = responses.get(qname);
    if (respQName == null) {
        String reqName = qname.getName();
        String respName;
        if (reqName.endsWith(REQUEST_SUFFIX)) {
            respName = reqName.substring(0, reqName.length() - REQUEST_SUFFIX.length()) + RESPONSE_SUFFIX;
        } else {
            respName = reqName + RESPONSE_SUFFIX;
        }
        respQName = new QName(respName, qname.getNamespace());
        responses.put(qname, respQName);
    }
    handler.setResponseQName(respQName);
}

From source file:cz.fi.muni.xkremser.editor.server.fedora.utils.FoxmlUtils.java

License:Open Source License

public static org.dom4j.Element createModsCollectionEl() {
    org.dom4j.Element modsCollection = DocumentHelper
            .createElement(new QName("modsCollection", Namespaces.mods));
    modsCollection.add(Namespaces.mods);
    String modsSchema = "http://www.loc.gov/standards/mods/v3/mods-3-3.xsd";
    modsCollection.addAttribute("schemaLocation", Namespaces.mods.getURI() + " " + modsSchema);
    return modsCollection;
}

From source file:cz.fi.muni.xkremser.editor.server.fedora.utils.FoxmlUtils.java

License:Open Source License

public static void addRelationshipToRelsExt(org.dom4j.Document relsExtDoc, RelsExtRelation reference) {
    org.dom4j.Element descriptionEl = findRelsExtDescriptionElement(relsExtDoc);
    org.dom4j.Element referenceEl = descriptionEl
            .addElement(new QName(reference.getRelationName().getStringRepresentation(), Namespaces.kramerius));
    String resourceStr = "info:fedora/".concat(Constants.FEDORA_UUID_PREFIX + reference.getTargetUuid());
    referenceEl.addAttribute(new QName("resource", Namespaces.rdf), resourceStr);
}

From source file:cz.fi.muni.xkremser.editor.server.fedora.utils.FoxmlUtils.java

License:Open Source License

public static org.dom4j.Document createRelsExtSkeleton(String uuid, String model, Policy policy) {
    Namespace rdfNs = Namespaces.rdf;
    Namespace krameriusNs = Namespaces.kramerius;
    org.dom4j.Element rootElement = DocumentHelper.createElement(new QName("RDF", rdfNs));
    rootElement.add(rdfNs);/*w  ww.  j  a v  a2  s .com*/
    rootElement.add(krameriusNs);
    org.dom4j.Document doc = DocumentHelper.createDocument(rootElement);
    org.dom4j.Element description = rootElement.addElement(new QName("Description", rdfNs));
    description.addAttribute(new QName("about", rdfNs),
            "info:fedora/".concat(Constants.FEDORA_UUID_PREFIX + uuid));
    org.dom4j.Element hasModel = description.addElement(new QName("hasModel", Namespaces.fedora_model));
    hasModel.addAttribute(new QName("resource", rdfNs), "info:fedora/model:".concat(model));
    org.dom4j.Element itemId = description.addElement(new QName("itemID", Namespaces.oai));
    itemId.addText(Constants.FEDORA_UUID_PREFIX + uuid);
    org.dom4j.Element krameriusPolicy = description.addElement(new QName("policy", krameriusNs));
    krameriusPolicy.addText("policy:".concat(policy.toString().toLowerCase()));
    return doc;
}

From source file:cz.fi.muni.xkremser.editor.server.newObject.FoxmlBuilder.java

License:Open Source License

private void createDocumentAndRootElement() {
    rootElement = DocumentHelper.createElement(new QName("digitalObject", Namespaces.foxml));
    document = DocumentHelper.createDocument(rootElement);
    document.getRootElement().add(Namespaces.foxml);
    document.getRootElement().add(Namespaces.xsi);
    document.getRootElement().addAttribute(new QName("schemaLocation", Namespaces.xsi),
            "info:fedora/fedora-system:def/foxml# http://www.fedora.info/definitions/1/0/foxml1-1.xsd");
    rootElement.addAttribute("VERSION", "1.1");
    rootElement.addAttribute("PID", pid);
}

From source file:cz.fi.muni.xkremser.editor.server.newObject.FoxmlBuilder.java

License:Open Source License

private Element createDatastreamElement(Element rootElement, String state, DATASTREAM_CONTROLGROUP controlGroup,
        DATASTREAM_ID dsId) {//  w w w  .j  a  v  a 2s  .co m
    Element dataStream = rootElement.addElement(new QName("datastream", Namespaces.foxml));
    dataStream.addAttribute("VERSIONABLE", VERSIONABLE.toString());
    dataStream.addAttribute("STATE", state);
    dataStream.addAttribute("CONTROL_GROUP", controlGroup.name());
    dataStream.addAttribute("ID", dsId.getValue());
    return dataStream;
}