View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.cxf;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  
13  import java.io.OutputStream;
14  import java.util.List;
15  
16  import javax.xml.bind.Marshaller;
17  import javax.xml.stream.XMLStreamException;
18  import javax.xml.stream.XMLStreamWriter;
19  
20  import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
21  import org.apache.cxf.interceptor.Fault;
22  import org.apache.cxf.jaxb.io.DataWriterImpl;
23  import org.apache.cxf.message.Exchange;
24  import org.apache.cxf.message.Message;
25  import org.apache.cxf.message.MessageContentsList;
26  import org.apache.cxf.phase.Phase;
27  import org.apache.cxf.service.Service;
28  import org.apache.cxf.service.model.BindingMessageInfo;
29  import org.apache.cxf.service.model.BindingOperationInfo;
30  import org.apache.cxf.service.model.MessagePartInfo;
31  
32  /**
33   * Jbi4Corba class to manage array marshalling errors (based on BareOutInterceptor CXF class).
34   * @author mpiraccini@imolinfo.it
35   */
36  @SuppressWarnings("unchecked")
37  public class Jbi4CorbaBareOutInterceptor extends
38  		AbstractOutDatabindingInterceptor {
39  		
40      /** The logger. */
41      private static final Logger LOG = 
42      	LoggerFactory.getLogger(Jbi4CorbaBareOutInterceptor.class);	
43  
44      /**
45       * Constructor.
46       */
47  	public Jbi4CorbaBareOutInterceptor() {
48  		super(Phase.MARSHAL);
49  		addAfter(Jbi4CorbaWrappedOutInterceptor.class.getName());
50  	}
51  	
52  	/**
53  	 * Constructor.	
54  	 * @param phase
55  	 */
56  	public Jbi4CorbaBareOutInterceptor(String phase) {
57          super(phase);
58          addAfter(Jbi4CorbaWrappedOutInterceptor.class.getName());
59      }
60  		
61  	public void handleMessage(Message message) {
62  		
63  		LOG.debug("handling message");
64  		Exchange exchange = message.getExchange();
65  		BindingOperationInfo operation = (BindingOperationInfo) exchange
66  				.get(BindingOperationInfo.class.getName());
67  
68  		if (operation == null) {
69  			return;
70  		}
71  
72  		MessageContentsList objs = MessageContentsList.getContentsList(message);
73  		if (objs == null || objs.size() == 0) {
74  			return;
75  		}
76  
77  		List<MessagePartInfo> parts = null;
78  		BindingMessageInfo bmsg = null;
79  		boolean client = isRequestor(message);
80  
81  		if (!client) {
82  			if (operation.getOutput() != null) {
83  				bmsg = operation.getOutput();
84  				parts = bmsg.getMessageParts();
85  			} else {
86  				// partial response to oneway
87  				return;
88  			}
89  		} else {
90  			// Input (for the consumer...)
91  			bmsg = operation.getInput();
92  			parts = bmsg.getMessageParts();
93  		}
94  
95  		Service service = exchange.get(Service.class);
96  		XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);        
97          XMLStreamWriter xmlWriter = origXmlWriter;
98          // If there's somthing before (the wrapper, if present) adds it.
99          if (xmlWriter != null) {
100             try {
101                 xmlWriter.writeCharacters("");
102                 xmlWriter.flush();
103             } catch (XMLStreamException e) {
104                 throw new Fault(e);
105             }
106         }
107         
108         OutputStream out = message.getContent(OutputStream.class);
109         if (out != null) {
110         
111         	DataWriterImpl writerImpl = (DataWriterImpl) getDataWriter(message, service, OutputStream.class);
112 		     	
113         	for (MessagePartInfo part : parts) {
114         		if (objs.hasValue(part)) {
115         			Object o = objs.get(part);
116         			Marshaller marshaller = writerImpl.createMarshaller(o, part);
117         			OutMarshallerHelper.marshal(marshaller, o, part, out);
118         		}
119         	}
120         } else {
121         	DataWriterImpl dataWriter = (DataWriterImpl) getDataWriter(message, service, XMLStreamWriter.class);            
122             for (MessagePartInfo part : parts) {
123                 if (objs.hasValue(part)) {
124                     Object o = objs.get(part);
125                     Marshaller marshaller = dataWriter.createMarshaller(o, part);
126                     OutMarshallerHelper.marshal(marshaller, o, part, xmlWriter);                    
127                 }
128             }
129         }	
130 	}
131 	
132 }