001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.marshaller.impl.alt;
020:
021: import org.apache.axiom.om.util.UUIDGenerator;
022: import org.apache.axis2.jaxws.ExceptionFactory;
023: import org.apache.axis2.jaxws.description.AttachmentDescription;
024: import org.apache.axis2.jaxws.i18n.Messages;
025: import org.apache.axis2.jaxws.utility.ConvertUtils;
026: import org.apache.axis2.transport.http.HTTPConstants;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import javax.activation.DataHandler;
031: import javax.imageio.IIOImage;
032: import javax.imageio.ImageWriter;
033: import javax.imageio.stream.ImageOutputStream;
034: import javax.mail.internet.InternetHeaders;
035: import javax.mail.internet.MimeBodyPart;
036: import javax.mail.internet.MimePartDataSource;
037: import javax.xml.transform.Source;
038:
039: import java.awt.Image;
040: import java.awt.image.BufferedImage;
041: import java.io.ByteArrayOutputStream;
042: import java.io.IOException;
043: import java.util.Iterator;
044:
045: /**
046: * The Attachment object has a similar function as the
047: * org.apache.axis2.jaxws.marshaller.impl.alt.Element object.
048: * The Element object is used to
049: * 1) Get the "element rendered" object that is marshalled/unmarshalled from the wire
050: * 2) Get the "type rendered" object that is used as the parameter value in the signature.
051: *
052: * The Attachment object has a similar role.
053: * The DataHandler object is used for marshalling/unmarshalling.
054: * And the getValue method is used to get the signature representation.
055: *
056: *
057: */
058: class Attachment {
059: private static final Log log = LogFactory.getLog(Attachment.class);
060: DataHandler dh = null;
061: String cid = null;
062: AttachmentDescription aDesc = null;
063: Object sigValue = null;
064: Class sigClass = null;
065:
066: /**
067: * Constructor used to set Attachment from wire unmarshalling
068: * @param dh
069: * @param cid
070: */
071: public Attachment(DataHandler dh, String cid) {
072: if (log.isDebugEnabled()) {
073: System.out.println("Construct with dh="
074: + dh.getContentType() + " cid=" + cid);
075: }
076: this .dh = dh;
077: this .cid = cid;
078: }
079:
080: /**
081: * Constructor used to create Attachment from signature data
082: * @param sigValue
083: * @param sigClass
084: */
085: public Attachment(Object sigValue, Class sigClass,
086: AttachmentDescription aDesc) {
087: this .sigValue = sigValue;
088: this .sigClass = sigClass;
089: this .aDesc = aDesc;
090: }
091:
092: /**
093: * @return DataHandler
094: */
095: public DataHandler getDataHandler() {
096: if (dh == null) {
097: dh = createDataHandler(sigValue, sigClass, aDesc
098: .getMimeTypes(), getContentID());
099: }
100: return dh;
101: }
102:
103: /**
104: * @return ContentID
105: */
106: public String getContentID() {
107: if (cid == null) {
108: cid = UUIDGenerator.getUUID();
109: }
110: return cid;
111: }
112:
113: private static DataHandler createDataHandler(Object value,
114: Class cls, String[] mimeTypes, String cid) {
115: if (log.isDebugEnabled()) {
116: System.out.println("Construct data handler for " + cls
117: + " cid=" + cid);
118: }
119: DataHandler dh = null;
120: if (cls.isAssignableFrom(DataHandler.class)) {
121: dh = (DataHandler) value;
122: try {
123: Object content = dh.getContent();
124: // If the content is a Source, convert to a String due to
125: // problems with the DataContentHandler
126: if (content instanceof Source) {
127: if (log.isDebugEnabled()) {
128: System.out
129: .println("Converting DataHandler Source content to "
130: + "DataHandlerString content");
131: }
132: byte[] bytes = (byte[]) ConvertUtils.convert(
133: content, byte[].class);
134: String newContent = new String(bytes);
135: return new DataHandler(newContent, mimeTypes[0]);
136: }
137: } catch (Exception e) {
138: throw ExceptionFactory.makeWebServiceException(e);
139: }
140: } else {
141: try {
142: byte[] bytes = createBytes(value, cls, mimeTypes);
143: // Create MIME Body Part
144: InternetHeaders ih = new InternetHeaders();
145: ih.setHeader(HTTPConstants.HEADER_CONTENT_TYPE,
146: mimeTypes[0]);
147: MimeBodyPart mbp = new MimeBodyPart(ih, bytes);
148:
149: //Create a data source for the MIME Body Part
150: MimePartDataSource ds = new MimePartDataSource(mbp);
151:
152: dh = new DataHandler(ds);
153: mbp.setHeader(HTTPConstants.HEADER_CONTENT_ID, cid);
154: } catch (Exception e) {
155: throw ExceptionFactory.makeWebServiceException(e);
156: }
157: }
158: return dh;
159: }
160:
161: private static byte[] createBytes(Object value, Class cls,
162: String[] mimeTypes) {
163: if (cls.isAssignableFrom(byte[].class)) {
164: return (byte[]) value;
165: } else if (cls.isAssignableFrom(String.class)) {
166: return ((String) value).getBytes();
167: } else if (cls.isAssignableFrom(Image.class)) {
168: return createBytesFromImage((Image) value, mimeTypes[0]);
169: } else if (ConvertUtils.isConvertable(value, byte[].class)) {
170: return (byte[]) ConvertUtils.convert(value, byte[].class);
171: } else {
172: throw ExceptionFactory.makeWebServiceException(Messages
173: .getMessage("convertProblem", cls.getName(),
174: "byte[]"));
175: }
176: }
177:
178: private static byte[] createBytesFromImage(Image image,
179: String mimeType) {
180: try {
181: ImageWriter imageWriter = null;
182: BufferedImage bufferedImage = (BufferedImage) image;
183: ByteArrayOutputStream baos = new ByteArrayOutputStream();
184: Iterator iterator = javax.imageio.ImageIO
185: .getImageWritersByMIMEType(mimeType);
186: if (iterator.hasNext()) {
187: imageWriter = (ImageWriter) iterator.next();
188: }
189: ImageOutputStream ios = javax.imageio.ImageIO
190: .createImageOutputStream(baos);
191: imageWriter.setOutput(ios);
192: imageWriter.write(new IIOImage(bufferedImage, null, null));
193: ios.flush();
194: imageWriter.dispose();
195: return baos.toByteArray();
196: } catch (IOException e) {
197: throw ExceptionFactory.makeWebServiceException(e);
198: }
199:
200: }
201: }
|