Source code: org/apache/axis/encoding/ser/JAFDataHandlerSerializer.java
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.axis.encoding.ser;
18
19 import java.io.IOException;
20
21 import javax.activation.DataHandler;
22 import javax.xml.namespace.QName;
23
24 import org.apache.axis.Constants;
25 import org.apache.axis.Part;
26 import org.apache.axis.attachments.Attachments;
27 import org.apache.axis.components.logger.LogFactory;
28 import org.apache.axis.encoding.SerializationContext;
29 import org.apache.axis.encoding.Serializer;
30 import org.apache.axis.soap.SOAPConstants;
31 import org.apache.axis.utils.Messages;
32 import org.apache.axis.wsdl.fromJava.Types;
33 import org.apache.commons.logging.Log;
34 import org.w3c.dom.Element;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.helpers.AttributesImpl;
37
38 /**
39 * JAFDataHandler Serializer
40 * @author Rick Rineholt
41 * Modified by Rich Scheuerle <scheu@us.ibm.com>
42 */
43 public class JAFDataHandlerSerializer implements Serializer {
44
45 protected static Log log =
46 LogFactory.getLog(JAFDataHandlerSerializer.class.getName());
47
48 /**
49 * Serialize a JAF DataHandler quantity.
50 */
51 public void serialize(QName name, Attributes attributes,
52 Object value, SerializationContext context)
53 throws IOException
54 {
55 DataHandler dh= (DataHandler)value;
56 //Add the attachment content to the message.
57 Attachments attachments= context.getCurrentMessage().getAttachmentsImpl();
58
59 if (attachments == null) {
60 // Attachments apparently aren't supported.
61 // Instead of throwing NullPointerException like
62 // we used to do, throw something meaningful.
63 throw new IOException(Messages.getMessage("noAttachments"));
64 }
65 SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants();
66 Part attachmentPart= attachments.createAttachmentPart(dh);
67
68 AttributesImpl attrs = new AttributesImpl();
69 if (attributes != null && 0 < attributes.getLength())
70 attrs.setAttributes(attributes); //copy the existing ones.
71
72 int typeIndex=-1;
73 if((typeIndex = attrs.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI,
74 "type")) != -1){
75
76 //Found a xsi:type which should not be there for attachments.
77 attrs.removeAttribute(typeIndex);
78 }
79
80 boolean doTheDIME = false;
81 if(attachments.getSendType() == Attachments.SEND_TYPE_DIME)
82 doTheDIME = true;
83
84 attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(),
85 "CDATA", doTheDIME ? attachmentPart.getContentId() : attachmentPart.getContentIdRef() );
86
87 context.startElement(name, attrs);
88 context.endElement(); //There is no data to so end the element.
89 }
90
91 public String getMechanismType() { return Constants.AXIS_SAX; }
92
93 /**
94 * Return XML schema for the specified type, suitable for insertion into
95 * the <types> element of a WSDL document, or underneath an
96 * <element> or <attribute> declaration.
97 *
98 * @param javaType the Java Class we're writing out schema for
99 * @param types the Java2WSDL Types object which holds the context
100 * for the WSDL being generated.
101 * @return a type element containing a schema simpleType/complexType
102 * @see org.apache.axis.wsdl.fromJava.Types
103 */
104 public Element writeSchema(Class javaType, Types types) throws Exception {
105 return null;
106 }
107 }