Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.sling.launchpad.base.impl;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   import java.net.URL;
   22   import java.net.URLConnection;
   23   
   24   /**
   25    * The <code>ContextConnection</code> extends the
   26    * <code>java.net.URLConnection</code> to provide access to a resource which
   27    * is available from {@link ResourceProvider} provided to {@link Sling}.
   28    * <p>
   29    * This class is implemented by actually connecting to a resource URL which is
   30    * provided by the resource provider and delegating the relevant method calls.
   31    * Currently only {@link #getContentLength()}, {@link #getContentType()},
   32    * {@link #getInputStream()} and {@link #getLastModified()} are supported.
   33    */
   34   public class ContextConnection extends URLConnection {
   35   
   36       /**
   37        * The {@link ResourceProvider} to which requests for content access are
   38        * delegated.
   39        */
   40       private final ResourceProvider resourceProvider;
   41   
   42       /**
   43        * The delegatee <code>URLConnection</code> to which some of the method
   44        * calls are forwarded.
   45        */
   46       private URLConnection delegatee;
   47   
   48       /**
   49        * Creates an instance of this context connection.
   50        *
   51        * @param url The original URL whose path part is used to address the
   52        *            resource from the resource provider.
   53        * @param resourceProvider The {@link ResourceProvider} to which requests
   54        *            for content access are delegated.
   55        */
   56       ContextConnection(URL url, ResourceProvider resourceProvider) {
   57           super(url);
   58           this.resourceProvider = resourceProvider;
   59       }
   60   
   61       /**
   62        * Accesses the the resource from the underlaying resource provider at the
   63        * URL's path.
   64        */
   65       public void connect() throws IOException {
   66           if (!this.connected) {
   67               URL contextURL = resourceProvider.getResource(url.getPath());
   68               if (contextURL == null) {
   69                   throw new IOException("Resource " + url.getPath()
   70                       + " does not exist");
   71               }
   72   
   73               delegatee = contextURL.openConnection();
   74               connected = true;
   75           }
   76       }
   77   
   78       /**
   79        * Returns the length in bytes of the resource or -1 if this connection has
   80        * not been connected yet.
   81        */
   82       public int getContentLength() {
   83           return (delegatee == null) ? -1 : delegatee.getContentLength();
   84       }
   85   
   86       /**
   87        * Returns a guess at the content type of the resource or <code>null</code>
   88        * if this connection has not been connected yet.
   89        */
   90       public String getContentType() {
   91           return (delegatee == null) ? null : delegatee.getContentType();
   92       }
   93   
   94       /**
   95        * Returns a <code>InputStream</code> on the resource. If this connection
   96        * is not connected yet, the conneciton is opened.
   97        *
   98        * @throws IOException may be thrown if an error occurrs opening the
   99        *             connection or accessing the content as an
  100        *             <code>InputStream</code>.
  101        */
  102       public InputStream getInputStream() throws IOException {
  103           connect();
  104           return delegatee.getInputStream();
  105       }
  106   
  107       /**
  108        * Returns the last modification timestamp of the resource or -1 if this
  109        * connection has not been connected yet.
  110        */
  111       public long getLastModified() {
  112           return (delegatee == null) ? 0 : delegatee.getLastModified();
  113       }
  114   }

Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl » [javadoc | source]