Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

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

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.jeedevframework.jpush.server.xmpp.handler.IQRegisterHandler.java

License:Open Source License

/**
 * Handles the received IQ packet.//from   ww w.j  a v a2 s. c om
 * 
 * @param packet the packet
 * @return the response to send back
 * @throws UnauthorizedException if the user is not authorized
 */
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ reply = null;

    ClientSession session = sessionManager.getSession(packet.getFrom());
    if (session == null) {
        log.error("Session not found for key " + packet.getFrom());
        reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.internal_server_error);
        return reply;
    }

    if (IQ.Type.get.equals(packet.getType())) {
        reply = IQ.createResultIQ(packet);
        if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
            // TODO
        } else {
            reply.setTo((JID) null);
            reply.setChildElement(probeResponse.createCopy());
        }
    } else if (IQ.Type.set.equals(packet.getType())) {
        try {
            Element query = packet.getChildElement();
            if (query.element("remove") != null) {
                if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                    // TODO
                } else {
                    throw new UnauthorizedException();
                }
            } else {
                String username = query.elementText("username");
                String password = query.elementText("password");
                String email = query.elementText("email");
                String name = query.elementText("name");

                // Verify the username
                if (username != null) {
                    Stringprep.nodeprep(username);
                }

                // Deny registration of users with no password
                if (password == null || password.trim().length() == 0) {
                    reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.not_acceptable);
                    return reply;
                }

                if (email != null && email.matches("\\s*")) {
                    email = null;
                }

                if (name != null && name.matches("\\s*")) {
                    name = null;
                }

                User user = null;
                if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                    try {
                        user = userService.getUserByUsername(session.getUsername());
                    } catch (UserNotFoundException e) {
                        log.info(session.getUsername() + ":" + e.getMessage());
                    }

                } else {
                    try {
                        user = userService.getUserByUsername(session.getUsername());
                    } catch (UserNotFoundException e) {
                        log.info(session.getUsername() + ":" + e.getMessage());
                    }
                }

                if ((user == null) || (user.getId() == null) || (user.getId() == 0)) {
                    user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    user.setEmail(email);
                    user.setName(name);
                    userService.saveUser(user);
                }

                //reply = IQ.createResultIQ(packet);

                Element userprobeResponse = DocumentHelper.createElement(QName.get("query", NAMESPACE));
                userprobeResponse.addElement("username").setText(user.getUsername());
                userprobeResponse.addElement("password").setText(user.getPassword());
                reply = IQ.createResultIQ(packet);
                reply.setChildElement(userprobeResponse);
            }
        } catch (Exception ex) {
            log.error(ex);
            reply = IQ.createResultIQ(packet);
            reply.setChildElement(packet.getChildElement().createCopy());
            if (ex instanceof UserExistsException) {
                reply.setError(PacketError.Condition.conflict);
            } else if (ex instanceof UserNotFoundException) {
                reply.setError(PacketError.Condition.bad_request);
            } else if (ex instanceof StringprepException) {
                reply.setError(PacketError.Condition.jid_malformed);
            } else if (ex instanceof IllegalArgumentException) {
                reply.setError(PacketError.Condition.not_acceptable);
            } else {
                reply.setError(PacketError.Condition.internal_server_error);
            }
        }
    }

    // Send the response directly to the session
    if (reply != null) {
        session.process(reply);
    }
    return null;
}

From source file:org.jeedevframework.jpush.server.xmpp.push.NotificationManager.java

License:Open Source License

/**
 * Creates a new notification IQ and returns it.
 *//*ww  w. ja v  a2s  . com*/
private IQ createNotificationIQ(String apiKey, String appId, String showType, String title, String message,
        String uri, Integer timeToLive) {
    //Random random = new Random();
    //String id = Integer.toHexString(random.nextInt());
    String id = String.valueOf(System.nanoTime());

    Element notification = DocumentHelper.createElement(QName.get("notification", NOTIFICATION_NAMESPACE));
    notification.addElement("id").setText(id);
    notification.addElement("apiKey").setText(apiKey);
    notification.addElement("showType").setText(showType);
    notification.addElement("title").setText(title);
    notification.addElement("message").setText(message);
    notification.addElement("uri").setText(uri);

    IQ iq = new IQ();
    iq.setType(IQ.Type.set);
    iq.setChildElement(notification);

    return iq;
}

From source file:org.jin.dic.data.pub.ldoce.v5.Convert2Html.java

License:Open Source License

private static byte[] convert(String data) throws DocumentException, IOException {
    data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + rmES.matcher(rmDummyTag.matcher(data).replaceAll("")).replaceAll("");
    data = data.replaceAll("\\|", ",");
    _ByteArrayOutputStream bos = new _ByteArrayOutputStream();
    _ByteArrayInputStream bis = new _ByteArrayInputStream(data.getBytes("utf-8"));
    SAXReader saxR = null;// ww w  . ja va2s .  c  o m
    Document doc = null, des = null;
    Element root = null, desRoot = null;
    XMLWriter xmlWriter = null;
    OutputFormat fmt = null;
    saxR = new SAXReader();
    doc = saxR.read(bis);
    root = doc.getRootElement();

    des = DocumentHelper.createDocument();
    desRoot = DocumentHelper.createElement("span");
    desRoot.addAttribute("class", getClass(root));
    Element child;
    List children = root.elements();
    for (int i = 0; i < children.size(); i++) {
        child = (Element) children.get(i);
        addChildren(child, desRoot);
    }
    des.setRootElement(desRoot);

    fmt = OutputFormat.createCompactFormat();
    fmt.setEncoding("utf-16le");
    fmt.setTrimText(false);
    xmlWriter = new XMLWriter(bos, fmt);
    xmlWriter.write(des);
    xmlWriter.close();
    return bos.toByteArray();
}

From source file:org.jin.dic.data.pub.ldoce.v5.Convert2Html.java

License:Open Source License

private static void addChildren(Element s, Element d) {
    Element span = DocumentHelper.createElement("span");
    d.add(span);/*from   w w  w.  j  av  a2s.co  m*/
    if (!s.getName().equalsIgnoreCase("base")) {
        String c;
        if (s.getName().equals("span")) {
            c = s.attributeValue("class");
            if (c == null || c.length() == 0) {
                c = null;
            } else {
                c = getClass(s);
            }
        } else {
            c = getClass(s);
        }
        if (c != null && c.length() > 0)
            span.addAttribute("class", c);
    }
    Iterator i = s.nodeIterator();
    Node node;
    while (i.hasNext()) {
        node = (Node) i.next();
        if (node instanceof Element)
            addChildren((Element) node, span);
        else
            span.add((Node) node.clone());
    }

}

From source file:org.jinglenodes.credit.ChargeServiceProcessor.java

License:Open Source License

public ChargeServiceProcessor(final String elementName, final String xmlns) {
    this.xmlns = xmlns;
    this.requestElement = DocumentHelper.createElement(new QName(elementName, new Namespace("", xmlns)));
}

From source file:org.jinglenodes.credit.CreditServiceProcessor.java

License:Open Source License

public CreditServiceProcessor(final String elementName, final String xmlns) {
    this.xmlns = xmlns;
    this.requestElement = DocumentHelper.createElement(new QName(elementName, new Namespace("", xmlns)));
}

From source file:org.jinglenodes.jingle.Reason.java

License:Open Source License

public Element getElement() {
    final Element parent = DocumentHelper.createElement(REASON);
    final Element child = DocumentHelper.createElement(getType().toString().replace('_', '-'));

    parent.add(child);/*from w w  w.  ja v  a 2s . c  om*/

    if (text != null) {
        final Element text = DocumentHelper.createElement("text");
        parent.add(text);
        text.addText(getText());
    }

    return parent;
}

From source file:org.jinglenodes.relay.RelayEventIQ.java

License:Open Source License

public Element getChildElement() {

    final Element element = DocumentHelper.createElement(new QName(ELEMENT_NAME, new Namespace("", NAMESPACE)));

    if (event != null) {
        element.addAttribute("event", event);
    }//from  w  w w.ja va  2s  .  c om

    if (event != null) {
        element.addAttribute("time", event);
    }

    if (channelId != null) {
        element.addAttribute("id", channelId);
    }
    return element;

}

From source file:org.jinglenodes.relay.RelayIQ.java

License:Open Source License

public Element getChildElement() {

    final Element element = DocumentHelper.createElement(new QName(ELEMENT_NAME, new Namespace("", NAMESPACE)));

    if (host != null) {
        element.addAttribute("host", host);
    }/* w  w w  . j  av a2  s .  c  om*/

    element.addAttribute("localport", localport);

    element.addAttribute("remoteport", remoteport);

    if (channelId != null) {
        element.addAttribute("id", channelId);
    }
    if (protocol != null) {
        element.addAttribute("protocol", protocol);
    }

    return element;

}

From source file:org.jinglenodes.relay.RelayRedirectIQ.java

License:Open Source License

public Element getChildElement() {

    final Element element = DocumentHelper.createElement(new QName(ELEMENT_NAME, new Namespace("", NAMESPACE)));

    if (host != null) {
        element.addAttribute("host", host);
    }/*from   w ww  .  ja  v a2 s.c  o  m*/

    element.addAttribute("port", port);

    if (channelId != null) {
        element.addAttribute("id", channelId);
    }
    return element;

}