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.bare;
019:
020: import java.net.HttpURLConnection;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.w3c.dom.Document;
026:
027: import org.apache.cxf.binding.BindingFactoryManager;
028: import org.apache.cxf.binding.http.AbstractRestTest;
029: import org.apache.cxf.binding.http.HttpBindingFactory;
030: import org.apache.cxf.binding.http.URIMapper;
031: import org.apache.cxf.customer.bare.CustomerService;
032: import org.apache.cxf.endpoint.ServerImpl;
033: import org.apache.cxf.helpers.DOMUtils;
034: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.junit.Test;
037:
038: public class BareServiceTest extends AbstractRestTest {
039:
040: @Test
041: public void testCreation() throws Exception {
042: BindingFactoryManager bfm = getBus().getExtension(
043: BindingFactoryManager.class);
044: HttpBindingFactory factory = new HttpBindingFactory();
045: factory.setBus(getBus());
046: bfm.registerBindingFactory(HttpBindingFactory.HTTP_BINDING_ID,
047: factory);
048:
049: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
050: sf.setBus(getBus());
051: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
052: sf.setServiceClass(CustomerService.class);
053: sf.getServiceFactory().setWrapped(false);
054: sf.setAddress("http://localhost:9001/foo/");
055: sf.setServiceBean(new CustomerService());
056:
057: Map<String, Object> props = new HashMap<String, Object>();
058: props.put("contextMatchStrategy", "stem");
059: sf.setProperties(props);
060:
061: ServerImpl svr = (ServerImpl) sf.create();
062:
063: URIMapper mapper = (URIMapper) svr.getEndpoint().getService()
064: .get(URIMapper.class.getName());
065: assertNotNull(mapper);
066:
067: BindingOperationInfo bop = mapper.getOperation("/customers",
068: "GET", null);
069: assertNotNull(bop);
070: assertEquals("getCustomers", bop.getName().getLocalPart());
071:
072: bop = mapper.getOperation("/customers", "POST", null);
073: assertNotNull(bop);
074: assertEquals("addCustomer", bop.getName().getLocalPart());
075:
076: bop = mapper.getOperation("/customers/123", "GET", null);
077: assertNotNull(bop);
078: assertEquals("getCustomer", bop.getName().getLocalPart());
079:
080: bop = mapper.getOperation("/customers/123", "PUT", null);
081: assertNotNull(bop);
082: assertEquals("updateCustomer", bop.getName().getLocalPart());
083:
084: bop = mapper
085: .getOperation("/customers/details/123", "GET", null);
086: assertNotNull(bop);
087: assertEquals("getSomeDetails", bop.getName().getLocalPart());
088:
089: // TEST POST/GETs
090:
091: Document res = get("http://localhost:9001/foo/customers");
092: assertNotNull(res);
093:
094: addNamespace("c", "http://cxf.apache.org/jra");
095: assertValid("/c:customers", res);
096: assertValid("/c:customers/c:customer/c:id[text()='123']", res);
097: assertValid(
098: "/c:customers/c:customer/c:name[text()='Dan Diephouse']",
099: res);
100:
101: res = get("http://localhost:9001/foo/customers/123");
102: assertNotNull(res);
103:
104: assertValid("/c:customer", res);
105: assertValid("/c:customer/c:id[text()='123']", res);
106: assertValid("/c:customer/c:name[text()='Dan Diephouse']", res);
107:
108: // Try invalid customer
109: res = get("http://localhost:9001/foo/customers/0", 500);
110: assertNotNull(res);
111:
112: assertValid("//c:CustomerNotFoundDetails", res);
113:
114: res = put("http://localhost:9001/foo/customers/123",
115: "update.xml");
116: assertNotNull(res);
117:
118: assertValid("/c:updateCustomer", res);
119:
120: res = post("http://localhost:9001/foo/customers", "add.xml");
121: assertNotNull(res);
122:
123: assertValid("/c:addCustomer", res);
124:
125: // Get the updated document
126: res = get("http://localhost:9001/foo/customers/123");
127: assertNotNull(res);
128:
129: assertValid("/c:customer", res);
130: assertValid("/c:customer/c:id[text()='123']", res);
131: assertValid("/c:customer/c:name[text()='Danno Manno']", res);
132:
133: svr.stop();
134: }
135:
136: @Test
137: public void testSetContentType() throws Exception {
138: BindingFactoryManager bfm = getBus().getExtension(
139: BindingFactoryManager.class);
140: HttpBindingFactory factory = new HttpBindingFactory();
141: factory.setBus(getBus());
142: bfm.registerBindingFactory(HttpBindingFactory.HTTP_BINDING_ID,
143: factory);
144:
145: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
146: sf.setBus(getBus());
147: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
148: sf.setServiceClass(CustomerService.class);
149: sf.getServiceFactory().setWrapped(false);
150: sf.setAddress("http://localhost:9001/foo/");
151: sf.setServiceBean(new CustomerService());
152:
153: Map<String, Object> props = new HashMap<String, Object>();
154: props.put("Content-Type", "text/plain");
155: sf.setProperties(props);
156:
157: ServerImpl svr = (ServerImpl) sf.create();
158:
159: URL url = new URL("http://localhost:9001/foo/customers/123");
160: HttpURLConnection c = (HttpURLConnection) url.openConnection();
161: c.setRequestMethod("GET");
162:
163: assertEquals("text/plain", c.getContentType());
164:
165: c.disconnect();
166:
167: url = new URL("http://localhost:9001/foo/customers/bleh");
168: c = (HttpURLConnection) url.openConnection();
169: c.setRequestMethod("GET");
170:
171: String ct = c.getContentType();
172: assertTrue(ct.startsWith("text/plain"));
173:
174: svr.stop();
175: }
176:
177: @Test
178: public void testGetOnBadUnwrappedParam() throws Exception {
179: BindingFactoryManager bfm = getBus().getExtension(
180: BindingFactoryManager.class);
181: HttpBindingFactory factory = new HttpBindingFactory();
182: factory.setBus(getBus());
183: bfm.registerBindingFactory(HttpBindingFactory.HTTP_BINDING_ID,
184: factory);
185:
186: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
187: sf.setBus(getBus());
188: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
189: sf.setServiceBean(new TestService());
190: sf.getServiceFactory().setWrapped(false);
191: sf.setAddress("http://localhost:9001/test/");
192:
193: ServerImpl svr = (ServerImpl) sf.create();
194:
195: URL url = new URL("http://localhost:9001/test/topics/123");
196: HttpURLConnection c = (HttpURLConnection) url.openConnection();
197: c.setRequestMethod("GET");
198:
199: assertEquals("text/xml; charset=utf-8", c.getContentType());
200:
201: Document doc = DOMUtils.readXml(c.getErrorStream());
202: assertValid("//*[text()='You can not map a URI parameter to a "
203: + "simple type when in unwrapped mode!']", doc);
204:
205: svr.stop();
206: }
207:
208: @Test
209: public void testQueryParam() throws Exception {
210: BindingFactoryManager bfm = getBus().getExtension(
211: BindingFactoryManager.class);
212: HttpBindingFactory factory = new HttpBindingFactory();
213: factory.setBus(getBus());
214: bfm.registerBindingFactory(HttpBindingFactory.HTTP_BINDING_ID,
215: factory);
216:
217: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
218: sf.setBus(getBus());
219: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
220: sf.setServiceBean(new TestService());
221: sf.getServiceFactory().setWrapped(false);
222: sf.setAddress("http://localhost:9001/test/");
223:
224: ServerImpl svr = (ServerImpl) sf.create();
225:
226: URL url = new URL("http://localhost:9001/test/rest/foo");
227: HttpURLConnection c = (HttpURLConnection) url.openConnection();
228: c.setRequestMethod("GET");
229:
230: assertEquals("text/xml; charset=utf-8", c.getContentType());
231:
232: Document doc = DOMUtils.readXml(c.getInputStream());
233: addNamespace("b", "http://bare.http.binding.cxf.apache.org/");
234: assertValid("//b:getDataResponse", doc);
235:
236: svr.stop();
237: }
238: }
|