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.provider;
020:
021: import java.awt.*;
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027:
028: import javax.activation.DataSource;
029:
030: /**
031: * An impl class for javax.activation.DataSource interface.
032: *
033: */
034: public class DataSourceImpl implements DataSource {
035:
036: private final String fileName;
037:
038: private final String contentType;
039:
040: private byte[] byteArray;
041:
042: private ByteArrayOutputStream byteArrayOS;
043:
044: /**
045: * Constructor
046: *
047: * @param _contentType
048: * @param _fileName
049: * @param image
050: * @throws Exception
051: */
052: public DataSourceImpl(String _contentType, String _fileName,
053: Image image) throws Exception {
054: super ();
055:
056: try {
057: if ((_contentType == null) || (_contentType == "")) {
058: throw new NullPointerException(
059: "Type is NULL: Must provide content type");
060: }
061: if ((_fileName == null) || (_contentType == "")) {
062: throw new NullPointerException(
063: "File name is NULL: Must provide content file name");
064: }
065:
066: this .fileName = _fileName;
067: this .contentType = _contentType;
068:
069: if (image != null) {
070: byteArrayOS = new ByteArrayOutputStream();
071: AttachmentUtil.storeImage(this .contentType, image,
072: byteArrayOS);
073: }
074: } catch (Exception e) {
075: throw e;
076: }
077:
078: }
079:
080: /* (non-Javadoc)
081: * @see javax.activation.DataSource#getContentType()
082: */
083: public String getContentType() {
084: return this .contentType;
085: }
086:
087: /* (non-Javadoc)
088: * @see javax.activation.DataSource#getInputStream()
089: */
090: public InputStream getInputStream() throws IOException {
091: if (this .byteArrayOS.size() != 0) {
092: this .byteArray = this .byteArrayOS.toByteArray();
093: this .byteArrayOS.reset();
094: }
095:
096: if (this .byteArray == null) {
097: return new ByteArrayInputStream(new byte[0]);
098:
099: }
100: return new ByteArrayInputStream(this .byteArray);
101: }
102:
103: /* (non-Javadoc)
104: * @see javax.activation.DataSource#getName()
105: */
106: public String getName() {
107: return this .fileName;
108: }
109:
110: /* (non-Javadoc)
111: * @see javax.activation.DataSource#getOutputStream()
112: */
113: public OutputStream getOutputStream() throws IOException {
114: if (this .byteArrayOS.size() != 0) {
115: this.byteArray = this.byteArrayOS.toByteArray();
116: this.byteArrayOS.reset();
117: }
118: return this.byteArrayOS;
119: }
120: }
|