001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.OutputStream;
024: import java.net.URL;
025: import java.net.URLConnection;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.w3c.dom.Node;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.servicemix.tck.TestSupport;
034: import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
035: import org.springframework.context.support.AbstractXmlApplicationContext;
036:
037: /**
038: * @version $Revision: 556864 $
039: */
040: public class HttpTest extends TestSupport {
041: private static transient Log log = LogFactory
042: .getLog(HttpTest.class);
043:
044: protected String quote = "SUNW";
045:
046: protected void setUp() throws Exception {
047: System.setProperty("javax.net.debug", "all");
048: // The following properties will be used by default if not specified in the xml conf
049: /*
050: System.setProperty("javax.net.ssl.trustStore", getResourceFilePath("client.keystore"));
051: System.setProperty("javax.net.ssl.trustStorePassword", "password");
052: System.setProperty("javax.net.ssl.keyStore", getResourceFilePath("server.keystore"));
053: System.setProperty("javax.net.ssl.keyStorePassword", "password");
054: */
055: super .setUp();
056: }
057:
058: String getResourceFilePath(String resource) throws Exception {
059: URL url = getClass().getResource(resource);
060: File f = new File(url.toURI());
061: return f.toString();
062: }
063:
064: public void testCurrencyQuotes() throws Exception {
065: QName serviceName = new QName("http://servicemix.org/cheese/",
066: "httpSender");
067: String file = "request.xml";
068:
069: Object answer = requestServiceWithFileRequest(serviceName, file);
070: assertTrue("Shoud return a DOM Node: " + answer,
071: answer instanceof Node);
072: Node node = (Node) answer;
073: log.info(transformer.toString(node));
074:
075: String text = textValueOfXPath(node, "//Result").trim();
076:
077: log.info("Found price: " + text);
078:
079: assertTrue("price text should not be empty", text.length() > 0);
080:
081: }
082:
083: public void testWithURLConnection() throws Exception {
084: URLConnection connection = new URL("http://localhost:8912")
085: .openConnection();
086: connection.setDoOutput(true);
087: connection.setDoInput(true);
088: OutputStream os = connection.getOutputStream();
089:
090: // Post the request file.
091: InputStream fis = getClass().getResourceAsStream("request.xml");
092: int c;
093: while ((c = fis.read()) >= 0) {
094: os.write(c);
095: }
096: os.flush();
097: os.close();
098: fis.close();
099:
100: // Read the response.
101: BufferedReader in = new BufferedReader(new InputStreamReader(
102: connection.getInputStream()));
103: String inputLine;
104: while ((inputLine = in.readLine()) != null) {
105: log.info(inputLine);
106: }
107: in.close();
108: }
109:
110: protected AbstractXmlApplicationContext createBeanFactory() {
111: return new ClassPathXmlApplicationContext(
112: "org/apache/servicemix/components/http/example.xml");
113: }
114: }
|