/[hyperestraier]/upstream/0.5.2/java/NodeImpl.java
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/0.5.2/java/NodeImpl.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (hide annotations)
Wed Aug 3 15:21:15 2005 UTC (18 years, 11 months ago) by dpavlin
File size: 18069 byte(s)
import upstream version 0.5.2

1 dpavlin 9 /*************************************************************************************************
2     * Java interface of Hyper Estraier
3     * Copyright (C) 2004-2005 Mikio Hirabayashi
4     * All rights reserved.
5     * This file is part of Hyper Estraier.
6     * Redistribution and use in source and binary forms, with or without modification, are
7     * permitted provided that the following conditions are met:
8     *
9     * * Redistributions of source code must retain the above copyright notice, this list of
10     * conditions and the following disclaimer.
11     * * Redistributions in binary form must reproduce the above copyright notice, this list of
12     * conditions and the following disclaimer in the documentation and/or other materials
13     * provided with the distribution.
14     * * Neither the name of Mikio Hirabayashi nor the names of its contributors may be used to
15     * endorse or promote products derived from this software without specific prior written
16     * permission.
17     *
18     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
19     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20     * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26     * OF THE POSSIBILITY OF SUCH DAMAGE.
27     *************************************************************************************************/
28    
29    
30     package estraier;
31    
32     import java.util.*;
33     import java.io.*;
34     import java.net.*;
35    
36    
37    
38     /**
39     * Implementation of connection to P2P node.
40     */
41     public class NodeImpl implements Node {
42     //----------------------------------------------------------------
43     // private fields
44     //----------------------------------------------------------------
45     private int status;
46     private String url;
47     private String pxhost;
48     private int pxport;
49     private int timeout;
50     private String auth;
51     private String name;
52     private String label;
53     private int dnum;
54     private int wnum;
55     private double size;
56     //----------------------------------------------------------------
57     // public methods
58     //----------------------------------------------------------------
59     public NodeImpl(){
60     status = -1;
61     url = null;
62     pxhost = null;
63     pxport = -1;
64     timeout = -1;
65     auth = null;
66     name = null;
67     label = null;
68     dnum = -1;
69     wnum = -1;
70     size = -1.0;
71     }
72     public int status(){
73     return status;
74     }
75     public void set_url(String url){
76     this.url = url;
77     }
78     public void set_proxy(String host, int port){
79     pxhost = host;
80     pxport = port;
81     }
82     public void set_timeout(int msec){
83     timeout = msec;
84     }
85     public void set_auth(String name, String password){
86     auth = name + ":" + password;
87     }
88     public boolean put_doc(Document doc){
89     status = -1;
90     if(url == null) return false;
91     try {
92     URL purl = new URL(url);
93     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
94     purl.getPath() + "/put_doc");
95     List reqheads = new ArrayList(2);
96     if(auth != null)
97     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
98     reqheads.add("Content-Type: text/x-estraier-draft");
99     byte[] reqbody = doc.dump_draft().getBytes("UTF-8");
100     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
101     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
102     null, resbody);
103     if(status != 200) return false;
104     return true;
105     } catch(Exception e){
106     return false;
107     }
108     }
109     public boolean out_doc(int id){
110     status = -1;
111     if(url == null) return false;
112     try {
113     URL purl = new URL(url);
114     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
115     purl.getPath() + "/out_doc");
116     List reqheads = new ArrayList(2);
117     if(auth != null)
118     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
119     reqheads.add("Content-Type: application/x-www-form-urlencoded");
120     byte[] reqbody = ("id=" + id).getBytes("ISO-8859-1");
121     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
122     null, null);
123     if(status != 200) return false;
124     return true;
125     } catch(Exception e){
126     return false;
127     }
128     }
129     public boolean out_doc_by_uri(String uri){
130     status = -1;
131     if(url == null) return false;
132     try {
133     URL purl = new URL(url);
134     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
135     purl.getPath() + "/out_doc");
136     List reqheads = new ArrayList(2);
137     if(auth != null)
138     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
139     reqheads.add("Content-Type: application/x-www-form-urlencoded");
140     byte[] reqbody = ("uri=" + URLEncoder.encode(uri, "UTF-8")).getBytes("ISO-8859-1");
141     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
142     null, null);
143     if(status != 200) return false;
144     return true;
145     } catch(Exception e){
146     return false;
147     }
148     }
149     public Document get_doc(int id){
150     status = -1;
151     if(url == null) return null;
152     try {
153     URL purl = new URL(url);
154     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
155     purl.getPath() + "/get_doc");
156     List reqheads = new ArrayList(2);
157     if(auth != null)
158     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
159     reqheads.add("Content-Type: application/x-www-form-urlencoded");
160     byte[] reqbody = ("id=" + id).getBytes("ISO-8859-1");
161     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
162     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
163     null, resbody);
164     if(status != 200) return null;
165     return new DocumentImpl(resbody.toString("UTF-8"));
166     } catch(Exception e){
167     return null;
168     }
169     }
170     public Document get_doc_by_uri(String uri){
171     status = -1;
172     if(url == null) return null;
173     try {
174     URL purl = new URL(url);
175     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
176     purl.getPath() + "/get_doc");
177     List reqheads = new ArrayList(2);
178     if(auth != null)
179     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
180     reqheads.add("Content-Type: application/x-www-form-urlencoded");
181     byte[] reqbody = ("uri=" + URLEncoder.encode(uri, "UTF-8")).getBytes("ISO-8859-1");
182     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
183     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
184     null, resbody);
185     if(status != 200) return null;
186     return new DocumentImpl(resbody.toString("UTF-8"));
187     } catch(Exception e){
188     return null;
189     }
190     }
191     public String get_doc_attr(int id, String name){
192     status = -1;
193     if(url == null) return null;
194     try {
195     URL purl = new URL(url);
196     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
197     purl.getPath() + "/get_doc_attr");
198     List reqheads = new ArrayList(2);
199     if(auth != null)
200     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
201     reqheads.add("Content-Type: application/x-www-form-urlencoded");
202     String qstr = "id=" + id + "&attr=" + URLEncoder.encode(name, "UTF-8");
203     byte[] reqbody = qstr.getBytes("ISO-8859-1");
204     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
205     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
206     null, resbody);
207     if(status != 200) return null;
208     return resbody.toString("UTF-8").trim();
209     } catch(Exception e){
210     return null;
211     }
212     }
213     public String get_doc_attr_by_uri(String uri, String name){
214     status = -1;
215     if(url == null) return null;
216     try {
217     URL purl = new URL(url);
218     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
219     purl.getPath() + "/get_doc_attr");
220     List reqheads = new ArrayList(2);
221     if(auth != null)
222     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
223     reqheads.add("Content-Type: application/x-www-form-urlencoded");
224     String qstr = "uri=" + URLEncoder.encode(uri, "UTF-8") +
225     "&attr=" + URLEncoder.encode(name, "UTF-8");
226     byte[] reqbody = qstr.getBytes("ISO-8859-1");
227     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
228     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
229     null, resbody);
230     if(status != 200) return null;
231     return resbody.toString("UTF-8").trim();
232     } catch(Exception e){
233     return null;
234     }
235     }
236     public int uri_to_id(String uri){
237     status = -1;
238     if(url == null) return -1;
239     try {
240     URL purl = new URL(url);
241     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
242     purl.getPath() + "/uri_to_id");
243     List reqheads = new ArrayList(2);
244     if(auth != null)
245     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
246     reqheads.add("Content-Type: application/x-www-form-urlencoded");
247     byte[] reqbody = ("uri=" + URLEncoder.encode(uri, "UTF-8")).getBytes("ISO-8859-1");
248     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
249     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
250     null, resbody);
251     if(status != 200) return -1;
252     return Integer.parseInt(resbody.toString("ISO-8859-1").trim());
253     } catch(Exception e){
254     return -1;
255     }
256     }
257     public String name(){
258     if(name == null) set_info();
259     return name;
260     }
261     public String label(){
262     if(label == null) set_info();
263     return label;
264     }
265     public int doc_num(){
266     if(dnum < 0) set_info();
267     return dnum;
268     }
269     public int word_num(){
270     if(wnum < 0) set_info();
271     return wnum;
272     }
273     public double size(){
274     if(size < 0.0) set_info();
275     return size;
276     }
277     public NodeResult search(Condition cond, int depth){
278     status = -1;
279     if(url == null) return null;
280     try {
281     URL purl = new URL(url);
282     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
283     purl.getPath() + "/search");
284     List reqheads = new ArrayList(2);
285     if(auth != null)
286     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
287     reqheads.add("Content-Type: application/x-www-form-urlencoded");
288     byte[] reqbody = cond_to_query(cond, depth).getBytes("ISO-8859-1");
289     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
290     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
291     null, resbody);
292     if(status != 200) return null;
293     String[] lines = Utility.splitLines(resbody.toString("UTF-8"));
294     if(lines.length < 1) return null;
295     List docs = new ArrayList(31);
296     Map hints = new HashMap(31);
297     NodeResultImpl nres = new NodeResultImpl(docs, hints);
298     String border = lines[0];
299     int lnum = 1;
300     while(lnum < lines.length){
301     String line = lines[lnum++];
302     if(line.equals(border)) break;
303     int lidx = line.indexOf('\t');
304     if(lidx != -1){
305     String key = line.substring(0, lidx);
306     String value = line.substring(lidx + 1, line.length());
307     hints.put(key, value);
308     }
309     }
310     int snum = lnum;
311     boolean end = false;
312     while(lnum < lines.length){
313     String line = lines[lnum++];
314     if(line.startsWith(border)){
315     if(lnum > snum){
316     Map rdattrs = new HashMap(31);
317     StringBuffer sb = new StringBuffer();
318     int rlnum = snum;
319     while(rlnum < lnum - 1){
320     String rdline = lines[rlnum++];
321     if(rdline.length() < 1) break;
322     int lidx = rdline.indexOf('=');
323     if(lidx != -1){
324     String key = rdline.substring(0, lidx);
325     String value = rdline.substring(lidx + 1, rdline.length());
326     rdattrs.put(key, value);
327     }
328     }
329     while(rlnum < lnum - 1){
330     String rdline = lines[rlnum++];
331     sb.append(rdline);
332     sb.append("\n");
333     }
334     String rduri = (String)rdattrs.get("@uri");
335     String rdsnippet = sb.toString();
336     if(rduri != null){
337     ResultDocument doc = new ResultDocumentImpl(rduri, rdattrs, rdsnippet);
338     docs.add(doc);
339     }
340     }
341     snum = lnum;
342     if(line.substring(border.length(), line.length()).equals(":END")) end = true;
343     }
344     if(end) break;
345     }
346     return nres;
347     } catch(Exception e){
348     return null;
349     }
350     }
351     public boolean set_user(String name, int mode){
352     status = -1;
353     if(url == null) return false;
354     try {
355     URL purl = new URL(url);
356     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
357     purl.getPath() + "/_set_user");
358     List reqheads = new ArrayList(2);
359     if(auth != null)
360     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
361     reqheads.add("Content-Type: application/x-www-form-urlencoded");
362     String qstr = "name=" + URLEncoder.encode(name, "UTF-8") + "&mode=" + mode;
363     byte[] reqbody = qstr.getBytes("ISO-8859-1");
364     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
365     null, null);
366     if(status != 200) return false;
367     return true;
368     } catch(Exception e){
369     return false;
370     }
371     }
372     public boolean set_link(String url, String label, int credit){
373     status = -1;
374     if(this.url == null) return false;
375     try {
376     URL purl = new URL(this.url);
377     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
378     purl.getPath() + "/_set_link");
379     List reqheads = new ArrayList(2);
380     if(auth != null)
381     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
382     reqheads.add("Content-Type: application/x-www-form-urlencoded");
383     String qstr = "url=" + URLEncoder.encode(url, "UTF-8") +
384     "&label=" + URLEncoder.encode(label, "UTF-8");
385     if(credit >= 0) qstr = qstr + "&credit=" + credit;
386     byte[] reqbody = qstr.getBytes("ISO-8859-1");
387     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, reqbody,
388     null, null);
389     if(status != 200) return false;
390     return true;
391     } catch(Exception e){
392     return false;
393     }
394     }
395     //----------------------------------------------------------------
396     // private methods
397     //----------------------------------------------------------------
398     private void set_info(){
399     status = -1;
400     if(url == null) return;
401     try {
402     URL purl = new URL(url);
403     URL eurl = new URL("http://" + purl.getHost() + ":" + purl.getPort() +
404     purl.getPath() + "/inform");
405     List reqheads = new ArrayList(2);
406     if(auth != null)
407     reqheads.add("Authorization: Basic " + Utility.baseEncode(auth.getBytes()));
408     ByteArrayOutputStream resbody = new ByteArrayOutputStream();
409     status = Utility.shuttleURL(eurl.toString(), pxhost, pxport, reqheads, null,
410     null, resbody);
411     if(status != 200) return;
412     String resstr = resbody.toString("UTF-8");
413     String[] lines = Utility.splitLines(resstr);
414     if(lines.length < 1) return;
415     String[] fields = Utility.splitFields(lines[0]);
416     if(fields.length != 5) return;
417     name = fields[0];
418     label = fields[1];
419     dnum = Integer.parseInt(fields[2]);
420     wnum = Integer.parseInt(fields[3]);
421     size = Double.parseDouble(fields[4]);
422     if(dnum < 0 || wnum < 0 || size < 0.0){
423     dnum = -1;
424     wnum = -1;
425     size = -1.0;
426     }
427     } catch(Exception e){
428     name = null;
429     label = null;
430     dnum = -1;
431     wnum = -1;
432     size = -1.0;
433     }
434     }
435     private String cond_to_query(Condition cond, int depth){
436     StringBuffer sb = new StringBuffer();
437     try {
438     String phrase = cond.phrase();
439     sb.append("phrase=");
440     if(phrase != null) sb.append(URLEncoder.encode(phrase, "UTF-8"));
441     List attrs = cond.attrs();
442     Iterator it = cond.attrs().iterator();
443     for(int i = 1; it.hasNext(); i++){
444     sb.append("&attr=");
445     sb.append(i);
446     sb.append(URLEncoder.encode((String)it.next(), "UTF-8"));
447     }
448     String order = cond.order();
449     if(order != null){
450     sb.append("&order=");
451     sb.append(URLEncoder.encode(order, "UTF-8"));
452     }
453     int max = cond.max();
454     if(max >= 0){
455     sb.append("&max=");
456     sb.append(max);
457     }
458     int options = cond.options();
459     if(options > 0){
460     sb.append("&options=");
461     sb.append(options);
462     }
463     if(depth > 0){
464     sb.append("&depth=");
465     sb.append(depth);
466     }
467     } catch(UnsupportedEncodingException e){
468     throw new RuntimeException(e);
469     }
470     return sb.toString();
471     }
472     }
473    
474    
475    
476     /* END OF FILE */

  ViewVC Help
Powered by ViewVC 1.1.26