558 *
559 * @param name the name of a header field.
560 * @return the value of the named header field, or <code>null</code>
561 * if there is no such field in the header.
562 */
563 public String getHeaderField(String name) {
564 return null;
565 }
566
567 /**
568 * Returns an unmodifiable Map of the header fields.
569 * The Map keys are Strings that represent the
570 * response-header field names. Each Map value is an
571 * unmodifiable List of Strings that represents
572 * the corresponding field values.
573 *
574 * @return a Map of header fields
575 * @since 1.4
576 */
577 public Map<String,List<String>> getHeaderFields() {
578 return Collections.EMPTY_MAP;
579 }
580
581 /**
582 * Returns the value of the named field parsed as a number.
583 * <p>
584 * This form of <code>getHeaderField</code> exists because some
585 * connection types (e.g., <code>http-ng</code>) have pre-parsed
586 * headers. Classes for that connection type can override this method
587 * and short-circuit the parsing.
588 *
589 * @param name the name of the header field.
590 * @param Default the default value.
591 * @return the value of the named field, parsed as an integer. The
592 * <code>Default</code> value is returned if the field is
593 * missing or malformed.
594 */
595 public int getHeaderFieldInt(String name, int Default) {
596 String value = getHeaderField(name);
597 try {
598 return Integer.parseInt(value);
599 } catch (Exception e) { }
600 return Default;
601 }
602
603 /**
604 * Returns the value of the named field parsed as date.
605 * The result is the number of milliseconds since January 1, 1970 GMT
606 * represented by the named field.
607 * <p>
608 * This form of <code>getHeaderField</code> exists because some
609 * connection types (e.g., <code>http-ng</code>) have pre-parsed
610 * headers. Classes for that connection type can override this method
611 * and short-circuit the parsing.
612 *
613 * @param name the name of the header field.
614 * @param Default a default value.
615 * @return the value of the field, parsed as a date. The value of the
616 * <code>Default</code> argument is returned if the field is
617 * missing or malformed.
618 */
619 public long getHeaderFieldDate(String name, long Default) {
620 String value = getHeaderField(name);
621 try {
622 return Date.parse(value);
623 } catch (Exception e) { }
624 return Default;
625 }
626
627 /**
628 * Returns the key for the <code>n</code><sup>th</sup> header field.
629 * It returns <code>null</code> if there are fewer than <code>n+1</code> fields.
630 *
631 * @param n an index, where n>=0
632 * @return the key for the <code>n</code><sup>th</sup> header field,
633 * or <code>null</code> if there are fewer than <code>n+1</code>
634 * fields.
635 */
636 public String getHeaderFieldKey(int n) {
637 return null;
638 }
1093 return requests.findValue(key);
1094 }
1095
1096 /**
1097 * Returns an unmodifiable Map of general request
1098 * properties for this connection. The Map keys
1099 * are Strings that represent the request-header
1100 * field names. Each Map value is a unmodifiable List
1101 * of Strings that represents the corresponding
1102 * field values.
1103 *
1104 * @return a Map of the general request properties for this connection.
1105 * @throws IllegalStateException if already connected
1106 * @since 1.4
1107 */
1108 public Map<String,List<String>> getRequestProperties() {
1109 if (connected)
1110 throw new IllegalStateException("Already connected");
1111
1112 if (requests == null)
1113 return Collections.EMPTY_MAP;
1114
1115 return requests.getHeaders(null);
1116 }
1117
1118 /**
1119 * Sets the default value of a general request property. When a
1120 * <code>URLConnection</code> is created, it is initialized with
1121 * these properties.
1122 *
1123 * @param key the keyword by which the request is known
1124 * (e.g., "<code>accept</code>").
1125 * @param value the value associated with the key.
1126 *
1127 * @see java.net.URLConnection#setRequestProperty(java.lang.String,java.lang.String)
1128 *
1129 * @deprecated The instance specific setRequestProperty method
1130 * should be used after an appropriate instance of URLConnection
1131 * is obtained. Invoking this method will have no effect.
1132 *
1133 * @see #getDefaultRequestProperty(java.lang.String)
1176 *
1177 * @param fac the desired factory.
1178 * @exception Error if the factory has already been defined.
1179 * @exception SecurityException if a security manager exists and its
1180 * <code>checkSetFactory</code> method doesn't allow the operation.
1181 * @see java.net.ContentHandlerFactory
1182 * @see java.net.URLConnection#getContent()
1183 * @see SecurityManager#checkSetFactory
1184 */
1185 public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
1186 if (factory != null) {
1187 throw new Error("factory already defined");
1188 }
1189 SecurityManager security = System.getSecurityManager();
1190 if (security != null) {
1191 security.checkSetFactory();
1192 }
1193 factory = fac;
1194 }
1195
1196 private static Hashtable handlers = new Hashtable();
1197 private static final ContentHandler UnknownContentHandlerP = new UnknownContentHandler();
1198
1199 /**
1200 * Gets the Content Handler appropriate for this connection.
1201 * @param connection the connection to use.
1202 */
1203 synchronized ContentHandler getContentHandler()
1204 throws UnknownServiceException
1205 {
1206 String contentType = stripOffParameters(getContentType());
1207 ContentHandler handler = null;
1208 if (contentType == null)
1209 throw new UnknownServiceException("no content-type");
1210 try {
1211 handler = (ContentHandler) handlers.get(contentType);
1212 if (handler != null)
1213 return handler;
1214 } catch(Exception e) {
1215 }
1216
1217 if (factory != null)
1218 handler = factory.createContentHandler(contentType);
1219 if (handler == null) {
1220 try {
1221 handler = lookupContentHandlerClassFor(contentType);
1222 } catch(Exception e) {
1223 e.printStackTrace();
1224 handler = UnknownContentHandlerP;
1225 }
1226 handlers.put(contentType, handler);
1227 }
1228 return handler;
1229 }
1230
1231 /*
1257 * <pre>
1258 * {package-prefix}.{major}.{minor}
1259 * e.g.
1260 * YoyoDyne.experimental.text.plain
1261 * </pre>
1262 */
1263 private ContentHandler lookupContentHandlerClassFor(String contentType)
1264 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
1265 String contentHandlerClassName = typeToPackageName(contentType);
1266
1267 String contentHandlerPkgPrefixes =getContentHandlerPkgPrefixes();
1268
1269 StringTokenizer packagePrefixIter =
1270 new StringTokenizer(contentHandlerPkgPrefixes, "|");
1271
1272 while (packagePrefixIter.hasMoreTokens()) {
1273 String packagePrefix = packagePrefixIter.nextToken().trim();
1274
1275 try {
1276 String clsName = packagePrefix + "." + contentHandlerClassName;
1277 Class cls = null;
1278 try {
1279 cls = Class.forName(clsName);
1280 } catch (ClassNotFoundException e) {
1281 ClassLoader cl = ClassLoader.getSystemClassLoader();
1282 if (cl != null) {
1283 cls = cl.loadClass(clsName);
1284 }
1285 }
1286 if (cls != null) {
1287 ContentHandler handler =
1288 (ContentHandler)cls.newInstance();
1289 return handler;
1290 }
1291 } catch(Exception e) {
1292 }
1293 }
1294
1295 return UnknownContentHandlerP;
1296 }
1297
|
558 *
559 * @param name the name of a header field.
560 * @return the value of the named header field, or <code>null</code>
561 * if there is no such field in the header.
562 */
563 public String getHeaderField(String name) {
564 return null;
565 }
566
567 /**
568 * Returns an unmodifiable Map of the header fields.
569 * The Map keys are Strings that represent the
570 * response-header field names. Each Map value is an
571 * unmodifiable List of Strings that represents
572 * the corresponding field values.
573 *
574 * @return a Map of header fields
575 * @since 1.4
576 */
577 public Map<String,List<String>> getHeaderFields() {
578 return Collections.emptyMap();
579 }
580
581 /**
582 * Returns the value of the named field parsed as a number.
583 * <p>
584 * This form of <code>getHeaderField</code> exists because some
585 * connection types (e.g., <code>http-ng</code>) have pre-parsed
586 * headers. Classes for that connection type can override this method
587 * and short-circuit the parsing.
588 *
589 * @param name the name of the header field.
590 * @param Default the default value.
591 * @return the value of the named field, parsed as an integer. The
592 * <code>Default</code> value is returned if the field is
593 * missing or malformed.
594 */
595 public int getHeaderFieldInt(String name, int Default) {
596 String value = getHeaderField(name);
597 try {
598 return Integer.parseInt(value);
599 } catch (Exception e) { }
600 return Default;
601 }
602
603 /**
604 * Returns the value of the named field parsed as date.
605 * The result is the number of milliseconds since January 1, 1970 GMT
606 * represented by the named field.
607 * <p>
608 * This form of <code>getHeaderField</code> exists because some
609 * connection types (e.g., <code>http-ng</code>) have pre-parsed
610 * headers. Classes for that connection type can override this method
611 * and short-circuit the parsing.
612 *
613 * @param name the name of the header field.
614 * @param Default a default value.
615 * @return the value of the field, parsed as a date. The value of the
616 * <code>Default</code> argument is returned if the field is
617 * missing or malformed.
618 */
619 @SuppressWarnings("deprecation")
620 public long getHeaderFieldDate(String name, long Default) {
621 String value = getHeaderField(name);
622 try {
623 return Date.parse(value);
624 } catch (Exception e) { }
625 return Default;
626 }
627
628 /**
629 * Returns the key for the <code>n</code><sup>th</sup> header field.
630 * It returns <code>null</code> if there are fewer than <code>n+1</code> fields.
631 *
632 * @param n an index, where n>=0
633 * @return the key for the <code>n</code><sup>th</sup> header field,
634 * or <code>null</code> if there are fewer than <code>n+1</code>
635 * fields.
636 */
637 public String getHeaderFieldKey(int n) {
638 return null;
639 }
1094 return requests.findValue(key);
1095 }
1096
1097 /**
1098 * Returns an unmodifiable Map of general request
1099 * properties for this connection. The Map keys
1100 * are Strings that represent the request-header
1101 * field names. Each Map value is a unmodifiable List
1102 * of Strings that represents the corresponding
1103 * field values.
1104 *
1105 * @return a Map of the general request properties for this connection.
1106 * @throws IllegalStateException if already connected
1107 * @since 1.4
1108 */
1109 public Map<String,List<String>> getRequestProperties() {
1110 if (connected)
1111 throw new IllegalStateException("Already connected");
1112
1113 if (requests == null)
1114 return Collections.emptyMap();
1115
1116 return requests.getHeaders(null);
1117 }
1118
1119 /**
1120 * Sets the default value of a general request property. When a
1121 * <code>URLConnection</code> is created, it is initialized with
1122 * these properties.
1123 *
1124 * @param key the keyword by which the request is known
1125 * (e.g., "<code>accept</code>").
1126 * @param value the value associated with the key.
1127 *
1128 * @see java.net.URLConnection#setRequestProperty(java.lang.String,java.lang.String)
1129 *
1130 * @deprecated The instance specific setRequestProperty method
1131 * should be used after an appropriate instance of URLConnection
1132 * is obtained. Invoking this method will have no effect.
1133 *
1134 * @see #getDefaultRequestProperty(java.lang.String)
1177 *
1178 * @param fac the desired factory.
1179 * @exception Error if the factory has already been defined.
1180 * @exception SecurityException if a security manager exists and its
1181 * <code>checkSetFactory</code> method doesn't allow the operation.
1182 * @see java.net.ContentHandlerFactory
1183 * @see java.net.URLConnection#getContent()
1184 * @see SecurityManager#checkSetFactory
1185 */
1186 public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
1187 if (factory != null) {
1188 throw new Error("factory already defined");
1189 }
1190 SecurityManager security = System.getSecurityManager();
1191 if (security != null) {
1192 security.checkSetFactory();
1193 }
1194 factory = fac;
1195 }
1196
1197 private static Hashtable<String, ContentHandler> handlers = new Hashtable<String, ContentHandler>();
1198 private static final ContentHandler UnknownContentHandlerP = new UnknownContentHandler();
1199
1200 /**
1201 * Gets the Content Handler appropriate for this connection.
1202 * @param connection the connection to use.
1203 */
1204 synchronized ContentHandler getContentHandler()
1205 throws UnknownServiceException
1206 {
1207 String contentType = stripOffParameters(getContentType());
1208 ContentHandler handler = null;
1209 if (contentType == null)
1210 throw new UnknownServiceException("no content-type");
1211 try {
1212 handler = handlers.get(contentType);
1213 if (handler != null)
1214 return handler;
1215 } catch(Exception e) {
1216 }
1217
1218 if (factory != null)
1219 handler = factory.createContentHandler(contentType);
1220 if (handler == null) {
1221 try {
1222 handler = lookupContentHandlerClassFor(contentType);
1223 } catch(Exception e) {
1224 e.printStackTrace();
1225 handler = UnknownContentHandlerP;
1226 }
1227 handlers.put(contentType, handler);
1228 }
1229 return handler;
1230 }
1231
1232 /*
1258 * <pre>
1259 * {package-prefix}.{major}.{minor}
1260 * e.g.
1261 * YoyoDyne.experimental.text.plain
1262 * </pre>
1263 */
1264 private ContentHandler lookupContentHandlerClassFor(String contentType)
1265 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
1266 String contentHandlerClassName = typeToPackageName(contentType);
1267
1268 String contentHandlerPkgPrefixes =getContentHandlerPkgPrefixes();
1269
1270 StringTokenizer packagePrefixIter =
1271 new StringTokenizer(contentHandlerPkgPrefixes, "|");
1272
1273 while (packagePrefixIter.hasMoreTokens()) {
1274 String packagePrefix = packagePrefixIter.nextToken().trim();
1275
1276 try {
1277 String clsName = packagePrefix + "." + contentHandlerClassName;
1278 Class<?> cls = null;
1279 try {
1280 cls = Class.forName(clsName);
1281 } catch (ClassNotFoundException e) {
1282 ClassLoader cl = ClassLoader.getSystemClassLoader();
1283 if (cl != null) {
1284 cls = cl.loadClass(clsName);
1285 }
1286 }
1287 if (cls != null) {
1288 ContentHandler handler =
1289 (ContentHandler)cls.newInstance();
1290 return handler;
1291 }
1292 } catch(Exception e) {
1293 }
1294 }
1295
1296 return UnknownContentHandlerP;
1297 }
1298
|