1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 
   6 /*
   7  * Licensed to the Apache Software Foundation (ASF) under one or more
   8  * contributor license agreements.  See the NOTICE file distributed with
   9  * this work for additional information regarding copyright ownership.
  10  * The ASF licenses this file to You under the Apache License, Version 2.0
  11  * (the "License"); you may not use this file except in compliance with
  12  * the License.  You may obtain a copy of the License at
  13  *
  14  *     http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing, software
  17  * distributed under the License is distributed on an "AS IS" BASIS,
  18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19  * See the License for the specific language governing permissions and
  20  * limitations under the License.
  21  */
  22 
  23 package com.sun.org.apache.xerces.internal.util;
  24 
  25 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  26 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  27 import java.io.InputStream;
  28 import java.io.Reader;
  29 import java.util.HashMap;
  30 import java.util.Iterator;
  31 import java.util.Map;
  32 
  33 /**
  34  * This class represents an input source for an XML resource
  35  * retrievable over HTTP. In addition to the properties
  36  * provided by an <code>XMLInputSource</code> an HTTP input
  37  * source also has HTTP request properties and a preference
  38  * whether HTTP redirects will be followed. Note that these
  39  * properties will only be used if reading this input source
  40  * will induce an HTTP connection.
  41  *
  42  * @author Michael Glavassevich, IBM
  43  *
  44  */
  45 public final class HTTPInputSource extends XMLInputSource {
  46 
  47     //
  48     // Data
  49     //
  50 
  51     /** Preference for whether HTTP redirects should be followed. **/
  52     protected boolean fFollowRedirects = true;
  53 
  54     /** HTTP request properties. **/
  55     protected Map<String, String> fHTTPRequestProperties = new HashMap<>();
  56 
  57     //
  58     // Constructors
  59     //
  60 
  61     /**
  62      * Constructs an input source from just the public and system
  63      * identifiers, leaving resolution of the entity and opening of
  64      * the input stream up to the caller.
  65      *
  66      * @param publicId     The public identifier, if known.
  67      * @param systemId     The system identifier. This value should
  68      *                     always be set, if possible, and can be
  69      *                     relative or absolute. If the system identifier
  70      *                     is relative, then the base system identifier
  71      *                     should be set.
  72      * @param baseSystemId The base system identifier. This value should
  73      *                     always be set to the fully expanded URI of the
  74      *                     base system identifier, if possible.
  75      */
  76     public HTTPInputSource(String publicId, String systemId, String baseSystemId) {
  77         super(publicId, systemId, baseSystemId);
  78     } // <init>(String,String,String)
  79 
  80     /**
  81      * Constructs an input source from a XMLResourceIdentifier
  82      * object, leaving resolution of the entity and opening of
  83      * the input stream up to the caller.
  84      *
  85      * @param resourceIdentifier the XMLResourceIdentifier containing the information
  86      */
  87     public HTTPInputSource(XMLResourceIdentifier resourceIdentifier) {
  88         super(resourceIdentifier);
  89     } // <init>(XMLResourceIdentifier)
  90 
  91     /**
  92      * Constructs an input source from a byte stream.
  93      *
  94      * @param publicId     The public identifier, if known.
  95      * @param systemId     The system identifier. This value should
  96      *                     always be set, if possible, and can be
  97      *                     relative or absolute. If the system identifier
  98      *                     is relative, then the base system identifier
  99      *                     should be set.
 100      * @param baseSystemId The base system identifier. This value should
 101      *                     always be set to the fully expanded URI of the
 102      *                     base system identifier, if possible.
 103      * @param byteStream   The byte stream.
 104      * @param encoding     The encoding of the byte stream, if known.
 105      */
 106     public HTTPInputSource(String publicId, String systemId,
 107             String baseSystemId, InputStream byteStream, String encoding) {
 108         super(publicId, systemId, baseSystemId, byteStream, encoding);
 109     } // <init>(String,String,String,InputStream,String)
 110 
 111     /**
 112      * Constructs an input source from a character stream.
 113      *
 114      * @param publicId     The public identifier, if known.
 115      * @param systemId     The system identifier. This value should
 116      *                     always be set, if possible, and can be
 117      *                     relative or absolute. If the system identifier
 118      *                     is relative, then the base system identifier
 119      *                     should be set.
 120      * @param baseSystemId The base system identifier. This value should
 121      *                     always be set to the fully expanded URI of the
 122      *                     base system identifier, if possible.
 123      * @param charStream   The character stream.
 124      * @param encoding     The original encoding of the byte stream
 125      *                     used by the reader, if known.
 126      */
 127     public HTTPInputSource(String publicId, String systemId,
 128             String baseSystemId, Reader charStream, String encoding) {
 129         super(publicId, systemId, baseSystemId, charStream, encoding);
 130     } // <init>(String,String,String,Reader,String)
 131 
 132     //
 133     // Public methods
 134     //
 135 
 136     /**
 137      * Returns the preference whether HTTP redirects should
 138      * be followed. By default HTTP redirects will be followed.
 139      */
 140     public boolean getFollowHTTPRedirects() {
 141         return fFollowRedirects;
 142     } // getFollowHTTPRedirects():boolean
 143 
 144 
 145     /**
 146      * Sets the preference whether HTTP redirects should
 147      * be followed. By default HTTP redirects will be followed.
 148      */
 149     public void setFollowHTTPRedirects(boolean followRedirects) {
 150         fFollowRedirects = followRedirects;
 151     } // setFollowHTTPRedirects(boolean)
 152 
 153     /**
 154      * Returns the value of the request property
 155      * associated with the given property name.
 156      *
 157      * @param key the name of the request property
 158      * @return the value of the request property or
 159      * <code>null</code> if this property has not
 160      * been set
 161      */
 162     public String getHTTPRequestProperty(String key) {
 163         return fHTTPRequestProperties.get(key);
 164     } // getHTTPRequestProperty(String):String
 165 
 166     /**
 167      * Returns an iterator for the request properties this
 168      * input source contains. Each object returned by the
 169      * iterator is an instance of <code>java.util.Map.Entry</code>
 170      * where each key and value are a pair of strings corresponding
 171      * to the name and value of a request property.
 172      *
 173      * @return an iterator for the request properties this
 174      * input source contains
 175      */
 176     public Iterator<Map.Entry<String, String>> getHTTPRequestProperties() {
 177         return fHTTPRequestProperties.entrySet().iterator();
 178     } // getHTTPRequestProperties():Iterator
 179 
 180     /**
 181      * Sets the value of the request property
 182      * associated with the given property name.
 183      *
 184      * @param key the name of the request property
 185      * @param value the value of the request property
 186      */
 187     public void setHTTPRequestProperty(String key, String value) {
 188         if (value != null) {
 189             fHTTPRequestProperties.put(key, value);
 190         }
 191         else {
 192             fHTTPRequestProperties.remove(key);
 193         }
 194     } // setHTTPRequestProperty(String,String)
 195 
 196 } // class HTTPInputSource