org.jxmpp.util.XmppStringUtils Java Examples

The following examples show how to use org.jxmpp.util.XmppStringUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractXMPPConnection.java    From Smack with Apache License 2.0 6 votes vote down vote up
protected void sendStreamOpen() throws NotConnectedException, InterruptedException {
    // If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as
    // possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external
    // mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first
    // response from the server (see e.g. RFC 6120 ยง 9.1.1 Step 2.)
    CharSequence to = getXMPPServiceDomain();
    CharSequence from = null;
    CharSequence localpart = config.getUsername();
    if (localpart != null) {
        from = XmppStringUtils.completeJidFrom(localpart, to);
    }
    String id = getStreamId();

    StreamOpen streamOpen = new StreamOpen(to, from, id, config.getXmlLang(), StreamOpen.StreamContentNamespace.client);
    sendNonza(streamOpen);

    XmlEnvironment.Builder xmlEnvironmentBuilder = XmlEnvironment.builder();
    xmlEnvironmentBuilder.with(streamOpen);
    outgoingStreamXmlEnvironment = xmlEnvironmentBuilder.build();
}
 
Example #2
Source File: SoftPhoneManager.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Calls an individual user by their VCard information.
 *
 * @param jid the users JID.
 */
public void callByJID(String jid) {
    if (getStatus() == SipRegisterStatus.Registered) {
        final VCard vcard = SparkManager.getVCardManager().getVCard(XmppStringUtils.parseBareJid(jid));

        if (vcard != null) {
            String number = vcard.getPhoneWork("VOICE");
            if (!ModelUtil.hasLength(number)) {
                number = vcard.getPhoneHome("VOICE");
            }

            if (ModelUtil.hasLength(number)) {
                getDefaultGuiManager().dial(number);
            }
        }
    }
}
 
Example #3
Source File: IncomingCall.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Accepts a <code>JingleSessionRequest</code>.
 *
 * @param request the request.
 */
private void acceptSession(JingleSessionRequest request) {
    toasterManager.close();

    if (ringing != null) {
        ringing.stop();
    }

    // Start the call
    session.startIncoming();

    if (chatRoom == null) {
        chatRoom = SparkManager.getChatManager().getChatRoom( XmppStringUtils.parseBareJid(request.getFrom()) );
        SparkManager.getChatManager().getChatContainer().activateChatRoom(chatRoom);
        SparkManager.getChatManager().getChatContainer().getChatFrame().toFront();
        notifyRoom();
    }
}
 
Example #4
Source File: RoarPopupHelper.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the Nickname of the person sending the message
 * 
 * @param room
 *            the ChatRoom the message was sent in
 * @param message
 *            the actual message
 * @return nickname
 */
public static String getNickname(ChatRoom room, Message message) {
    String nickname = SparkManager.getUserManager().getUserNicknameFromJID(message.getFrom());
    if (room.getChatType() == Message.Type.groupchat) {
        nickname = XmppStringUtils.parseResource(nickname);
    }

    final JivePropertiesExtension extension = ((JivePropertiesExtension) message.getExtension( JivePropertiesExtension.NAMESPACE ));
    final boolean broadcast = extension != null && extension.getProperty( "broadcast" ) != null;

    if ((broadcast || message.getType() == Message.Type.normal || message.getType() == Message.Type.headline)
            && message.getBody() != null) {
        nickname = Res.getString("broadcast") + " - " + nickname;
    }
    return nickname;
}
 
Example #5
Source File: UserManager.java    From Spark with Apache License 2.0 6 votes vote down vote up
public String getNickname() {
    final VCardManager vCardManager = SparkManager.getVCardManager();
    VCard vcard = vCardManager.getVCard();
    if (vcard == null) {
        return SparkManager.getSessionManager().getUsername();
    }
    else {
        String nickname = vcard.getNickName();
        if (ModelUtil.hasLength(nickname)) {
            return nickname;
        }
        else {
            String firstName = vcard.getFirstName();
            if (ModelUtil.hasLength(firstName)) {
                return firstName;
            }
        }
    }

    // Default to node if nothing.
    String username = SparkManager.getSessionManager().getUsername();
    username = XmppStringUtils.unescapeLocalpart(username);

    return username;
}
 
Example #6
Source File: SendMessage.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
private SampleResult waitResponse(SampleResult res, String recipient) throws InterruptedException, SmackException {
    long time = 0;
    do {
        Iterator<Message> packets = responseMessages.iterator();
        Thread.sleep(conn.getPacketReplyTimeout() / 100); // optimistic
        while (packets.hasNext()) {
            Packet packet = packets.next();
            Message response = (Message) packet;
            if (XmppStringUtils.parseBareAddress(response.getFrom()).equals(recipient)) {
                packets.remove();
                res.setResponseData(response.toXML().toString().getBytes());
                if (response.getError() != null) {
                    res.setSuccessful(false);
                    res.setResponseCode("500");
                    res.setResponseMessage(response.getError().toString());
                }
                return res;
            }
        }
        time += conn.getPacketReplyTimeout() / 10;
        Thread.sleep(conn.getPacketReplyTimeout() / 10);
    } while (time < conn.getPacketReplyTimeout());
    throw new SmackException.NoResponseException();
}
 
Example #7
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a domain full JID from the given String.
 *
 * @param jid the JID.
 * @param context the JXMPP context.
 * @return a DomainFullJid.
 * @throws XmppStringprepException if an error happens.
 */
public static DomainFullJid domainFullFrom(String jid, JxmppContext context) throws XmppStringprepException {
	DomainFullJid domainResourceJid;
	if (context.isCachingEnabled()) {
		domainResourceJid = DOMAINRESOURCEJID_CACHE.lookup(jid);
		if (domainResourceJid != null) {
			return domainResourceJid;
		}
	}

	String domain = XmppStringUtils.parseDomain(jid);
	String resource = XmppStringUtils.parseResource(jid);
	try {
		domainResourceJid = new DomainAndResourcepartJid(domain, resource, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}

	if (context.isCachingEnabled()) {
		DOMAINRESOURCEJID_CACHE.put(jid, domainResourceJid);
	}
	return domainResourceJid;
}
 
Example #8
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a domain bare JID.
 *
 * @param jid the JID String.
 * @param context the JXMPP context.
 * @return a domain bare JID.
 * @throws XmppStringprepException if an error occurs.
 */
public static DomainBareJid domainBareFrom(String jid, JxmppContext context) throws XmppStringprepException {
	DomainBareJid domainJid;
	if (context.isCachingEnabled()) {
		domainJid = DOMAINJID_CACHE.lookup(jid);
		if (domainJid != null) {
			return domainJid;
		}
	}

	String domain = XmppStringUtils.parseDomain(jid);
	try {
		domainJid = new DomainpartJid(domain, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}

	if (context.isCachingEnabled()) {
		DOMAINJID_CACHE.put(jid, domainJid);
	}
	return domainJid;
}
 
Example #9
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link EntityFullJid} representing the given String.
 *
 * @param jid the JID's String.
 * @return a full JID representing the input String.
 * @throws XmppStringprepException if an error occurs.
 */
public static EntityFullJid entityFullFrom(String jid) throws XmppStringprepException {
	EntityFullJid fullJid = ENTITY_FULLJID_CACHE.lookup(jid);
	if (fullJid != null) {
		return fullJid;
	}

	String localpart = XmppStringUtils.parseLocalpart(jid);
	String domainpart = XmppStringUtils.parseDomain(jid);
	String resource = XmppStringUtils.parseResource(jid);
	try {
		fullJid = entityFullFrom(localpart, domainpart, resource);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}
	ENTITY_FULLJID_CACHE.put(jid, fullJid);
	return fullJid;
}
 
Example #10
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link EntityBareJid} representing the given unescaped String.
 *
 * @param unescapedJidString the input String.
 * @param context the JXMPP context.
 * @return a bare JID representing the given String.
 * @throws XmppStringprepException if an error occurs.
 */
public static EntityBareJid entityBareFromUnescaped(String unescapedJidString, JxmppContext context) throws XmppStringprepException {
	EntityBareJid bareJid;
	if (context.isCachingEnabled()) {
		bareJid = ENTITY_BAREJID_CACHE.lookup(unescapedJidString);
		if (bareJid != null) {
			return bareJid;
		}
	}

	String localpart = XmppStringUtils.parseLocalpart(unescapedJidString);
	// Some as from(String), but we escape the localpart
	localpart = XmppStringUtils.escapeLocalpart(localpart);

	String domainpart = XmppStringUtils.parseDomain(unescapedJidString);
	try {
		bareJid = new LocalAndDomainpartJid(localpart, domainpart, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(unescapedJidString, e);
	}

	if (context.isCachingEnabled()) {
		ENTITY_BAREJID_CACHE.put(unescapedJidString, bareJid);
	}
	return bareJid;
}
 
Example #11
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link EntityBareJid} representing the given String.
 *
 * @param jid the input String.
 * @param context the JXMPP context.
 * @return a bare JID representing the given String.
 * @throws XmppStringprepException if an error occurs.
 */
public static EntityBareJid entityBareFrom(String jid, JxmppContext context) throws XmppStringprepException {
	EntityBareJid bareJid;
	if (context.isCachingEnabled()) {
		bareJid = ENTITY_BAREJID_CACHE.lookup(jid);
		if (bareJid != null) {
			return bareJid;
		}
	}

	String localpart = XmppStringUtils.parseLocalpart(jid);
	String domainpart = XmppStringUtils.parseDomain(jid);
	try {
		bareJid = new LocalAndDomainpartJid(localpart, domainpart, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}

	if (context.isCachingEnabled()) {
		ENTITY_BAREJID_CACHE.put(jid, bareJid);
	}
	return bareJid;
}
 
Example #12
Source File: JidCreate.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link FullJid} representing the given String.
 *
 * @param jid the JID's String.
 * @return a full JID representing the input String.
 * @throws XmppStringprepException if an error occurs.
 */
public static FullJid fullFrom(String jid) throws XmppStringprepException {
	FullJid fullJid = FULLJID_CACHE.lookup(jid);
	if (fullJid != null) {
		return fullJid;
	}

	String localpart = XmppStringUtils.parseLocalpart(jid);
	String domainpart = XmppStringUtils.parseDomain(jid);
	String resource = XmppStringUtils.parseResource(jid);
	try {
		fullJid = fullFrom(localpart, domainpart, resource);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}
	FULLJID_CACHE.put(jid, fullJid);
	return fullJid;
}
 
Example #13
Source File: JidUtil.java    From jxmpp with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the given CharSequence is a valid entity bare JID. That
 * is, it must consists exactly of a local- and a domainpart
 * (&lt;localpart@domainpart&gt;).
 * <p>
 * This is a convenience method meant to validate user entered bare JIDs. If
 * the given {@code jid} is not a valid bare JID, then this method will
 * throw either {@link NotAEntityBareJidStringException} or
 * {@link XmppStringprepException}. The NotABareJidStringException will
 * contain a meaningful message explaining why the given CharSequence is not a
 * valid bare JID (e.g. "does not contain a '@' character").
 * </p>
 * 
 * @param jidcs the JID CharSequence
 * @return a BareJid instance representing the given JID CharSequence
 * @throws NotAEntityBareJidStringException if the given CharSequence is not a bare JID.
 * @throws XmppStringprepException if an error happens.
 */
public static EntityBareJid validateEntityBareJid(CharSequence jidcs) throws NotAEntityBareJidStringException, XmppStringprepException {
	String jid = jidcs.toString();
	final int atIndex = jid.indexOf('@');
	if (atIndex == -1) {
		throw new NotAEntityBareJidStringException("'" + jid + "' does not contain a '@' character");
	} else if (jid.indexOf('@', atIndex + 1) != -1) {
		throw new NotAEntityBareJidStringException("'" + jid + "' contains multiple '@' characters");
	}
	final String localpart = XmppStringUtils.parseLocalpart(jid);
	if (localpart.length() == 0) {
		throw new NotAEntityBareJidStringException("'" + jid + "' has empty localpart");
	}
	final String domainpart = XmppStringUtils.parseDomain(jid);
	if (domainpart.length() == 0) {
		throw new NotAEntityBareJidStringException("'" + jid + "' has empty domainpart");
	}
	return JidCreate.entityBareFromUnescaped(jid);
}
 
Example #14
Source File: UserManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Escapes a complete JID by examing the Node itself and escaping
 * when neccessary.
 *
 * @param jid the users JID
 * @return the escaped JID.
 */
public static String escapeJID(String jid) {
    if (jid == null) {
        return null;
    }

    final StringBuilder builder = new StringBuilder();
    String node = XmppStringUtils.parseLocalpart(jid);
    String restOfJID = jid.substring(node.length());
    builder.append(XmppStringUtils.escapeLocalpart(node));
    builder.append(restOfJID);
    return builder.toString();
}
 
Example #15
Source File: JID.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
private JID(String local, String domain, String resource) {
    mLocal = local;
    mDomain = domain;
    mResource = resource;

    mValid = !mLocal.isEmpty() && !mDomain.isEmpty()
            // NOTE: domain check could be stronger - compliant with RFC 6122, but
            // server does not accept most special characters
            // NOTE: resource not checked
            && JidUtil.isTypicalValidEntityBareJid(
                    XmppStringUtils.completeJidFrom(mLocal, mDomain));
}
 
Example #16
Source File: FastpathPlugin.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void leaveWorkgroup() {
    workgroupLabel.setText(FpRes.getString("workgroup") + ":");
    logoutButton.setVisible(false);
    joinButton.setVisible(true);
    comboBox.setVisible(true);

    comboBox.removeAllItems();
    // Log into workgroup
    DomainBareJid workgroupService = JidCreate.domainBareFromOrThrowUnchecked("workgroup." + SparkManager.getSessionManager().getServerAddress());
    EntityFullJid jid = SparkManager.getSessionManager().getJID();

    try {
        Collection<String> col = Agent.getWorkgroups(workgroupService, jid, SparkManager.getConnection());
        // Add workgroups to combobox
        Iterator<String> workgroups = col.iterator();
        while (workgroups.hasNext()) {
            String workgroup = workgroups.next();
            String componentAddress = XmppStringUtils.parseDomain(workgroup);
            setComponentAddress(componentAddress);
            comboBox.addItem(XmppStringUtils.parseLocalpart(workgroup));
        }
    }
    catch (XMPPException | SmackException | InterruptedException ee) {
        // If the user does not belong to a workgroup, then don't initialize the rest of the plugin.
        return;
    }

    try {
        agentSession.setOnline(false);
    }
    catch (XMPPException | SmackException | InterruptedException e1) {
        Log.error(e1);
    }
    litWorkspace.unload();
    wgroup = null;

    // UnRegister tab handler
    SparkManager.getChatManager().removeSparkTabHandler(fastpathTabHandler);
}
 
Example #17
Source File: GrowlTalker.java    From Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void onClickCallback( GntpNotification notification )
{
    Log.debug( "Callback clicked: " + notification );
    final String jid = XmppStringUtils.parseBareJid( (String) notification.getContext() );
    final ChatRoom room = SparkManager.getChatManager().getChatRoom( jid );
    SparkManager.getChatManager().getChatContainer().activateChatRoom( room );
    SparkManager.getChatManager().getChatContainer().requestFocusInWindow();
}
 
Example #18
Source File: ContactItem.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
* Updates the displayed name for the contact. This method tries to use an
* alias first. If that's not set, the nickname will be used instead. If
* that's not set either, the JID of the user will be used.
*/
  protected void setDisplayName() {
  	final String displayName = getDisplayName();

      int nickLength = displayName.length();

      LayoutSettings settings = LayoutSettingsManager.getLayoutSettings();
      int windowWidth = settings.getMainWindowBounds() != null ? settings.getMainWindowBounds().width : 50;

      if (nickLength > windowWidth) { // FIXME comparing pixel-width with character count - that can't be good.
          displayNameLabel.setText(XmppStringUtils.unescapeLocalpart(displayName).substring(0, windowWidth) + "...");
      } else {
          displayNameLabel.setText(XmppStringUtils.unescapeLocalpart(displayName));
      }
  }
 
Example #19
Source File: UserManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Unescapes a complete JID by examing the node itself and unescaping when necessary.
 *
 * @param jid the users jid.
 * @return the unescaped JID.
 */
public static String unescapeJID(BareJid jid) {
    if (jid == null) {
        return null;
    }

    final StringBuilder builder = new StringBuilder();
    Localpart node = jid.getLocalpartOrNull();
    Domainpart restOfJID = jid.getDomain();
    if (node != null) {
        builder.append(XmppStringUtils.unescapeLocalpart(node.toString()));
    }
    builder.append(restOfJID);
    return builder.toString();
}
 
Example #20
Source File: JidCreate.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
/**
 * Get a {@link Jid} from the given unescaped String.
 *
 * @param unescapedJidString a unescaped String representing a JID.
 * @return a JID.
 * @throws XmppStringprepException if an error occurs.
 */
public static Jid fromUnescaped(String unescapedJidString) throws XmppStringprepException {
	String localpart = XmppStringUtils.parseLocalpart(unescapedJidString);
	// Some as from(String), but we escape the localpart
	localpart = XmppStringUtils.escapeLocalpart(localpart);

	String domainpart = XmppStringUtils.parseDomain(unescapedJidString);
	String resource = XmppStringUtils.parseResource(unescapedJidString);
	try {
		return from(localpart, domainpart, resource);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(unescapedJidString, e);
	}
}
 
Example #21
Source File: SASLExternalMechanism.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
protected byte[] getAuthenticationText() {
    if (authorizationId != null) {
      return toBytes(authorizationId.toString());
    }

    if (StringUtils.isNullOrEmpty(authenticationId)) {
        return null;
    }

    return toBytes(XmppStringUtils.completeJidFrom(authenticationId, serviceName));
}
 
Example #22
Source File: JidCreate.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
/**
 * Get a {@link BareJid} representing the given String.
 *
 * @param jid the input String.
 * @param context the JXMPP context.
 * @return a bare JID representing the given String.
 * @throws XmppStringprepException if an error occurs.
 */
public static BareJid bareFrom(String jid, JxmppContext context) throws XmppStringprepException {
	BareJid bareJid;
	if (context.isCachingEnabled()) {
		bareJid = BAREJID_CACHE.lookup(jid);
		if (bareJid != null) {
			return bareJid;
		}
	}

	String localpart = XmppStringUtils.parseLocalpart(jid);
	String domainpart = XmppStringUtils.parseDomain(jid);
	try {
		if (localpart.length() != 0) {
			bareJid = new LocalAndDomainpartJid(localpart, domainpart, context);
		} else {
			bareJid = new DomainpartJid(domainpart, context);
		}
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jid, e);
	}

	if (context.isCachingEnabled()) {
		BAREJID_CACHE.put(jid, bareJid);
	}
	return bareJid;
}
 
Example #23
Source File: JidCreate.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
/**
 * Get a {@link Jid} from the given String.
 *
 * @param jidString the input String.
 * @param context the JXMPP context.
 * @return the Jid represented by the input String.
 * @throws XmppStringprepException if an error occurs.
 * @see #from(CharSequence)
 */
public static Jid from(String jidString, JxmppContext context) throws XmppStringprepException {
	String localpart = XmppStringUtils.parseLocalpart(jidString);
	String domainpart = XmppStringUtils.parseDomain(jidString);
	String resource = XmppStringUtils.parseResource(jidString);
	try {
		return from(localpart, domainpart, resource, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(jidString, e);
	}
}
 
Example #24
Source File: JidCreate.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
/**
 * Get a {@link EntityFullJid} representing the given unescaped String.
 *
 * @param unescapedJidString the JID's String.
 * @param context the JXMPP context.
 * @return a full JID representing the input String.
 * @throws XmppStringprepException if an error occurs.
 */
public static EntityFullJid entityFullFromUnescaped(String unescapedJidString, JxmppContext context) throws XmppStringprepException {
	EntityFullJid fullJid;
	if (context.isCachingEnabled()) {
		fullJid = ENTITY_FULLJID_CACHE.lookup(unescapedJidString);
		if (fullJid != null) {
			return fullJid;
		}
	}

	String localpart = XmppStringUtils.parseLocalpart(unescapedJidString);
	// Some as from(String), but we escape the localpart
	localpart = XmppStringUtils.escapeLocalpart(localpart);

	String domainpart = XmppStringUtils.parseDomain(unescapedJidString);
	String resource = XmppStringUtils.parseResource(unescapedJidString);
	try {
		fullJid = new LocalDomainAndResourcepartJid(localpart, domainpart, resource, context);
	} catch (XmppStringprepException e) {
		throw new XmppStringprepException(unescapedJidString, e);
	}

	if (context.isCachingEnabled()) {
		ENTITY_FULLJID_CACHE.put(unescapedJidString, fullJid);
	}
	return fullJid;
}
 
Example #25
Source File: RosterDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Method to handle the Add-Button
    */
   private void addContactButton() {
String errorMessage = Res.getString("title.error");
String jid = getJID();

UIManager.put("OptionPane.okButtonText", Res.getString("ok"));

if(jid.length()==0)
{
    JOptionPane.showMessageDialog(dialog, Res.getString("message.invalid.jid.error"),
		Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
	return;
}

String contact = UserManager.escapeJID(jid);
String nickname = nicknameField.getText();
String group = (String) groupBox.getSelectedItem();

Transport transport = null;
if (publicBox.isSelected()) {
    AccountItem item = (AccountItem) accounts.getSelectedItem();
    transport = item.getTransport();
}

if (transport == null) {
    if (!contact.contains("@")) {
	contact = contact + "@"
		+ SparkManager.getConnection().getXMPPServiceDomain();
    }
} else {
    if (!contact.contains("@")) {
	contact = contact + "@" + transport.getXMPPServiceDomain();
    }
}

if (!ModelUtil.hasLength(nickname) && ModelUtil.hasLength(contact)) {
    // Try to load nickname from VCard
    VCard vcard = new VCard();
    try {
		EntityBareJid contactJid = JidCreate.entityBareFrom(contact);
		vcard.load(SparkManager.getConnection(), contactJid);
	nickname = vcard.getNickName();
    } catch (XMPPException | SmackException | XmppStringprepException | InterruptedException e1) {
	Log.error(e1);
    }
    // If no nickname, use first name.
    if (!ModelUtil.hasLength(nickname)) {
	nickname = XmppStringUtils.parseLocalpart(contact);
    }
    nicknameField.setText(nickname);
}

ContactGroup contactGroup = contactList.getContactGroup(group);
boolean isSharedGroup = contactGroup != null
	&& contactGroup.isSharedGroup();

if (isSharedGroup) {
    errorMessage = Res
	    .getString("message.cannot.add.contact.to.shared.group");
} else if (!ModelUtil.hasLength(contact)) {
    errorMessage = Res.getString("message.specify.contact.jid");
} else if (!XmppStringUtils.parseBareJid(contact).contains("@")) {
    errorMessage = Res.getString("message.invalid.jid.error");
} else if (!ModelUtil.hasLength(group)) {
    errorMessage = Res.getString("message.specify.group");
} else if (ModelUtil.hasLength(contact) && ModelUtil.hasLength(group)
	&& !isSharedGroup) {
    addEntry();
    dialog.setVisible(false);
   } else {

    JOptionPane.showMessageDialog(dialog, errorMessage,
	    Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
}
   }
 
Example #26
Source File: IncomingCallUI.java    From Spark with Apache License 2.0 4 votes vote down vote up
public IncomingCallUI(String jid) {
    setLayout(new GridBagLayout());

    this.jid = XmppStringUtils.parseBareJid(jid);

    vcard = SparkManager.getVCardManager().getVCardFromMemory(XmppStringUtils.parseBareJid(jid));

    final JLabel topLabel = new JLabel();
    topLabel.setIcon(JinglePhoneRes.getImageIcon("INCOMING_CALL_IMAGE"));
    topLabel.setHorizontalTextPosition(JLabel.RIGHT);
    topLabel.setFont(new Font("Dialog", Font.BOLD, 15));
    topLabel.setText(JingleResources.getString("label.voice.request.from"));
    topLabel.setForeground(Color.DARK_GRAY);

    // Add Top Label
    add(topLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

    // Add Calller Block
    buildCallerBlock();

    // Add Buttons
    addButtons();


}
 
Example #27
Source File: JID.java    From desktopclient-java with GNU General Public License v3.0 4 votes vote down vote up
/** Return unescaped local part. */
public String local() {
    return XmppStringUtils.unescapeLocalpart(mLocal);
}
 
Example #28
Source File: ChatRoomOpeningListener.java    From Spark with Apache License 2.0 4 votes vote down vote up
@Override
   public void chatRoomOpened(final ChatRoom room) {

if (!(room instanceof ChatRoomImpl)) // Check for 1on1 Chat
{
    return;
}

final ChatRoomButton sendGameButton = new ChatRoomButton("BS");
room.getToolBar().addChatRoomButton(sendGameButton);
final String opponentJID = ((ChatRoomImpl) room).getJID();

sendGameButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

	final GameOfferPacket offer = new GameOfferPacket();
	offer.setTo(opponentJID);
	offer.setType(IQ.Type.get);

	room.getTranscriptWindow().insertCustomText(
		BsRes.getString("request"), false, false,
		Color.BLUE);
		try
		{
			SparkManager.getConnection().sendStanza(offer);
		}
		catch ( SmackException.NotConnectedException e1 )
		{
			Log.warning( "Unable to send offer to " + opponentJID, e1 );
		}

		SparkManager.getConnection().addAsyncStanzaListener(
		new StanzaListener() {
		    @Override
		    public void processPacket(Stanza stanza) {

			GameOfferPacket answer = (GameOfferPacket) stanza;
			answer.setStartingPlayer(offer
				.isStartingPlayer());
			answer.setGameID(offer.getGameID());
			String name = XmppStringUtils.parseLocalpart(opponentJID);
			if (answer.getType() == IQ.Type.result) {
			    // ACCEPT

			    room.getTranscriptWindow()
				    .insertCustomText(BsRes.getString("accepted", name), false,
					    false, Color.BLUE);

			    createWindow(answer, opponentJID);
			} else {
			    // DECLINE
			    room.getTranscriptWindow()
				    .insertCustomText(BsRes.getString("declined", name), false,
					    false, Color.RED);
			}

		    }
		}, new PacketIDFilter(offer.getPacketID()));

    }

});

   }
 
Example #29
Source File: JID.java    From desktopclient-java with GNU General Public License v3.0 4 votes vote down vote up
/** Return JID as escaped string. */
public String string() {
    return XmppStringUtils.completeJidFrom(mLocal, mDomain, mResource);
}
 
Example #30
Source File: HistoryTranscript.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Builds html string with the stored messages
    * @return String containing the messages as html 
    */
   public final String buildString(List<HistoryMessage> messages){
   	StringBuilder builder = new StringBuilder();
   	final String personalNickname = SparkManager.getUserManager().getNickname();
	Date lastPost = null;
	Jid broadcastnick = null;
	boolean initialized = false;

	for (HistoryMessage message : messages) {
		String color = "blue";

		Jid from = message.getFrom();
		String nickname = SparkManager.getUserManager()
				.getUserNicknameFromJID(message.getFrom().asBareJid());
		String body = org.jivesoftware.spark.util.StringUtils
				.escapeHTMLTags(message.getBody());
		if (nickname.equals(message.getFrom())) {
			BareJid otherJID = message
					.getFrom().asBareJid();
			EntityBareJid myJID = SparkManager.getSessionManager()
					.getBareUserAddress();

			if (otherJID.equals(myJID)) {
				nickname = personalNickname;
			} else {
				nickname = XmppStringUtils.parseLocalpart(nickname);
				broadcastnick = message.getFrom();
			}
		}

		if (!from.asBareJid().equals(
				SparkManager.getSessionManager().getBareUserAddress())) {
			color = "red";
		}

		long lastPostTime = lastPost != null ? lastPost.getTime() : 0;

		int diff;
		if (DateUtils.getDaysDiff(lastPostTime, message.getDate()
				.getTime()) != 0) {
			diff = DateUtils.getDaysDiff(lastPostTime, message
					.getDate().getTime());
		} else {
			diff = DateUtils.getDayOfWeek(lastPostTime)
					- DateUtils.getDayOfWeek(message.getDate()
							.getTime());
		}

		if (diff != 0) {
			if (initialized) {
				builder.append("<tr><td><br></td></tr>");
			}
			builder.append(
					"<tr><td colspan=2><font face=dialog size=3 color=black><b><u>")
					.append(notificationDateFormatter.format(message
							.getDate()))
					.append("</u></b></font></td></tr>");
			initialized = true;
		}

		String value = "(" + messageDateFormatter.format(message.getDate()) + ") ";

		builder.append("<tr valign=top><td colspan=2>");
		builder.append("<font face=dialog size=3 color='").append(color).append("'>");
		builder.append(value);
		if (broadcastnick == null){
			builder.append(nickname + ": ");
		} else {
			builder.append(broadcastnick + ": ");
		}
		builder.append("</font>");
		builder.append("<font face=dialog size=3>");
		builder.append(body);
		builder.append("</font>");
		builder.append("</td></tr><br>");

		lastPost = message.getDate();
		broadcastnick = null;
	}
	builder.append("</table></body></html>");

	return builder.toString();
}