]> sjero.net Git - linphone/blob - p2pproxy/src/org/linphone/p2pproxy/core/sipproxy/superpeers/P2pUserRegistrationAdvertisement.java
4e68219774bf7ffe563145c5ef180a008d4d3e9c
[linphone] / p2pproxy / src / org / linphone / p2pproxy / core / sipproxy / superpeers / P2pUserRegistrationAdvertisement.java
1 /*
2 p2pproxy
3 Copyright (C) 2007  Jehan Monnier ()
4
5 P2pUserRegistrationAdvertisement.java - .
6
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.
11
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.
16
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.
20 */
21 package org.linphone.p2pproxy.core.sipproxy.superpeers;
22
23
24 import java.io.Serializable;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Enumeration;
28
29 import org.apache.log4j.Logger;
30
31
32
33
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;
46
47 /**
48  * derivated from jxta Advertisement tutorial
49  * <pre>
50  * &lt;?xml version="1.0"?>
51  * &lt;!DOCTYPE jxta:System>
52  * &lt;jxta:System xmlns:jxta="http://jxta.org">
53  *   &lt;id>NULL &lt;/id>
54  *   &lt;user-name>address where the sip endpoint is currently registered&lt;/user-name>
55  *   &lt;registrar-address>address where the sip endpoint is currently registered&lt;/registrar-address>
56  * &lt;/jxta:System>
57  * </pre>
58  */
59 public class P2pUserRegistrationAdvertisement extends ExtendableAdvertisement implements Comparable, Cloneable, Serializable {
60    /**
61     * Instantiator
62     */
63    public static class Instantiator implements AdvertisementFactory.Instantiator {
64
65       /**
66        * Returns the identifying type of this Advertisement.
67        *
68        * @return String the type of advertisement
69        */
70       public String getAdvertisementType() {
71          return P2pUserRegistrationAdvertisement.getAdvertisementType();
72       }
73
74       /**
75        * Constructs an instance of <CODE>Advertisement</CODE> matching the
76        * type specified by the <CODE>advertisementType</CODE> parameter.
77        *
78        * @return The instance of <CODE>Advertisement</CODE> or null if it
79        *         could not be created.
80        */
81       public Advertisement newInstance() {
82          return new P2pUserRegistrationAdvertisement();
83       }
84
85       /**
86        * Constructs an instance of <CODE>Advertisement</CODE> matching the
87        * type specified by the <CODE>advertisementType</CODE> parameter.
88        *
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.
93        */
94       public Advertisement newInstance(net.jxta.document.Element root) {
95          return new P2pUserRegistrationAdvertisement(root);
96       }
97    }
98    private ID mId ;;
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);
106    /**
107     * 
108     */
109    public P2pUserRegistrationAdvertisement(Element root) {
110
111       TextElement doc = (TextElement) root;
112
113       if (!getAdvertisementType().equals(doc.getName())) {
114          throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
115       }
116       initialize(doc);
117
118    }
119    public P2pUserRegistrationAdvertisement() {
120
121       // TODO Auto-generated constructor stub
122    }
123    /* (non-Javadoc)
124     * @see net.jxta.document.ExtendableAdvertisement#getDocument(net.jxta.document.MimeMediaType)
125     */
126    @Override
127    public Document getDocument(MimeMediaType asMimeType) {
128
129       StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
130             getAdvertisementType());
131       if (adv instanceof Attributable) {
132          ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
133       }
134       Element e;
135       e = adv.createElement(ID_TAG, getID().toString());
136       adv.appendChild(e);
137       e = adv.createElement(USER_NAME_TAG, getUserName().trim());
138       adv.appendChild(e);
139       e = adv.createElement(REGISTRAR_ADDRESS_TAG, getRegistrarAddress().trim());
140       adv.appendChild(e);
141       return adv;
142    }
143
144    @Override
145    public ID getID() {
146       return mId;
147    }
148
149    @Override
150    public String[] getIndexFields() {
151       return mIndexs;
152    }
153    public static String getAdvertisementType() {
154       return "jxta:p2p-proxy-user-registration";
155    }
156    /* (non-Javadoc)
157     * @see net.jxta.document.Advertisement#toString()
158     */
159    @Override
160    public String toString() {
161       // TODO Auto-generated method stub
162       return super.toString();
163    }
164    public int compareTo(Object other) {
165       return getID().toString().compareTo(other.toString());
166    }
167    /**
168     * Intialize a System advertisement from a portion of a structured document.
169     *
170     * @param root document root
171     */
172    protected void initialize(Element root) {
173       if (!TextElement.class.isInstance(root)) {
174          throw new IllegalArgumentException(getClass().getName() +
175                " only supports TextElement");
176       }
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 " +
181                doc.getName());
182       }
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());
188          }
189       }
190    }
191    /**
192     * Process an individual element from the document.
193     *
194     * @param elem the element to be processed.
195     * @return true if the element was recognized, otherwise false.
196     */
197    protected boolean handleElement(TextElement elem) {
198       if (elem.getName().equals(ID_TAG)) {
199          try {
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());
205          }
206          catch (ClassCastException badID) {
207             throw new IllegalArgumentException("Id is not a known id type: " +
208                   elem.getTextValue());
209          }
210          return true;
211       } else if (elem.getName().equals(USER_NAME_TAG)) {
212          setUserName(elem.getTextValue());
213          return true;
214       } else if (elem.getName().equals(REGISTRAR_ADDRESS_TAG)) {
215          setRegistrarAddress(elem.getTextValue());
216          return true;
217       } else {
218          return false;
219       }
220    }
221    public void setID(ID id) {
222       mId = id;
223    }
224    @Override
225    public String getBaseAdvType() {
226       // TODO Auto-generated method stub
227       return null;
228    }
229    /**
230     * @return Returns the mName.
231     */
232    public String getRegistrarAddress() {
233       return mRegistrarAddress;
234    }
235    /**
236     * @param name The mName to set.
237     */
238    public void setRegistrarAddress(String anAddress) {
239       mRegistrarAddress = anAddress;
240    }
241    /**
242     * @return Returns the mName.
243     */
244    public String getUserName() {
245       return mUserUri;
246    }
247    /**
248     * @param name The mName to set.
249     */
250    public void setUserName(String anName) {
251       mUserUri = anName;
252    }
253    /* (non-Javadoc)
254     * @see java.lang.Object#equals(java.lang.Object)
255     */
256    @Override
257    public boolean equals(Object obj) {
258
259       if (this == obj) {
260           return true;
261       }
262       if (obj instanceof P2pUserRegistrationAdvertisement) {
263          P2pUserRegistrationAdvertisement adv = (P2pUserRegistrationAdvertisement) obj;
264           return getID().equals(adv.getID());
265       }
266
267       return false;
268   
269    }
270
271 }