Skip to main content

Dynamically configure StreamingAttachment properties

1 reply [Last post]
DimaA6_ABC
Offline
Joined: 2012-02-01
Points: 0

I'm developing JAX-WS web service. My Java class looks like (with annotations):

@MTOM
@WebService(endpointInterface = "mtomtest.wsserver.mtomserver")
@StreamingAttachment(parseEagerly=true, memoryThreshold=4000000L, dir="D:\\projects\\123\\files\\temp")
public class mtomserverImpl implements mtomserver {

Note hardcoded values for StreamingAttachment annotation. I want to be able to dynamically initialize those values from my code - any way of dong that? So I want to do something like (on server):

configureMyself() {
setMemoryThreshold(12345);
setTempDir("c:\\mydirectory");
}

Is this possible with JAX-WS RI?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
DimaA6_ABC
Offline
Joined: 2012-02-01
Points: 0

Found a way which does not involve modifications of JAX-WS classes.

  1. Inherit from WSServlet:

public class WSServletWithStreamingAttachmentInit extends WSServlet {

2. Override init method:

@Override public void init(ServletConfig servletConfig) throws ServletException {

super.init(servletConfig);

// somehow obtain streaming attachment parameters you want to override, I loaded them from servlet config

// now call the method to override those params

modifyStreamingAttachmentFeature(dir, nThreshold);

}

3. Finally the method to override params:

protected void modifyStreamingAttachmentFeature(String directory, long threshold) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

// tricky part - delegate field of WSServlet class is private. Use reflection to access it

Field fld = WSServlet.class.getDeclaredField("delegate");

fld.setAccessible(true);

WSServletDelegate dlgt = (WSServletDelegate)fld.get(this);

if (dlgt.adapters != null) {

for (ServletAdapter adapter : dlgt.adapters) {

WSBinding binding = adapter.getEndpoint().getBinding();

WSFeatureList wsfl = binding.getFeatures();

for (WebServiceFeature ft: wsfl) {

if (ft instanceof StreamingAttachmentFeature) {

((StreamingAttachmentFeature)ft).setDir(directory);

((StreamingAttachmentFeature)ft).setMemoryThreshold(threshold);

4. In web.xml, specify your class as servlet rather than WSServlet (obviously include your class into your web app):

<servlet-class>mtomtest.wsserver.WSServletWithStreamingAttachmentInit</servlet-class>