Statistics
| Revision:

root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / protocols / Z3950Protocol.java @ 2658

History | View | Annotate | Download (8.94 KB)

1
/*
2
 * Created on 27-abr-2005
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Style - Code Templates
6
 */
7
package es.gva.cit.catalogClient.protocols;
8

    
9

    
10
import java.io.ByteArrayInputStream;
11
import java.io.InputStream;
12
import java.net.URL;
13
import java.util.Properties;
14
import java.util.StringTokenizer;
15
import java.util.Vector;
16

    
17
import org.w3c.dom.Document;
18
import org.w3c.dom.Node;
19

    
20
import es.gva.cit.catalogClient.metadataXML.XMLTree;
21
import es.gva.cit.catalogClient.metadataXML.XMLTreeNumberOfRecordsAnswer;
22
import es.gva.cit.catalogClient.utils.Strings;
23

    
24
import com.k_int.IR.IRQuery;
25
import com.k_int.IR.InformationFragment;
26
import com.k_int.IR.InformationFragmentSource;
27
import com.k_int.IR.RecordFormatSpecification;
28
import com.k_int.IR.SearchException;
29
import com.k_int.IR.SearchTask;
30
import com.k_int.IR.Searchable;
31
import com.k_int.IR.TimeoutExceededException;
32
import com.k_int.gen.Z39_50_APDU_1995.InitializeResponse_type;
33

    
34

    
35
/**
36
 * @author Administrador
37
 *
38
 * TODO To change the template for this generated type comment go to
39
 * Window - Preferences - Java - Code Style - Code Templates
40
 */
41
public class Z3950Protocol extends Z3950OriginBean implements IProtocols {
42
        public static final char ISO2709_RS = 035;
43
        public static final char ISO2709_FS = 036;
44
        public static final char ISO2709_IDFS = 037;
45
        private static final String PREFIX_QUERY_TYPE = "PREFIX";
46
        private static final String CCL_QUERY_TYPE = "CCL";;
47
                
48
        private int auth_type = 0; // 0=none, 1=anonymous, 2=open, 3=idpass
49
        private String principal = null;
50
        private String group = null;
51
        private String credentials = null;
52
        
53
        public Node[] doQuery(URL url,Object object,int firstRecord){
54
                String message = (String) object;
55
                Node[] nodes; 
56
                
57
                //Search properties
58
                Properties props = new Properties();
59
                props.setProperty("ServiceHost",url.getHost());
60
                props.setProperty("ServicePort",String.valueOf(url.getPort()));
61
                //props.setProperty("service_short_name","");
62
            //props.setProperty("service_long_name","");
63
            //props.setProperty("default_record_syntax","xml");
64
                //props.setProperty("service_user_principal","");
65
            //props.setProperty("service_user_group","");
66
            //props.setProperty("service_user_credentials","");
67
                                    
68
                //Search Object
69
            Searchable zServer = new com.k_int.z3950.IRClient.Z3950Origin();
70
            zServer.init(props);
71
                    
72
                //Create the query object
73
            IRQuery e = new IRQuery();
74
                e.collections = new Vector();
75
                e.collections.add(getDatabase(url));
76
            e.query = new com.k_int.IR.QueryModels.PrefixString(new String(message));
77
                                         
78
            //create the search task
79
            SearchTask st = (SearchTask) zServer.createTask(e,null);
80
                                                                                                                   
81
            try {
82
                        // time to wait
83
                        st.evaluate(1000000);
84
                                                                
85
                        RecordFormatSpecification spec = new RecordFormatSpecification("xml",null,"f");
86
                        InformationFragmentSource ifs = st.getTaskResultSet();
87
                        InformationFragment frag = null;
88
                        Document[] doms = new Document[ifs.getFragmentCount()];
89
                                                                                                
90
                        int lastRecord;
91
                        if (ifs.getFragmentCount() > 10){
92
                                nodes = new Node[11];
93
                                if ((ifs.getFragmentCount() - firstRecord) < 10)
94
                                        lastRecord = ifs.getFragmentCount();
95
                                else
96
                                        lastRecord = firstRecord + 9;
97
                        }else{
98
                                nodes = new Node[ifs.getFragmentCount() + 1];
99
                                lastRecord = ifs.getFragmentCount();
100
                        }
101
                                
102
                        nodes[0] = XMLTreeNumberOfRecordsAnswer.getNode(ifs.getFragmentCount(),
103
                                        firstRecord,lastRecord);
104
                                                                
105
                        int index = 1;                
106
                        for ( int i = firstRecord ; i<= lastRecord; i++ ){
107
                                //Fragment
108
                                frag = ifs.getFragment(i,spec);
109
                                                                        
110
                                String cadena = frag.toString();
111
                                //This is a strange character
112
                                cadena = Strings.replace(cadena,"?","");
113
                                //Change the format.
114
                                cadena = Strings.replace(cadena,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
115
                                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
116
                                                                                
117
                                //System.out.println(cadena);
118
                                        
119
                                InputStream buffer = new ByteArrayInputStream(cadena.getBytes());
120
                                nodes[index] = XMLTree.XMLToTree(buffer);
121
                                index++;
122
                        }
123
           } catch (TimeoutExceededException e1) {
124
                        // TODO Auto-generated catch block
125
                        e1.printStackTrace();
126
                        return null;
127
                } catch (SearchException e1) {
128
                        // TODO Auto-generated catch block
129
                        e1.printStackTrace();
130
                        return null;
131
                } catch (Exception e2) {
132
                        //TODO Salta cuando no nos devuelve nada
133
                        e2.printStackTrace();
134
                        return null;
135
                }
136
                    
137
                zServer.destroy();
138
                zServer = null;        
139
            return nodes;
140
    }
141
        
142
        public String getDatabase(URL url){
143
                StringTokenizer sti = new StringTokenizer(url.getPath(),"/");
144
                if (sti.countTokens() == 0)
145
                        return "Default";
146
                else
147
                        return sti.nextToken();
148
        }
149
        
150
        public String openConnection(URL url){
151
            String hostname = url.getHost();
152
                  String port = String.valueOf(url.getPort());
153
                String cadena = "";
154
            
155
                try{
156
                    InitializeResponse_type resp = connect(hostname,port,auth_type,principal,group,credentials);
157

    
158
                    if (resp.result.booleanValue() == true ){
159
                            if ( resp.referenceId != null )
160
                                    cadena = "Reference ID : " + new String(resp.referenceId) + "\n";
161
                            cadena = cadena + "Implementation ID : " + resp.implementationId + "\n";
162
                            cadena = cadena + "Implementation Name : " + resp.implementationName + "\n" ;
163
                            cadena = cadena + "Implementation Version : " + resp.implementationVersion + "\n";
164
                            
165
                    }else{
166
                            System.out.println(" Failed to establish association");
167
                            return null;
168
                    }
169
            }catch ( Exception e ){
170
                    e.printStackTrace();
171
                    return null;
172
            }
173
            return cadena;
174
        }
175
        
176
        public static boolean isProtocolSupported(URL url){
177
                try{
178
                        //Search properties
179
                        Properties props = new Properties();
180
                        props.setProperty("ServiceHost",url.getHost());
181
                        props.setProperty("ServicePort",String.valueOf(url.getPort()));
182
          
183
                        //Search Object
184
                        Searchable zServer = new com.k_int.z3950.IRClient.Z3950Origin();
185
                        zServer.init(props);
186
                        
187
                        //        Create the query object
188
                    IRQuery e = new IRQuery();
189
                    e.collections = new Vector();
190
                    e.collections.add(url.getHost()+":"+String.valueOf(url.getPort()));
191
                    e.query = new com.k_int.IR.QueryModels.PrefixString("@attrset geo @attr 1=4 @attr 4=6 @attr 2=3 \"a\"" );
192
                
193
                    //create the search task
194
                    SearchTask st = (SearchTask) zServer.createTask(e,null);
195
                    
196
                          st.evaluate(100000);
197
                
198
                }catch(Exception e){
199
                        System.err.println(e.toString());
200
                        return false;
201
                }
202
                return true;
203

    
204
        }
205
         
206
         private void DisplayISO2709(byte[] octets)
207
          {
208
            String orig = new String(octets);
209

    
210
            int record_length = Integer.parseInt(orig.substring(0,5));   // Positions 0-4 are length of record
211
            char record_status = orig.charAt(5);
212
            char type_of_record = orig.charAt(6);
213
            int indicator_length = 2;                                    // Default indicator length
214
            int subfield_code_length = 2;                                // Default indicator length
215
            char character_coding_scheme = orig.charAt(9);
216
            int base_address = Integer.parseInt(orig.substring(12,17));  //
217
         
218
            int length_field_length = Character.digit(orig.charAt(20),10);
219
            int length_entry = Character.digit(orig.charAt(21),10);
220
         
221
            if ( Character.isDigit(orig.charAt(10)) )
222
              indicator_length = Character.digit(orig.charAt(10), 10);
223
         
224
            if ( Character.isDigit(orig.charAt(11)) )
225
              subfield_code_length = Character.digit(orig.charAt(11), 10);
226
         
227
            int offset = 24;
228

    
229
            while ( orig.charAt(offset) != ISO2709_FS )
230
            {
231
              // int tag = Integer.parseInt(orig.substring(offset, offset+3));
232
              String tag = orig.substring(offset, offset+3);
233
              System.out.print(tag+" ");
234
              offset += 3;
235
              int data_length = Integer.parseInt(orig.substring(offset, offset+length_field_length));
236
              offset += length_field_length;
237
              int data_offset = Integer.parseInt(orig.substring(offset, offset+length_entry));
238
              offset += length_entry;
239
         
240
              String this_tag = orig.substring(base_address+data_offset+2, base_address+data_offset+data_length);
241

    
242
         
243
              StringTokenizer st = new StringTokenizer(this_tag, ""+ISO2709_RS+ISO2709_FS+ISO2709_IDFS, true);
244
         
245
              String subfield = null;
246
         
247
              while ( st.hasMoreTokens() )
248
              {
249
                subfield = st.nextToken();
250
         
251
                if ( subfield.charAt(0) == ISO2709_RS )
252
                {
253
                  // System.err.print("Record sep ");
254
                }
255
                else if ( subfield.charAt(0) == ISO2709_FS )
256
                {
257
                  // System.err.print("Field sep ");
258
                }
259
                else if ( subfield.charAt(0) == ISO2709_IDFS )
260
                {
261
                  subfield = st.nextToken();
262
                  System.out.print("$"+subfield.charAt(0)+" "+subfield.substring(1, subfield.length()));
263
                }
264
                else
265
                {
266
                  System.out.print(subfield);
267
                }
268
              }
269
              System.out.println("");
270
            }
271
          }
272

    
273
          private void displayGRS(Vector v)
274
          {
275
            com.k_int.IR.Syntaxes.GRS1 grs_rec = new com.k_int.IR.Syntaxes.GRS1("Repository",
276
                                                                                "Coll", 
277
                                                                                "", 
278
                                                                                v,
279
                                                                                null);
280
            System.out.println(grs_rec.toString());
281
          }
282

    
283
}