alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Axis 2 example source code file (DataSourceImpl.java)

This example Axis 2 source code file (DataSourceImpl.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Axis 2 tags/keywords

awt, bytearrayinputstream, bytearrayoutputstream, bytearrayoutputstream, datasourceimpl, exception, image, io, ioexception, ioexception, must, must, null, nullpointerexception, string, string

The Axis 2 DataSourceImpl.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.jaxws.provider;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

/**
 * An impl class for javax.activation.DataSource interface.
 *
 */
public class DataSourceImpl implements DataSource {

    private final String fileName;

    private final String contentType;

    private byte[] byteArray;

    private ByteArrayOutputStream byteArrayOS;
    
	/**
	 * Constructor
	 * 
	 * @param _contentType
	 * @param _fileName
	 * @param image
	 * @throws Exception
	 */
	public DataSourceImpl(String _contentType, String _fileName, Image image) throws Exception{
		super();
        
        try {
        	if((_contentType == null) || (_contentType == "")){
        		throw new NullPointerException("Type is NULL: Must provide content type");
        	}
        	if((_fileName == null) || (_contentType == "")){
        		throw new NullPointerException("File name is NULL: Must provide content file name");
        	}
        	
        	this.fileName = _fileName;
    		this.contentType = _contentType;
        	
            if (image != null) {
            	byteArrayOS = new ByteArrayOutputStream();
                AttachmentUtil.storeImage(this.contentType, image, byteArrayOS);
            }
        } catch (Exception e) {
            throw e;
        }

	}

	/* (non-Javadoc)
	 * @see javax.activation.DataSource#getContentType()
	 */
	public String getContentType() {
		return this.contentType;
	}

	/* (non-Javadoc)
	 * @see javax.activation.DataSource#getInputStream()
	 */
	public InputStream getInputStream() throws IOException {
		if (this.byteArrayOS.size() != 0) {
			this.byteArray = this.byteArrayOS.toByteArray();
            this.byteArrayOS.reset();
        }
		
		if(this.byteArray == null){
			return new ByteArrayInputStream(new byte[0]);
			
		}
        return new ByteArrayInputStream(this.byteArray);
	}

	/* (non-Javadoc)
	 * @see javax.activation.DataSource#getName()
	 */
	public String getName() {
		return this.fileName;
	}

	/* (non-Javadoc)
	 * @see javax.activation.DataSource#getOutputStream()
	 */
	public OutputStream getOutputStream() throws IOException {
		if (this.byteArrayOS.size() != 0) {
			this.byteArray = this.byteArrayOS.toByteArray();
            this.byteArrayOS.reset();
        }
        return this.byteArrayOS;
	}
}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 DataSourceImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.