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.object;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.cxf.BusException;
025: import org.apache.cxf.binding.BindingFactoryManager;
026: import org.apache.cxf.endpoint.Server;
027: import org.apache.cxf.frontend.ServerFactoryBean;
028: import org.apache.cxf.helpers.CastUtils;
029: import org.apache.cxf.message.ExchangeImpl;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.message.MessageImpl;
032: import org.apache.cxf.service.model.BindingInfo;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.apache.cxf.service.model.EndpointInfo;
035: import org.apache.cxf.service.model.ServiceInfo;
036: import org.apache.cxf.test.AbstractCXFTest;
037: import org.apache.cxf.transport.Conduit;
038: import org.apache.cxf.transport.ConduitInitiator;
039: import org.apache.cxf.transport.ConduitInitiatorManager;
040: import org.apache.cxf.transport.MessageObserver;
041: import org.apache.cxf.transport.local.LocalConduit;
042: import org.apache.cxf.transport.local.LocalTransportFactory;
043: import org.junit.Test;
044:
045: public class LocalServerRegistrationTest extends AbstractCXFTest {
046: private Message response;
047:
048: @Test
049: public void testServer() throws Exception {
050: // Enable the auto registration of a default local endpoint when we use other transports
051: BindingFactoryManager bfm = getBus().getExtension(
052: BindingFactoryManager.class);
053: ObjectBindingFactory obj = (ObjectBindingFactory) bfm
054: .getBindingFactory(ObjectBindingFactory.BINDING_ID);
055: obj.setAutoRegisterLocalEndpoint(true);
056:
057: // Create an HTTP endpoint
058: ServerFactoryBean sfb = new ServerFactoryBean();
059: sfb.setServiceClass(EchoImpl.class);
060: sfb.setAddress("http://localhost:9001/echo");
061: Server server = sfb.create();
062:
063: List<Object> content = new ArrayList<Object>();
064: content.add("Hello");
065:
066: ServiceInfo serviceInfo = server.getEndpoint()
067: .getEndpointInfo().getService();
068: BindingInfo bi = serviceInfo.getBindings().iterator().next();
069: BindingOperationInfo bop = bi.getOperations().iterator().next();
070:
071: assertNotNull(bop.getOperationInfo());
072:
073: MessageImpl m = new MessageImpl();
074: m.setContent(List.class, content);
075: m.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
076: m.put(ObjectBinding.BINDING, bop.getBinding().getName());
077: m.put(ObjectBinding.OPERATION, bop.getName());
078:
079: ExchangeImpl ex = new ExchangeImpl();
080: ex.setInMessage(m);
081:
082: Conduit c = getLocalConduit("local://" + server);
083: ex.setConduit(c);
084:
085: c.setMessageObserver(new MessageObserver() {
086: public void onMessage(Message message) {
087: response = message;
088: }
089: });
090: c.prepare(m);
091: c.close(m);
092:
093: Thread.sleep(1000);
094: assertNotNull(response);
095:
096: List<?> content2 = CastUtils.cast((List<?>) response
097: .getContent(List.class));
098: assertNotNull(content2);
099: assertEquals(1, content2.size());
100:
101: }
102:
103: private Conduit getLocalConduit(String string) throws BusException,
104: IOException {
105: ConduitInitiatorManager cim = getBus().getExtension(
106: ConduitInitiatorManager.class);
107:
108: ConduitInitiator ci = cim
109: .getConduitInitiator(LocalTransportFactory.TRANSPORT_ID);
110: EndpointInfo ei = new EndpointInfo();
111: ei.setAddress(string);
112: return ci.getConduit(ei);
113: }
114: }
|