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.message.attachments;
020:
021: import org.apache.axis2.jaxws.ExceptionFactory;
022: import org.apache.axis2.jaxws.message.Message;
023: import org.apache.axis2.jaxws.utility.JavaUtils;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import javax.activation.DataHandler;
028: import javax.xml.bind.attachment.AttachmentUnmarshaller;
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032:
033: /**
034: * JAXBAttachmentUnmarshaller
035: * <p/>
036: * An implementation of the <link>javax.xml.bind.attachment.AttachmentUnmarshaller</link> that is
037: * used for deserializing XOP elements into their corresponding binary data packages.
038: */
039: public class JAXBAttachmentUnmarshaller extends AttachmentUnmarshaller {
040:
041: private static final Log log = LogFactory
042: .getLog(JAXBAttachmentUnmarshaller.class);
043:
044: private Message message;
045:
046: public JAXBAttachmentUnmarshaller(Message message) {
047: this .message = message;
048: }
049:
050: @Override
051: public boolean isXOPPackage() {
052:
053: // Any message that is received might contain MTOM.
054: // So always return true.
055: boolean value = true;
056:
057: if (log.isDebugEnabled()) {
058: log.debug("isXOPPackage returns " + value);
059: }
060: return value;
061: }
062:
063: @Override
064: public byte[] getAttachmentAsByteArray(String cid) {
065: if (log.isDebugEnabled()) {
066: log.debug("Attempting to retrieve attachment [" + cid
067: + "] as a byte[]");
068: }
069: DataHandler dh = getAttachmentAsDataHandler(cid);
070: if (dh != null) {
071: try {
072: return convert(dh);
073: } catch (IOException ioe) {
074: if (log.isDebugEnabled()) {
075: log
076: .debug("Exception occurred while getting the byte[] "
077: + ioe);
078: }
079: throw ExceptionFactory.makeWebServiceException(ioe);
080: }
081: }
082: if (log.isDebugEnabled()) {
083: log.debug("returning null byte[]");
084: }
085: return null;
086: }
087:
088: @Override
089: public DataHandler getAttachmentAsDataHandler(String cid) {
090: if (log.isDebugEnabled()) {
091: log.debug("Attempting to retrieve attachment [" + cid
092: + "] as a DataHandler");
093: }
094:
095: DataHandler dh = message.getDataHandler(cid);
096: if (dh != null) {
097: return dh;
098: } else {
099: String cid2 = getNewCID(cid);
100: if (log.isDebugEnabled()) {
101: log.debug("A dataHandler was not found for [" + cid
102: + "] trying [" + cid2 + "]");
103: }
104: dh = message.getDataHandler(cid2);
105: if (dh != null) {
106: return dh;
107: }
108: }
109: // No Data Handler found
110: throw ExceptionFactory
111: .makeWebServiceException("A data handler was not found "
112: + "for content id " + cid);
113: }
114:
115: /**
116: * @param cid
117: * @return cid with translated characters
118: */
119: private String getNewCID(String cid) {
120: // TODO This method only converts : and /
121: // A more complete fix is needed.
122: String cid2 = cid;
123: cid2 = JavaUtils.replace(cid2, "%3A", ":");
124: cid2 = JavaUtils.replace(cid2, "%2F", "/");
125: return cid2;
126: }
127:
128: /**
129: * Read the bytes from the DataHandler
130: *
131: * @param dh
132: * @return byte[]
133: * @throws IOException
134: */
135: private byte[] convert(DataHandler dh) throws IOException {
136: if (log.isDebugEnabled()) {
137: log.debug("Reading byte[] from DataHandler " + dh);
138: }
139: InputStream is = dh.getInputStream();
140: if (log.isDebugEnabled()) {
141: log.debug("DataHandler InputStream " + is);
142: }
143: ByteArrayOutputStream baos = new ByteArrayOutputStream();
144: byte[] b = new byte[1024];
145: int num = is.read(b);
146: if (log.isDebugEnabled()) {
147: if (num <= 0) {
148: log
149: .debug("DataHandler InputStream contains no data. num="
150: + num);
151: }
152: }
153: while (num > 0) {
154: baos.write(b, 0, num);
155: num = is.read(b);
156: }
157: return baos.toByteArray();
158: }
159: }
|