Example usage for org.dom4j.dom DOMElement DOMElement

List of usage examples for org.dom4j.dom DOMElement DOMElement

Introduction

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

Prototype

public DOMElement(String name, Namespace namespace) 

Source Link

Usage

From source file:com.hand.hemp.push.server.xmpp.handler.IQRegisterHandler.java

License:Open Source License

/**
 * Handles the received IQ packet.//from   w  w  w  . j av a  2 s  .c o m
 *
 * @param packet the packet
 * @return the response to send back
 * @throws UnauthorizedException if the client 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 {
                // generate username from server side
                //String username = query.elementText("username");
                String username = null;
                String password = query.elementText("password");
                String email = query.elementText("email");
                String name = query.elementText("name");
                String androidId = query.elementText("androidId");
                String resourceName = query.elementText("resourceName");

                // Deny registration if androidId is null or resourceName is null
                if (androidId == null || "".equals(androidId) || resourceName == null
                        || "".equals(resourceName)) {
                    reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.not_acceptable);
                    return reply;
                } else {
                    username = IQRegisterHandler.genUsername(androidId, resourceName);
                    log.debug("generated username: " + username);
                }

                // removed because username generate on server
                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;
                }

                HpnsClient client;
                boolean updateHpnsClient = false;
                if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                    client = clientService.getUser(Long.valueOf(session.getUsername()));
                    updateHpnsClient = true;
                } else {
                    try {
                        client = clientService.getUserByUsername(username);
                        log.debug("update exist client");
                        updateHpnsClient = true;
                    } catch (UserNotFoundException unfe) {
                        log.debug("create new client: " + unfe.getMessage());
                        client = new HpnsClient();
                    }
                }
                client.setPassword(password);
                client.setEmail(email);
                client.setName(name);
                client.setAndroidId(androidId);
                client.setResourceName(resourceName);
                if (updateHpnsClient) {
                    client.setLastUpdatedBy("system");
                    client.setLastUpdateDate(new Date());
                    clientService.updateUser(client);
                } else {
                    client.setUsername(username);
                    client.setCreatedBy("system");
                    client.setRegDate(new Date());
                    clientService.saveUser(client);
                }

                //                    HpnsClient hpnsClient;
                //                    if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                //                        hpnsClient = hpnsClientFacade.getClientByUsername(session.getUsername());
                //                    } else {
                //                        hpnsClient = new HpnsClient();
                //                    }
                //                    hpnsClient.setUsername(username);
                //                    hpnsClient.setPassword(password);
                //                    hpnsClient.setEmail(email);
                //                    hpnsClient.setName(name);
                //                    hpnsClientFacade.create(hpnsClient);
                reply = IQ.createResultIQ(packet);
                DOMElement queryEle = new DOMElement("query", Namespace.get("jabber:iq:register"));
                DOMElement usernameEle = new DOMElement("username", Namespace.get("jabber:iq:register"));
                usernameEle.setText(username);
                queryEle.add(usernameEle);
                reply.setChildElement(queryEle);
            }
        } 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:esa.mo.tools.stubgen.GeneratorXsd.java

License:Open Source License

private Object updateMessageField(Object any) {
    if (null != any) {
        if (any instanceof JAXBElement) {
            JAXBElement re = (JAXBElement) any;
            if (re.getValue() instanceof TypeReference) {
                throw new IllegalArgumentException("Direct type not supported in message body of : "
                        + re.getValue().getClass().getSimpleName());
            } else if (re.getValue() instanceof NamedElementReferenceWithCommentType) {
                NamedElementReferenceWithCommentType ne = (NamedElementReferenceWithCommentType) re.getValue();
                String partType = createCompositeElementsDetails(null, false, null, ne.getType(), true, true,
                        null).getTypeName();

                if (ne.getType().isList()) {
                    // the message part is a list, XML schema does not support this at this level so we need to warn
                    getLog().warn(/*w w w  .j  a v  a 2s .com*/
                            "XML Schema does not support top level elements with multiple occurrences of type "
                                    + partType);
                }

                DOMElement e = new DOMElement(
                        new org.dom4j.QName("element", Namespace.get("http://www.w3.org/2001/XMLSchema")), 2);
                e.setAttribute("name", ne.getName());
                e.setAttribute("type", partType);

                return e;
            } else {
                throw new IllegalArgumentException(
                        "Unexpected type in message body of : " + re.getValue().getClass().getSimpleName());
            }
        } else if (!(any instanceof Element)) {
            throw new IllegalArgumentException(
                    "Unexpected type in message body of : " + any.getClass().getSimpleName());
        }
    }

    return any;
}