1 17 package org.apache.servicemix.components.http; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStream ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 27 import javax.xml.namespace.QName ; 28 29 import org.apache.servicemix.tck.TestSupport; 30 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 31 import org.springframework.context.support.AbstractXmlApplicationContext; 32 import org.w3c.dom.Node ; 33 34 37 public class HttpTest extends TestSupport { 38 39 protected String quote = "SUNW"; 40 41 protected void setUp() throws Exception { 42 System.setProperty("javax.net.debug", "all"); 43 50 super.setUp(); 51 } 52 53 String getResourceFilePath(String resource) throws Exception { 54 URL url = getClass().getResource(resource); 55 File f = new File (url.toURI()); 56 return f.toString(); 57 } 58 59 public void testCurrencyQuotes() throws Exception { 60 QName serviceName = new QName ("http://servicemix.org/cheese/", "httpSender"); 61 String file = "request.xml"; 62 63 Object answer = requestServiceWithFileRequest(serviceName, file); 64 assertTrue("Shoud return a DOM Node: " + answer, answer instanceof Node ); 65 Node node = (Node ) answer; 66 System.out.println(transformer.toString(node)); 67 68 String text = textValueOfXPath(node, "//Result").trim(); 69 70 System.out.println("Found price: " + text); 71 72 assertTrue("price text should not be empty", text.length() > 0); 73 74 } 75 76 public void testWithURLConnection() throws Exception { 77 URLConnection connection = new URL ("http://localhost:8912").openConnection(); 78 connection.setDoOutput(true); 79 connection.setDoInput(true); 80 OutputStream os = connection.getOutputStream(); 81 82 InputStream fis = getClass().getResourceAsStream("request.xml"); 84 int c; 85 while ((c = fis.read()) >= 0) { 86 os.write(c); 87 } 88 os.flush(); 89 os.close(); 90 fis.close(); 91 92 BufferedReader in = new BufferedReader (new InputStreamReader (connection.getInputStream())); 94 String inputLine; 95 while ((inputLine = in.readLine()) != null) { 96 System.out.println(inputLine); 97 } 98 in.close(); 99 } 100 101 protected AbstractXmlApplicationContext createBeanFactory() { 102 return new ClassPathXmlApplicationContext("org/apache/servicemix/components/http/example.xml"); 103 } 104 } 105 | Popular Tags |