Save This Page
Home » glassfish-v2ur2-b04-src » javax » activation » [javadoc | source]
    1   /*
    2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    3    *
    4    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    5    *
    6    * The contents of this file are subject to the terms of either the GNU
    7    * General Public License Version 2 only ("GPL") or the Common Development
    8    * and Distribution License("CDDL") (collectively, the "License").  You
    9    * may not use this file except in compliance with the License. You can obtain
   10    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   11    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   12    * language governing permissions and limitations under the License.
   13    *
   14    * When distributing the software, include this License Header Notice in each
   15    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   16    * Sun designates this particular file as subject to the "Classpath" exception
   17    * as provided by Sun in the GPL Version 2 section of the License file that
   18    * accompanied this code.  If applicable, add the following below the License
   19    * Header, with the fields enclosed by brackets [] replaced by your own
   20    * identifying information: "Portions Copyrighted [year]
   21    * [name of copyright owner]"
   22    *
   23    * Contributor(s):
   24    *
   25    * If you wish your version of this file to be governed by only the CDDL or
   26    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   27    * elects to include this software in this distribution under the [CDDL or GPL
   28    * Version 2] license."  If you don't indicate a single choice of license, a
   29    * recipient has the option to distribute your version of this file under
   30    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   31    * its licensees as provided above.  However, if you add GPL Version 2 code
   32    * and therefore, elected the GPL Version 2 license, then the option applies
   33    * only if the new code is made subject to such option by the copyright
   34    * holder.
   35    */
   36   
   37   /*
   38    * @(#)URLDataSource.java	1.11 07/05/14
   39    */
   40   
   41   package javax.activation;
   42   
   43   import java.net.URL;
   44   import java.net.URLConnection;
   45   import java.io.InputStream;
   46   import java.io.OutputStream;
   47   import java.io.IOException;
   48   
   49   /**
   50    * The URLDataSource class provides an object that wraps a <code>URL</code>
   51    * object in a DataSource interface. URLDataSource simplifies the handling
   52    * of data described by URLs within the JavaBeans Activation Framework
   53    * because this class can be used to create new DataHandlers. <i>NOTE: The
   54    * DataHandler object creates a URLDataSource internally,
   55    * when it is constructed with a URL.</i>
   56    *
   57    * @see javax.activation.DataSource
   58    * @see javax.activation.DataHandler
   59    */
   60   public class URLDataSource implements DataSource {
   61       private URL url = null;
   62       private URLConnection url_conn = null;
   63   
   64       /**
   65        * URLDataSource constructor. The URLDataSource class will
   66        * not open a connection to the URL until a method requiring it
   67        * to do so is called.
   68        *
   69        * @param url The URL to be encapsulated in this object.
   70        */
   71       public URLDataSource(URL url) {
   72   	this.url = url;
   73       }
   74   
   75       /**
   76        * Returns the value of the URL content-type header field.
   77        * It calls the URL's <code>URLConnection.getContentType</code> method
   78        * after retrieving a URLConnection object.
   79        * <i>Note: this method attempts to call the <code>openConnection</code>
   80        * method on the URL. If this method fails, or if a content type is not
   81        * returned from the URLConnection, getContentType returns
   82        * "application/octet-stream" as the content type.</i>
   83        *
   84        * @return the content type.
   85        */
   86       public String getContentType() {
   87   	String type = null;
   88   
   89   	try {
   90   	    if (url_conn == null)
   91   		url_conn = url.openConnection();
   92   	} catch (IOException e) { }
   93   	
   94   	if (url_conn != null)
   95   	    type = url_conn.getContentType();
   96   
   97   	if (type == null)
   98   	    type = "application/octet-stream";
   99   	
  100   	return type;
  101       }
  102   
  103       /**
  104        * Calls the <code>getFile</code> method on the URL used to
  105        * instantiate the object.
  106        *
  107        * @return the result of calling the URL's getFile method.
  108        */
  109       public String getName() {
  110   	return url.getFile();
  111       }
  112   
  113       /**
  114        * The getInputStream method from the URL. Calls the
  115        * <code>openStream</code> method on the URL.
  116        *
  117        * @return the InputStream.
  118        */
  119       public InputStream getInputStream() throws IOException {
  120   	return url.openStream();
  121       }
  122   
  123       /**
  124        * The getOutputStream method from the URL. First an attempt is
  125        * made to get the URLConnection object for the URL. If that
  126        * succeeds, the getOutputStream method on the URLConnection
  127        * is returned.
  128        *
  129        * @return the OutputStream.
  130        */
  131       public OutputStream getOutputStream() throws IOException {
  132   	// get the url connection if it is available
  133   	url_conn = url.openConnection();
  134   	
  135   	if (url_conn != null) {
  136   	    url_conn.setDoOutput(true);
  137   	    return url_conn.getOutputStream();
  138   	} else
  139   	    return null;
  140       }
  141   
  142       /**
  143        * Return the URL used to create this DataSource.
  144        *
  145        * @return The URL.
  146        */
  147       public URL getURL() {
  148   	return url;
  149       }
  150   }

Save This Page
Home » glassfish-v2ur2-b04-src » javax » activation » [javadoc | source]