Java Code Examples for org.jxmpp.util.XmppStringUtils#parseLocalpart()

The following examples show how to use org.jxmpp.util.XmppStringUtils#parseLocalpart() . 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: 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
 * (<localpart@domainpart>).
 * <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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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();
}