Statistics
| Revision:

gvsig-catalog / trunk / appCatalog / src / org / gvsig / catalog / protocols / Z3950Connection.java @ 6

History | View | Annotate | Download (6.95 KB)

1
package org.gvsig.catalog.protocols;
2

    
3
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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
 * For more information, contact:
22
 *
23
 *  Generalitat Valenciana
24
 *   Conselleria d'Infraestructures i Transport
25
 *   Av. Blasco Ib??ez, 50
26
 *   46010 VALENCIA
27
 *   SPAIN
28
 *
29
 *      +34 963862235
30
 *   gvsig@gva.es
31
 *      www.gvsig.gva.es
32
 *
33
 *    or
34
 *
35
 *   IVER T.I. S.A
36
 *   Salamanca 50
37
 *   46005 Valencia
38
 *   Spain
39
 *
40
 *   +34 963163400
41
 *   dac@iver.es
42
 */
43
/* CVS MESSAGES:
44
 *
45
 * $Id: Z3950Connection.java 561 2007-07-27 06:49:30 +0000 (Fri, 27 Jul 2007) jpiera $
46
 * $Log$
47
 * Revision 1.1.2.6.4.3  2007/07/23 07:14:25  jorpiell
48
 * Catalog refactoring
49
 *
50
 * Revision 1.1.2.6.4.2  2007/07/13 12:00:35  jorpiell
51
 * Add the posibility to add a new panel
52
 *
53
 * Revision 1.1.2.6.4.1  2007/07/10 11:18:04  jorpiell
54
 * Added the registers
55
 *
56
 * Revision 1.1.2.6  2006/11/15 00:08:08  jjdelcerro
57
 * *** empty log message ***
58
 *
59
 * Revision 1.4  2006/11/13 07:52:58  jorpiell
60
 * Se han incluido los cambios que se hicieron para Chile: cambio de la librer?a jzKit, carga de ArcIms y WCS
61
 *
62
 * Revision 1.3  2006/10/02 08:29:07  jorpiell
63
 * Modificados los cambios del Branch 10 al head
64
 *
65
 * Revision 1.1.2.3  2006/10/02 07:13:43  jorpiell
66
 * Comprobada una NullPointerException que generaba una excepci?n
67
 *
68
 * Revision 1.1.2.2  2006/09/25 06:48:38  jorpiell
69
 * A?adida la funcionalidad de mostrar el documento de texto cuando no es un XML
70
 *
71
 * Revision 1.1.2.1  2006/09/20 12:01:07  jorpiell
72
 * Se ha cambiado la versi?n de la jzkit, se ha incorporado la funcionalidad de cargar arcims
73
 *
74
 * Revision 1.1  2006/09/20 11:20:17  jorpiell
75
 * Se ha cambiado la versi?n de la librer?a jzkit de la 1 a la 2.0
76
 *
77
 *
78
 */
79
import java.net.URI;
80
import java.util.StringTokenizer;
81
import java.util.Vector;
82

    
83
import org.gvsig.catalog.drivers.GetRecordsReply;
84
import org.gvsig.catalog.metadataxml.XMLNode;
85
import org.gvsig.catalog.metadataxml.XMLTree;
86
import org.jzkit.a2j.codec.util.OIDRegister;
87
import org.jzkit.search.provider.iface.IRQuery;
88
import org.jzkit.search.provider.iface.SearchException;
89
import org.jzkit.search.provider.iface.Searchable;
90
import org.jzkit.search.util.RecordModel.ExplicitRecordFormatSpecification;
91
import org.jzkit.search.util.RecordModel.InformationFragment;
92
import org.jzkit.search.util.RecordModel.RecordFormatSpecification;
93
import org.jzkit.search.util.RecordModel.SUTRS;
94
import org.jzkit.search.util.RecordModel.XMLRecord;
95
import org.jzkit.search.util.ResultSet.IRResultSet;
96
import org.jzkit.search.util.ResultSet.IRResultSetException;
97
import org.jzkit.search.util.ResultSet.IRResultSetStatus;
98
import org.jzkit.z3950.client.SynchronousOriginBean;
99
import org.jzkit.z3950.gen.v3.Z39_50_APDU_1995.InitializeResponse_type;
100

    
101

    
102
public class Z3950Connection extends SynchronousOriginBean
103
{
104
  public static final char ISO2709_RS = 035;
105
  public static final char ISO2709_FS = 036;
106
  public static final char ISO2709_IDFS = 037;
107
  private static final String PREFIX_QUERY_TYPE = "PREFIX";
108
  private static final String CCL_QUERY_TYPE = "CCL";
109

    
110
  private int auth_type = 0; // 0=none, 1=anonymous, 2=open, 3=idpass
111
  private String principal = null;
112
  private String group = null;
113
  private String credentials = null; 
114
  private InitializeResponse_type resp = null;
115
  private URI uri = null;
116
  private String host = null;
117
  private int port = 0;
118
  private String database = null;
119
  
120
  
121
  public Z3950Connection(URI uri){
122
          super(new OIDRegister("/a2j.properties"));
123
          this.uri = uri;
124
          this.host = uri.getHost();
125
          this.port = uri.getPort();
126
          StringTokenizer sti = new StringTokenizer(uri.getPath(), "/");
127
      if (sti.countTokens() == 0) {
128
          this.database = "geo";
129
      } else {
130
          this.database = sti.nextToken();
131
      }
132
  }
133
  
134
  /**
135
   * 
136
   * @param url
137
   * @return
138
   */
139
  public String connect(){
140
                   if (resp == null){
141
                    resp = connect(host,port,auth_type,principal,group,credentials);
142
            }
143
            StringBuffer string = new StringBuffer();            
144
            if (resp == null){
145
                    return null;
146
            }
147
            if (resp.result.booleanValue() == true) {
148
                    if (resp.referenceId != null) {
149
                            string.append("Reference ID : " + new String(resp.referenceId) +
150
                            "\n");
151
                    }
152
                    string.append("Implementation ID : " +
153
                                    resp.implementationId + "\n");
154
                    string.append("Implementation Name : " +
155
                                    resp.implementationName + "\n");
156
                    string.append("Implementation Version : " +
157
                                    resp.implementationVersion + "\n");
158
            } else {
159
                    System.out.println(" Failed to establish association");
160
                    return null;
161
            }
162
            
163
            return string.toString();
164
  }
165

    
166
  public GetRecordsReply search(GetRecordsReply recordsReply, String sQuery,int firstRecord){
167
          try{
168
                  IRQuery query = new IRQuery();
169
                  query.collections = new Vector();
170
                  query.collections.add(database);
171
                  query.query = new org.jzkit.search.util.QueryModel.PrefixString.PrefixString(sQuery);
172
                  
173
                  Searchable s = Z3950ConnectionFactory.getSearchable(uri);
174
                  IRResultSet result = s.evaluate(query);
175
                  result.waitForStatus(IRResultSetStatus.COMPLETE|IRResultSetStatus.FAILURE,0);
176
                
177
                  int numOfResults = result.getFragmentCount();
178
                  recordsReply.setNumRecords(numOfResults);
179
                                  
180
                  RecordFormatSpecification rfs = new ExplicitRecordFormatSpecification(null,null,"f");
181
                  InformationFragment[] fragment = result.getFragment(firstRecord,10,rfs);
182

    
183
                  for (int i=0 ; i<fragment.length ; i++){
184
                          try {
185
                                  String answer = "";                                  
186
                                  if (fragment[i] instanceof XMLRecord){                                           
187
                                          XMLRecord xml = (XMLRecord)fragment[i];        
188
                                          answer = xml.toString();
189
                                  }else {
190
                                          SUTRS sutr = (SUTRS)fragment[i];
191
                                          answer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
192
                                          "<" + XMLNode.ISNOTXML + ">" + 
193
                                          sutr.toString() + 
194
                                          "</" + XMLNode.ISNOTXML + ">";
195
                                  }                                    
196
                                  recordsReply.addRecord(uri, XMLTree.xmlToTree(answer));
197
                          } catch (Exception e1) {
198
                                  // TODO Auto-generated catch block
199
                                  e1.printStackTrace();
200
                          }
201
                  }        
202
          }catch(SearchException e){
203
                  e.printStackTrace();
204
          } catch (IRResultSetException e) {
205
                  // TODO Auto-generated catch block
206
                  e.printStackTrace();
207
          } catch (Exception e) {
208
                  // TODO Auto-generated catch block
209
                  e.printStackTrace();
210
          }          
211
      return recordsReply;
212
  } 
213
}