如何在Jetty中嵌入时将AxisConfiguration传递给AxisServlet

时间:2014-01-16 15:59:17

标签: java ssl axis2 embedded-jetty mutual-authentication

某些背景:

我正在尝试对我们公开的某些Axis2服务使用SSL相互身份验证。问题是Axis2内置服务器不支持它。所以我想使用嵌入式Jetty来实现它并在其中部署AxisServlet。

问题:

似乎没有办法直接将AxisConfiguration(包含所有可用服务的定义)传递给servlet。

我是如何尝试的:

ConfigurationContext context = ConfigurationContextFactory.createDefaultConfigurationContext();

File serviceArchiveFile = new File("<path to my aar file which is loaded properly>");
AxisServiceGroup serviceGroup = DeploymentEngine.loadServiceGroup(serviceArchiveFile, context);

AxisConfiguration axiConfiguration = context.getAxisConfiguration();
axiConfiguration.addServiceGroup(serviceGroup);

AxisServlet axisServlet = new AxisServlet();

Server server = new Server(8080);
org.mortbay.jetty.servlet.Context root = new org.mortbay.jetty.servlet.Context(server,"/",org.mortbay.jetty.servlet.Context.SESSIONS);
ServletHolder holder=new ServletHolder(axisServlet);

// Trying to pass the Config context via the parameters map
Map parameters = new HashMap();
parameters.put(AxisServlet.CONFIGURATION_CONTEXT, context);
holder.setInitParameters(parameters);

root.addServlet(holder,"/services/*");

server.start();

该方法的问题是AxisServlet的init(ServletConfig)方法永远不会被调用,因此每当我尝试在8080上调用该服务时,我都会得到一个nullPointer异常:

java.lang.NullPointerException
  at org.apache.axis2.transport.http.AxisServlet.initContextRoot(AxisServlet.java:586)
  at org.apache.axis2.transport.http.AxisServlet.preprocessRequest(AxisServlet.java:605)
  at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:241)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:442)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:356)
  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:226)
  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:615)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:141)
  at org.mortbay.jetty.Server.handle(Server.java:265)
  at org.mortbay.jetty.HttpConnection.handlerRequest(HttpConnection.java:420)
  at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:666)
  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:487)
  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:197)
  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:336)
  at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:183)
  at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:475)

根据Axis2源代码,这是因为配置上下文永远不会在AxisServlet中设置。所以现在最重要的问题是:

1)是否可以直接在AxisServlet中设置配置上下文?我没有找到任何方法这样做

2)为什么Jetty不发送init参数并且不在servlet上调用init(ServletConfig config)方法?我通过调试器确认它永远不会被调用。

2 个答案:

答案 0 :(得分:1)

如果要加载配置上下文,可以尝试直接从文件系统加载配置上下文,如下所示。

ConfigurationContext myConfigContext =             ConfigurationContextFactory.createConfigurationContextFromFileSystem(“”,“axis2.xml的绝对路径”);

更多细节: http://wso2.com/library/585/

我相信我已经回答了你的部分问题。

另外,要将axis2配置加载到AxisServlet,请将以下配置添加到您的web.xml

<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</
servlet-class>
<init-param>
<param-name>axis2.xml.url</param-name>
<param-value>path to you axis2.xml</param-value>
<param-name>axis2.repository.url</param-name>
<param-value>path to your repo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

答案 1 :(得分:0)

您应该使用 WebAppContext ,而不是使用简单的jetty Context 。这允许您添加axis2 ConfigurationContext jetty上下文。

像这样:

Server server = new Server(1111);
WebAppContext root = new WebAppContext();
root.setAttribute(AxisServlet.CONFIGURATION_CONTEXT, context);
AxisServlet s = new AxisServlet();
ServletHolder holder = new ServletHolder(s);
root.addServlet(holder, "/axis2/*");