001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.xerces.util;
018:
019: import java.io.InputStream;
020: import java.io.Reader;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.xerces.xni.XMLResourceIdentifier;
027: import org.apache.xerces.xni.parser.XMLInputSource;
028:
029: /**
030: * This class represents an input source for an XML resource
031: * retrievable over HTTP. In addition to the properties
032: * provided by an <code>XMLInputSource</code> an HTTP input
033: * source also has HTTP request properties and a preference
034: * whether HTTP redirects will be followed. Note that these
035: * properties will only be used if reading this input source
036: * will induce an HTTP connection.
037: *
038: * @author Michael Glavassevich, IBM
039: *
040: * @version $Id: HTTPInputSource.java 447241 2006-09-18 05:12:57Z mrglavas $
041: */
042: public final class HTTPInputSource extends XMLInputSource {
043:
044: //
045: // Data
046: //
047:
048: /** Preference for whether HTTP redirects should be followed. **/
049: protected boolean fFollowRedirects = true;
050:
051: /** HTTP request properties. **/
052: protected Map fHTTPRequestProperties = new HashMap();
053:
054: //
055: // Constructors
056: //
057:
058: /**
059: * Constructs an input source from just the public and system
060: * identifiers, leaving resolution of the entity and opening of
061: * the input stream up to the caller.
062: *
063: * @param publicId The public identifier, if known.
064: * @param systemId The system identifier. This value should
065: * always be set, if possible, and can be
066: * relative or absolute. If the system identifier
067: * is relative, then the base system identifier
068: * should be set.
069: * @param baseSystemId The base system identifier. This value should
070: * always be set to the fully expanded URI of the
071: * base system identifier, if possible.
072: */
073: public HTTPInputSource(String publicId, String systemId,
074: String baseSystemId) {
075: super (publicId, systemId, baseSystemId);
076: } // <init>(String,String,String)
077:
078: /**
079: * Constructs an input source from a XMLResourceIdentifier
080: * object, leaving resolution of the entity and opening of
081: * the input stream up to the caller.
082: *
083: * @param resourceIdentifier the XMLResourceIdentifier containing the information
084: */
085: public HTTPInputSource(XMLResourceIdentifier resourceIdentifier) {
086: super (resourceIdentifier);
087: } // <init>(XMLResourceIdentifier)
088:
089: /**
090: * Constructs an input source from a byte stream.
091: *
092: * @param publicId The public identifier, if known.
093: * @param systemId The system identifier. This value should
094: * always be set, if possible, and can be
095: * relative or absolute. If the system identifier
096: * is relative, then the base system identifier
097: * should be set.
098: * @param baseSystemId The base system identifier. This value should
099: * always be set to the fully expanded URI of the
100: * base system identifier, if possible.
101: * @param byteStream The byte stream.
102: * @param encoding The encoding of the byte stream, if known.
103: */
104: public HTTPInputSource(String publicId, String systemId,
105: String baseSystemId, InputStream byteStream, String encoding) {
106: super (publicId, systemId, baseSystemId, byteStream, encoding);
107: } // <init>(String,String,String,InputStream,String)
108:
109: /**
110: * Constructs an input source from a character stream.
111: *
112: * @param publicId The public identifier, if known.
113: * @param systemId The system identifier. This value should
114: * always be set, if possible, and can be
115: * relative or absolute. If the system identifier
116: * is relative, then the base system identifier
117: * should be set.
118: * @param baseSystemId The base system identifier. This value should
119: * always be set to the fully expanded URI of the
120: * base system identifier, if possible.
121: * @param charStream The character stream.
122: * @param encoding The original encoding of the byte stream
123: * used by the reader, if known.
124: */
125: public HTTPInputSource(String publicId, String systemId,
126: String baseSystemId, Reader charStream, String encoding) {
127: super (publicId, systemId, baseSystemId, charStream, encoding);
128: } // <init>(String,String,String,Reader,String)
129:
130: //
131: // Public methods
132: //
133:
134: /**
135: * Returns the preference whether HTTP redirects should
136: * be followed. By default HTTP redirects will be followed.
137: */
138: public boolean getFollowHTTPRedirects() {
139: return fFollowRedirects;
140: } // getFollowHTTPRedirects():boolean
141:
142: /**
143: * Sets the preference whether HTTP redirects should
144: * be followed. By default HTTP redirects will be followed.
145: */
146: public void setFollowHTTPRedirects(boolean followRedirects) {
147: fFollowRedirects = followRedirects;
148: } // setFollowHTTPRedirects(boolean)
149:
150: /**
151: * Returns the value of the request property
152: * associated with the given property name.
153: *
154: * @param key the name of the request property
155: * @return the value of the request property or
156: * <code>null</code> if this property has not
157: * been set
158: */
159: public String getHTTPRequestProperty(String key) {
160: return (String) fHTTPRequestProperties.get(key);
161: } // getHTTPRequestProperty(String):String
162:
163: /**
164: * Returns an iterator for the request properties this
165: * input source contains. Each object returned by the
166: * iterator is an instance of <code>java.util.Map.Entry</code>
167: * where each key and value are a pair of strings corresponding
168: * to the name and value of a request property.
169: *
170: * @return an iterator for the request properties this
171: * input source contains
172: */
173: public Iterator getHTTPRequestProperties() {
174: return fHTTPRequestProperties.entrySet().iterator();
175: } // getHTTPRequestProperties():Iterator
176:
177: /**
178: * Sets the value of the request property
179: * associated with the given property name.
180: *
181: * @param key the name of the request property
182: * @param value the value of the request property
183: */
184: public void setHTTPRequestProperty(String key, String value) {
185: if (value != null) {
186: fHTTPRequestProperties.put(key, value);
187: } else {
188: fHTTPRequestProperties.remove(key);
189: }
190: } // setHTTPRequestProperty(String,String)
191:
192: } // class HTTPInputSource
|