Modified: incubator/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java?view=diff&rev=518860&r1=518859&r2=518860 ============================================================================== --- incubator/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java (original) +++ incubator/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java Thu Mar 15 21:27:21 2007 @@ -20,22 +20,38 @@ package org.apache.cxf.transport.servlet; import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.cxf.Bus; +import org.apache.cxf.helpers.HttpHeaderHelper; +import org.apache.cxf.io.AbstractWrappedOutputStream; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageImpl; import org.apache.cxf.service.model.EndpointInfo; +import org.apache.cxf.transport.AbstractDestination; +import org.apache.cxf.transport.Conduit; import org.apache.cxf.transport.ConduitInitiator; import org.apache.cxf.transport.MessageObserver; import org.apache.cxf.transport.http.AbstractHTTPDestination; public class ServletDestination extends AbstractHTTPDestination { + + public static final String HTTP_REQUEST = + "HTTP_SERVLET_REQUEST"; + public static final String HTTP_RESPONSE = + "HTTP_SERVLET_RESPONSE"; static final Logger LOG = Logger.getLogger(ServletDestination.class.getName()); @@ -65,9 +81,48 @@ protected Logger getLogger() { return LOG; } - + + /** + * @param inMessage the incoming message + * @return the inbuilt backchannel + */ + protected Conduit getInbuiltBackChannel(Message inMessage) { + HttpServletResponse response = (HttpServletResponse)inMessage.get(HTTP_RESPONSE); + return new BackChannelConduit(response); + } - + + + /** + * Copy the request headers into the message. + * + * @param message the current message + * @param headers the current set of headers + */ + protected void copyRequestHeaders(Message message, Map<String, List<String>> headers) { + HttpServletRequest req = (HttpServletRequest)message.get(HTTP_REQUEST); + for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) { + String fname = (String)e.nextElement(); + + List<String> values; + if (headers.containsKey(fname)) { + values = headers.get(fname); + } else { + values = new ArrayList<String>(); + headers.put(HttpHeaderHelper.getHeaderKey(fname), values); + } + for (Enumeration e2 = req.getHeaders(fname); e2.hasMoreElements();) { + String val = (String)e2.nextElement(); + values.add(val); + } + } + } + /** + * Copy the response headers into the response. + * + * @param message the current message + * @param headers the current set of headers + */ protected void copyResponseHeaders(Message message, HttpServletResponse response) { String ct = (String) message.get(Message.CONTENT_TYPE); String enc = (String) message.get(Message.ENCODING); @@ -80,7 +135,9 @@ } else if (enc != null) { response.setContentType("text/xml; charset=" + enc); } - } + + } + protected void doMessage(MessageImpl inMessage) throws IOException { @@ -98,7 +155,104 @@ } } - } + } + + protected class BackChannelConduit + extends AbstractDestination.AbstractBackChannelConduit { + + protected HttpServletResponse response; + + BackChannelConduit(HttpServletResponse resp) { + response = resp; + } + + /** + * Send an outbound message, assumed to contain all the name-value + * mappings of the corresponding input message (if any). + * + * @param message the message to be sent. + */ + public void send(Message message) throws IOException { + message.put(HTTP_RESPONSE, response); + message.setContent(OutputStream.class, + new WrappedOutputStream(message, response)); + } + } + + private class WrappedOutputStream extends AbstractWrappedOutputStream { + + protected HttpServletResponse response; + + WrappedOutputStream(Message m, HttpServletResponse resp) { + super(m); + response = resp; + } + + /** + * Perform any actions required on stream flush (freeze headers, + * reset output stream ... etc.) + */ + protected void doFlush() throws IOException { + OutputStream responseStream = flushHeaders(outMessage); + if (null != responseStream && !alreadyFlushed()) { + resetOut(responseStream, true); + } + } + + /** + * Perform any actions required on stream closure (handle response etc.) + */ + protected void doClose() { + commitResponse(); + } + + protected void onWrite() throws IOException { + } + + private void commitResponse() { + try { + response.flushBuffer(); + } catch (IOException e) { + LOG.severe(e.getMessage()); + } + } + } + + protected OutputStream flushHeaders(Message outMessage) throws IOException { + updateResponseHeaders(outMessage); + Object responseObj = outMessage.get(HTTP_RESPONSE); + OutputStream responseStream = null; + if (responseObj instanceof HttpServletResponse) { + HttpServletResponse response = (HttpServletResponse)responseObj; + + Integer i = (Integer)outMessage.get(Message.RESPONSE_CODE); + if (i != null) { + int status = i.intValue(); + response.setStatus(status); + } else { + response.setStatus(HttpURLConnection.HTTP_OK); + } + + copyResponseHeaders(outMessage, response); + responseStream = response.getOutputStream(); + + if (isOneWay(outMessage)) { + response.flushBuffer(); + } + } else { + LOG.log(Level.WARNING, "UNEXPECTED_RESPONSE_TYPE_MSG", responseObj.getClass()); + throw new IOException("UNEXPECTED_RESPONSE_TYPE_MSG" + responseObj.getClass()); + } + + if (isOneWay(outMessage)) { + outMessage.remove(HTTP_RESPONSE); + } + return responseStream; + } + + protected boolean isOneWay(Message message) { + return message.getExchange() != null && message.getExchange().isOneWay(); + } public MessageObserver getMessageObserver() { return this.incomingObserver;
Modified: incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java?view=diff&rev=518860&r1=518859&r2=518860 ============================================================================== --- incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java (original) +++ incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitTest.java Thu Mar 15 21:27:21 2007 @@ -33,9 +33,6 @@ import java.util.List; import java.util.Map; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; - import junit.framework.TestCase; import org.apache.cxf.bus.CXFBusImpl; @@ -66,8 +63,8 @@ private Proxy proxy; private Message inMessage; private MessageObserver observer; - private ServletOutputStream os; - private ServletInputStream is; + private OutputStream os; + private InputStream is; private IMocksControl control; public void setUp() throws Exception { @@ -142,8 +139,7 @@ contentTypes.add("text/xml"); contentTypes.add("charset=utf8"); headers.put("content-type", contentTypes); - message.put(Message.PROTOCOL_HEADERS, headers); - + message.put(Message.PROTOCOL_HEADERS, headers); AuthorizationPolicy authPolicy = new AuthorizationPolicy(); authPolicy.setUserName("BJ"); @@ -203,9 +199,9 @@ ((HttpURLConnection)connection).setChunkedStreamingMode(2048); EasyMock.expectLastCall(); } - } + } } - + CXFBusImpl bus = new CXFBusImpl(); URL decoupledURL = null; if (decoupled) { @@ -226,14 +222,13 @@ } control.replay(); - + HTTPConduit conduit = new HTTPConduit(bus, endpointInfo, null, connectionFactory); conduit.retrieveConnectionFactory(); - if (send) { conduit.getClient().setConnectionTimeout(303030); conduit.getClient().setReceiveTimeout(404040); @@ -244,14 +239,14 @@ } } } - + if (decoupled) { conduit.getClient().setDecoupledEndpoint(decoupledURL.toString()); assertNotNull("expected back channel", conduit.getBackChannel()); } else { assertNull("unexpected back channel", conduit.getBackChannel()); } - + observer = new MessageObserver() { public void onMessage(Message m) { inMessage = m; @@ -289,7 +284,7 @@ } - os = EasyMock.createMock(ServletOutputStream.class); + os = EasyMock.createMock(OutputStream.class); connection.getOutputStream(); EasyMock.expectLastCall().andReturn(os); os.write(PAYLOAD.getBytes(), 0, PAYLOAD.length()); @@ -303,9 +298,9 @@ EasyMock.expectLastCall(); verifyHandleResponse(decoupled); - + control.replay(); - + wrappedOS.flush(); wrappedOS.flush(); wrappedOS.close(); @@ -375,7 +370,7 @@ String responseString = Integer.toString(responseCode); EasyMock.expectLastCall().andReturn(responseString).times(2); } - is = EasyMock.createMock(ServletInputStream.class); + is = EasyMock.createMock(InputStream.class); connection.getInputStream(); EasyMock.expectLastCall().andReturn(is); } @@ -395,10 +390,10 @@ inMessage.get(DECOUPLED_CHANNEL_MESSAGE)); assertEquals("unexpected HTTP_REQUEST set", false, - inMessage.containsKey(AbstractHTTPDestination.HTTP_REQUEST)); + inMessage.containsKey(HTTPConduit.HTTP_REQUEST)); assertEquals("unexpected HTTP_RESPONSE set", false, - inMessage.containsKey(AbstractHTTPDestination.HTTP_RESPONSE)); + inMessage.containsKey(HTTPConduit.HTTP_RESPONSE)); assertEquals("unexpected Message.ASYNC_POST_RESPONSE_DISPATCH set", false, inMessage.containsKey(Message.ASYNC_POST_RESPONSE_DISPATCH)); Modified: incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyContextInspectorTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyContextInspectorTest.java?view=diff&rev=518860&r1=518859&r2=518860 ============================================================================== --- incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyContextInspectorTest.java (original) +++ incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyContextInspectorTest.java Thu Mar 15 21:27:21 2007 @@ -23,17 +23,17 @@ import org.easymock.classextension.EasyMock; import org.easymock.classextension.IMocksControl; -import org.mortbay.jetty.handler.ContextHandler; +import org.mortbay.http.HttpContext; public class JettyContextInspectorTest extends TestCase { private static final String CONTEXT_PATH = "/foo/bar"; - private ContextHandler context; + private HttpContext context; private IMocksControl control; public void setUp() throws Exception { control = EasyMock.createNiceControl(); - context = control.createMock(ContextHandler.class); + context = control.createMock(HttpContext.class); context.getContextPath(); EasyMock.expectLastCall().andReturn(CONTEXT_PATH); control.replay(); Modified: incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyHTTPDestinationTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyHTTPDestinationTest.java?view=diff&rev=518860&r1=518859&r2=518860 ============================================================================== --- incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyHTTPDestinationTest.java (original) +++ incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/http/JettyHTTPDestinationTest.java Thu Mar 15 21:27:21 2007 @@ -21,16 +21,15 @@ import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; - import junit.framework.TestCase; import org.apache.cxf.Bus; @@ -38,25 +37,23 @@ 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.configuration.security.SSLServerPolicy; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.io.AbstractCachedOutputStream; 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.transport.Conduit; import org.apache.cxf.transport.ConduitInitiator; import org.apache.cxf.transport.MessageObserver; +import org.apache.cxf.transports.http.QueryHandler; +import org.apache.cxf.transports.http.QueryHandlerRegistry; import org.apache.cxf.transports.http.configuration.HTTPServerPolicy; import org.apache.cxf.ws.addressing.EndpointReferenceType; import org.apache.cxf.wsdl.EndpointReferenceUtils; import org.easymock.classextension.EasyMock; -import org.mortbay.jetty.HttpFields; -import org.mortbay.jetty.Request; -import org.mortbay.jetty.Response; -import org.mortbay.jetty.handler.AbstractHandler; +import org.easymock.classextension.IMocksControl; +import org.mortbay.http.handler.AbstractHttpHandler; public class JettyHTTPDestinationTest extends TestCase { protected static final String AUTH_HEADER = "Authorization"; @@ -80,21 +77,26 @@ private ServerEngine engine; private HTTPServerPolicy policy; private JettyHTTPDestination destination; - private Request request; - private Response response; + private TestHttpRequest request; + private TestHttpResponse response; private Message inMessage; private Message outMessage; private MessageObserver observer; - private ServletInputStream is; - private ServletOutputStream os; - + private InputStream is; + private OutputStream os; + private IMocksControl control; + private WSDLQueryHandler wsdlQueryHandler; + private QueryHandlerRegistry queryHandlerRegistry; + private List<QueryHandler> queryHandlerList; public void setUp() throws Exception { - //control = EasyMock.createNiceControl(); + control = EasyMock.createNiceControl(); } public void tearDown() { + //control.verify(); + control = null; bus = null; conduitInitiator = null; decoupledBackChannel = null; @@ -124,25 +126,35 @@ } public void testDoServiceRedirectURL() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); setUpDoService(true); - destination.doService(request, response); + destination.doService(request, response); + + assertEquals("unexpected sendRedirect calls", + 1, + response.getSendRedirectCallCount()); + assertEquals("unexpected commit calls", + 1, + response.getCommitCallCount()); + assertEquals("unexpected setHandled calls", + 1, + request.getHandledCallCount()); } public void testDoService() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); setUpDoService(false); destination.doService(request, response); verifyDoService(); } public void testDoServiceWithHttpGET() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); setUpDoService(false, false, false, "GET", - "?customerId=abc&cutomerAdd=def", 200); + "?customerId=abc&cutomerAdd=def"); destination.doService(request, response); assertNotNull("unexpected null message", inMessage); @@ -151,15 +163,29 @@ "GET"); assertEquals("unexpected path", inMessage.get(Message.PATH_INFO), - "/bar/foo"); + "bar/foo"); assertEquals("unexpected query", inMessage.get(Message.QUERY_STRING), "?customerId=abc&cutomerAdd=def"); } + + public void testDoServiceWithHttpGETandQueryWSDL() throws Exception { + destination = setUpDestination(false, true); + setUpDoService(false, + false, + false, + "GET", + "?wsdl"); + + destination.doService(request, response); + assertNotNull("unexpected null response", response); + assertEquals("text/xml", response.getContentType()); + + } public void testGetAnonBackChannel() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); setUpDoService(false); destination.doService(request, response); setUpInMessage(); @@ -174,7 +200,7 @@ } public void testGetBackChannelSend() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); setUpDoService(false, true); destination.doService(request, response); setUpInMessage(); @@ -186,8 +212,8 @@ } public void testGetBackChannelSendFault() throws Exception { - destination = setUpDestination(false); - setUpDoService(false, true, 500); + destination = setUpDestination(false, false); + setUpDoService(false, true); destination.doService(request, response); setUpInMessage(); Conduit backChannel = @@ -198,8 +224,8 @@ } public void testGetBackChannelSendOneway() throws Exception { - destination = setUpDestination(false); - setUpDoService(false, true, 500); + destination = setUpDestination(false, false); + setUpDoService(false, true); destination.doService(request, response); setUpInMessage(); Conduit backChannel = @@ -210,9 +236,9 @@ } public void testGetBackChannelSendDecoupled() throws Exception { - destination = setUpDestination(false); + destination = setUpDestination(false, false); replyTo = getEPR(NOWHERE + "response/foo"); - setUpDoService(false, true, true, 202); + setUpDoService(false, true, true); destination.doService(request, response); setUpInMessage(); @@ -236,56 +262,56 @@ public void testServerPolicyInServiceModel() throws Exception { - policy = new HTTPServerPolicy(); address = getEPR("bar/foo"); bus = new CXFBusImpl(); - conduitInitiator = EasyMock.createMock(ConduitInitiator.class); + conduitInitiator = control.createMock(ConduitInitiator.class); + engine = control.createMock(ServerEngine.class); endpointInfo = new EndpointInfo(); endpointInfo.setAddress(NOWHERE + "bar/foo"); - endpointInfo.addExtensor(policy); - endpointInfo.addExtensor(new SSLServerPolicy()); - - engine = EasyMock.createMock(ServerEngine.class); - EasyMock.replay(); + + HTTPServerPolicy customPolicy = new HTTPServerPolicy(); + endpointInfo.addExtensor(customPolicy); - AbstractHTTPDestination dest = new JettyHTTPDestination(bus, + control.replay(); + + JettyHTTPDestination dest = new JettyHTTPDestination(bus, conduitInitiator, endpointInfo, engine); - assertEquals(policy, dest.getServer()); + assertEquals(customPolicy, dest.getServer()); } - + private JettyHTTPDestination setUpDestination() throws Exception { - return setUpDestination(false); + return setUpDestination(false, false); }; - private JettyHTTPDestination setUpDestination(boolean contextMatchOnStem) - throws Exception { - - policy = new HTTPServerPolicy(); + private JettyHTTPDestination setUpDestination(boolean contextMatchOnStem, boolean mockedBus) + throws Exception { address = getEPR("bar/foo"); - bus = new CXFBusImpl(); - conduitInitiator = EasyMock.createMock(ConduitInitiator.class); + if (!mockedBus) { + bus = new CXFBusImpl(); + } else { + bus = control.createMock(Bus.class); + } - engine = EasyMock.createMock(ServerEngine.class); + conduitInitiator = control.createMock(ConduitInitiator.class); + engine = control.createMock(ServerEngine.class); endpointInfo = new EndpointInfo(); endpointInfo.setAddress(NOWHERE + "bar/foo"); - endpointInfo.addExtensor(policy); - endpointInfo.getExtensor(SSLServerPolicy.class); - endpointInfo.addExtensor(new SSLServerPolicy()); - + engine.addServant(EasyMock.eq(new URL(NOWHERE + "bar/foo")), - EasyMock.isA(AbstractHandler.class)); - EasyMock.expectLastCall(); - EasyMock.replay(engine); + EasyMock.isA(AbstractHttpHandler.class)); + + control.replay(); JettyHTTPDestination dest = new JettyHTTPDestination(bus, conduitInitiator, endpointInfo, engine); - dest.retrieveEngine(); + dest.retrieveEngine(); + policy = dest.getServer(); observer = new MessageObserver() { public void onMessage(Message m) { inMessage = m; @@ -296,10 +322,11 @@ } private void setUpRemoveServant() throws Exception { - EasyMock.reset(engine); + control.verify(); + control.reset(); engine.removeServant(EasyMock.eq(new URL(NOWHERE + "bar/foo"))); EasyMock.expectLastCall(); - EasyMock.replay(engine); + control.replay(); } private void setUpDoService(boolean setRedirectURL) throws Exception { @@ -311,97 +338,67 @@ setUpDoService(setRedirectURL, sendResponse, false); - } - - private void setUpDoService(boolean setRedirectURL, - boolean sendResponse, int status) throws Exception { - String method = "POST"; - String query = "?name"; - setUpDoService(setRedirectURL, sendResponse, false, method, query, status); - } - - private void setUpDoService(boolean setRedirectURL, - boolean sendResponse, boolean decoupled, int status) throws Exception { - String method = "POST"; - String query = "?name"; - setUpDoService(setRedirectURL, sendResponse, decoupled, method, query, status); - } + } private void setUpDoService(boolean setRedirectURL, boolean sendResponse, boolean decoupled) throws Exception { String method = "POST"; String query = "?name"; - setUpDoService(setRedirectURL, sendResponse, decoupled, method, query, 200); + setUpDoService(setRedirectURL, sendResponse, decoupled, method, query); } - + private void setUpDoService(boolean setRedirectURL, boolean sendResponse, boolean decoupled, String method, - String query, - int status) throws Exception { - is = EasyMock.createMock(ServletInputStream.class); - os = EasyMock.createMock(ServletOutputStream.class); - request = EasyMock.createMock(Request.class); - response = EasyMock.createMock(Response.class); - - request.getMethod(); - EasyMock.expectLastCall().andReturn(method); + String query) throws Exception { + + control.verify(); + control.reset(); + + is = EasyMock.createMock(InputStream.class); + os = EasyMock.createMock(OutputStream.class); - if ("GET".equals(method)) { - request.getQueryString(); - EasyMock.expectLastCall().andReturn(query); - } + // EasyMock does not seem able to properly mock calls to HttpRequest + // or HttpResponse - expectations set seem to be ignored. + // Hence we use hand-crafted sub-classes instead of mocks. + // + //request = EasyMock.createMock(HttpRequest.class); + //response = EasyMock.createMock(HttpResponse.class); + request = new TestHttpRequest(method, is, "bar/foo", query); + response = new TestHttpResponse(os); if (setRedirectURL) { policy.setRedirectURL(NOWHERE + "foo/bar"); - response.sendRedirect(EasyMock.eq(NOWHERE + "foo/bar")); - EasyMock.expectLastCall(); - response.flushBuffer(); - EasyMock.expectLastCall(); - request.setHandled(true); - EasyMock.expectLastCall(); - } else { // method is POST - EasyMock.expect(request.getMethod()).andReturn(method); - EasyMock.expect(request.getInputStream()).andReturn(is); - EasyMock.expect(request.getContextPath()).andReturn("/bar"); - EasyMock.expect(request.getPathInfo()).andReturn("/foo"); - EasyMock.expect(request.getQueryString()).andReturn(query); - EasyMock.expect(request.getContentType()).andReturn("text/xml charset=utf8"); - - HttpFields httpFields = new HttpFields(); - httpFields.add("content-type", "text/xml"); - httpFields.add("content-type", "charset=utf8"); - httpFields.put(JettyHTTPDestinationTest.AUTH_HEADER, JettyHTTPDestinationTest.BASIC_AUTH); - - EasyMock.expect(request.getHeaderNames()).andReturn(httpFields.getFieldNames()); - request.getHeaders("content-type"); - EasyMock.expectLastCall().andReturn(httpFields.getValues("content-type")); - request.getHeaders(JettyHTTPDestinationTest.AUTH_HEADER); - EasyMock.expectLastCall().andReturn(httpFields.getValues(JettyHTTPDestinationTest.AUTH_HEADER)); - - EasyMock.expect(request.getInputStream()).andReturn(is); - request.setHandled(true); - EasyMock.expectLastCall(); - response.flushBuffer(); - EasyMock.expectLastCall(); - if (sendResponse) { - response.setStatus(status); - EasyMock.expectLastCall(); - response.setContentType("text/xml charset=utf8"); - EasyMock.expectLastCall(); - response.addHeader(EasyMock.isA(String.class), EasyMock.isA(String.class)); - EasyMock.expectLastCall().anyTimes(); - response.getOutputStream(); - EasyMock.expectLastCall().andReturn(os); - response.getStatus(); - EasyMock.expectLastCall().andReturn(status).anyTimes(); - response.flushBuffer(); - EasyMock.expectLastCall(); + //response.sendRedirect(EasyMock.eq(NOWHERE + "foo/bar")); + //EasyMock.expectLastCall(); + //response.commit(); + //EasyMock.expectLastCall(); + //request.setHandled(true); + //EasyMock.expectLastCall(); + } else { + //request.getMethod(); + //EasyMock.expectLastCall().andReturn("POST").times(2); + //request.getInputStream(); + //EasyMock.expectLastCall().andReturn(is); + //request.getPath(); + //EasyMock.expectLastCall().andReturn("bar/foo"); + //request.getQuery(); + //EasyMock.expectLastCall().andReturn(QUERY); + //request.setHandled(true); + //EasyMock.expectLastCall(); + //response.commit(); + //EasyMock.expectLastCall(); + //if (sendResponse) { + // response.getOutputStream(); + // EasyMock.expectLastCall().andReturn(os); + // response.commit(); + // EasyMock.expectLastCall(); + //} + if ("GET".equals(method) && "?wsdl".equals(query)) { + verifyGetWSDLQuery(); } - request.getAttribute("javax.net.ssl.session"); - EasyMock.expectLastCall().andReturn(null); } if (decoupled) { @@ -409,15 +406,11 @@ conduitInitiator.getConduit(EasyMock.isA(EndpointInfo.class), EasyMock.eq(replyTo)); EasyMock.expectLastCall().andReturn(decoupledBackChannel); - decoupledBackChannel.setMessageObserver(EasyMock.isA(MessageObserver.class)); - EasyMock.expectLastCall(); - decoupledBackChannel.send(EasyMock.isA(Message.class)); + decoupledBackChannel.send(EasyMock.eq(outMessage)); EasyMock.expectLastCall(); - EasyMock.replay(conduitInitiator); - EasyMock.replay(decoupledBackChannel); } - EasyMock.replay(response); - EasyMock.replay(request); + + control.replay(); } private void setUpInMessage() { @@ -443,6 +436,23 @@ challenges.add(CUSTOM_CHALLENGE); responseHeaders.put(CHALLENGE_HEADER, challenges); } + + private void verifyGetWSDLQuery() throws Exception { + wsdlQueryHandler = control.createMock(WSDLQueryHandler.class); + queryHandlerRegistry = control.createMock(QueryHandlerRegistry.class); + queryHandlerList = new ArrayList<QueryHandler>(); + queryHandlerList.add(wsdlQueryHandler); + bus.getExtension(QueryHandlerRegistry.class); + EasyMock.expectLastCall().andReturn(queryHandlerRegistry); + queryHandlerRegistry.getHandlers(); + EasyMock.expectLastCall().andReturn(queryHandlerList); + wsdlQueryHandler.isRecognizedQuery("http://localhost/bar/foo?wsdl", endpointInfo); + EasyMock.expectLastCall().andReturn(true); + wsdlQueryHandler.getResponseContentType("http://localhost/bar/foo?wsdl"); + EasyMock.expectLastCall().andReturn("text/xml"); + wsdlQueryHandler.writeResponse("http://localhost/bar/foo?wsdl", endpointInfo, os); + EasyMock.expectLastCall().once(); + } private void verifyDoService() throws Exception { assertNotNull("unexpected null message", inMessage); @@ -457,22 +467,38 @@ "POST"); assertEquals("unexpected path", inMessage.get(Message.PATH_INFO), - "/bar/foo"); + "bar/foo"); assertEquals("unexpected query", inMessage.get(Message.QUERY_STRING), "?name"); - assertNull("unexpected query", - inMessage.get(TLSSessionInfo.class)); - verifyRequestHeaders(); + verifyRequestHeaders(); - + + assertEquals("unexpected getMethod calls", + 1, + request.getMethodCallCount()); + assertEquals("unexpected getInputStream calls", + 1, + request.getInputStreamCallCount()); + assertEquals("unexpected getPath calls", + 1, + request.getPathCallCount()); + assertEquals("unexpected getQuery calls", + 1, + request.getQueryCallCount()); + assertEquals("unexpected setHandled calls", + 1, + request.getHandledCallCount()); } private void verifyRequestHeaders() throws Exception { Map<String, List<String>> requestHeaders = CastUtils.cast((Map<?, ?>)inMessage.get(Message.PROTOCOL_HEADERS)); assertNotNull("expected request headers", - requestHeaders); + requestHeaders); + assertEquals("expected getFieldNames", + 1, + request.getFieldNamesCallCount()); List<String> values = requestHeaders.get("content-type"); assertNotNull("expected field", values); assertEquals("unexpected values", 2, values.size()); @@ -499,10 +525,10 @@ CastUtils.cast((Map<?, ?>)outMsg.get(Message.PROTOCOL_HEADERS)); assertNotNull("expected response headers", responseHeaders); - /*assertEquals("expected addField", + assertEquals("expected addField", 3, response.getAddFieldCallCount()); - Enumeration e = response.getHeaders(CHALLENGE_HEADER); + Enumeration e = response.getFieldValues(CHALLENGE_HEADER); List<String> challenges = new ArrayList<String>(); while (e.hasMoreElements()) { challenges.add((String)e.nextElement()); @@ -512,7 +538,7 @@ assertTrue("expected challenge", challenges.contains(DIGEST_CHALLENGE)); assertTrue("expected challenge", - challenges.contains(CUSTOM_CHALLENGE));*/ + challenges.contains(CUSTOM_CHALLENGE)); } private void verifyBackChannelSend(Conduit backChannel, @@ -532,7 +558,10 @@ OutputStream responseOS = outMsg.getContent(OutputStream.class); assertNotNull("expected output stream", responseOS); assertTrue("unexpected output stream type", - responseOS instanceof AbstractCachedOutputStream); + responseOS instanceof AbstractCachedOutputStream); + assertEquals("expected commit", + 1, + response.getCommitCallCount()); outMsg.put(Message.RESPONSE_CODE, status); responseOS.write(PAYLOAD.getBytes()); @@ -542,28 +571,44 @@ OutputStream underlyingOS = ((AbstractCachedOutputStream)responseOS).getOut(); assertTrue("unexpected underlying output stream type", - underlyingOS instanceof ByteArrayOutputStream); + underlyingOS instanceof ByteArrayOutputStream); + assertEquals("expected getOutputStream", + 0, + response.getOutputStreamCallCount()); outMsg.getExchange().setOneWay(oneway); responseOS.flush(); - + assertEquals("expected setStatus", + 1, + response.getStatusCallCount()); assertEquals("unexpected status", status, response.getStatus()); - + if (status == 500) { + assertEquals("unexpected status message", + "Internal Server Error", + response.getReason()); + } verifyResponseHeaders(outMsg); - + assertEquals("expected getOutputStream", + 1, + response.getOutputStreamCallCount()); underlyingOS = ((AbstractCachedOutputStream)responseOS).getOut(); assertFalse("unexpected underlying output stream type: " + underlyingOS.getClass(), underlyingOS instanceof ByteArrayOutputStream); - + assertEquals("expected commit", + oneway ? 2 : 1, + response.getCommitCallCount()); if (oneway) { assertNull("unexpected HTTP response", outMsg.get(JettyHTTPDestination.HTTP_RESPONSE)); } else { assertNotNull("expected HTTP response", outMsg.get(JettyHTTPDestination.HTTP_RESPONSE)); - responseOS.close(); + responseOS.close(); + assertEquals("expected commit", + 2, + response.getCommitCallCount()); } } Modified: incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/https/HttpsURLConnectionFactoryTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/https/HttpsURLConnectionFactoryTest.java?view=diff&rev=518860&r1=518859&r2=518860 ============================================================================== --- incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/https/HttpsURLConnectionFactoryTest.java (original) +++ incubator/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/https/HttpsURLConnectionFactoryTest.java Thu Mar 15 21:27:21 2007 @@ -27,6 +27,8 @@ import junit.framework.TestCase; +import org.apache.cxf.configuration.security.FiltersType; +import org.apache.cxf.configuration.security.ObjectFactory; import org.apache.cxf.configuration.security.SSLClientPolicy; @@ -35,6 +37,11 @@ protected static final String DROP_BACK_SRC_DIR = "../../../../../../../" + "src/test/java/org/apache/cxf/transport/https/"; + + private static final String[] EXPORT_CIPHERS = + {"SSL_RSA_WITH_NULL_MD5", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_WITH_DES_CBC_SHA"}; + private static final String[] NON_EXPORT_CIPHERS = + {"SSL_RSA_WITH_RC4_128_MD5", "SSL_RSA_WITH_3DES_EDE_CBC_SHA"}; private TestHttpsURLConnection connection; @@ -124,7 +131,7 @@ */ public void testSetAllData() throws Exception { - + String keyStoreStr = getPath("resources/defaultkeystore"); SSLClientPolicy sslClientPolicy = new SSLClientPolicy(); sslClientPolicy.setKeystore(keyStoreStr); @@ -141,7 +148,13 @@ sslClientPolicy.setCertValidator("Anything"); sslClientPolicy.setProxyHost("Anything"); sslClientPolicy.setProxyPort(new Long(1234)); - + for (int i = 0; i < EXPORT_CIPHERS.length; i++) { + sslClientPolicy.getCiphersuites().add(EXPORT_CIPHERS[i]); + } + for (int i = 0; i < NON_EXPORT_CIPHERS.length; i++) { + sslClientPolicy.getCiphersuites().add(NON_EXPORT_CIPHERS[i]); + } + String trustStoreStr = getPath("resources/defaulttruststore"); sslClientPolicy.setTrustStore(trustStoreStr); TestLogHandler handler = new TestLogHandler(); @@ -182,8 +195,16 @@ + "algorithm has not been set in configuration " + "so the default value PKIX will be used.")); - assertTrue("Ciphersuites is being being read from somewhere unknown", handler - .checkLogContainsString("The cipher suite has not been set, default values " + "will be used.")); + assertFalse("Ciphersuites config not picked up", handler + .checkLogContainsString("The cipher suites have not been configured, " + + "default values will be used.")); + assertFalse("Unexpected included ciphersuite filter", + handler.checkLogContainsString("suite is included by the filter.")); + assertFalse("Unexpected excluded ciphersuite fuilter", + handler.checkLogContainsString("suite is excluded by the filter.")); + assertFalse("Unexpected ciphersuite filtering", + handler.checkLogContainsString("The enabled cipher suites have been filtered down to")); + assertTrue("Truststore type not being read", handler .checkLogContainsString("The key store type has been set in " + "configuration to JKS")); @@ -197,6 +218,100 @@ .checkLogContainsString("Unsupported SSLClientPolicy property : MaxChainLength")); assertTrue("CertValidator caching set but no warning about not supported", handler .checkLogContainsString("Unsupported SSLClientPolicy property : CertValidator")); + } + + public void testDefaultedCipherSuiteFilters() throws Exception { + + String keyStoreStr = getPath("resources/defaultkeystore"); + SSLClientPolicy sslClientPolicy = new SSLClientPolicy(); + sslClientPolicy.setKeystore(keyStoreStr); + sslClientPolicy.setKeystoreType("JKS"); + + sslClientPolicy.setKeyPassword("defaultkeypass"); + sslClientPolicy.setKeystorePassword("defaultkeypass"); + sslClientPolicy.setTrustStoreType("JKS"); + sslClientPolicy.setSecureSocketProtocol("TLSv1"); + + String trustStoreStr = getPath("resources/defaulttruststore"); + sslClientPolicy.setTrustStore(trustStoreStr); + TestLogHandler handler = new TestLogHandler(); + HttpsURLConnectionFactory factory = createFactory(sslClientPolicy, + "https://dummyurl", + handler); + + factory.decorate(connection); + + assertTrue("Ciphersuites is being being read from somewhere unknown", + handler.checkLogContainsString("The cipher suites have not been configured," + + " falling back to cipher suite filters.")); + assertTrue("Expected defaulted ciphersuite filters", + handler.checkLogContainsString("The cipher suite filters have not been configured," + + " falling back to default filters.")); + for (int i = 0; i < EXPORT_CIPHERS.length; i++) { + assertTrue("Expected included ciphersuite not included: " + EXPORT_CIPHERS[i], + handler.checkLogContainsString(EXPORT_CIPHERS[i] + + " cipher suite is included by the filter.")); + } + for (int i = 0; i < NON_EXPORT_CIPHERS.length; i++) { + assertTrue("Expected excluded ciphersuite not included: " + NON_EXPORT_CIPHERS[i], + handler.checkLogContainsString(NON_EXPORT_CIPHERS[i] + + " cipher suite is excluded by the filter.")); + } + assertTrue("Expected excluded ciphersuite not included", + handler.checkLogContainsString("The enabled cipher suites have been filtered down to")); + + } + + public void testNonDefaultedCipherSuiteFilters() throws Exception { + + String keyStoreStr = getPath("resources/defaultkeystore"); + SSLClientPolicy sslClientPolicy = new SSLClientPolicy(); + sslClientPolicy.setKeystore(keyStoreStr); + sslClientPolicy.setKeystoreType("JKS"); + + sslClientPolicy.setKeyPassword("defaultkeypass"); + sslClientPolicy.setKeystorePassword("defaultkeypass"); + sslClientPolicy.setTrustStoreType("JKS"); + sslClientPolicy.setSecureSocketProtocol("TLSv1"); + + // reverse default sense of include/exlcude + FiltersType filters = new ObjectFactory().createFiltersType(); + for (int i = 0; i < NON_EXPORT_CIPHERS.length; i++) { + filters.getInclude().add(NON_EXPORT_CIPHERS[i]); + } + for (int i = 0; i < EXPORT_CIPHERS.length; i++) { + filters.getExclude().add(EXPORT_CIPHERS[i]); + } + sslClientPolicy.setCiphersuiteFilters(filters); + + String trustStoreStr = getPath("resources/defaulttruststore"); + sslClientPolicy.setTrustStore(trustStoreStr); + TestLogHandler handler = new TestLogHandler(); + HttpsURLConnectionFactory factory = createFactory(sslClientPolicy, + "https://dummyurl", + handler); + + factory.decorate(connection); + + assertTrue("Ciphersuites is being being read from somewhere unknown", + handler.checkLogContainsString("The cipher suites have not been configured," + + " falling back to cipher suite filters.")); + assertFalse("Unexpected defaulted ciphersuite filters", + handler.checkLogContainsString("The cipher suite filters have not been configured," + + " falling back to default filters.")); + for (int i = 0; i < NON_EXPORT_CIPHERS.length; i++) { + assertTrue("Expected included ciphersuite not included: " + NON_EXPORT_CIPHERS[i], + handler.checkLogContainsString(NON_EXPORT_CIPHERS[i] + + " cipher suite is included by the filter.")); + } + for (int i = 0; i < EXPORT_CIPHERS.length; i++) { + assertTrue("Expected excluded ciphersuite not included: " + EXPORT_CIPHERS[i], + handler.checkLogContainsString(EXPORT_CIPHERS[i] + + " cipher suite is excluded by the filter.")); + } + assertTrue("Expected excluded ciphersuite not included", + handler.checkLogContainsString("The enabled cipher suites have been filtered down to")); + } public void testAllValidDataJKS() throws Exception {
