import java.util.Vector;
import java.net.*;
import java.io.*;
/** An AdditionalResultsFacade
defines an internal search engine
* that unmarshalls SearchResult
from the Server.
*
* @version 1.0
* @author Deric Bertrand - November 1999
*/
public class AdditionalResultsFacade extends SearchEngineFacade
{
/** The ASP Session ID */
private String m_SessionID = null;
/** The number or Search Results returned */
private int m_ResultCount;
/** The State of the SearchResultsCollector
on the Server */
private int m_RemoteCollectorState;
/**
* Sets the value of the ASP Session ID
* @param asSessionID The ASP Session ID (cookie)
*/
public void setSessionId(String asSessionID)
{
m_SessionID = asSessionID;
}
/**
* Submits a query request to the AdditionalResults.ASP
and unmarshalls the
* searialized results into a SearchResult
collection
* @param urlName The URL of the AdditionalResults.ASP
* @param listingIndex Starting index of the query
* @param theCollection The Vector
of SearchResult
* @return The number of results obtained from the current search request
*/
public int SubmitQuery(String urlName, int listingIndex, Vector theCollection)
throws java.net.MalformedURLException, java.io.IOException, java.text.ParseException, Exception
{
HttpURLConnection theConnection;
BufferedReader in = null;
SearchResult searchResult;
URL theURL;
String resultLine;
int resultCount;
// Must have a sessionID to proceed
if ( m_SessionID == null)
throw new Exception("Session ID must be set before connecting to ASP.");
// Append the search index to the url
if ( listingIndex > +0)
urlName += ("?listingIndex=" + listingIndex);
theURL = new URL( urlName);
// Handle the scenario of the AdditionalResults.asp finding an empty collection,
// but the collector is still searching
do
{
theConnection = (HttpURLConnection) theURL.openConnection();
theConnection.setRequestProperty("Cookie", m_SessionID);
// added exception handling - 11/11/99 - Deric Bertrand
if (( theConnection.getResponseCode() == -1) ||(theConnection.getResponseCode() != HttpURLConnection.HTTP_OK))
throw new IOException("Bad response from URLconnect");
// Get the header fields
for (int i=1; theConnection.getHeaderFieldKey(i) != null; i++)
{
if (theConnection.getHeaderFieldKey(i).equals("result-count"))
m_ResultCount = Integer.parseInt(theConnection.getHeaderField(i));
else if (theConnection.getHeaderFieldKey(i).equals("collector-state"))
m_RemoteCollectorState = Integer.parseInt(theConnection.getHeaderField(i));
}
in = new BufferedReader(new InputStreamReader(theConnection.getInputStream()));
if (m_ResultCount <= 0 && m_RemoteCollectorState < SearchResultsCollector.S_Done)
in.close();
} while ( m_ResultCount <= 0 && m_RemoteCollectorState < SearchResultsCollector.S_Done);
// Marshall the Search Results
for ( resultCount=0; resultCount < m_ResultCount; resultCount++)
{
resultLine = in.readLine();
searchResult = new SearchResult(resultLine);
if ( searchResult.getRank() < listingIndex)
throw new Exception(
"Invalid Search Result retrieved for listingIndex"
);
theCollection.addElement( searchResult);
}
in.close();
return resultCount;
}
}