001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.asset;
016    
017    import java.io.InputStream;
018    import java.net.URL;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.Location;
022    import org.apache.tapestry.IRequestCycle;
023    import org.apache.tapestry.Tapestry;
024    
025    /**
026     *  A reference to an external URL.  {@link ExternalAsset}s are not
027     *  localizable.
028     *
029     *  @author Howard Lewis Ship
030     * 
031     **/
032    
033    public class ExternalAsset extends AbstractAsset
034    {
035        private String _URL;
036    
037        public ExternalAsset(String URL, Location location)
038        {
039            super(null, location);
040            
041            _URL = URL;
042        }
043    
044        /**
045         *  Simply returns the URL of the external asset.
046         *
047         **/
048    
049        public String buildURL(IRequestCycle cycle)
050        {
051            return _URL;
052        }
053    
054        public InputStream getResourceAsStream(IRequestCycle cycle)
055        {
056            URL url;
057    
058            try
059            {
060                url = new URL(_URL);
061    
062                return url.openStream();
063            }
064            catch (Exception ex)
065            {
066                // MalrformedURLException or IOException
067    
068                throw new ApplicationRuntimeException(Tapestry.format("ExternalAsset.resource-missing", _URL), ex);
069            }
070    
071        }
072    
073        public String toString()
074        {
075            return "ExternalAsset[" + _URL + "]";
076        }
077    }