Coverage Report - org.apache.commons.feedparser.network.BaseResourceRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseResourceRequest
0%
0/97
0%
0/22
1.643
 
 1  
 /*
 2  
  * Copyright 1999,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.feedparser.network;
 18  
 
 19  
 import java.io.ByteArrayInputStream;
 20  
 import java.io.ByteArrayOutputStream;
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.OutputStream;
 24  
 import java.net.HttpURLConnection;
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 
 28  
 /**
 29  
  * 
 30  
  * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
 31  
  * @version $Id: BaseResourceRequest.java 373622 2006-01-30 22:53:00Z mvdb $
 32  
  */
 33  0
 public abstract class BaseResourceRequest implements ResourceRequest {
 34  
 
 35  0
     public static boolean FOLLOW_REDIRECTS = true;
 36  
     
 37  0
     private String resource = null;
 38  
 
 39  0
     private DataEvent event = new DataEvent();
 40  
 
 41  0
     private long _ifModifiedSince = -1;
 42  
 
 43  0
     private long _responseCode = HttpURLConnection.HTTP_OK;
 44  
 
 45  0
     private String _etag = null;
 46  
     
 47  0
     private byte[] data = new byte[0];
 48  
 
 49  0
     private boolean localCache = false;
 50  
 
 51  0
     private boolean followRedirects = FOLLOW_REDIRECTS;
 52  
 
 53  
     /**
 54  
      * A single resource request can now have a given event listener.
 55  
      */
 56  0
     private NetworkEventListener eventListener = null;
 57  
 
 58  0
     private HashMap requestHeaders = new HashMap();
 59  
     
 60  
     /**
 61  
      * 
 62  
      * Get the value of <code>resource</code>.
 63  
      *
 64  
      * 
 65  
      */
 66  
     public String getResource() { 
 67  
         
 68  0
         return this.resource;
 69  
         
 70  
     }
 71  
 
 72  
     /**
 73  
      * 
 74  
      * Set the value of <code>resource</code>.
 75  
      *
 76  
      * 
 77  
      */
 78  
     public void setResource( String resource ) { 
 79  
         
 80  0
         this.resource = resource;
 81  
         
 82  0
     }
 83  
 
 84  
     /**
 85  
      * Fire a new ArchiveEvent
 86  
      *
 87  
      * 
 88  
      */
 89  
     public void fireDataEvent( long count ) {
 90  
 
 91  0
         event.count = count;
 92  0
         event.resource = resource;
 93  
         
 94  0
         fireDataEvent( event );
 95  
         
 96  0
     }
 97  
 
 98  
     public void fireInit() {
 99  
 
 100  0
         DataEvent event = new DataEvent();
 101  0
         event.request = this;
 102  
         
 103  0
         Iterator i = ResourceRequestFactory.getNetworkEventListeners();
 104  
 
 105  0
         while ( i.hasNext() ) {
 106  0
             ((NetworkEventListener)i.next()).init( event );
 107  
         } 
 108  
 
 109  0
         if ( eventListener != null )
 110  0
             eventListener.init( event );
 111  0
     }
 112  
 
 113  
     /**
 114  
      * Fire a new ArchiveEvent
 115  
      *
 116  
      * 
 117  
      */
 118  
     public void fireDataEvent( DataEvent event ) {
 119  
 
 120  0
         event.request = this;
 121  
         
 122  0
         Iterator i = ResourceRequestFactory.getNetworkEventListeners();
 123  
 
 124  0
         while ( i.hasNext() ) {
 125  0
             ((NetworkEventListener)i.next()).dataEvent( event );
 126  
         } 
 127  
 
 128  0
         if ( eventListener != null )
 129  0
             eventListener.dataEvent( event );
 130  0
     }
 131  
 
 132  
     public void fireOnClosed() {
 133  
 
 134  0
         Iterator i = ResourceRequestFactory.getNetworkEventListeners();
 135  
 
 136  0
         while ( i.hasNext() ) {
 137  0
             ((NetworkEventListener)i.next()).onClosed();
 138  
         } 
 139  
 
 140  0
         if ( eventListener != null )
 141  0
             eventListener.onClosed();
 142  0
     }
 143  
 
 144  
     /**
 145  
      * @see ResourceRequest
 146  
      * 
 147  
      */
 148  
     public String getInputStreamAsString() throws IOException {
 149  0
         return new String( getInputStreamAsByteArray() );
 150  
     }
 151  
 
 152  
     /**
 153  
      * @see ResourceRequest
 154  
      * 
 155  
      */
 156  
     public byte[] getInputStreamAsByteArray() throws IOException {
 157  
 
 158  0
         InputStream is = getInputStream();
 159  
 
 160  0
         int contentLength = -1;
 161  
 
 162  
         try {
 163  
 
 164  0
             contentLength = getContentLength() + 5000;
 165  
 
 166  0
         } catch ( IOException e ) { e.printStackTrace(); }
 167  
 
 168  0
         if ( contentLength == -1  ) {
 169  
 
 170  
             //use a larger default than what's provided with the
 171  
             //ByteArrayOutputStream
 172  
 
 173  0
             contentLength = 100000;
 174  
         } 
 175  
 
 176  
         //include length of content from the original site with contentLength
 177  0
         ByteArrayOutputStream bos = new ByteArrayOutputStream( contentLength );
 178  
       
 179  
         //now process the Reader...
 180  0
         byte data[] = new byte[200];
 181  
     
 182  0
         int readCount = 0;
 183  
 
 184  0
         while( ( readCount = is.read( data )) > 0 ) {
 185  0
             bos.write( data, 0, readCount );
 186  
         }
 187  
 
 188  0
         is.close();
 189  0
         bos.close();
 190  
 
 191  0
         return bos.toByteArray();
 192  
 
 193  
     }
 194  
 
 195  
     /**
 196  
      * @see ResourceRequest
 197  
      * 
 198  
      */
 199  
     public InputStream getLocalInputStream() throws NetworkException {
 200  
 
 201  
         try { 
 202  
             
 203  
             byte[] data;
 204  
             
 205  0
             if ( this.data.length > 0 ) {
 206  
                 
 207  
                 //we have cached this... return the cached value.
 208  0
                 data = this.data;
 209  
                 
 210  
             } else {
 211  
                 
 212  0
                 data = getInputStreamAsByteArray();
 213  
                 
 214  0
                 if ( localCache )
 215  0
                     this.data = data;
 216  
                 
 217  
             }
 218  
             
 219  0
             return new ByteArrayInputStream( data );
 220  
             
 221  0
         } catch ( NetworkException n ) {
 222  0
             throw n;
 223  0
         } catch ( Throwable t ) {
 224  0
             throw new NetworkException( t );
 225  
         }
 226  
 
 227  
     }
 228  
 
 229  
     public byte[] getLocalInputStreamAsByteArray() throws IOException {
 230  
         //FIXME: this needs to use the cache.
 231  0
         return this.data;
 232  
     }
 233  
 
 234  
     public void setLocalCache( boolean v ) {
 235  0
         this.localCache = v;
 236  0
     }
 237  
     
 238  
     /**
 239  
      * Copy this resource request to the given OutputStream
 240  
      *
 241  
      * 
 242  
      */
 243  
     public void toOutputStream( OutputStream out ) throws IOException {
 244  
 
 245  0
         InputStream is = getInputStream();
 246  
         
 247  
         //now process the Reader...
 248  0
         byte data[] = new byte[200];
 249  
     
 250  0
         int readCount = 0;
 251  
 
 252  0
         while( ( readCount = is.read( data )) > 0 ) {
 253  
             
 254  0
             out.write( data, 0, readCount );
 255  
         }
 256  
 
 257  0
         is.close();
 258  
 
 259  0
     }
 260  
 
 261  
     public long getIfModifiedSince() {
 262  0
         return _ifModifiedSince;
 263  
     }
 264  
 
 265  
     public void setIfModifiedSince( long ifModifiedSince ) {
 266  0
         this._ifModifiedSince = ifModifiedSince;
 267  0
     }
 268  
 
 269  
     public String getEtag() {
 270  0
         return _etag;
 271  
     }
 272  
     
 273  
     public void setEtag( String etag ) {
 274  0
         this._etag = etag;
 275  0
     }
 276  
 
 277  
     /**
 278  
      * Get and set an HTTP style response code.  Only used with HTTP URLs.
 279  
      *
 280  
      * 
 281  
      */
 282  
     public long getResponseCode() {
 283  0
         return this._responseCode;
 284  
     }
 285  
     
 286  
     public void setResponseCode( int responseCode ) {
 287  0
         this._responseCode = responseCode;
 288  0
     }
 289  
 
 290  
     public int getContentLength() throws IOException {
 291  0
         return -1;
 292  
     }
 293  
 
 294  
     public void setEventListener( NetworkEventListener eventListener ) {
 295  0
         this.eventListener = eventListener;
 296  0
     }
 297  
     
 298  
    public String getHeaderField( String name ) {
 299  
        //default impl always returns null
 300  0
        return  null;
 301  
     }
 302  
 
 303  
     public void setRequestHeaderField( String name, String value ) {
 304  0
         requestHeaders.put( name, value );
 305  0
     }
 306  
 
 307  
     public Iterator getRequestHeaderFields() {
 308  0
         return requestHeaders.keySet().iterator();
 309  
     }
 310  
 
 311  
     public String getRequestHeaderField( String name ) {
 312  0
         return (String)requestHeaders.get( name );
 313  
     }
 314  
 
 315  
     public void setRequestMethod( String method ) throws NetworkException {
 316  0
         throw new NetworkException( "not implemented" );
 317  
     }
 318  
 
 319  
     public boolean getFollowRedirects() {
 320  0
         return followRedirects;
 321  
     }
 322  
 
 323  
     public void setFollowRedirects( boolean v ) {
 324  0
         this.followRedirects = v;
 325  0
     }
 326  
 
 327  
     public String getResourceFromRedirect() {
 328  0
         return getResource();
 329  
     }
 330  
     
 331  
 }