/*
* File: AmazonItemSearcher.java
* Created on 8 / desembre / 2007, 16:07
*
* Copyright (C) 2008 M.Àngels Cerveró Abelló
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client.ws.milanas.searcher;
import client.ws.milanas.exceptions.SOAPConnectionException;
import client.ws.milanas.exceptions.SOAPEnvelopeCreationException;
import client.ws.milanas.helpers.SOAPEnvelopeCreator;
import client.ws.milanas.helpers.beans.Item;
import client.ws.milanas.helpers.dom.DOMItemHandlerFactory;
import client.ws.milanas.helpers.dom.DOMItemHandler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Document;
/**
*
* @author milana
*/
public class AmazonItemSearcher implements ItemSearcher
{
static private SOAPConnection connection;
static private Properties properties = new Properties();
/** Creates a new instance of AmazonItemSearcher */
public AmazonItemSearcher()
{
ClassLoader cl = this.getClass().getClassLoader();
try
{
properties.load(cl.getResourceAsStream("MilanasWSClient.properties"));
}
catch(IOException ioe){}
}
/**
* Creates a new SOAP connection for the WebService client
*/
public void createConnection()
{
try
{
connection = SOAPConnectionFactory.newInstance().createConnection();
}
catch(SOAPException se){}
}
/**
* Closes the SOAP connection of the client
*/
public void closeConnection()
{
try
{
connection.close();
}
catch(SOAPException se){}
}
/**
* Searches books into Amazon's shop using the WebService "ItemSearch".
* The books will be found using specific words (author, title) or abstract ones
* (keywords). In addition, condition and sorting method can be defined.
*/
public ArrayList<Item> searchBooks(String author, String title, String keywords, String condition, String sort) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("Author", author);
return searchItem(parameters, "Books", title, keywords, condition, sort);
}
/**
* Searches music (CDs) into Amazon's shop using the WebService "ItemSearch".
* The music will be found using specific words (artist, title) or abstract ones
* (keywords). In addition, condition and sorting method can be defined.
*/
public ArrayList<Item> searchMusic(String artist, String title, String keywords, String condition, String sort) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("Artist", artist);
return searchItem(parameters, "Music", title, keywords, condition, sort);
}
/**
* Looks up from an especific book into Amazon's shop using the WebService "ItemLookup".
* This book will be found using its identifier (ASIN).
*/
public Item lookupBook(String asin) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
return lookupItem(asin, "Books");
}
/**
* Looks up from an especific music into Amazon's shop using the WebService "ItemLookup".
* This music will be found using its identifier (ASIN).
*/
public Item lookupMusic(String asin) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
return lookupItem(asin, "Music");
}
private ArrayList<Item> searchItem(HashMap<String, String> parameters, String cathegory, String title, String keywords, String condition, String sort) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
parameters.put("SearchIndex", cathegory);
parameters.put("Title", title);
parameters.put("Keywords", keywords);
parameters.put("Condition", condition);
parameters.put("Sort", sort);
return sendSOAPMessages("ItemSearch", cathegory, parameters);
}
private Item lookupItem(String asin, String cathegory) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("ItemId", asin);
parameters.put("ReviewSort", "-HelpfulVotes");
parameters.put("ResponseGroup", "EditorialReview,Images,ItemAttributes,Reviews,SalesRank,OfferFull");
return sendSOAPMessage("ItemLookup", cathegory, parameters);
}
/**
* Sends multiple SOAP request messages to Amazon.
*/
private ArrayList<Item> sendSOAPMessages(String operation, String cathegory, HashMap<String, String> parameters) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
int numPages, i = 0;
ArrayList content = new ArrayList();
Document doc = null;
DOMItemHandler dih = null;
SOAPMessage response = null;
SOAPMessage message = SOAPEnvelopeCreator.createAmazonSearchSOAPEnvelope(operation, parameters, properties);
try
{
response = connection.call(message, properties.getProperty("endpoint"));
dih = DOMItemHandlerFactory.newInstance().newDOMContentHandler(cathegory);
doc = response.getSOAPBody().extractContentAsDocument();
numPages = dih.getNumberOfPages(doc);
content = dih.getContent(doc);
for(i = 2; i <= numPages; i++)
{
parameters.put("ItemPage", new Integer(i).toString());
message = SOAPEnvelopeCreator.createAmazonSearchSOAPEnvelope(operation, parameters, properties);
response = connection.call(message, properties.getProperty("endpoint"));
content.addAll(dih.getContent(response.getSOAPBody().extractContentAsDocument()));
}
}
catch(SOAPException se)
{
throw new SOAPConnectionException();
}
return content;
}
/**
* Sends a SOAP request message to Amazon
*/
private Item sendSOAPMessage(String operation, String cathegory, HashMap<String, String> parameters) throws SOAPEnvelopeCreationException, SOAPConnectionException
{
int numPages, i = 0;
Item item = new Item();
Document doc = null;
DOMItemHandler dih = null;
SOAPMessage response = null;
SOAPMessage message = SOAPEnvelopeCreator.createAmazonSearchSOAPEnvelope(operation, parameters, properties);
try
{
response = connection.call(message, properties.getProperty("endpoint"));
dih = DOMItemHandlerFactory.newInstance().newDOMContentHandler(cathegory);
doc = response.getSOAPBody().extractContentAsDocument();
item = dih.getItemContent(doc);
}
catch(SOAPException se)
{
throw new SOAPConnectionException();
}
return item;
}
}