Return-Path: Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: (qmail 54697 invoked from network); 12 Dec 2010 20:30:48 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 12 Dec 2010 20:30:48 -0000 Received: (qmail 90448 invoked by uid 500); 12 Dec 2010 20:30:48 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 90391 invoked by uid 500); 12 Dec 2010 20:30:48 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 90384 invoked by uid 99); 12 Dec 2010 20:30:48 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Dec 2010 20:30:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Dec 2010 20:30:39 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5E22D23889BF; Sun, 12 Dec 2010 20:30:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1044907 [1/2] - in /cxf/trunk/rt/transports: http-osgi/ http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/ http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/ http/src/main/java/org/apache/cxf/transport/http/ http/src/main... Date: Sun, 12 Dec 2010 20:30:17 -0000 To: commits@cxf.apache.org From: cschneider@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101212203017.5E22D23889BF@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cschneider Date: Sun Dec 12 20:30:16 2010 New Revision: 1044907 URL: http://svn.apache.org/viewvc?rev=1044907&view=rev Log: CXF-3160 Extracting service list generation from ServletController and OsgiServletController. Moving some more code out of http-osgi Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/FormattedServiceListWriter.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListGeneratorServlet.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListWriter.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/UnformattedServiceListWriter.java Modified: cxf/trunk/rt/transports/http-osgi/ (props changed) cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiDestination.java cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServlet.java cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistry.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractCXFServlet.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java Propchange: cxf/trunk/rt/transports/http-osgi/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Sun Dec 12 20:30:16 2010 @@ -8,3 +8,7 @@ eclipse-classes .project .wtpmodules + +.springBeans + +.springWebflow Modified: cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiDestination.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiDestination.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiDestination.java (original) +++ cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiDestination.java Sun Dec 12 20:30:16 2010 @@ -58,8 +58,4 @@ public class OsgiDestination extends Abs return LOG; } - protected Bus getBus() { - return bus; - } - } Modified: cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServlet.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServlet.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServlet.java (original) +++ cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServlet.java Sun Dec 12 20:30:16 2010 @@ -20,45 +20,42 @@ package org.apache.cxf.transport.http_os import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.cxf.message.ExchangeImpl; -import org.apache.cxf.message.MessageImpl; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.cxf.transport.servlet.AbstractHTTPServlet; +import org.apache.cxf.transport.servlet.servicelist.ServiceListGeneratorServlet; public class OsgiServlet extends AbstractHTTPServlet { - private DestinationRegistry transport; + private DestinationRegistry destinationRegistry; private OsgiServletController controller; + private HttpServlet serviceListGenerator; - public OsgiServlet(DestinationRegistry transport) { - this.transport = transport; + public OsgiServlet(DestinationRegistry destinationRegistry) { + this(destinationRegistry, new ServiceListGeneratorServlet(destinationRegistry, null)); + } + + public OsgiServlet(DestinationRegistry destinationRegistry, HttpServlet serviceListGenerator) { + this.destinationRegistry = destinationRegistry; + this.serviceListGenerator = serviceListGenerator; } public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); - controller = new OsgiServletController(this.getTransport(), this.getServletConfig()); + controller = new OsgiServletController(servletConfig, + this.destinationRegistry, + serviceListGenerator); } @Override public void destroy() { } - public DestinationRegistry getTransport() { - return transport; - } - public void invoke(HttpServletRequest request, HttpServletResponse res) throws ServletException { controller.invoke(request, res); } - - protected MessageImpl createInMessage() { - return new MessageImpl(); - } - protected ExchangeImpl createExchange() { - return new ExchangeImpl(); - } } Modified: cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java (original) +++ cxf/trunk/rt/transports/http-osgi/src/main/java/org/apache/cxf/transport/http_osgi/OsgiServletController.java Sun Dec 12 20:30:16 2010 @@ -19,15 +19,12 @@ package org.apache.cxf.transport.http_osgi; import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; import java.util.Set; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,7 +33,6 @@ import org.apache.cxf.common.logging.Log import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.resource.ResourceManager; import org.apache.cxf.service.model.EndpointInfo; -import org.apache.cxf.service.model.OperationInfo; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.cxf.transport.servlet.AbstractServletController; @@ -46,11 +42,11 @@ import org.apache.cxf.wsdl.http.AddressT public class OsgiServletController extends AbstractServletController { private static final Logger LOG = LogUtils.getL7dLogger(OsgiServlet.class); - - private DestinationRegistry destinationRegistry; - public OsgiServletController(DestinationRegistry destinationRegistry, ServletConfig config) { - super(config); - this.destinationRegistry = destinationRegistry; + + public OsgiServletController(ServletConfig config, + DestinationRegistry destinationRegistry, + HttpServlet serviceListGenerator) { + super(config, destinationRegistry, serviceListGenerator); } private synchronized void updateDests(HttpServletRequest request) { @@ -59,7 +55,6 @@ public class OsgiServletController exten } String base = forcedBaseAddress == null ? getBaseURL(request) : forcedBaseAddress; - Set paths = destinationRegistry.getDestinationsPaths(); for (String path : paths) { AbstractHTTPDestination d2 = destinationRegistry.getDestinationForPath(path); @@ -84,7 +79,7 @@ public class OsgiServletController exten || StringUtils.isEmpty(request.getPathInfo()) || "/".equals(request.getPathInfo())) { updateDests(request); - generateServiceList(request, res); + serviceListGenerator.service(request, res); } else { d = destinationRegistry.checkRestfulRequest(address); if (d == null || d.getMessageObserver() == null) { @@ -98,7 +93,7 @@ public class OsgiServletController exten } } else { EndpointInfo ei = d.getEndpointInfo(); - Bus bus = ((OsgiDestination)d).getBus(); + Bus bus = d.getBus(); ClassLoader orig = Thread.currentThread().getContextClassLoader(); try { ResourceManager manager = bus.getExtension(ResourceManager.class); @@ -109,9 +104,9 @@ public class OsgiServletController exten Thread.currentThread().setContextClassLoader(loader); } } - Iterable queryHandlers = bus.getExtension(QueryHandlerRegistry.class) - .getHandlers(); - if (!StringUtils.isEmpty(request.getQueryString()) && queryHandlers != null) { + QueryHandlerRegistry queryHandlerRegistry = bus.getExtension(QueryHandlerRegistry.class); + + if (!StringUtils.isEmpty(request.getQueryString()) && queryHandlerRegistry != null) { // update the EndPoint Address with request url if ("GET".equals(request.getMethod())) { @@ -122,7 +117,8 @@ public class OsgiServletController exten String baseUri = request.getRequestURL().toString() + "?" + request.getQueryString(); - QueryHandler selectedHandler = findQueryHandler(queryHandlers, ei, ctxUri, baseUri); + QueryHandler selectedHandler = + findQueryHandler(queryHandlerRegistry, ei, ctxUri, baseUri); if (selectedHandler != null) { respondUsingQueryHandler(selectedHandler, res, ei, ctxUri, baseUri); @@ -142,136 +138,5 @@ public class OsgiServletController exten } } - private QueryHandler findQueryHandler(Iterable handlers, EndpointInfo ei, String ctxUri, - String baseUri) { - for (QueryHandler qh : handlers) { - if (qh.isRecognizedQuery(baseUri, ctxUri, ei)) { - return qh; - } - } - return null; - } - - private void respondUsingQueryHandler(QueryHandler selectedHandler, HttpServletResponse res, - EndpointInfo ei, String ctxUri, String baseUri) throws IOException, - ServletException { - res.setContentType(selectedHandler.getResponseContentType(baseUri, ctxUri)); - OutputStream out = res.getOutputStream(); - try { - selectedHandler.writeResponse(baseUri, ctxUri, ei, out); - out.flush(); - } catch (Exception e) { - LOG.warning(selectedHandler.getClass().getName() - + " Exception caught writing response: " - + e.getMessage()); - throw new ServletException(e); - } - } - - protected void generateServiceList(HttpServletRequest request, HttpServletResponse response) - throws IOException { - response.setContentType("text/html; charset=UTF-8"); - - response.getWriter().write(""); - response.getWriter().write(""); - if (serviceListStyleSheet != null) { - response.getWriter().write( - ""); - } else { - response.getWriter().write( - ""); - } - response.getWriter().write(""); - response.getWriter().write("CXF - Service list"); - response.getWriter().write(""); - - Collection destinations = destinationRegistry.getDestinations(); - - if (destinations.size() > 0) { - writeSOAPEndpoints(response, destinations); - writeRESTfulEndpoints(response, destinations); - } else { - response.getWriter().write("No services have been found."); - } - - response.getWriter().write(""); - } - - private void writeSOAPEndpoints(HttpServletResponse response, - Collection destinations) - throws IOException { - response.getWriter().write("Available SOAP services:
"); - response.getWriter().write(""); - for (AbstractHTTPDestination sd : destinations) { - if (null != sd.getEndpointInfo().getName() - && null != sd.getEndpointInfo().getInterface()) { - response.getWriter().write(""); - } - } - response.getWriter().write("
"); - response.getWriter().write("" - + sd.getEndpointInfo().getInterface().getName().getLocalPart() - + ""); - response.getWriter().write("
    "); - for (OperationInfo oi : sd.getEndpointInfo().getInterface().getOperations()) { - response.getWriter().write("
  • " + oi.getName().getLocalPart() + "
  • "); - } - response.getWriter().write("
"); - response.getWriter().write("
"); - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write("Endpoint address: " - + "" + address + ""); - response.getWriter().write("
WSDL : " - + "" - + sd.getEndpointInfo().getService().getName() + ""); - response.getWriter().write("
Target namespace: " - + "" - + sd.getEndpointInfo().getService().getTargetNamespace() + ""); - response.getWriter().write("


"); - } - - - private void writeRESTfulEndpoints(HttpServletResponse response, - Collection destinations) - throws IOException { - - List restfulDests = new ArrayList(); - for (AbstractHTTPDestination sd : destinations) { - // use some more reasonable check - though this one seems to be the only option at the moment - if (null == sd.getEndpointInfo().getInterface()) { - restfulDests.add(sd); - } - } - if (restfulDests.size() == 0) { - return; - } - - response.getWriter().write("Available RESTful services:
"); - response.getWriter().write(""); - for (AbstractHTTPDestination sd : destinations) { - if (null == sd.getEndpointInfo().getInterface()) { - response.getWriter().write(""); - } - } - response.getWriter().write("
"); - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write("Endpoint address: " - + "" + address + ""); - response.getWriter().write("
WADL : " - + "" - + address + "?_wadl&type=xml" + ""); - response.getWriter().write("
"); - } - - protected void generateNotFound(HttpServletRequest request, HttpServletResponse res) throws IOException { - res.setStatus(404); - res.setContentType("text/html"); - res.getWriter().write("No service was found."); - } } Modified: cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java (original) +++ cxf/trunk/rt/transports/http-osgi/src/test/java/org/apache/cxf/transport/http_osgi/OsgiServletTest.java Sun Dec 12 20:30:16 2010 @@ -19,7 +19,6 @@ package org.apache.cxf.transport.http_osgi; -import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -27,29 +26,25 @@ import java.util.Set; import java.util.TreeSet; import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.namespace.QName; import org.apache.cxf.Bus; -import org.apache.cxf.message.ExchangeImpl; -import org.apache.cxf.message.Message; -import org.apache.cxf.message.MessageImpl; -import org.apache.cxf.security.SecurityContext; import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.service.model.InterfaceInfo; import org.apache.cxf.service.model.ServiceInfo; +import org.apache.cxf.transport.AbstractDestination; import org.apache.cxf.transport.MessageObserver; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; -import org.apache.cxf.transport.http.HTTPSession; import org.apache.cxf.transports.http.QueryHandler; import org.apache.cxf.transports.http.QueryHandlerRegistry; -import org.apache.cxf.wsdl.EndpointReferenceUtils; import org.apache.cxf.wsdl.http.AddressType; import org.easymock.classextension.EasyMock; import org.easymock.classextension.IMocksControl; @@ -57,12 +52,29 @@ import org.easymock.classextension.IMock import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class OsgiServletTest extends Assert { + private final class TestOsgiServletController extends OsgiServletController { + private boolean invokeDestinationCalled; + + + private TestOsgiServletController(ServletConfig config, + DestinationRegistry destinationRegistry, + HttpServlet serviceListGenerator) { + super(config, destinationRegistry, serviceListGenerator); + } + + @Override + public void invokeDestination(HttpServletRequest req, + HttpServletResponse res, + AbstractHTTPDestination d) throws ServletException { + invokeDestinationCalled = true; + } + } + private static final String ADDRESS = "http://bar/snafu"; private static final String ROOT = "http://localhost:8080/"; private static final QName QNAME = new QName(ADDRESS, "foobar"); @@ -70,25 +82,20 @@ public class OsgiServletTest extends Ass private static final String URI = "/cxf" + PATH; private static final String SERVICES = "/cxf/services"; private static final String QUERY = "wsdl"; - private static final String VERB = "POST"; private static final String TEXT = "text/html"; private static final String TEXT_LIST = "text/html; charset=UTF-8"; private static final String XML = "text/xml"; - private static final String ENCODING = "UTF-8"; private static final String NO_SERVICE = "No service was found."; private IMocksControl control; private Bus bus; private DestinationRegistry registry; - private OsgiDestination destination; + private AbstractHTTPDestination destination; private ServletConfig config; - private ServletContext context; private HttpServletRequest request; private HttpServletResponse response; - private MessageImpl message; private MessageObserver observer; private AddressType extensor; - private ExchangeImpl exchange; private EndpointInfo endpoint; private Set paths; @@ -97,13 +104,10 @@ public class OsgiServletTest extends Ass control = EasyMock.createNiceControl(); bus = control.createMock(Bus.class); registry = control.createMock(DestinationRegistry.class); - destination = control.createMock(OsgiDestination.class); - context = control.createMock(ServletContext.class); + destination = control.createMock(AbstractHTTPDestination.class); config = control.createMock(ServletConfig.class); request = control.createMock(HttpServletRequest.class); response = control.createMock(HttpServletResponse.class); - message = control.createMock(MessageImpl.class); - exchange = control.createMock(ExchangeImpl.class); observer = control.createMock(MessageObserver.class); extensor = control.createMock(AddressType.class); endpoint = new EndpointInfo(); @@ -121,14 +125,9 @@ public class OsgiServletTest extends Ass bus = null; registry = null; destination = null; - context = null; config = null; request = null; response = null; - message = null; - exchange = null; - destination = null; - exchange = null; observer = null; extensor = null; } @@ -139,27 +138,24 @@ public class OsgiServletTest extends Ass setUpResponse(404, TEXT, NO_SERVICE); control.replay(); - - OsgiServlet servlet = setUpServlet(); - + OsgiServlet servlet = new OsgiServlet(registry); + servlet.init(config); servlet.invoke(request, response); - control.verify(); } @Test public void testInvokeGetServices() throws Exception { setUpRequest(SERVICES, null, 1); - setUpResponse(0, TEXT_LIST, - "Endpoint address: " - + "" + ADDRESS + ""); + HttpServlet serviceListGenerator = control.createMock(HttpServlet.class); + serviceListGenerator.service(EasyMock.isA(ServletRequest.class), EasyMock.isA(ServletResponse.class)); + EasyMock.expectLastCall(); + control.replay(); - - OsgiServlet servlet = setUpServlet(); - + OsgiServlet servlet = new OsgiServlet(registry, serviceListGenerator); + servlet.init(config); servlet.invoke(request, response); - control.verify(); } @@ -170,11 +166,9 @@ public class OsgiServletTest extends Ass "No services have been found."); control.replay(); - - OsgiServlet servlet = setUpServlet(); - + OsgiServlet servlet = new OsgiServlet(registry); + servlet.init(config); servlet.invoke(request, response); - control.verify(); } @@ -184,42 +178,42 @@ public class OsgiServletTest extends Ass setUpQuery(); control.replay(); - - OsgiServlet servlet = setUpServlet(); - + OsgiServlet servlet = new OsgiServlet(registry); + servlet.init(config); servlet.invoke(request, response); - control.verify(); } @Test - @Ignore public void testInvokeDestination() throws Exception { setUpRequest(URI, PATH, -2); - setUpMessage(); - - control.replay(); - - OsgiServlet servlet = setUpServlet(); + HttpServlet serviceListGenerator = control.createMock(HttpServlet.class); - servlet.invoke(request, response); - + control.replay(); + TestOsgiServletController controller = + new TestOsgiServletController(config, registry, serviceListGenerator); + controller.invoke(request, response); control.verify(); + Assert.assertTrue(controller.invokeDestinationCalled); } @Test - @Ignore public void testInvokeRestful() throws Exception { setUpRequest(URI, null, -1); - setUpRestful(); - setUpMessage(); + paths.add(ADDRESS); + // TODO How can the registry first return null then destination for the same path? + EasyMock.expect(registry.getDestinationForPath(ADDRESS)).andReturn(null); + EasyMock.expect(registry.getDestinationForPath(ADDRESS)).andReturn(destination); + EasyMock.expect(registry.checkRestfulRequest(EasyMock.isA(String.class))).andReturn(destination); + EasyMock.expect(destination.getMessageObserver()).andReturn(observer); + endpoint.addExtensor(extensor); + extensor.setLocation(EasyMock.eq(ROOT + "/cxf/Soap" + ADDRESS)); + EasyMock.expectLastCall(); control.replay(); - - OsgiServlet servlet = setUpServlet(); - + OsgiServlet servlet = new OsgiServlet(registry); + servlet.init(config); servlet.invoke(request, response); - control.verify(); } @@ -249,42 +243,12 @@ public class OsgiServletTest extends Ass for (int i = 0; i < destinationCount; i++) { destinations.add(destination); } - EasyMock.expect(registry.getDestinations()).andReturn(destinations); + EasyMock.expect(registry.getDestinations()).andReturn(destinations).anyTimes(); + EasyMock.expect(registry.getSortedDestinations()). + andReturn(destinations.toArray(new AbstractDestination[]{})).anyTimes(); } } - private void setUpMessage() throws Exception { - ServletInputStream sis = control.createMock(ServletInputStream.class); - EasyMock.expect(request.getInputStream()).andReturn(sis); - message.setContent(EasyMock.eq(InputStream.class), EasyMock.same(sis)); - EasyMock.expectLastCall(); - setUpProperty(AbstractHTTPDestination.HTTP_REQUEST, request); - setUpProperty(AbstractHTTPDestination.HTTP_RESPONSE, response); - setUpProperty(AbstractHTTPDestination.HTTP_CONTEXT, context); - setUpProperty(AbstractHTTPDestination.HTTP_CONFIG, config); - EasyMock.expect(request.getMethod()).andReturn(VERB); - setUpProperty(Message.HTTP_REQUEST_METHOD, VERB); - setUpProperty(Message.REQUEST_URI, URI); - setUpProperty(Message.QUERY_STRING, QUERY); - EasyMock.expect(request.getContentType()).andReturn(XML); - setUpProperty(Message.CONTENT_TYPE, XML); - EasyMock.expect(request.getHeader("Accept")).andReturn(XML); - setUpProperty(Message.ACCEPT_CONTENT_TYPE, XML); - destination.getAddress(); - EasyMock.expectLastCall().andReturn(EndpointReferenceUtils.getEndpointReference(PATH)); - setUpProperty(Message.BASE_PATH, PATH); - message.put(EasyMock.eq(SecurityContext.class), EasyMock.isA(SecurityContext.class)); - EasyMock.expect(request.getCharacterEncoding()).andReturn(ENCODING); - setUpProperty(Message.ENCODING, ENCODING); - exchange.setSession(EasyMock.isA(HTTPSession.class)); - EasyMock.expectLastCall(); - } - - private void setUpProperty(String name, Object value) { - message.put(EasyMock.eq(name), EasyMock.same(value)); - EasyMock.expectLastCall().andReturn(null).anyTimes(); - } - private void setUpResponse(int status, String responseType, String ... responseMsgs) throws Exception { @@ -328,23 +292,4 @@ public class OsgiServletTest extends Ass EasyMock.expectLastCall(); } - private void setUpRestful() { - paths.add(ADDRESS); - EasyMock.expect(registry.getDestinationForPath(ADDRESS)).andReturn(null); - EasyMock.expect(registry.getDestinationForPath(ADDRESS)).andReturn(destination).times(2); - EasyMock.expect(destination.getMessageObserver()).andReturn(observer); - endpoint.addExtensor(extensor); - extensor.setLocation(EasyMock.eq(ROOT + "/cxf/Soap" + ADDRESS)); - EasyMock.expectLastCall(); - } - - private OsgiServlet setUpServlet() { - OsgiServlet servlet = new OsgiServlet(registry); - try { - servlet.init(config); - } catch (ServletException ex) { - // ignore - } - return servlet; - } } Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java Sun Dec 12 20:30:16 2010 @@ -135,6 +135,10 @@ public abstract class AbstractHTTPDestin initConfig(); } + public Bus getBus() { + return bus; + } + private AuthorizationPolicy getAuthorizationPolicyFromMessage(String credentials) { if (credentials == null || StringUtils.isEmpty(credentials.trim())) { return null; @@ -169,12 +173,6 @@ public abstract class AbstractHTTPDestin return ex == null ? false : ex.isOneWay(); } - public void invoke(final ServletContext context, - final HttpServletRequest req, - final HttpServletResponse resp) throws IOException { - invoke(null, context, req, resp); - } - public void invoke(final ServletConfig config, final ServletContext context, final HttpServletRequest req, Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistry.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistry.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistry.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistry.java Sun Dec 12 20:30:16 2010 @@ -19,9 +19,10 @@ package org.apache.cxf.transport.http; import java.util.Collection; -import java.util.List; import java.util.Set; +import org.apache.cxf.transport.AbstractDestination; + public interface DestinationRegistry { void addDestination(String path, AbstractHTTPDestination destination); @@ -36,7 +37,7 @@ public interface DestinationRegistry { Collection getDestinations(); - List getSortedDestinations(); + AbstractDestination[] getSortedDestinations(); Set getDestinationsPaths(); Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java Sun Dec 12 20:30:16 2010 @@ -30,6 +30,8 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import org.apache.cxf.transport.AbstractDestination; + public class DestinationRegistryImpl implements DestinationRegistry { private ConcurrentMap destinations @@ -94,7 +96,7 @@ public class DestinationRegistryImpl imp } - public List getSortedDestinations() { + public AbstractDestination[] getSortedDestinations() { List dest2 = new LinkedList( getDestinations()); Collections.sort(dest2, new Comparator() { @@ -112,7 +114,7 @@ public class DestinationRegistryImpl imp } }); - return dest2; + return dest2.toArray(new AbstractDestination[]{}); } public Set getDestinationsPaths() { Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractCXFServlet.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractCXFServlet.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractCXFServlet.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractCXFServlet.java Sun Dec 12 20:30:16 2010 @@ -25,6 +25,7 @@ import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -34,6 +35,7 @@ import org.apache.cxf.BusFactory; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.transport.DestinationFactory; import org.apache.cxf.transport.DestinationFactoryManager; +import org.apache.cxf.transport.servlet.servicelist.ServiceListGeneratorServlet; @@ -53,10 +55,12 @@ public abstract class AbstractCXFServlet } public ServletController createServletController(ServletConfig servletConfig) { + HttpServlet serviceListGeneratorServlet = + new ServiceListGeneratorServlet(servletTransportFactory.getRegistry(), bus); ServletController newController = new ServletController(servletTransportFactory.getRegistry(), servletConfig, - bus); + serviceListGeneratorServlet); servletTransportFactory.setServletController(newController); if (servletConfig.getInitParameter("disable-address-updates") == null) { newController.setDisableAddressUpdates(disableAddressUpdates); Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java Sun Dec 12 20:30:16 2010 @@ -20,17 +20,23 @@ package org.apache.cxf.transport.servlet; import java.io.IOException; +import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.common.util.UrlUtils; +import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.transport.http.AbstractHTTPDestination; +import org.apache.cxf.transport.http.DestinationRegistry; +import org.apache.cxf.transports.http.QueryHandler; +import org.apache.cxf.transports.http.QueryHandlerRegistry; public abstract class AbstractServletController { protected static final String DEFAULT_LISTINGS_CLASSIFIER = "/services"; @@ -43,13 +49,15 @@ public abstract class AbstractServletCon protected String title; protected String serviceListRelativePath = DEFAULT_LISTINGS_CLASSIFIER; protected ServletConfig servletConfig; - - protected AbstractServletController() { - - } - - protected AbstractServletController(ServletConfig config) { + protected DestinationRegistry destinationRegistry; + protected HttpServlet serviceListGenerator; + + protected AbstractServletController(ServletConfig config, + DestinationRegistry destinationRegistry, + HttpServlet serviceListGenerator) { this.servletConfig = config; + this.destinationRegistry = destinationRegistry; + this.serviceListGenerator = serviceListGenerator; init(); } @@ -81,27 +89,24 @@ public abstract class AbstractServletCon String hideServiceList = servletConfig.getInitParameter("hide-service-list-page"); if (hideServiceList != null) { - isHideServiceList = Boolean.valueOf(hideServiceList); + this.isHideServiceList = Boolean.valueOf(hideServiceList); } String isDisableAddressUpdates = servletConfig.getInitParameter("disable-address-updates"); if (isDisableAddressUpdates != null) { - disableAddressUpdates = Boolean.valueOf(isDisableAddressUpdates); + this.disableAddressUpdates = Boolean.valueOf(isDisableAddressUpdates); } String isForcedBaseAddress = servletConfig.getInitParameter("base-address"); if (isForcedBaseAddress != null) { - forcedBaseAddress = isForcedBaseAddress; + this.forcedBaseAddress = isForcedBaseAddress; } - String serviceListTransform = servletConfig.getInitParameter("service-list-stylesheet"); - if (serviceListTransform != null) { - serviceListStyleSheet = serviceListTransform; + try { + serviceListGenerator.init(servletConfig); + } catch (ServletException e) { + throw new RuntimeException(e.getMessage(), e); } String serviceListPath = servletConfig.getInitParameter("service-list-path"); if (serviceListPath != null) { - serviceListRelativePath = serviceListPath; - } - String configTitle = servletConfig.getInitParameter("service-list-title"); - if (configTitle != null) { - title = configTitle; + this.serviceListRelativePath = serviceListPath; } } @@ -154,7 +159,44 @@ public abstract class AbstractServletCon LOG.fine("Finished servicing http request on thread: " + Thread.currentThread()); } } + } + + protected QueryHandler findQueryHandler(QueryHandlerRegistry queryHandlerRegistry, + EndpointInfo ei, + String ctxUri, + String baseUri) { + if (queryHandlerRegistry == null) { + return null; + } + Iterable handlers = queryHandlerRegistry.getHandlers(); + for (QueryHandler qh : handlers) { + if (qh.isRecognizedQuery(baseUri, ctxUri, ei)) { + return qh; + } + } + return null; + } + protected void respondUsingQueryHandler(QueryHandler selectedHandler, HttpServletResponse res, + EndpointInfo ei, String ctxUri, String baseUri) throws IOException, + ServletException { + res.setContentType(selectedHandler.getResponseContentType(baseUri, ctxUri)); + OutputStream out = res.getOutputStream(); + try { + selectedHandler.writeResponse(baseUri, ctxUri, ei, out); + out.flush(); + } catch (Exception e) { + LOG.warning(selectedHandler.getClass().getName() + + " Exception caught writing response: " + + e.getMessage()); + throw new ServletException(e); + } } + protected void generateNotFound(HttpServletRequest request, HttpServletResponse res) throws IOException { + res.setStatus(404); + res.setContentType("text/html"); + res.getWriter().write("No service was found."); + } + } Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java?rev=1044907&r1=1044906&r2=1044907&view=diff ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java (original) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java Sun Dec 12 20:30:16 2010 @@ -20,27 +20,21 @@ package org.apache.cxf.transport.servlet import java.io.IOException; import java.io.OutputStream; -import java.io.PrintWriter; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.cxf.Bus; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.common.util.StringUtils; -import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.service.model.EndpointInfo; -import org.apache.cxf.service.model.OperationInfo; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.cxf.transports.http.QueryHandler; @@ -49,19 +43,13 @@ import org.apache.cxf.wsdl.http.AddressT public class ServletController extends AbstractServletController { - private static final Logger LOG = LogUtils.getL7dLogger(ServletController.class); - - private Bus bus; + private static final Logger LOG = LogUtils.getL7dLogger(ServletController.class); private volatile String lastBase = ""; - - private final DestinationRegistry destinationRegistry; public ServletController(DestinationRegistry destinationRegistry, - ServletConfig config, - Bus b) { - super(config); - this.destinationRegistry = destinationRegistry; - this.bus = b; + ServletConfig config, + HttpServlet serviceListGeneratorServlet) { + super(config, destinationRegistry, serviceListGeneratorServlet); } String getLastBaseURL() { @@ -121,14 +109,7 @@ public class ServletController extends A || StringUtils.isEmpty(request.getPathInfo()) || "/".equals(request.getPathInfo()))) { updateDests(request); - - if (request.getParameter("stylesheet") != null) { - renderStyleSheet(request, res); - } else if ("false".equals(request.getParameter("formatted"))) { - generateUnformattedServiceList(request, res); - } else { - generateServiceList(request, res); - } + serviceListGenerator.service(request, res); } else { d = destinationRegistry.checkRestfulRequest(address); if (d == null) { @@ -142,7 +123,7 @@ public class ServletController extends A } } else { ei = d.getEndpointInfo(); - + Bus bus = d.getBus(); if ("GET".equals(request.getMethod()) && null != request.getQueryString() && request.getQueryString().length() > 0 @@ -189,221 +170,4 @@ public class ServletController extends A } } - - - @SuppressWarnings("unchecked") - protected void generateServiceList(HttpServletRequest request, HttpServletResponse response) - throws IOException { - response.setContentType("text/html; charset=UTF-8"); - - response.getWriter().write(""); - response.getWriter().write(""); - if (serviceListStyleSheet != null) { - response.getWriter().write( - ""); - } else { - response.getWriter().write( - ""); - } - response.getWriter().write(""); - if (title != null) { - response.getWriter().write("" + title + ""); - } else { - response.getWriter().write("CXF - Service list"); - } - response.getWriter().write(""); - - List destinations = destinationRegistry.getSortedDestinations(); - - if (destinations.size() > 0) { - List privateEndpoints = - (List)bus.getProperty("org.apache.cxf.private.endpoints"); - //TODO : we may introduce a bus extension instead - Map atomMap = - (Map)bus.getProperty("org.apache.cxf.extensions.logging.atom.pull"); - writeSOAPEndpoints(response, destinations, privateEndpoints, atomMap); - writeRESTfulEndpoints(response, destinations, privateEndpoints, atomMap); - } else { - response.getWriter().write("No services have been found."); - } - - response.getWriter().write(""); - } - - private void writeSOAPEndpoints(HttpServletResponse response, List destinations, - List privateEndpoints, Map atomMap) - throws IOException { - response.getWriter().write("Available SOAP services:
"); - response.getWriter().write(""); - for (AbstractHTTPDestination sd : destinations) { - - if (null != sd.getEndpointInfo().getName() - && null != sd.getEndpointInfo().getInterface() - && !isPrivate(sd.getEndpointInfo(), privateEndpoints)) { - response.getWriter().write(""); - } - } - response.getWriter().write("
"); - response.getWriter().write("" - + sd.getEndpointInfo().getInterface().getName().getLocalPart() - + ""); - response.getWriter().write("
    "); - for (OperationInfo oi : sd.getEndpointInfo().getInterface().getOperations()) { - if (oi.getProperty("operation.is.synthetic") != Boolean.TRUE) { - response.getWriter().write("
  • " + oi.getName().getLocalPart() + "
  • "); - } - } - response.getWriter().write("
"); - response.getWriter().write("
"); - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write("Endpoint address: " - + "" + address + ""); - response.getWriter().write("
WSDL : " - + "" - + sd.getEndpointInfo().getService().getName() + ""); - response.getWriter().write("
Target namespace: " - + "" - + sd.getEndpointInfo().getService().getTargetNamespace() + ""); - addAtomLinkIfNeeded(address, atomMap, response.getWriter()); - response.getWriter().write("


"); - } - - - private void writeRESTfulEndpoints(HttpServletResponse response, - List destinations, - List privateEndpoints, - Map atomMap) - throws IOException { - - List restfulDests = new ArrayList(); - for (AbstractHTTPDestination sd : destinations) { - // use some more reasonable check - though this one seems to be the only option at the moment - if (null == sd.getEndpointInfo().getInterface() - && !isPrivate(sd.getEndpointInfo(), privateEndpoints)) { - restfulDests.add(sd); - } - } - if (restfulDests.size() == 0) { - return; - } - - response.getWriter().write("Available RESTful services:
"); - response.getWriter().write(""); - for (AbstractHTTPDestination sd : restfulDests) { - response.getWriter().write(""); - } - response.getWriter().write("
"); - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write("Endpoint address: " - + "" + address + ""); - response.getWriter().write("
WADL : " - + "" - + address + "?_wadl&type=xml" + ""); - addAtomLinkIfNeeded(address, atomMap, response.getWriter()); - response.getWriter().write("
"); - } - - private boolean isPrivate(EndpointInfo ei, List privateAddresses) { - if (privateAddresses != null) { - for (String s : privateAddresses) { - if (ei.getAddress().endsWith(s)) { - return true; - } - } - } - return false; - } - - private static void addAtomLinkIfNeeded(String address, Map extMap, - PrintWriter pw) { - String atomAddress = getExtensionEndpointAddress(address, extMap); - if (atomAddress != null) { - pw.write("
Atom Log Feed : " - + "" + atomAddress + ""); - } - } - - private static String getExtensionEndpointAddress(String endpointAddress, Map extMap) { - if (extMap != null) { - for (Map.Entry entry : extMap.entrySet()) { - if (endpointAddress.endsWith(entry.getKey())) { - endpointAddress = - endpointAddress.substring(0, endpointAddress.length() - entry.getKey().length()); - endpointAddress += entry.getValue(); - return endpointAddress; - } - } - } - return null; - } - - private void renderStyleSheet(HttpServletRequest request, - HttpServletResponse response) throws IOException { - response.setContentType("text/css; charset=UTF-8"); - - URL url = this.getClass().getResource("servicelist.css"); - if (url != null) { - IOUtils.copy(url.openStream(), response.getOutputStream()); - } - } - - - protected void generateUnformattedServiceList(HttpServletRequest request, - HttpServletResponse response) throws IOException { - response.setContentType("text/plain; charset=UTF-8"); - - List destinations = destinationRegistry.getSortedDestinations(); - if (destinations.size() > 0) { - writeUnformattedSOAPEndpoints(response, destinations, request.getParameter("wsdlList")); - writeUnformattedRESTfulEndpoints(response, destinations); - } else { - response.getWriter().write("No services have been found."); - } - } - - private void writeUnformattedSOAPEndpoints(HttpServletResponse response, - List destinations, - Object renderParam) - throws IOException { - boolean renderWsdlList = "true".equals(renderParam); - - for (AbstractHTTPDestination sd : destinations) { - - if (null != sd.getEndpointInfo().getInterface()) { - - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write(address); - - if (renderWsdlList) { - response.getWriter().write("?wsdl"); - } - response.getWriter().write('\n'); - } - } - response.getWriter().write('\n'); - } - - private void writeUnformattedRESTfulEndpoints(HttpServletResponse response, - List destinations) - throws IOException { - for (AbstractHTTPDestination sd : destinations) { - if (null == sd.getEndpointInfo().getInterface()) { - String address = sd.getEndpointInfo().getAddress(); - response.getWriter().write(address + "?_wadl&_type=xml"); - response.getWriter().write('\n'); - } - } - } - - protected void generateNotFound(HttpServletRequest request, HttpServletResponse res) throws IOException { - res.setStatus(404); - res.setContentType("text/html"); - res.getWriter().write("No service was found."); - } - } Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/FormattedServiceListWriter.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/FormattedServiceListWriter.java?rev=1044907&view=auto ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/FormattedServiceListWriter.java (added) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/FormattedServiceListWriter.java Sun Dec 12 20:30:16 2010 @@ -0,0 +1,148 @@ +/** + * 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.servlet.servicelist; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Map; + +import org.apache.cxf.service.model.OperationInfo; +import org.apache.cxf.transport.AbstractDestination; + +public class FormattedServiceListWriter implements ServiceListWriter { + private String styleSheetPath; + private String title; + private Map atomMap; + + public FormattedServiceListWriter(String styleSheetPath, + String title, + Map atomMap) { + this.styleSheetPath = styleSheetPath; + this.title = title; + this.atomMap = atomMap; + } + + public String getContentType() { + return "text/html; charset=UTF-8"; + } + + public void writeServiceList(PrintWriter writer, + AbstractDestination[] soapDestinations, + AbstractDestination[] restDestinations) throws IOException { + writer.write(""); + writer.write(""); + writer.write(""); + writer.write(""); + if (title != null) { + writer.write("" + title + ""); + } else { + writer.write("CXF - Service list"); + } + writer.write(""); + + if (soapDestinations.length > 0 || restDestinations.length > 0) { + writeSOAPEndpoints(writer, soapDestinations); + writeRESTfulEndpoints(writer, restDestinations); + } else { + writer.write("No services have been found."); + } + + writer.write(""); + } + + private void writeSOAPEndpoints(PrintWriter writer, + AbstractDestination[] destinations) + throws IOException { + writer.write("Available SOAP services:
"); + writer.write(""); + for (AbstractDestination sd : destinations) { + writerSoapEndpoint(writer, sd); + } + writer.write("


"); + } + + private void writerSoapEndpoint(PrintWriter writer, AbstractDestination sd) { + writer.write(""); + writer.write("" + + sd.getEndpointInfo().getInterface().getName().getLocalPart() + ""); + writer.write("
    "); + for (OperationInfo oi : sd.getEndpointInfo().getInterface().getOperations()) { + if (oi.getProperty("operation.is.synthetic") != Boolean.TRUE) { + writer.write("
  • " + oi.getName().getLocalPart() + "
  • "); + } + } + writer.write("
"); + writer.write(""); + String address = sd.getEndpointInfo().getAddress(); + writer.write("Endpoint address: " + "" + + address + ""); + writer.write("
WSDL : " + "" + sd.getEndpointInfo().getService().getName() + ""); + writer.write("
Target namespace: " + + "" + + sd.getEndpointInfo().getService().getTargetNamespace() + ""); + addAtomLinkIfNeeded(address, atomMap, writer); + writer.write(""); + } + + private void writeRESTfulEndpoints(PrintWriter writer, AbstractDestination[] restfulDests) + throws IOException { + writer.write("Available RESTful services:
"); + writer.write(""); + for (AbstractDestination sd : restfulDests) { + writeRESTfulEndpoint(writer, sd); + } + writer.write("
"); + } + + private void writeRESTfulEndpoint(PrintWriter writer, AbstractDestination sd) { + writer.write(""); + String address = sd.getEndpointInfo().getAddress(); + writer.write("Endpoint address: " + "" + + address + ""); + writer.write("
WADL : " + "" + address + "?_wadl&type=xml" + ""); + addAtomLinkIfNeeded(address, atomMap, writer); + writer.write(""); + } + + private static void addAtomLinkIfNeeded(String address, Map extMap, PrintWriter pw) { + String atomAddress = getExtensionEndpointAddress(address, extMap); + if (atomAddress != null) { + pw.write("
Atom Log Feed : " + "" + atomAddress + ""); + } + } + + private static String getExtensionEndpointAddress(String endpointAddress, Map extMap) { + if (extMap != null) { + for (Map.Entry entry : extMap.entrySet()) { + if (endpointAddress.endsWith(entry.getKey())) { + endpointAddress = endpointAddress.substring(0, endpointAddress.length() + - entry.getKey().length()); + endpointAddress += entry.getValue(); + return endpointAddress; + } + } + } + return null; + } + +} Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListGeneratorServlet.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListGeneratorServlet.java?rev=1044907&view=auto ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListGeneratorServlet.java (added) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListGeneratorServlet.java Sun Dec 12 20:30:16 2010 @@ -0,0 +1,175 @@ +/** + * 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.servlet.servicelist; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.cxf.Bus; +import org.apache.cxf.helpers.IOUtils; +import org.apache.cxf.service.model.EndpointInfo; +import org.apache.cxf.transport.AbstractDestination; +import org.apache.cxf.transport.http.DestinationRegistry; + +public class ServiceListGeneratorServlet extends HttpServlet { + private DestinationRegistry destinationRegistry; + private Bus bus; + private String serviceListStyleSheet; + private String title; + + public ServiceListGeneratorServlet(DestinationRegistry destinationRegistry, Bus bus) { + this.destinationRegistry = destinationRegistry; + this.bus = bus; + this.title = "CXF - Service list"; + } + + public void setServiceListStyleSheet(String serviceListStyleSheet) { + this.serviceListStyleSheet = serviceListStyleSheet; + } + + public void setTitle(String title) { + this.title = title; + } + + + @SuppressWarnings("unchecked") + @Override + public void service(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + PrintWriter writer = response.getWriter(); + AbstractDestination[] destinations = destinationRegistry.getSortedDestinations(); + if (request.getParameter("stylesheet") != null) { + renderStyleSheet(request, response); + return; + } + List privateEndpoints; + Map atomMap; + + if (bus != null) { + privateEndpoints = (List)bus.getProperty("org.apache.cxf.private.endpoints"); + // TODO : we may introduce a bus extension instead + + atomMap = (Map)bus + .getProperty("org.apache.cxf.extensions.logging.atom.pull"); + } else { + privateEndpoints = new ArrayList(); + atomMap = new HashMap(); + } + + AbstractDestination[] soapEndpoints = getSOAPEndpoints(destinations, privateEndpoints); + AbstractDestination[] restEndpoints = getRestEndpoints(destinations, privateEndpoints); + ServiceListWriter serviceListWriter; + if ("false".equals(request.getParameter("formatted"))) { + boolean renderWsdlList = "true".equals(request.getParameter("wsdlList")); + serviceListWriter = new UnformattedServiceListWriter(renderWsdlList); + } else { + String styleSheetPath; + if (serviceListStyleSheet != null) { + styleSheetPath = request.getContextPath() + "/" + serviceListStyleSheet; + + } else { + styleSheetPath = request.getRequestURI() + "/?stylesheet=1"; + } + serviceListWriter = new FormattedServiceListWriter(styleSheetPath, title, atomMap); + + } + response.setContentType(serviceListWriter.getContentType()); + serviceListWriter.writeServiceList(writer, soapEndpoints, restEndpoints); + } + + + private boolean isPrivate(EndpointInfo ei, List privateEndpoints) { + if (privateEndpoints != null) { + for (String s : privateEndpoints) { + if (ei.getAddress().endsWith(s)) { + return true; + } + } + } + return false; + } + + private AbstractDestination[] getSOAPEndpoints(AbstractDestination[] destinations, + List privateEndpoints) { + List soapEndpoints = new ArrayList(); + for (AbstractDestination sd : destinations) { + if (null != sd.getEndpointInfo().getName() && null != sd.getEndpointInfo().getInterface() + && !isPrivate(sd.getEndpointInfo(), privateEndpoints)) { + soapEndpoints.add(sd); + } + } + return soapEndpoints.toArray(new AbstractDestination[]{}); + } + + private AbstractDestination[] getRestEndpoints(AbstractDestination[] destinations, + List privateEndpoints) { + List restfulDests = new ArrayList(); + for (AbstractDestination sd : destinations) { + // use some more reasonable check - though this one seems to be the only option at the moment + if (null == sd.getEndpointInfo().getInterface() + && !isPrivate(sd.getEndpointInfo(), privateEndpoints)) { + restfulDests.add(sd); + } + } + return restfulDests.toArray(new AbstractDestination[]{}); + } + + + private void renderStyleSheet(HttpServletRequest request, HttpServletResponse response) + throws IOException { + response.setContentType("text/css; charset=UTF-8"); + URL url = this.getClass().getResource("servicelist.css"); + if (url != null) { + IOUtils.copy(url.openStream(), response.getOutputStream()); + } + } + + public void init(ServletConfig servletConfig) { + String configServiceListStyleSheet = servletConfig.getInitParameter("service-list-stylesheet"); + if (configServiceListStyleSheet != null) { + this.serviceListStyleSheet = configServiceListStyleSheet; + } + String configTitle = servletConfig.getInitParameter("service-list-title"); + if (configTitle != null) { + this.title = configTitle; + } + } + + public ServletConfig getServletConfig() { + return null; + } + + public String getServletInfo() { + return null; + } + + public void destroy() { + } +} Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListWriter.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListWriter.java?rev=1044907&view=auto ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListWriter.java (added) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/ServiceListWriter.java Sun Dec 12 20:30:16 2010 @@ -0,0 +1,32 @@ +/** + * 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.servlet.servicelist; + +import java.io.IOException; +import java.io.PrintWriter; + +import org.apache.cxf.transport.AbstractDestination; + +public interface ServiceListWriter { + String getContentType(); + void writeServiceList(PrintWriter writer, + AbstractDestination[] soapDestinations, + AbstractDestination[] restDestinations) throws IOException; + +} Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/UnformattedServiceListWriter.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/UnformattedServiceListWriter.java?rev=1044907&view=auto ============================================================================== --- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/UnformattedServiceListWriter.java (added) +++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/servicelist/UnformattedServiceListWriter.java Sun Dec 12 20:30:16 2010 @@ -0,0 +1,70 @@ +/** + * 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.servlet.servicelist; + +import java.io.IOException; +import java.io.PrintWriter; + +import org.apache.cxf.transport.AbstractDestination; + +public class UnformattedServiceListWriter implements ServiceListWriter { + boolean renderWsdlList; + + public UnformattedServiceListWriter(boolean renderWsdlList) { + this.renderWsdlList = renderWsdlList; + } + + public String getContentType() { + return "text/plain; charset=UTF-8"; + } + + public void writeServiceList(PrintWriter writer, + AbstractDestination[] soapDestinations, + AbstractDestination[] restDestinations) throws IOException { + if (soapDestinations.length > 0 && restDestinations.length > 0) { + writeUnformattedSOAPEndpoints(writer, soapDestinations); + writeUnformattedRESTfulEndpoints(writer, restDestinations); + } else { + writer.write("No services have been found."); + } + } + + private void writeUnformattedSOAPEndpoints(PrintWriter writer, + AbstractDestination[] destinations) throws IOException { + for (AbstractDestination sd : destinations) { + String address = sd.getEndpointInfo().getAddress(); + writer.write(address); + + if (renderWsdlList) { + writer.write("?wsdl"); + } + writer.write('\n'); + } + writer.write('\n'); + } + + private void writeUnformattedRESTfulEndpoints(PrintWriter writer, + AbstractDestination[] destinations) throws IOException { + for (AbstractDestination sd : destinations) { + String address = sd.getEndpointInfo().getAddress(); + writer.write(address + "?_wadl&_type=xml\n"); + } + } + +}