public static LocalComponentSession createSession(String serverName, XmlPullParser xpp, Connection connection)
            throws XmlPullParserException {
        String domain = xpp.getAttributeValue("", "to");
        boolean allowMultiple = xpp.getAttributeValue("", "allowMultiple") != null;

        Log.debug("LocalComponentSession: [ExComp] Starting registration of new external component for domain: {}", domain);

        // Retrieve list of namespaces declared in current element (OF-2556)
        connection.setAdditionalNamespaces(XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(xpp));

        final Element stream = DocumentHelper.createElement(QName.get("stream", "stream", "http://etherx.jabber.org/streams"));
        final Document document = DocumentHelper.createDocument(stream);
        document.setXMLEncoding(StandardCharsets.UTF_8.toString());
        stream.add(Namespace.get("jabber:component:accept"));

        // Check that a domain was provided in the stream header
        if (domain == null) {
            Log.debug("LocalComponentSession: [ExComp] Domain not specified in stanza: {}", xpp.getText());
            // Include the bad-format in the response and close the underlying connection.
            connection.deliverRawText(StringUtils.asUnclosedStream(document));
            connection.close(new StreamError(StreamError.Condition.bad_format, "Domain not specified in 'to' attribute."));
            return null;
        }
        stream.addAttribute("from", domain);

        // Get the requested subdomain
        String subdomain = domain;
        int index = domain.indexOf(serverName);
        if (index > -1) {
            subdomain = domain.substring(0, index -1);
        }
        domain = subdomain + "." + serverName;
        JID componentJID = new JID(domain);
        // Check that an external component for the specified subdomain may connect to this server
        if (!ExternalComponentManager.canAccess(subdomain)) {
            Log.debug("LocalComponentSession: [ExComp] Component is not allowed to connect with subdomain: {}", subdomain);
            // Close the underlying connection
            connection.deliverRawText(StringUtils.asUnclosedStream(document));
            connection.close(new StreamError(StreamError.Condition.host_unknown, "Component is not allowed to connect with the requested subdomain."));
            return null;
        }
        // Check that a secret key was configured in the server
        String secretKey = ExternalComponentManager.getSecretForComponent(subdomain);
        if (secretKey == null) {
            Log.debug("LocalComponentSession: [ExComp] A shared secret for the component was not found.");
            // Include the internal-server-error in the response and close the underlying connection.
            connection.deliverRawText(StringUtils.asUnclosedStream(document));
            connection.close(new StreamError(StreamError.Condition.internal_server_error));
            return null;
        }
        // Check that the requested subdomain is not already in use
        if (!allowMultiple && InternalComponentManager.getInstance().hasComponent(componentJID)) {
            Log.debug("LocalComponentSession: [ExComp] Another component is already using domain: {}", domain);
            // Domain already occupied so return a conflict error and close the connection
            connection.deliverRawText(StringUtils.asUnclosedStream(document));
            connection.close(new StreamError(StreamError.Condition.conflict, "The requested domain is already being used by another component."));
            return null;
        }

        // Create a ComponentSession for the external component
        LocalComponentSession session = SessionManager.getInstance().createComponentSession(componentJID, connection);
        connection.registerCloseListener(new ConnectionCloseListener() {
            @Override
            public CompletableFuture<Void> onConnectionClosing(@Nullable Object handback) {
                return CompletableFuture.runAsync(() -> SessionManager.getInstance().removeComponentSession((LocalComponentSession) handback));
            }
        }, session);
        session.component = new LocalExternalComponent(session, connection);

        try {
            Log.debug("LocalComponentSession: [ExComp] Send stream header with ID: {} for component with domain: {}", session.getStreamID(), domain);
            stream.addAttribute("id", session.getStreamID().toString());

            connection.deliverRawText(StringUtils.asUnclosedStream(document));

            // Return session although session has not been authentication yet. Until it is authenticated traffic will
            // be rejected except for authentication requests
            session.defaultSubdomain = subdomain;
            return session;
        }
        catch (Exception e) {
            Log.error("An error occurred while creating a ComponentSession", e);
            // Close the underlying connection
            connection.close(new StreamError(StreamError.Condition.internal_server_error));
            return null;
        }
    }
Returns a newly created session between the server and a component. The session will be
created and returned only if all the checkings were correct.<p>

A domain will be bound for the new connecting component. This method is following
the JEP-114 where the domain to bind is sent in the TO attribute of the stream header.

@param serverName the name of the server where the session is connecting to.
@param xpp     the parser that is reading the provided XML through the connection.
@param connection the connection with the component.
@return a newly created session between the server and a component.
@throws XmlPullParserException if there was an XML error while creating the session.
xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalComponentSession.java