|
Apache CXF example source code file (JettyHTTPDestinationTest.java)
The Apache CXF JettyHTTPDestinationTest.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.transport.http_jetty; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import org.apache.cxf.Bus; import org.apache.cxf.BusException; import org.apache.cxf.BusFactory; import org.apache.cxf.bus.CXFBusImpl; import org.apache.cxf.common.util.Base64Utility; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.configuration.security.AuthorizationPolicy; import org.apache.cxf.continuations.SuspendedInvocationException; import org.apache.cxf.endpoint.EndpointResolverRegistry; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.io.AbstractWrappedOutputStream; import org.apache.cxf.message.ExchangeImpl; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageImpl; import org.apache.cxf.security.transport.TLSSessionInfo; import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.service.model.ServiceInfo; import org.apache.cxf.transport.Conduit; import org.apache.cxf.transport.ConduitInitiator; import org.apache.cxf.transport.ConduitInitiatorManager; import org.apache.cxf.transport.Destination; import org.apache.cxf.transport.MessageObserver; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transports.http.QueryHandler; import org.apache.cxf.transports.http.QueryHandlerRegistry; import org.apache.cxf.transports.http.StemMatchingQueryHandler; import org.apache.cxf.transports.http.configuration.HTTPServerPolicy; import org.apache.cxf.ws.addressing.AddressingProperties; import org.apache.cxf.ws.addressing.EndpointReferenceType; import org.apache.cxf.ws.addressing.JAXWSAConstants; import org.apache.cxf.ws.policy.PolicyEngine; import org.apache.cxf.wsdl.EndpointReferenceUtils; import org.easymock.classextension.EasyMock; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.junit.After; import org.junit.Assert; import org.junit.Test; public class JettyHTTPDestinationTest extends Assert { protected static final String AUTH_HEADER = "Authorization"; protected static final String USER = "copernicus"; protected static final String PASSWD = "epicycles"; protected static final String BASIC_AUTH = "Basic " + Base64Utility.encode((USER + ":" + PASSWD).getBytes()); private static final String NOWHERE = "http://nada.nothing.nowhere.null/"; private static final String PAYLOAD = "message payload"; private static final String CHALLENGE_HEADER = "WWW-Authenticate"; private static final String BASIC_CHALLENGE = "Basic realm=terra"; private static final String DIGEST_CHALLENGE = "Digest realm=luna"; private static final String CUSTOM_CHALLENGE = "Custom realm=sol"; private Bus bus; private Bus threadDefaultBus; private Conduit decoupledBackChannel; private EndpointInfo endpointInfo; private EndpointReferenceType address; private EndpointReferenceType replyTo; private JettyHTTPServerEngine engine; private HTTPServerPolicy policy; private JettyHTTPDestination destination; private Request request; private Response response; private Message inMessage; private Message outMessage; private MessageObserver observer; private ServletInputStream is; private ServletOutputStream os; private QueryHandler wsdlQueryHandler; private QueryHandlerRegistry queryHandlerRegistry; private List<QueryHandler> queryHandlerList; private JettyHTTPTransportFactory transportFactory; /** * This class replaces the engine in the Jetty Destination. */ private class EasyMockJettyHTTPDestination extends JettyHTTPDestination { public EasyMockJettyHTTPDestination( Bus b, JettyHTTPTransportFactory ci, EndpointInfo endpointInfo, JettyHTTPServerEngine easyMockEngine ) throws IOException { super(b, ci, endpointInfo); engine = easyMockEngine; } @Override public void retrieveEngine() { // Leave engine alone. } } @After public void tearDown() { bus = null; transportFactory = null; decoupledBackChannel = null; address = null; replyTo = null; engine = null; request = null; response = null; inMessage = null; outMessage = null; is = null; os = null; destination = null; BusFactory.setDefaultBus(null); } @Test public void testGetAddress() throws Exception { destination = setUpDestination(); EndpointReferenceType ref = destination.getAddress(); assertNotNull("unexpected null address", ref); assertEquals("unexpected address", EndpointReferenceUtils.getAddress(ref), StringUtils.addDefaultPortIfMissing(EndpointReferenceUtils.getAddress(address))); assertEquals("unexpected service name local part", EndpointReferenceUtils.getServiceName(ref, bus).getLocalPart(), "Service"); assertEquals("unexpected portName", EndpointReferenceUtils.getPortName(ref), "Port"); } @Test public void testRandomPortAllocation() throws Exception { transportFactory = new JettyHTTPTransportFactory(); transportFactory.setBus(new CXFBusImpl()); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setName(new QName("bla", "Service")); EndpointInfo ei = new EndpointInfo(serviceInfo, ""); ei.setName(new QName("bla", "Port")); Destination d1 = transportFactory.getDestination(ei); URL url = new URL(d1.getAddress().getAddress().getValue()); assertTrue("No random port has been allocated", url.getPort() > 0); } @Test public void testSuspendedException() throws Exception { destination = setUpDestination(false, false); setUpDoService(false); final RuntimeException ex = new RuntimeException(); observer = new MessageObserver() { public void onMessage(Message m) { throw new SuspendedInvocationException(ex); } }; destination.setMessageObserver(observer); try { destination.doService(request, response); fail("Suspended invocation swallowed"); } catch (RuntimeException runtimeEx) { assertSame("Original exception is not preserved", ex, runtimeEx); } } @Test public void testContinuationsIgnored() throws Exception { HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setName(new QName("bla", "Service")); EndpointInfo ei = new EndpointInfo(serviceInfo, ""); ei.setName(new QName("bla", "Port")); final JettyHTTPServerEngine httpEngine = new JettyHTTPServerEngine(); httpEngine.setContinuationsEnabled(false); JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory() { @Override public JettyHTTPServerEngine retrieveJettyHTTPServerEngine(int port) { return httpEngine; } }; transportFactory = new JettyHTTPTransportFactory(); transportFactory.setBus(new CXFBusImpl()); transportFactory.getBus().setExtension( factory, JettyHTTPServerEngineFactory.class); TestJettyDestination testDestination = new TestJettyDestination(transportFactory.getBus(), transportFactory, ei); testDestination.finalizeConfig(); Message mi = testDestination.retrieveFromContinuation(httpRequest); assertNull("Continuations must be ignored", mi); } @Test public void testGetMultiple() throws Exception { transportFactory = new JettyHTTPTransportFactory(); transportFactory.setBus(new CXFBusImpl()); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setName(new QName("bla", "Service")); EndpointInfo ei = new EndpointInfo(serviceInfo, ""); ei.setName(new QName("bla", "Port")); ei.setAddress("http://foo"); Destination d1 = transportFactory.getDestination(ei); Destination d2 = transportFactory.getDestination(ei); assertEquals(d1, d2); d2.shutdown(); Destination d3 = transportFactory.getDestination(ei); assertNotSame(d1, d3); } @Test public void testRemoveServant() throws Exception { destination = setUpDestination(); setUpRemoveServant(); destination.setMessageObserver(null); } @Test public void testDoServiceRedirectURL() throws Exception { destination = setUpDestination(false, false); setUpDoService(true); destination.doService(request, response); } @Test public void testDoService() throws Exception { Bus defaultBus = new CXFBusImpl(); assertSame("Default thread bus has not been set", defaultBus, BusFactory.getThreadDefaultBus()); destination = setUpDestination(false, false); setUpDoService(false); assertSame("Default thread bus has been unexpectedly reset", defaultBus, BusFactory.getThreadDefaultBus()); destination.doService(request, response); verifyDoService(); assertSame("Default thread bus has not been reset", defaultBus, BusFactory.getThreadDefaultBus()); } @Test public void testDoServiceWithHttpGET() throws Exception { destination = setUpDestination(false, false); setUpDoService(false, false, false, "GET", "?customerId=abc&cutomerAdd=def", 200); destination.doService(request, response); assertNotNull("unexpected null message", inMessage); assertEquals("unexpected method", inMessage.get(Message.HTTP_REQUEST_METHOD), "GET"); assertEquals("unexpected path", inMessage.get(Message.PATH_INFO), "/bar/foo"); assertEquals("unexpected query", inMessage.get(Message.QUERY_STRING), "?customerId=abc&cutomerAdd=def"); } @Test public void testDoServiceWithHttpGETandStemMatchingQueryWSDL() throws Exception { destination = setUpDestination(false, true); setUpQueryHandler(true); setUpDoService(false, false, false, "GET", "?wsdl", 200); destination.doService(request, response); assertNotNull("unexpected null response", response); } @Test public void testDoServiceWithHttpGETandNonStemMatchingQueryWSDL() throws Exception { destination = setUpDestination(false, true); setUpQueryHandler(false); setUpDoService(false, false, false, "GET", "?wsdl", 200); destination.doService(request, response); assertNotNull("unexpected null response", response); } @Test public void testGetAnonBackChannel() throws Exception { destination = setUpDestination(false, false); setUpDoService(false); destination.doService(request, response); setUpInMessage(); Conduit backChannel = destination.getBackChannel(inMessage, null, null); assertNotNull("expected back channel", backChannel); assertEquals("unexpected target", EndpointReferenceUtils.ANONYMOUS_ADDRESS, backChannel.getTarget().getAddress().getValue()); } @Test public void testGetBackChannelSend() throws Exception { destination = setUpDestination(false, false); setUpDoService(false, true); destination.doService(request, response); setUpInMessage(); Conduit backChannel = destination.getBackChannel(inMessage, null, null); outMessage = setUpOutMessage(); backChannel.prepare(outMessage); verifyBackChannelSend(backChannel, outMessage, 200); } @Test public void testGetBackChannelSendFault() throws Exception { destination = setUpDestination(false, false); setUpDoService(false, true, 500); destination.doService(request, response); setUpInMessage(); Conduit backChannel = destination.getBackChannel(inMessage, null, null); outMessage = setUpOutMessage(); backChannel.prepare(outMessage); verifyBackChannelSend(backChannel, outMessage, 500); } @Test public void testGetBackChannelSendOneway() throws Exception { destination = setUpDestination(false, false); setUpDoService(false, true, 500); destination.doService(request, response); setUpInMessage(); Conduit backChannel = destination.getBackChannel(inMessage, null, null); outMessage = setUpOutMessage(); backChannel.prepare(outMessage); verifyBackChannelSend(backChannel, outMessage, 500, true); } @Test public void testGetBackChannelSendDecoupled() throws Exception { destination = setUpDestination(false, false); replyTo = getEPR(NOWHERE + "response/foo"); setUpDoService(false, true, true, 202); destination.doService(request, response); setUpInMessage(); Message partialResponse = setUpOutMessage(); partialResponse.put(Message.PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE); Conduit partialBackChannel = destination.getBackChannel(inMessage, partialResponse, replyTo); partialBackChannel.prepare(partialResponse); verifyBackChannelSend(partialBackChannel, partialResponse, 202); outMessage = setUpOutMessage(); Conduit fullBackChannel = destination.getBackChannel(inMessage, null, replyTo); fullBackChannel.prepare(outMessage); } @Test public void testServerPolicyInServiceModel() throws Exception { policy = new HTTPServerPolicy(); address = getEPR("bar/foo"); bus = new CXFBusImpl(); transportFactory = new JettyHTTPTransportFactory(); transportFactory.setBus(bus); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setName(new QName("bla", "Service")); endpointInfo = new EndpointInfo(serviceInfo, ""); endpointInfo.setName(new QName("bla", "Port")); endpointInfo.addExtensor(policy); engine = EasyMock.createMock(JettyHTTPServerEngine.class); EasyMock.replay(); endpointInfo.setAddress(NOWHERE + "bar/foo"); JettyHTTPDestination dest = new EasyMockJettyHTTPDestination( bus, transportFactory, endpointInfo, engine); assertEquals(policy, dest.getServer()); } @Test public void testMultiplexGetAddressWithId() throws Exception { destination = setUpDestination(); final String id = "ID2"; EndpointReferenceType refWithId = destination.getAddressWithId(id); assertNotNull(refWithId); assertNotNull(refWithId.getReferenceParameters()); assertNotNull(refWithId.getReferenceParameters().getAny()); assertTrue("it is an element", refWithId.getReferenceParameters().getAny().get(0) instanceof JAXBElement); JAXBElement el = (JAXBElement) refWithId.getReferenceParameters().getAny().get(0); assertEquals("match our id", el.getValue(), id); } @Test public void testMultiplexGetAddressWithIdForAddress() throws Exception { destination = setUpDestination(); destination.setMultiplexWithAddress(true); final String id = "ID3"; EndpointReferenceType refWithId = destination.getAddressWithId(id); assertNotNull(refWithId); assertNull(refWithId.getReferenceParameters()); assertTrue("match our id", EndpointReferenceUtils.getAddress(refWithId).indexOf(id) != -1); } @Test public void testMultiplexGetIdForAddress() throws Exception { destination = setUpDestination(); destination.setMultiplexWithAddress(true); final String id = "ID3"; EndpointReferenceType refWithId = destination.getAddressWithId(id); String pathInfo = EndpointReferenceUtils.getAddress(refWithId); Map<String, Object> context = new HashMap Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF JettyHTTPDestinationTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.