[Carrot2-cvs] carrot2/components/carrot2-shared-lib/src/com/dawidweiss/carrot/tools QueryFilterCompo
Brought to you by:
dawidweiss,
stachoo
From: <daw...@us...> - 2003-10-08 12:31:03
|
Update of /cvsroot/carrot2/carrot2/components/carrot2-shared-lib/src/com/dawidweiss/carrot/tools In directory sc8-pr-cvs1:/tmp/cvs-serv15661/src/com/dawidweiss/carrot/tools Modified Files: QueryFilterComponent.java Log Message: A couple of unused variables removed, QueryFilterComponent refined to support input parameters. Index: QueryFilterComponent.java =================================================================== RCS file: /cvsroot/carrot2/carrot2/components/carrot2-shared-lib/src/com/dawidweiss/carrot/tools/QueryFilterComponent.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** QueryFilterComponent.java 19 Sep 2003 10:14:54 -0000 1.1.1.1 --- QueryFilterComponent.java 8 Oct 2003 12:30:53 -0000 1.2 *************** *** 20,30 **** import org.put.util.net.http.HTTPFormSubmitter; import org.put.util.net.http.Parameter; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; - import java.io.InputStreamReader; - import java.io.Reader; - import java.net.MalformedURLException; import java.net.URL; --- 20,32 ---- import org.put.util.net.http.HTTPFormSubmitter; import org.put.util.net.http.Parameter; + + import java.io.*; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; + import java.util.*; + import java.util.Iterator; + import java.util.Map; *************** *** 41,44 **** --- 43,47 ---- * @param carrotRequestStream The data stream to be sent in a POST request as * "carrot-xchange-data" parameter. + * @param params Configuration params for the filter component. * * @return An InputStream to what the component returned. *************** *** 46,54 **** * @throws IOException If an error occurrs. */ ! public InputStream queryInputComponent(URL componentServiceURL, Reader carrotRequest) throws IOException { FormActionInfo actionInfo = new FormActionInfo(componentServiceURL, "post"); FormParameters queryArgs = new FormParameters(); HTTPFormSubmitter submitter = new HTTPFormSubmitter(actionInfo); --- 49,64 ---- * @throws IOException If an error occurrs. */ ! public InputStream queryInputComponent(URL componentServiceURL, Map params, InputStream carrotRequest) throws IOException { FormActionInfo actionInfo = new FormActionInfo(componentServiceURL, "post"); FormParameters queryArgs = new FormParameters(); + + for (Iterator i = params.keySet().iterator();i.hasNext();) + { + String key = (String) i.next(); + queryArgs.addParameter(new Parameter(key, params.get(key), false)); + } + HTTPFormSubmitter submitter = new HTTPFormSubmitter(actionInfo); *************** *** 67,102 **** { InputStream requestXML = null; ! URL service; try { ! service = new URL(args[0]); ! ! if (args.length > 1) ! { ! try ! { ! URL resourceURL = new URL(args[1]); ! requestXML = resourceURL.openStream(); } ! catch (MalformedURLException e) ! { ! // attempt to open a file ! requestXML = new FileInputStream(args[1]); } } ! else ! { ! requestXML = System.in; } } catch (Exception e) { ! System.err.println("An exception occurred: " + e.toString()); ! System.err.println( ! "Usage: QueryFilterComponent [serviceURL] {request.xml (URL/ or a file)}" ! ); ! System.err.println("[] - required, {} - optional."); ! return; } --- 77,148 ---- { InputStream requestXML = null; ! URL service = null; ! Map params = new HashMap(); + if (args.length == 0) { + help(); + return; + } + + int i = 0; try { ! while (i<args.length) { ! if ("-service".equalsIgnoreCase(args[i])) { ! if (service != null) { ! System.err.println("Service URL can be defined only once."); ! return; ! } ! service = new URL(args[i+1]); ! i += 2; } ! else ! if ("-param".equalsIgnoreCase(args[i])) { ! if (params.containsKey(args[i+1])) { ! System.err.println("Param value already defined for key: " + args[i+1]); ! return; ! } ! params.put(args[i+1], args[i+2]); ! i+=3; ! } ! else ! if ("-help".equalsIgnoreCase(args[i])) { ! help(); ! return; ! } ! else { ! if (requestXML != null) { ! System.err.println("Input stream already defined, cannot accept option: " ! + args[i]); ! return; ! } ! File inputStream = new File(args[i]); ! if (!inputStream.exists() || !inputStream.canRead() || !inputStream.isFile()) { ! System.err.println("Something wrong with the input stream: " ! + inputStream.getAbsolutePath()); ! return; ! } ! System.err.println("Reading input from: " ! + args[i]); ! requestXML = new BufferedInputStream( new FileInputStream( inputStream)); ! i++; } } ! ! if (service == null) { ! System.err.println("No service URL."); ! return; } + if (requestXML == null) + requestXML = System.in; + } + catch (ArrayIndexOutOfBoundsException e) { + System.err.println("Missing argument for option: " + + args[i]); + return; } catch (Exception e) { ! System.err.println("An exception occurred (use '-help' for help): " + e.toString()); return; } *************** *** 106,120 **** try { ! InputStream result = me.queryInputComponent( ! service, new InputStreamReader(requestXML, "UTF-8") ! ); if (result == null) { ! System.err.println("Error in response (null returned from the submitter)"); } byte [] output = org.put.util.io.FileHelper.readFullyAndCloseInput(result); System.out.write(output); System.err.println("Bytes of response: " + output.length); } --- 152,165 ---- try { ! InputStream result = me.queryInputComponent( service, params, requestXML ); if (result == null) { ! System.err.println("Error in response (null returned from the HTTP POST submitter)"); } byte [] output = org.put.util.io.FileHelper.readFullyAndCloseInput(result); System.out.write(output); + System.out.flush(); System.err.println("Bytes of response: " + output.length); } *************** *** 123,126 **** --- 168,190 ---- System.err.println("An exception occurred when querying: " + e.toString()); } + finally { + if (requestXML != System.in) { + try + { + requestXML.close(); + } + catch (IOException e1) + { + } + } + } + } + + + public static void help() { + System.err.println( + "Usage: QueryFilterComponent [-service url] [-param name value] {request file XML}" + ); + System.err.println("[] - required, {} - optional (if not present, stdio is read)"); } } |