3 Copyright (C) 2007 Jehan Monnier ()
5 P2pUserRegistrationAdvertisement.java - .
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 package org.linphone.p2pproxy.core.sipproxy.superpeers;
24 import java.io.Serializable;
26 import java.net.URISyntaxException;
27 import java.util.Enumeration;
29 import org.apache.log4j.Logger;
34 import net.jxta.document.Advertisement;
35 import net.jxta.document.AdvertisementFactory;
36 import net.jxta.document.Attributable;
37 import net.jxta.document.Document;
38 import net.jxta.document.Element;
39 import net.jxta.document.ExtendableAdvertisement;
40 import net.jxta.document.MimeMediaType;
41 import net.jxta.document.StructuredDocument;
42 import net.jxta.document.StructuredDocumentFactory;
43 import net.jxta.document.TextElement;
44 import net.jxta.id.ID;
45 import net.jxta.id.IDFactory;
48 * derivated from jxta Advertisement tutorial
50 * <?xml version="1.0"?>
51 * <!DOCTYPE jxta:System>
52 * <jxta:System xmlns:jxta="http://jxta.org">
53 * <id>NULL </id>
54 * <user-name>address where the sip endpoint is currently registered</user-name>
55 * <registrar-address>address where the sip endpoint is currently registered</registrar-address>
59 public class P2pUserRegistrationAdvertisement extends ExtendableAdvertisement implements Comparable, Cloneable, Serializable {
63 public static class Instantiator implements AdvertisementFactory.Instantiator {
66 * Returns the identifying type of this Advertisement.
68 * @return String the type of advertisement
70 public String getAdvertisementType() {
71 return P2pUserRegistrationAdvertisement.getAdvertisementType();
75 * Constructs an instance of <CODE>Advertisement</CODE> matching the
76 * type specified by the <CODE>advertisementType</CODE> parameter.
78 * @return The instance of <CODE>Advertisement</CODE> or null if it
79 * could not be created.
81 public Advertisement newInstance() {
82 return new P2pUserRegistrationAdvertisement();
86 * Constructs an instance of <CODE>Advertisement</CODE> matching the
87 * type specified by the <CODE>advertisementType</CODE> parameter.
89 * @param root Specifies a portion of a StructuredDocument which will
90 * be converted into an Advertisement.
91 * @return The instance of <CODE>Advertisement</CODE> or null if it
92 * could not be created.
94 public Advertisement newInstance(net.jxta.document.Element root) {
95 return new P2pUserRegistrationAdvertisement(root);
99 private String mRegistrarAddress;
100 private String mUserUri;
101 public final static String REGISTRAR_ADDRESS_TAG = "registrar-address";
102 public final static String USER_NAME_TAG = "registration-user-name";
103 private final static String ID_TAG = "ID";
104 private final static String[] mIndexs = {USER_NAME_TAG};
105 private final static Logger mLog = Logger.getLogger(P2pUserRegistrationAdvertisement.class);
109 public P2pUserRegistrationAdvertisement(Element root) {
111 TextElement doc = (TextElement) root;
113 if (!getAdvertisementType().equals(doc.getName())) {
114 throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
119 public P2pUserRegistrationAdvertisement() {
121 // TODO Auto-generated constructor stub
124 * @see net.jxta.document.ExtendableAdvertisement#getDocument(net.jxta.document.MimeMediaType)
127 public Document getDocument(MimeMediaType asMimeType) {
129 StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
130 getAdvertisementType());
131 if (adv instanceof Attributable) {
132 ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
135 e = adv.createElement(ID_TAG, getID().toString());
137 e = adv.createElement(USER_NAME_TAG, getUserName().trim());
139 e = adv.createElement(REGISTRAR_ADDRESS_TAG, getRegistrarAddress().trim());
150 public String[] getIndexFields() {
153 public static String getAdvertisementType() {
154 return "jxta:p2p-proxy-user-registration";
157 * @see net.jxta.document.Advertisement#toString()
160 public String toString() {
161 // TODO Auto-generated method stub
162 return super.toString();
164 public int compareTo(Object other) {
165 return getID().toString().compareTo(other.toString());
168 * Intialize a System advertisement from a portion of a structured document.
170 * @param root document root
172 protected void initialize(Element root) {
173 if (!TextElement.class.isInstance(root)) {
174 throw new IllegalArgumentException(getClass().getName() +
175 " only supports TextElement");
177 TextElement doc = (TextElement) root;
178 if (!doc.getName().equals(getAdvertisementType())) {
179 throw new IllegalArgumentException("Could not construct : "
180 + getClass().getName() + "from doc containing a " +
183 Enumeration elements = doc.getChildren();
184 while (elements.hasMoreElements()) {
185 TextElement elem = (TextElement) elements.nextElement();
186 if (!handleElement(elem)) {
187 mLog.warn("Unhandleded element \'" + elem.getName() + "\' in " + doc.getName());
192 * Process an individual element from the document.
194 * @param elem the element to be processed.
195 * @return true if the element was recognized, otherwise false.
197 protected boolean handleElement(TextElement elem) {
198 if (elem.getName().equals(ID_TAG)) {
200 URI id = new URI(elem.getTextValue());
201 setID(IDFactory.fromURI(id));
202 } catch (URISyntaxException badID) {
203 throw new IllegalArgumentException("unknown ID format in advertisement: " +
204 elem.getTextValue());
206 catch (ClassCastException badID) {
207 throw new IllegalArgumentException("Id is not a known id type: " +
208 elem.getTextValue());
211 } else if (elem.getName().equals(USER_NAME_TAG)) {
212 setUserName(elem.getTextValue());
214 } else if (elem.getName().equals(REGISTRAR_ADDRESS_TAG)) {
215 setRegistrarAddress(elem.getTextValue());
221 public void setID(ID id) {
225 public String getBaseAdvType() {
226 // TODO Auto-generated method stub
230 * @return Returns the mName.
232 public String getRegistrarAddress() {
233 return mRegistrarAddress;
236 * @param name The mName to set.
238 public void setRegistrarAddress(String anAddress) {
239 mRegistrarAddress = anAddress;
242 * @return Returns the mName.
244 public String getUserName() {
248 * @param name The mName to set.
250 public void setUserName(String anName) {
254 * @see java.lang.Object#equals(java.lang.Object)
257 public boolean equals(Object obj) {
262 if (obj instanceof P2pUserRegistrationAdvertisement) {
263 P2pUserRegistrationAdvertisement adv = (P2pUserRegistrationAdvertisement) obj;
264 return getID().equals(adv.getID());