mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 18:59:41 +02:00
Refactoring work.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5361 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
e558ee8fa6
commit
1df8baa6f7
10 changed files with 238 additions and 252 deletions
|
@ -1,10 +1,6 @@
|
|||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A Privacy IQ Packet, is used by the {@see PrivacyListManager} and {@see PrivacyProvider} to allow
|
||||
|
@ -34,7 +30,7 @@ public class Privacy extends IQ {
|
|||
private String defaultName;
|
||||
/** itemLists holds the set of privacy items classified in lists. It is a map where the
|
||||
* key is the name of the list and the value a collection with privacy items. **/
|
||||
private Map itemLists = new HashMap();
|
||||
private Map<String, List<PrivacyItem>> itemLists = new HashMap<String, List<PrivacyItem>>();
|
||||
|
||||
/**
|
||||
* Set or update a privacy list with {@link PrivacyItem}.
|
||||
|
@ -43,7 +39,7 @@ public class Privacy extends IQ {
|
|||
* @param listItem the {@link PrivacyItem} that rules the list.
|
||||
* @return the privacy List.
|
||||
*/
|
||||
public List setPrivacyList(String listName, List listItem) {
|
||||
public List setPrivacyList(String listName, List<PrivacyItem> listItem) {
|
||||
// Add new list to the itemLists
|
||||
this.getItemLists().put(listName, listItem);
|
||||
return listItem;
|
||||
|
@ -54,9 +50,9 @@ public class Privacy extends IQ {
|
|||
*
|
||||
* @return the active List.
|
||||
*/
|
||||
public List setActivePrivacyList() {
|
||||
public List<PrivacyItem> setActivePrivacyList() {
|
||||
this.setActiveName(this.getDefaultName());
|
||||
return (List) this.getItemLists().get(this.getActiveName());
|
||||
return this.getItemLists().get(this.getActiveName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,12 +77,12 @@ public class Privacy extends IQ {
|
|||
*
|
||||
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
|
||||
*/
|
||||
public List getActivePrivacyList() {
|
||||
public List<PrivacyItem> getActivePrivacyList() {
|
||||
// Check if we have the default list
|
||||
if (this.getActiveName() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return (List) this.getItemLists().get(this.getActiveName());
|
||||
return this.getItemLists().get(this.getActiveName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,12 +91,12 @@ public class Privacy extends IQ {
|
|||
*
|
||||
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
|
||||
*/
|
||||
public List getDefaultPrivacyList() {
|
||||
public List<PrivacyItem> getDefaultPrivacyList() {
|
||||
// Check if we have the default list
|
||||
if (this.getDefaultName() == null) {
|
||||
return null;
|
||||
} else {
|
||||
return (List) this.getItemLists().get(this.getDefaultName());
|
||||
return this.getItemLists().get(this.getDefaultName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,8 +106,8 @@ public class Privacy extends IQ {
|
|||
* @param listName the name of the list to get.
|
||||
* @return a List with {@link PrivacyItem}
|
||||
*/
|
||||
public List getPrivacyList(String listName) {
|
||||
return (List) this.getItemLists().get(listName);
|
||||
public List<PrivacyItem> getPrivacyList(String listName) {
|
||||
return this.getItemLists().get(listName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,10 +117,10 @@ public class Privacy extends IQ {
|
|||
* @return a List with {@link PrivacyItem}
|
||||
*/
|
||||
public PrivacyItem getItem(String listName, int order) {
|
||||
Iterator values = getPrivacyList(listName).iterator();
|
||||
Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
|
||||
PrivacyItem itemFound = null;
|
||||
while (itemFound == null && values.hasNext()) {
|
||||
PrivacyItem element = (PrivacyItem) values.next();
|
||||
PrivacyItem element = values.next();
|
||||
if (element.getOrder() == order) {
|
||||
itemFound = element;
|
||||
}
|
||||
|
@ -211,7 +207,7 @@ public class Privacy extends IQ {
|
|||
* @return a map where the key is the name of the list and the value the
|
||||
* collection of privacy items.
|
||||
*/
|
||||
public Map getItemLists() {
|
||||
public Map<String, List<PrivacyItem>> getItemLists() {
|
||||
return itemLists;
|
||||
}
|
||||
|
||||
|
@ -245,7 +241,7 @@ public class Privacy extends IQ {
|
|||
/**
|
||||
* Sets whether the receiver allows or declines the use of a default list.
|
||||
*
|
||||
* @param declineActiveList indicates if the receiver declines the use of a default list.
|
||||
* @param declineDefaultList indicates if the receiver declines the use of a default list.
|
||||
*/
|
||||
public void setDeclineDefaultList(boolean declineDefaultList) {
|
||||
this.declineDefaultList = declineDefaultList;
|
||||
|
@ -256,7 +252,7 @@ public class Privacy extends IQ {
|
|||
*
|
||||
* @return a Set with Strings containing every list names.
|
||||
*/
|
||||
public Set getPrivacyListNames() {
|
||||
public Set<String> getPrivacyListNames() {
|
||||
return this.itemLists.keySet();
|
||||
}
|
||||
|
||||
|
@ -282,20 +278,16 @@ public class Privacy extends IQ {
|
|||
}
|
||||
|
||||
// Add the list with their privacy items
|
||||
int listNameSize = this.getItemLists().size();
|
||||
Iterator listItemsPairs = this.getItemLists().entrySet().iterator();
|
||||
for (int i = 0; i < listNameSize; i++) {
|
||||
Map.Entry entry = (Map.Entry) listItemsPairs.next();
|
||||
String listName = (String) entry.getKey();
|
||||
List items = (List) entry.getValue();
|
||||
for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
|
||||
String listName = entry.getKey();
|
||||
List<PrivacyItem> items = entry.getValue();
|
||||
// Begin the list tag
|
||||
if (items.isEmpty()) {
|
||||
buf.append("<list name=\"").append(listName).append("\"/>");
|
||||
} else {
|
||||
buf.append("<list name=\"").append(listName).append("\">");
|
||||
}
|
||||
for (Iterator itemIterator = items.iterator(); itemIterator.hasNext();) {
|
||||
PrivacyItem item = (PrivacyItem) itemIterator.next();
|
||||
for (PrivacyItem item : items) {
|
||||
// Append the item xml representation
|
||||
buf.append(item.toXML());
|
||||
}
|
||||
|
@ -308,8 +300,7 @@ public class Privacy extends IQ {
|
|||
// Add packet extensions, if any are defined.
|
||||
buf.append(getExtensionsXML());
|
||||
buf.append("</query>");
|
||||
String generatedXML = buf.toString();
|
||||
return generatedXML;
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -196,7 +196,7 @@ public class PrivacyItem {
|
|||
*
|
||||
* @return the type of communication it represent.
|
||||
*/
|
||||
public String getType() {
|
||||
public Type getType() {
|
||||
if (this.getRule() == null) {
|
||||
return null;
|
||||
} else {
|
||||
|
@ -298,7 +298,7 @@ public class PrivacyItem {
|
|||
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
|
||||
* Available values are: [jid|group|subscription]
|
||||
*/
|
||||
private String type;
|
||||
private Type type;
|
||||
/**
|
||||
* The value hold the element identifier to apply the action.
|
||||
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
|
||||
|
@ -310,24 +310,10 @@ public class PrivacyItem {
|
|||
private String value;
|
||||
|
||||
/**
|
||||
* JID being analyzed should have a resource match, domain match or bare JID match.
|
||||
*/
|
||||
public static final String GROUP = "group";
|
||||
/**
|
||||
* JID being analyzed should belong to a roster group of the list's owner.
|
||||
*/
|
||||
public static final String JID = "jid";
|
||||
/**
|
||||
* JID being analyzed should belong to a contact present in the owner's roster with
|
||||
* the specified subscription status.
|
||||
*/
|
||||
public static final String SUBSCRIPTION = "subscription";
|
||||
|
||||
/**
|
||||
* If the type is "subscription", then the 'value' attribute MUST be one of "both",
|
||||
* "to", "from", or "none"
|
||||
*/
|
||||
public static final String SUBSCRIPTION_BOTH = "subscription";
|
||||
public static final String SUBSCRIPTION_BOTH = "both";
|
||||
public static final String SUBSCRIPTION_TO = "to";
|
||||
public static final String SUBSCRIPTION_FROM = "from";
|
||||
public static final String SUBSCRIPTION_NONE = "none";
|
||||
|
@ -336,25 +322,11 @@ public class PrivacyItem {
|
|||
* Returns the type constant associated with the String value.
|
||||
*/
|
||||
protected static PrivacyRule fromString(String value) {
|
||||
String type = null;
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (SUBSCRIPTION.equalsIgnoreCase(value)) {
|
||||
type = SUBSCRIPTION;
|
||||
}
|
||||
else if (GROUP.equalsIgnoreCase(value)) {
|
||||
type = GROUP;
|
||||
}
|
||||
else if (JID.equalsIgnoreCase(value)) {
|
||||
type = JID;
|
||||
}
|
||||
// Default to available.
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
PrivacyRule rule = new PrivacyRule();
|
||||
rule.setType(type);
|
||||
rule.setType(Type.valueOf(value.toLowerCase()));
|
||||
return rule;
|
||||
}
|
||||
|
||||
|
@ -364,16 +336,16 @@ public class PrivacyItem {
|
|||
*
|
||||
* @return the type of communication it represent.
|
||||
*/
|
||||
public String getType() {
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the action associated with the item, it can allow or deny the communication.
|
||||
*
|
||||
* @param allow indicates if the receiver allows or denies the communication.
|
||||
* @param type indicates if the receiver allows or denies the communication.
|
||||
*/
|
||||
private void setType(String type) {
|
||||
private void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
@ -419,9 +391,9 @@ public class PrivacyItem {
|
|||
* @param value is the identifier to apply the action.
|
||||
*/
|
||||
private void setSuscriptionValue(String value) {
|
||||
String setValue = null;
|
||||
String setValue;
|
||||
if (value == null) {
|
||||
setValue = null;
|
||||
// Do nothing
|
||||
}
|
||||
if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) {
|
||||
setValue = SUBSCRIPTION_BOTH;
|
||||
|
@ -448,7 +420,26 @@ public class PrivacyItem {
|
|||
* @return if the receiver represents a subscription rule.
|
||||
*/
|
||||
public boolean isSuscription () {
|
||||
return this.getValue() == SUBSCRIPTION;
|
||||
return this.getType() == Type.subscription;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
|
||||
*/
|
||||
protected static enum Type {
|
||||
/**
|
||||
* JID being analyzed should belong to a roster group of the list's owner.
|
||||
*/
|
||||
group,
|
||||
/**
|
||||
* JID being analyzed should have a resource match, domain match or bare JID match.
|
||||
*/
|
||||
jid,
|
||||
/**
|
||||
* JID being analyzed should belong to a contact present in the owner's roster with
|
||||
* the specified subscription status.
|
||||
*/
|
||||
subscription
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ package org.jivesoftware.smack.packet;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -67,7 +66,7 @@ public class XMPPError {
|
|||
private Type type;
|
||||
private String condition;
|
||||
private String message;
|
||||
private List applicationExtensions = null;
|
||||
private List<PacketExtension> applicationExtensions = null;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -77,7 +76,7 @@ public class XMPPError {
|
|||
* If the Condition is not predefined, invocations should be like
|
||||
* new XMPPError(new XMPPError.Condition("my_own_error"));
|
||||
*
|
||||
* @param code the error code.
|
||||
* @param condition the error condition.
|
||||
*/
|
||||
public XMPPError(Condition condition) {
|
||||
this.init(condition);
|
||||
|
@ -91,8 +90,8 @@ public class XMPPError {
|
|||
* If the Condition is not predefined, invocations should be like
|
||||
* new XMPPError(new XMPPError.Condition("my_own_error"), "Error Explanation");
|
||||
*
|
||||
* @param code the error code.
|
||||
* @param message a message describing the error.
|
||||
* @param condition the error condition.
|
||||
* @param messageText a message describing the error.
|
||||
*/
|
||||
public XMPPError(Condition condition, String messageText) {
|
||||
this.init(condition);
|
||||
|
@ -103,7 +102,7 @@ public class XMPPError {
|
|||
* Creates a new error with the specified code and no message.
|
||||
*
|
||||
* @param code the error code.
|
||||
* @Deprecated new errors should be created using the constructor XMPPError(condition)
|
||||
* @deprecated new errors should be created using the constructor XMPPError(condition)
|
||||
*/
|
||||
public XMPPError(int code) {
|
||||
this.code = code;
|
||||
|
@ -116,7 +115,7 @@ public class XMPPError {
|
|||
*
|
||||
* @param code the error code.
|
||||
* @param message a message describing the error.
|
||||
* @Deprecated new errors should be created using the constructor XMPPError(condition, message)
|
||||
* @deprecated new errors should be created using the constructor XMPPError(condition, message)
|
||||
*/
|
||||
public XMPPError(int code, String message) {
|
||||
this.code = code;
|
||||
|
@ -135,7 +134,8 @@ public class XMPPError {
|
|||
* @param condition the error condition.
|
||||
* @param message a message describing the error.
|
||||
*/
|
||||
public XMPPError(int code, Type type, String condition, String message, List extension) {
|
||||
public XMPPError(int code, Type type, String condition, String message,
|
||||
List<PacketExtension> extension) {
|
||||
this.code = code;
|
||||
this.type = type;
|
||||
this.condition = condition;
|
||||
|
@ -205,7 +205,7 @@ public class XMPPError {
|
|||
buf.append("<error code=\"").append(code).append("\"");
|
||||
if (type != null) {
|
||||
buf.append(" type=\"");
|
||||
buf.append(type.value);
|
||||
buf.append(type.name());
|
||||
buf.append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
|
@ -218,8 +218,7 @@ public class XMPPError {
|
|||
buf.append(message);
|
||||
buf.append("</text>");
|
||||
}
|
||||
for (Iterator extensions = this.getExtensions(); extensions.hasNext();) {
|
||||
PacketExtension element = (PacketExtension) extensions.next();
|
||||
for (PacketExtension element : this.getExtensions()) {
|
||||
buf.append(element.toXML());
|
||||
}
|
||||
buf.append("</error>");
|
||||
|
@ -228,6 +227,9 @@ public class XMPPError {
|
|||
|
||||
public String toString() {
|
||||
StringBuffer txt = new StringBuffer();
|
||||
if (condition != null) {
|
||||
txt.append(condition);
|
||||
}
|
||||
txt.append("(").append(code).append(")");
|
||||
if (message != null) {
|
||||
txt.append(" ").append(message);
|
||||
|
@ -242,11 +244,11 @@ public class XMPPError {
|
|||
*
|
||||
* @return an Iterator for the error extensions.
|
||||
*/
|
||||
public synchronized Iterator getExtensions() {
|
||||
public synchronized List<PacketExtension> getExtensions() {
|
||||
if (applicationExtensions == null) {
|
||||
return Collections.EMPTY_LIST.iterator();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return Collections.unmodifiableList(new ArrayList(applicationExtensions)).iterator();
|
||||
return Collections.unmodifiableList(applicationExtensions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,8 +263,7 @@ public class XMPPError {
|
|||
if (applicationExtensions == null || elementName == null || namespace == null) {
|
||||
return null;
|
||||
}
|
||||
for (Iterator i=applicationExtensions.iterator(); i.hasNext(); ) {
|
||||
PacketExtension ext = (PacketExtension)i.next();
|
||||
for (PacketExtension ext : applicationExtensions) {
|
||||
if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) {
|
||||
return ext;
|
||||
}
|
||||
|
@ -277,7 +278,7 @@ public class XMPPError {
|
|||
*/
|
||||
public synchronized void addExtension(PacketExtension extension) {
|
||||
if (applicationExtensions == null) {
|
||||
applicationExtensions = new ArrayList();
|
||||
applicationExtensions = new ArrayList<PacketExtension>();
|
||||
}
|
||||
applicationExtensions.add(extension);
|
||||
}
|
||||
|
@ -287,7 +288,7 @@ public class XMPPError {
|
|||
*
|
||||
* @param extension a packet extension.
|
||||
*/
|
||||
public synchronized void setExtension(List extension) {
|
||||
public synchronized void setExtension(List<PacketExtension> extension) {
|
||||
applicationExtensions = extension;
|
||||
}
|
||||
|
||||
|
@ -303,55 +304,12 @@ public class XMPPError {
|
|||
* <li>XMPPError.Type.CONTINUE - proceed (the condition was only a warning)
|
||||
* </ul>
|
||||
*/
|
||||
public static class Type {
|
||||
|
||||
public static final Type WAIT = new Type("wait");
|
||||
public static final Type CANCEL = new Type("cancel");
|
||||
public static final Type MODIFY = new Type("modify");
|
||||
public static final Type AUTH = new Type("auth");
|
||||
public static final Type CONTINUE = new Type("continue");
|
||||
|
||||
/**
|
||||
* Converts a String into the corresponding types. Valid String values
|
||||
* that can be converted to types are: "wait", "cancel", "modify", "auth" or a user defined.
|
||||
*
|
||||
* @param type the String value to covert.
|
||||
* @return the corresponding Type.
|
||||
*/
|
||||
public static Type fromString(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
type = type.toLowerCase();
|
||||
if (CANCEL.toString().equals(type)) {
|
||||
return CANCEL;
|
||||
}
|
||||
else if (CONTINUE.toString().equals(type)) {
|
||||
return CONTINUE;
|
||||
}
|
||||
else if (WAIT.toString().equals(type)) {
|
||||
return WAIT;
|
||||
}
|
||||
else if (MODIFY.toString().equals(type)) {
|
||||
return MODIFY;
|
||||
}
|
||||
else if (AUTH.toString().equals(type)) {
|
||||
return AUTH;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String value;
|
||||
|
||||
private Type(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
public static enum Type {
|
||||
WAIT,
|
||||
CANCEL,
|
||||
MODIFY,
|
||||
AUTH,
|
||||
CONTINUE
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -403,7 +361,7 @@ public class XMPPError {
|
|||
private int code;
|
||||
private Type type;
|
||||
private Condition condition;
|
||||
private static HashMap instances = errorSpecifications();
|
||||
private static HashMap<Condition, ErrorSpecification> instances = errorSpecifications();
|
||||
|
||||
private ErrorSpecification(Condition condition, Type type, int code) {
|
||||
this.code = code;
|
||||
|
@ -411,8 +369,8 @@ public class XMPPError {
|
|||
this.condition = condition;
|
||||
}
|
||||
|
||||
private static HashMap errorSpecifications() {
|
||||
HashMap instances = new HashMap(22);
|
||||
private static HashMap<Condition, ErrorSpecification> errorSpecifications() {
|
||||
HashMap<Condition, ErrorSpecification> instances = new HashMap<Condition, ErrorSpecification>(22);
|
||||
instances.put(Condition.interna_server_error, new ErrorSpecification(
|
||||
Condition.interna_server_error, Type.WAIT, 500));
|
||||
instances.put(Condition.forbidden, new ErrorSpecification(Condition.forbidden,
|
||||
|
@ -466,7 +424,7 @@ public class XMPPError {
|
|||
}
|
||||
|
||||
protected static ErrorSpecification specFor(Condition condition) {
|
||||
return (ErrorSpecification) instances.get(condition);
|
||||
return instances.get(condition);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue