001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.http;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.HttpURLConnection;
025: import java.net.MalformedURLException;
026: import java.net.ProtocolException;
027: import java.net.URL;
028:
029: import javax.xml.parsers.ParserConfigurationException;
030: import javax.xml.transform.TransformerException;
031:
032: import org.w3c.dom.Document;
033:
034: import org.xml.sax.SAXException;
035:
036: import org.apache.cxf.Bus;
037: import org.apache.cxf.BusException;
038: import org.apache.cxf.bus.spring.SpringBusFactory;
039: import org.apache.cxf.helpers.DOMUtils;
040: import org.apache.cxf.helpers.IOUtils;
041: import org.apache.cxf.test.AbstractCXFTest;
042:
043: public abstract class AbstractRestTest extends AbstractCXFTest {
044: boolean debug;
045:
046: protected Bus createBus() throws BusException {
047: return new SpringBusFactory().createBus();
048: }
049:
050: protected Document get(String urlStr) throws MalformedURLException,
051: IOException, SAXException, ParserConfigurationException {
052: return get(urlStr, null);
053: }
054:
055: protected Document get(String urlStr, Integer resCode)
056: throws MalformedURLException, IOException, SAXException,
057: ParserConfigurationException {
058: URL url = new URL(urlStr);
059: HttpURLConnection c = (HttpURLConnection) url.openConnection();
060:
061: if (resCode != null) {
062: assertEquals(resCode.intValue(), c.getResponseCode());
063: }
064: InputStream is;
065: if (c.getResponseCode() >= 400) {
066: is = c.getErrorStream();
067: } else {
068: is = c.getInputStream();
069: }
070: return DOMUtils.readXml(is);
071: }
072:
073: protected Document post(String urlStr, String message)
074: throws MalformedURLException, IOException, SAXException,
075: ParserConfigurationException {
076: return doMethod(urlStr, message, "POST");
077: }
078:
079: protected Document put(String urlStr, String message)
080: throws MalformedURLException, IOException, SAXException,
081: ParserConfigurationException {
082: return doMethod(urlStr, message, "PUT");
083: }
084:
085: protected Document doMethod(String urlStr, String message,
086: String method) throws MalformedURLException, IOException,
087: SAXException, ParserConfigurationException {
088: return doMethod(urlStr, message, method, "application/xml");
089: }
090:
091: protected Document doMethod(String urlStr, String message,
092: String method, String ct) throws MalformedURLException,
093: IOException, SAXException, ParserConfigurationException {
094:
095: InputStream is = invoke(urlStr, message, method, ct);
096: Document res = DOMUtils.readXml(is);
097:
098: if (debug) {
099: try {
100: DOMUtils.writeXml(res, System.out);
101: } catch (TransformerException e) {
102: throw new RuntimeException(e);
103: }
104: }
105: return res;
106: }
107:
108: protected byte[] doMethodBytes(String urlStr, String message,
109: String method, String ct) throws MalformedURLException,
110: IOException, SAXException, ParserConfigurationException {
111:
112: InputStream is = invoke(urlStr, message, method, ct);
113:
114: ByteArrayOutputStream out = new ByteArrayOutputStream();
115: IOUtils.copy(is, out);
116: out.close();
117: is.close();
118:
119: return out.toByteArray();
120: }
121:
122: private InputStream invoke(String urlStr, String message,
123: String method, String ct) throws MalformedURLException,
124: IOException, ProtocolException {
125: URL url = new URL(urlStr);
126: HttpURLConnection c = (HttpURLConnection) url.openConnection();
127: c.setRequestMethod(method);
128:
129: if (ct != null) {
130: c.setRequestProperty("Content-Type", ct);
131: }
132:
133: if (message != null) {
134: c.setDoOutput(true);
135: OutputStream out = c.getOutputStream();
136: InputStream msgIs = getResourceAsStream(message);
137: assertNotNull(msgIs);
138:
139: IOUtils.copy(msgIs, out);
140: out.close();
141: msgIs.close();
142: }
143:
144: return c.getInputStream();
145: }
146: }
|