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