Revision: 1801
Author:
rh...@virginia.edu
Date: Thu Jun 11 17:50:33 2015 UTC
Log: New code to allow connecting to a Z3950 server that requires a
username and password.
https://code.google.com/p/solrmarc/source/detail?r=1801
Added:
/trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950RecordIter.java
Modified:
/trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950Importer.java
/trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950MarcReader.java
/trunk/lib/solrmarc/src/org/solrmarc/z3950/ZClient.java
=======================================
--- /dev/null
+++ /trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950RecordIter.java Thu Jun
11 17:50:33 2015 UTC
@@ -0,0 +1,50 @@
+package org.solrmarc.z3950;
+
+import java.util.Iterator;
+
+import org.marc4j.marc.Record;
+
+public class Z3950RecordIter implements Iterator<Record>
+{
+ ZClient client;
+ String resultSetName;
+ int numInResultSet;
+ int currentCursor;
+ String remapStr = null;
+
+ public Z3950RecordIter(ZClient client, String resultSetName, int
numInResultSet)
+ {
+ this.client = client;
+ this.resultSetName = resultSetName;
+ this.numInResultSet = numInResultSet;
+ currentCursor = 0;
+ }
+
+ public Z3950RecordIter(ZClient zClient, String resultSetName, int
numInResultSet, String remapStr)
+ {
+ this(zClient, resultSetName, numInResultSet);
+ this.remapStr = remapStr;
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ if (currentCursor < numInResultSet) return(true);
+ return false;
+ }
+
+ @Override
+ public Record next()
+ {
+ Record rec = client.getRecord(++this.currentCursor,
this.resultSetName);
+ return(rec);
+ }
+
+ @Override
+ public void remove()
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
=======================================
--- /trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950Importer.java Mon Jan
19 21:36:55 2009 UTC
+++ /trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950Importer.java Thu Jun
11 17:50:33 2015 UTC
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
+import java.util.Iterator;
import org.marc4j.marc.Record;
import org.solrmarc.marc.MarcImporter;
@@ -133,9 +134,10 @@
recordCounter++;
try{
- Record record = newclient.getRecordByIDNum(recordNum);
+ Iterator<Record> recordIter =
newclient.getRecordByIDStr(""+recordNum);
// newclient.cmdFind("@attrset bib-1 @attr 1=1016 house");
// Record record = newclient.getRecord(recordNum);
+ Record record = recordIter.next();
if (record != null)
{
//System.out.println("Adding record " + recordCounter
+ ": " + record.getControlNumber() + " (" + df.format( (
recordCounter/totalRecords) * 100 ) + "% complete)");
=======================================
--- /trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950MarcReader.java Tue Apr
12 21:04:46 2011 UTC
+++ /trunk/lib/solrmarc/src/org/solrmarc/z3950/Z3950MarcReader.java Thu Jun
11 17:50:33 2015 UTC
@@ -26,6 +26,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -49,28 +50,16 @@
private boolean opened = false;
private Vector<String> recids = null;
private int curRecNum = 0;
+ private Iterator<Record> curRecIter = null;
private Record curRec = null;
- public Z3950MarcReader(String hostPort, String[] args)
+ public Z3950MarcReader(ZClient client, String[] args)
{
- newclient = new ZClient();
- Package ir_package = Package.getPackage("
com.k_int.IR");
- Package a2j_runtime_package =
Package.getPackage("com.k_int.codec.runtime");
-
-// System.out.println("JZKit command line z39.50 client $Revision:
1.53 $");
-
- String parms[] = hostPort.split(":");
- opened = newclient.openConnection(parms[0], parms[1]);
- if (parms.length >= 3)
- {
- newclient.cmdBase(parms[3]);
- }
- newclient.cmdElements("F");
- newclient.cmdFormat("usmarc");
+ newclient = client;
recids = new Vector<String>();
for (int i = 0; i < args.length; i++)
{
- if (args[i].matches("u?[0-9]+"))
+ if (args[i].matches("u?[0-9]+") ||
args[i].matches("[0-9]*:.*"))
{
recids.add(args[i]);
}
@@ -101,24 +90,24 @@
{
System.err.println("Error: unable to find and open
record-id-list: "+ filename);
}
- catch (IOException e)
- {
- System.err.println("Error: reading from
record-id-list: "+ filename);
- }
+// catch (IOException e)
+// {
+// System.err.println("Error: reading from
record-id-list: "+ filename);
+// }
}
}
}
- private int getIdFromIdString(String idstr)
+ private String getIdFromIdString(String idstr)
{
- int idnum = -1;
+ String idnum = null;
if (idstr.matches("u[0-9]+"))
{
- idnum = Integer.parseInt(idstr.substring(1));
+ idnum = idstr.substring(1);
}
else if (idstr.matches("[0-9]+"))
{
- idnum = Integer.parseInt(idstr);
+ idnum = idstr;
}
return(idnum);
}
@@ -155,38 +144,82 @@
}
return(curRec != null);
}
+
+ private Iterator<Record> nextIter()
+ {
+ Iterator<Record> recIter = null;
+ String nextRecStr = (curRecNum < recids.size()) ?
recids.elementAt(curRecNum++) : null;
+ if (nextRecStr == null) return(recIter);
+ String recNo = getIdFromIdString(nextRecStr);
+ if (recNo != null)
+ recIter = newclient.getRecordByIDStr(recNo);
+ else
+ {
+ String[] parts = nextRecStr.split(":", 2);
+ if (parts.length != 2)
+ return(recIter);
+ recIter = newclient.getRecordBySearchStr(parts[0], parts[1]);
+ }
+ return(recIter);
+ }
public Record next()
{
- Record record = null;
if (curRec != null)
{
Record tmprec = curRec;
curRec = null;
return(tmprec);
}
- while (record == null)
+ while (true)
{
- String nextRecStr = (curRecNum < recids.size()) ?
recids.elementAt(curRecNum++) : null;
- if (nextRecStr == null) return(null);
- int recNo = getIdFromIdString(nextRecStr);
- record = newclient.getRecordByIDNum(recNo);
+ if (curRecIter != null && curRecIter.hasNext())
+ return(curRecIter.next());
+ curRecIter = nextIter();
+ if (curRecIter == null) break;
}
- return(record);
+ return(null);
+ }
+
+ public static ZClient makeZClient(String hostPortDbUserPass)
+ {
+ ZClient newclient = new ZClient();
+ Package ir_package = Package.getPackage("
com.k_int.IR");
+ Package a2j_runtime_package =
Package.getPackage("com.k_int.codec.runtime");
+
+// System.out.println("JZKit command line z39.50 client $Revision:
1.53 $");
+
+ String parms[] = hostPortDbUserPass.split(":");
+ String host = parms[0];
+ String port = parms[1];
+ String database = parms.length > 2 ? parms[2] : null;
+ String userId = parms.length > 3 ? parms[3] : null;
+ String pass = parms.length > 4 ? parms[4] : null;
+ boolean opened = newclient.openConnection(host, port, userId,
pass);
+ if (database != null)
+ {
+ newclient.cmdBase(database);
+ }
+ newclient.cmdElements("F");
+ newclient.cmdFormat("usmarc");
+ if (opened)
+ return(newclient);
+ else
+ return(null);
}
public static void main(String args[])
{
- String server = "
virgo.lib.virginia.edu:2200";
- if (args.length > 0 &&
args[0].matches("[A-Za-z0-9]+[.][A-Za-z0-9]+[.][A-Za-z0-9]+[.][A-Za-z0-9]+:[0-9]+"))
+ String hostPortDbUserPass = "
virgo.lib.virginia.edu:2200";
+ if (args.length > 0 &&
args[0].matches("[A-Za-z0-9][A-Za-z0-9.]+[A-Za-z0-9]+:[0-9]+.*"))
{
- server = args[0];
+ hostPortDbUserPass = args[0];
String[] tmpArgs = args;
args = new String[tmpArgs.length - 1];
System.arraycopy(tmpArgs, 1, args, 0, tmpArgs.length-1);
}
-
- MarcReader reader = new Z3950MarcReader(server, args);
+ ZClient zclient = makeZClient(hostPortDbUserPass);
+ MarcReader reader = new Z3950MarcReader(zclient, args);
OutputStream marcOutput = null;
PrintStream output = null;
// if (args.length >= 2)
=======================================
--- /trunk/lib/solrmarc/src/org/solrmarc/z3950/ZClient.java Fri Apr 29
21:19:53 2011 UTC
+++ /trunk/lib/solrmarc/src/org/solrmarc/z3950/ZClient.java Thu Jun 11
17:50:33 2015 UTC
@@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.util.Enumeration;
+import java.util.Iterator;
import java.util.Vector;
import com.k_int.gen.Z39_50_APDU_1995.DefaultDiagFormat_type;
@@ -87,7 +88,8 @@
newclient.verbose = true;
MarcStreamWriter writer = new MarcStreamWriter(System.out);
- Record rec = newclient.getRecordByIDNum(1);
+ Iterator<Record> recIter = newclient.getRecordByIDStr("1");
+ Record rec = recIter.next();
writer.write(rec);
System.err.println(rec.toString());
@@ -125,12 +127,36 @@
}
return (respVal);
}
+
+ public boolean openConnection(String hostname, String portnum, String
userID, String password)
+ {
+ boolean respVal = false;
+
+ try
+ {
+ if (verbose) System.err.println("Attempting connection to "+
hostname + " : " + portnum);
+ InitializeResponse_type resp = connect(hostname, portnum,
+ ((userID == null) ? 0 : 3), userID, group, password);
+
+ if (verbose) System.err.println("Received response from
connect");
+ respVal = resp.result.booleanValue();
+ if (respVal != true)
+ {
+ if (verbose) System.err.println(" Failed to establish
association");
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ return (respVal);
+ }
- public int cmdFind(String args)
+ public int cmdFind(String args, String resultSetName)
{
SearchResponse_type resp = null;
int numResults = 0;
- current_result_set_name = "RS" + (result_set_count++);
+ current_result_set_name = resultSetName;
if (verbose) System.err.println("Calling find, query= " + args);
try
@@ -183,27 +209,67 @@
}
return(numResults);
}
+
+ public int cmdFind(String args)
+ {
+ String curResSetName = "RS" + (result_set_count++);
+ return(cmdFind(args, curResSetName));
+ }
-
- public Record getRecordByIDNum(int idnum)
+// public Record getRecordByIDNum(int idnum)
+// {
+// Iterator<Record> iter = getRecordBySearchStr("1016", "^C"+idnum);
+//
+//
+//
+// cmdFind("@attrset bib-1 @attr 1=1016 \"^C"+idnum+"\"");
+// if (verbose) System.err.println("requesting record by ID:" +
idnum);
+// Record rec = getRecord(1);
+// if (verbose) System.err.println("getting record by ID:" + idnum);
+// if (rec != null)
+// {
+// if (verbose) System.err.println("adding ID to record:" +
idnum);
+// rec.addVariableField(new ControlFieldImpl("001", "u"+idnum));
+// }
+// return(rec);
+// }
+
+ public Iterator<Record> getRecordByIDStr(String idstr)
{
- cmdFind("@attrset bib-1 @attr 1=1016 \"^C"+idnum+"\"");
- if (verbose) System.err.println("requesting record by ID:" +
idnum);
- Record rec = getRecord(1);
- if (verbose) System.err.println("getting record by ID:" + idnum);
- if (rec != null)
- {
- if (verbose) System.err.println("adding ID to record:" +
idnum);
- rec.addVariableField(new ControlFieldImpl("001", "u"+idnum));
- }
- return(rec);
+ int numFound = cmdFind("@attrset bib-1 @attr 1=1016
\"^C"+idstr+"\"", "default");
+ Z3950RecordIter iter = new Z3950RecordIter(this, "default",
numFound, "u"+idstr);
+ return(iter);
+
+
+// cmdFind("@attrset bib-1 @attr 1=1016 \"^C"+idnum+"\"");
+// if (verbose) System.err.println("requesting record by ID:" +
idnum);
+// Record rec = getRecord(1);
+// if (verbose) System.err.println("getting record by ID:" + idnum);
+// if (rec != null)
+// {
+// if (verbose) System.err.println("adding ID to record:" +
idnum);
+// rec.addVariableField(new ControlFieldImpl("001", "u"+idnum));
+// }
+// return(rec);
+ }
+
+ public Iterator<Record> getRecordBySearchStr(String attr, String value)
+ {
+ int numFound = cmdFind("@attrset bib-1 @attr 1="+attr+"
\""+value+"\"", "default");
+ Z3950RecordIter iter = new Z3950RecordIter(this, "default",
numFound);
+ return(iter);
}
public Record getRecord(int startAt)
+ {
+ return(getRecord(startAt, current_result_set_name));
+ }
+
+ public Record getRecord(int startAt, String resultSetName)
{
// Format for present command is n [ + n ]
// System.err.println("Present "+args);
- String setname = current_result_set_name;
+ String setname = resultSetName;
try
{