You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Garry Watkins <ga...@gmail.com> on 2009/02/23 20:06:53 UTC

How do I setup up both a servlet engine and webservices that run on the same port?

I have an application that I need both a web server, and webservices
to run together on the same port.  How do I configure cxf to work?

Example

I need to run on http://192.168.10.100:9000/
	From this directory I will serve misc html and resources for a flex
application

Then I need to run two different web services

  http://192.168.10.100:9000/ws/SomeWebService1
  http://192.168.10.100:9000/ws/SomeWebService2

How do I set this up?

This is what I have so far.  Please help I have been beating myself up
all morning.  I would also prefer to do this without the spring
configuration.  I already have a zillion jars so I don't really want
that many more.

TIA

Garry

	private void setupHTTPServer(Properties props) throws Exception {
         SelectChannelConnector con = new SelectChannelConnector();
	    String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
	    con.setHost(ipAddress);

	    int port = 8080;
         try {
             port =
Integer.parseInt(props.getProperty("slx.http.port"));
         }
         catch(NumberFormatException nfe){
             // do nothing default to 8080
         }
	    	con.setPort(port);

	    	JettyHTTPServerEngineFactory sef = new
JettyHTTPServerEngineFactory();
	    	JettyHTTPServerEngine eng =
sef.createJettyHTTPServerEngine(port, "http");
	    	eng.setConnector(con);
	    	
	    	List<Handler> handlers = new ArrayList<Handler>();
		ResourceHandler resource_handler = new ResourceHandler();
		resource_handler.setResourceBase("../webServerRoot");
		handlers.add(resource_handler);

		ServletHandler servlet_handler = new ServletHandler();
		servlet_handler.addServletWithMapping(, "/MasterStatus");
		servlet_handler.addServletWithMapping(SendEmployeesServlet.class, "/
SendEmployees");

		handlers.add(servlet_handler);
		
		eng.setHandlers(handlers);
	}

	private void setupServices(Properties props) throws Exception {
		String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
		String port = props.getProperty(HTTP_PORT);		
		Enumeration<?> keys = props.keys();
		while(keys.hasMoreElements()){
			String key = (String)keys.nextElement();
			if (key.startsWith(WEB_SERVICE_URL)){
				String url = props.getProperty(key).replace("{http.ipAddress}",
ipAddress).replace("{http.port}", port);

				String subKey = key.replaceFirst(WEB_SERVICE_URL, "");
				String implKey = WEB_SERVICE_IMPL + subKey;
				String implClassName = props.getProperty(implKey);

				Class<?> klass = Class.forName(implClassName);
				Constructor<?> konstr = klass.getConstructor(new Class[]{});
				Object svcImpl = konstr.newInstance(new Object[]{});

				JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
			    svrFactory.setServiceClass(klass);
			    svrFactory.setAddress(url);
			    svrFactory.setServiceBean(svcImpl);

			    svrFactory.create();
			}
		}
	}

Re: How do I setup up both a servlet engine and webservices that run on the same port?

Posted by Benson Margulies <bi...@gmail.com>.
You can configure handlers for jetty that point one URL to CXF and
others to other servlets.

On Mon, Feb 23, 2009 at 2:06 PM, Garry Watkins <ga...@gmail.com> wrote:
> I have an application that I need both a web server, and webservices
> to run together on the same port.  How do I configure cxf to work?
>
> Example
>
> I need to run on http://192.168.10.100:9000/
>        From this directory I will serve misc html and resources for a flex
> application
>
> Then I need to run two different web services
>
>  http://192.168.10.100:9000/ws/SomeWebService1
>  http://192.168.10.100:9000/ws/SomeWebService2
>
> How do I set this up?
>
> This is what I have so far.  Please help I have been beating myself up
> all morning.  I would also prefer to do this without the spring
> configuration.  I already have a zillion jars so I don't really want
> that many more.
>
> TIA
>
> Garry
>
>        private void setupHTTPServer(Properties props) throws Exception {
>        SelectChannelConnector con = new SelectChannelConnector();
>            String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>            con.setHost(ipAddress);
>
>            int port = 8080;
>        try {
>            port =
> Integer.parseInt(props.getProperty("slx.http.port"));
>        }
>        catch(NumberFormatException nfe){
>            // do nothing default to 8080
>        }
>                con.setPort(port);
>
>                JettyHTTPServerEngineFactory sef = new
> JettyHTTPServerEngineFactory();
>                JettyHTTPServerEngine eng =
> sef.createJettyHTTPServerEngine(port, "http");
>                eng.setConnector(con);
>
>                List<Handler> handlers = new ArrayList<Handler>();
>                ResourceHandler resource_handler = new ResourceHandler();
>                resource_handler.setResourceBase("../webServerRoot");
>                handlers.add(resource_handler);
>
>                ServletHandler servlet_handler = new ServletHandler();
>                servlet_handler.addServletWithMapping(, "/MasterStatus");
>
>  servlet_handler.addServletWithMapping(SendEmployeesServlet.class, "/
> SendEmployees");
>
>                handlers.add(servlet_handler);
>
>                eng.setHandlers(handlers);
>        }
>
>        private void setupServices(Properties props) throws Exception {
>                String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>                String port = props.getProperty(HTTP_PORT);
>                Enumeration<?> keys = props.keys();
>                while(keys.hasMoreElements()){
>                        String key = (String)keys.nextElement();
>                        if (key.startsWith(WEB_SERVICE_URL)){
>                                String url =
> props.getProperty(key).replace("{http.ipAddress}",
> ipAddress).replace("{http.port}", port);
>
>                                String subKey =
> key.replaceFirst(WEB_SERVICE_URL, "");
>                                String implKey = WEB_SERVICE_IMPL + subKey;
>                                String implClassName =
> props.getProperty(implKey);
>
>                                Class<?> klass =
> Class.forName(implClassName);
>                                Constructor<?> konstr =
> klass.getConstructor(new Class[]{});
>                                Object svcImpl = konstr.newInstance(new
> Object[]{});
>
>                                JaxWsServerFactoryBean svrFactory = new
> JaxWsServerFactoryBean();
>                            svrFactory.setServiceClass(klass);
>                            svrFactory.setAddress(url);
>                            svrFactory.setServiceBean(svcImpl);
>
>                            svrFactory.create();
>                        }
>                }
>        }

Re: How do I setup up both a servlet engine and webservices that run on the same port?

Posted by David Bosschaert <da...@gmail.com>.
Hi Garry,

I wrote a blog post that explains how this can be done with the
Distributed OSGi implementation, that's based on CXF.
It serves a GWT webapp and a Web Service from the same machine+port
just different contexts. In my case they need to be served from the
same host + port because of the single origin policy that the browser
applies to the webapp.
You can find the blog post here:
http://coderthoughts.blogspot.com/2009/02/distributed-osgi-powered-ajax-webapp.html

It's achieved by using the CXFNonSpringServlet and registering that
with the OSGi HTTP Service. In a non-osgi case you could still use one
of the CXF Servlets and register them with your servlet engine.
For more info see:
http://svn.apache.org/repos/asf/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java

Hope this helps,

David

Re: How do I setup up both a servlet engine and webservices that run on the same port?

Posted by Garry Watkins <ga...@gmail.com>.
I needed it to be light weight because it is an agent program, that  
might be run on a standard user machine without a web server.

Regardless here is the code that works.

private void setupWebServices(Properties props) throws Exception {
         System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,  
BusFactory.DEFAULT_BUS_FACTORY);
         try {
             // Start up the jetty embedded server
     	    int port = 8080;
             try {
                 port =  
Integer.parseInt(props.getProperty("slx.http.port"));
             }
             catch(NumberFormatException nfe){
                 // do nothing default to 8080
             }
             server = new Server(port);

             ContextHandlerCollection contexts = new  
ContextHandlerCollection();
             server.setHandler(contexts);

             Context root = new Context(contexts, "/",  
Context.SESSIONS);

             root.setResourceBase("../webServerRoot");
             root.addServlet(DefaultServlet.class, "/*");

             CXFNonSpringServlet cxf = new CXFNonSpringServlet();
             ServletHolder servlet = new ServletHolder(cxf);
             servlet.setName("ws");
             servlet.setForcedPath("ws");
             root.addServlet(servlet, "/ws/*");

             server.start();

             Bus bus = cxf.getBus();
             BusFactory.setDefaultBus(bus);

             Enumeration<?> keys = props.keys();
     		while(keys.hasMoreElements()){
     			String key = (String)keys.nextElement();
     			if (key.startsWith(WEB_SERVICE_URL)){
     				String url = props.getProperty(key);
     				
     				String subKey = key.replaceFirst(WEB_SERVICE_URL, "");
     				String implKey = WEB_SERVICE_IMPL + subKey;
     				String implClassName = props.getProperty(implKey);

     				Class<?> klass = Class.forName(implClassName);
     				Constructor<?> konstr = klass.getConstructor(new Class[]{});
     				Object svcImpl = konstr.newInstance(new Object[]{});
     				Endpoint.publish(url, svcImpl);
     			}
     		}
         }
         catch (Exception e) {
             throw new RuntimeException(e);
         }
         finally {
             // clean up the system properties
             System.clearProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
         }
}

Thanks
Garry

On Feb 23, 2009, at 2:33 PM, Andrew Clegg wrote:

> Err... Why not host your cxf services as a servlet in Tomcat, and  
> run your other web site as the ROOT webapp?
>
> Or insert favourite servlet container above.
>
> Andrew.
>
>
> On 23 Feb 2009, at 19:06, Garry Watkins <ga...@gmail.com>  
> wrote:
>
>> I have an application that I need both a web server, and webservices
>> to run together on the same port.  How do I configure cxf to work?
>>
>> Example
>>
>> I need to run on http://192.168.10.100:9000/
>>   From this directory I will serve misc html and resources for a flex
>> application
>>
>> Then I need to run two different web services
>>
>> http://192.168.10.100:9000/ws/SomeWebService1
>> http://192.168.10.100:9000/ws/SomeWebService2
>>
>> How do I set this up?
>>
>> This is what I have so far.  Please help I have been beating myself  
>> up
>> all morning.  I would also prefer to do this without the spring
>> configuration.  I already have a zillion jars so I don't really want
>> that many more.
>>
>> TIA
>>
>> Garry
>>
>>   private void setupHTTPServer(Properties props) throws Exception {
>>       SelectChannelConnector con = new SelectChannelConnector();
>>       String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>>       con.setHost(ipAddress);
>>
>>       int port = 8080;
>>       try {
>>           port =
>> Integer.parseInt(props.getProperty("slx.http.port"));
>>       }
>>       catch(NumberFormatException nfe){
>>           // do nothing default to 8080
>>       }
>>           con.setPort(port);
>>
>>           JettyHTTPServerEngineFactory sef = new
>> JettyHTTPServerEngineFactory();
>>           JettyHTTPServerEngine eng =
>> sef.createJettyHTTPServerEngine(port, "http");
>>           eng.setConnector(con);
>>
>>           List<Handler> handlers = new ArrayList<Handler>();
>>       ResourceHandler resource_handler = new ResourceHandler();
>>       resource_handler.setResourceBase("../webServerRoot");
>>       handlers.add(resource_handler);
>>
>>       ServletHandler servlet_handler = new ServletHandler();
>>       servlet_handler.addServletWithMapping(, "/MasterStatus");
>>        
>> servlet_handler.addServletWithMapping(SendEmployeesServlet.class, "/
>> SendEmployees");
>>
>>       handlers.add(servlet_handler);
>>
>>       eng.setHandlers(handlers);
>>   }
>>
>>   private void setupServices(Properties props) throws Exception {
>>       String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>>       String port = props.getProperty(HTTP_PORT);
>>       Enumeration<?> keys = props.keys();
>>       while(keys.hasMoreElements()){
>>           String key = (String)keys.nextElement();
>>           if (key.startsWith(WEB_SERVICE_URL)){
>>               String url =  
>> props.getProperty(key).replace("{http.ipAddress}",
>> ipAddress).replace("{http.port}", port);
>>
>>               String subKey = key.replaceFirst(WEB_SERVICE_URL, "");
>>               String implKey = WEB_SERVICE_IMPL + subKey;
>>               String implClassName = props.getProperty(implKey);
>>
>>               Class<?> klass = Class.forName(implClassName);
>>               Constructor<?> konstr = klass.getConstructor(new  
>> Class[]{});
>>               Object svcImpl = konstr.newInstance(new Object[]{});
>>
>>               JaxWsServerFactoryBean svrFactory = new  
>> JaxWsServerFactoryBean();
>>               svrFactory.setServiceClass(klass);
>>               svrFactory.setAddress(url);
>>               svrFactory.setServiceBean(svcImpl);
>>
>>               svrFactory.create();
>>           }
>>       }
>>   }


Re: How do I setup up both a servlet engine and webservices that run on the same port?

Posted by Andrew Clegg <an...@nervechannel.com>.
Err... Why not host your cxf services as a servlet in Tomcat, and run  
your other web site as the ROOT webapp?

Or insert favourite servlet container above.

Andrew.


On 23 Feb 2009, at 19:06, Garry Watkins <ga...@gmail.com> wrote:

> I have an application that I need both a web server, and webservices
> to run together on the same port.  How do I configure cxf to work?
>
> Example
>
> I need to run on http://192.168.10.100:9000/
>    From this directory I will serve misc html and resources for a flex
> application
>
> Then I need to run two different web services
>
> http://192.168.10.100:9000/ws/SomeWebService1
> http://192.168.10.100:9000/ws/SomeWebService2
>
> How do I set this up?
>
> This is what I have so far.  Please help I have been beating myself up
> all morning.  I would also prefer to do this without the spring
> configuration.  I already have a zillion jars so I don't really want
> that many more.
>
> TIA
>
> Garry
>
>    private void setupHTTPServer(Properties props) throws Exception {
>        SelectChannelConnector con = new SelectChannelConnector();
>        String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>        con.setHost(ipAddress);
>
>        int port = 8080;
>        try {
>            port =
> Integer.parseInt(props.getProperty("slx.http.port"));
>        }
>        catch(NumberFormatException nfe){
>            // do nothing default to 8080
>        }
>            con.setPort(port);
>
>            JettyHTTPServerEngineFactory sef = new
> JettyHTTPServerEngineFactory();
>            JettyHTTPServerEngine eng =
> sef.createJettyHTTPServerEngine(port, "http");
>            eng.setConnector(con);
>
>            List<Handler> handlers = new ArrayList<Handler>();
>        ResourceHandler resource_handler = new ResourceHandler();
>        resource_handler.setResourceBase("../webServerRoot");
>        handlers.add(resource_handler);
>
>        ServletHandler servlet_handler = new ServletHandler();
>        servlet_handler.addServletWithMapping(, "/MasterStatus");
>         
> servlet_handler.addServletWithMapping(SendEmployeesServlet.class, "/
> SendEmployees");
>
>        handlers.add(servlet_handler);
>
>        eng.setHandlers(handlers);
>    }
>
>    private void setupServices(Properties props) throws Exception {
>        String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
>        String port = props.getProperty(HTTP_PORT);
>        Enumeration<?> keys = props.keys();
>        while(keys.hasMoreElements()){
>            String key = (String)keys.nextElement();
>            if (key.startsWith(WEB_SERVICE_URL)){
>                String url =  
> props.getProperty(key).replace("{http.ipAddress}",
> ipAddress).replace("{http.port}", port);
>
>                String subKey = key.replaceFirst(WEB_SERVICE_URL, "");
>                String implKey = WEB_SERVICE_IMPL + subKey;
>                String implClassName = props.getProperty(implKey);
>
>                Class<?> klass = Class.forName(implClassName);
>                Constructor<?> konstr = klass.getConstructor(new  
> Class[]{});
>                Object svcImpl = konstr.newInstance(new Object[]{});
>
>                JaxWsServerFactoryBean svrFactory = new  
> JaxWsServerFactoryBean();
>                svrFactory.setServiceClass(klass);
>                svrFactory.setAddress(url);
>                svrFactory.setServiceBean(svcImpl);
>
>                svrFactory.create();
>            }
>        }
>    }