001: // HttpRequestMessage.java
002: // $Id: HttpRequestMessage.java,v 1.31 2003/02/16 22:54:51 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.net.URL;
011:
012: import org.w3c.www.mime.MimeParser;
013: import org.w3c.www.mime.MimeParserException;
014:
015: public class HttpRequestMessage extends HttpEntityMessage {
016: // HTTP Request message well-known headers
017: public static int H_ACCEPT = 31;
018: public static int H_ACCEPT_CHARSET = 32;
019: public static int H_ACCEPT_ENCODING = 33;
020: public static int H_ACCEPT_LANGUAGE = 34;
021: public static int H_AUTHORIZATION = 35;
022: public static int H_EXPECT = 36;
023: public static int H_FROM = 37;
024: public static int H_HOST = 38;
025: public static int H_IF_MODIFIED_SINCE = 39;
026: public static int H_IF_MATCH = 40;
027: public static int H_IF_NONE_MATCH = 41;
028: public static int H_IF_RANGE = 42;
029: public static int H_IF_UNMODIFIED_SINCE = 43;
030: public static int H_MAX_FORWARDS = 44;
031: public static int H_PROXY_AUTHORIZATION = 45;
032: public static int H_RANGE = 46;
033: public static int H_REFERER = 47;
034: public static int H_TE = 48;
035: public static int H_USER_AGENT = 49;
036:
037: static {
038: registerHeader("Accept", "org.w3c.www.http.HttpAcceptList",
039: H_ACCEPT);
040: registerHeader("Accept-Charset",
041: "org.w3c.www.http.HttpAcceptCharsetList",
042: H_ACCEPT_CHARSET);
043: registerHeader("Accept-Encoding",
044: "org.w3c.www.http.HttpAcceptEncodingList",
045: H_ACCEPT_ENCODING);
046: registerHeader("Accept-Language",
047: "org.w3c.www.http.HttpAcceptLanguageList",
048: H_ACCEPT_LANGUAGE);
049: registerHeader("Authorization",
050: "org.w3c.www.http.HttpCredential", H_AUTHORIZATION);
051: registerHeader("From", "org.w3c.www.http.HttpString", H_FROM);
052: registerHeader("Host", "org.w3c.www.http.HttpString", H_HOST);
053: registerHeader("If-Modified-Since",
054: "org.w3c.www.http.HttpDate", H_IF_MODIFIED_SINCE);
055: registerHeader("If-Match",
056: "org.w3c.www.http.HttpEntityTagList", H_IF_MATCH);
057: registerHeader("If-None-Match",
058: "org.w3c.www.http.HttpEntityTagList", H_IF_NONE_MATCH);
059: registerHeader("If-Range", "org.w3c.www.http.HttpEntityTag",
060: H_IF_RANGE);
061: registerHeader("If-Unmodified-Since",
062: "org.w3c.www.http.HttpDate", H_IF_UNMODIFIED_SINCE);
063: registerHeader("Max-Forwards", "org.w3c.www.http.HttpInteger",
064: H_MAX_FORWARDS);
065: registerHeader("Proxy-Authorization",
066: "org.w3c.www.http.HttpCredential",
067: H_PROXY_AUTHORIZATION);
068: registerHeader("Range", "org.w3c.www.http.HttpRangeList",
069: H_RANGE);
070: registerHeader("Referer", "org.w3c.www.http.HttpString",
071: H_REFERER);
072: registerHeader("User-Agent", "org.w3c.www.http.HttpString",
073: H_USER_AGENT);
074: registerHeader("Expect", "org.w3c.www.http.HttpString",
075: H_EXPECT);
076: registerHeader("TE", "org.w3c.www.http.HttpAcceptEncodingList",
077: H_TE);
078: }
079:
080: /**
081: * The method to execute on the target resource.
082: */
083: protected String method = "GET".intern();
084: /**
085: * The target resource, identified by its URL.
086: */
087: protected URL url = null;
088: /**
089: * The proxy to use for that request, if any.
090: */
091: protected URL proxy = null;
092:
093: protected String sProxy = null;
094:
095: /**
096: * This message is about to be emited, emit the request-line first !
097: * @param out The output stream to emit the request to.
098: * @exception IOException If some IO error occured while emiting the
099: * request.
100: */
101:
102: protected void startEmit(OutputStream out, int what)
103: throws IOException {
104: if ((what & EMIT_HEADERS) != EMIT_HEADERS)
105: return;
106: // I am not sure (at all) whether this belongs here or in some subclass
107: if ((major >= 1) && !hasHeader(H_HOST)) {
108: String h = ((((url.getPort() == 80) && url.getProtocol()
109: .equalsIgnoreCase("http"))
110: || ((url.getPort() == 443) && url.getProtocol()
111: .equalsIgnoreCase("https")) || (url
112: .getPort() == -1)) ? url.getHost() : url.getHost()
113: + ":" + url.getPort());
114: setHeaderValue(H_HOST, HttpFactory.makeString(h));
115: }
116: // Emit the request line:
117: HttpBuffer buf = new HttpBuffer();
118: buf.append(method);
119: buf.append(' ');
120: if (proxy != null) {
121: buf.append(url.toExternalForm());
122: } else {
123: String sUrl = url.getFile();
124: // as to jdk1.4 getFile can be "" in that case
125: // we can use it when method = OPTIONS
126: if (sUrl.length() == 0) {
127: if (method == HTTP.OPTIONS) {
128: buf.append('*');
129: } else {
130: buf.append('/');
131: }
132: } else {
133: buf.append(sUrl);
134: }
135: }
136: buf.append(' ');
137: buf.append(getVersion());
138: buf.append('\r');
139: buf.append('\n');
140: buf.emit(out);
141: }
142:
143: public void dump(OutputStream out) {
144: // Dump the reply status line first, and then the headers
145: try {
146: startEmit(out, EMIT_HEADERS);
147: } catch (Exception ex) {
148: }
149: super .dump(out);
150: }
151:
152: /**
153: * @return A boolean <strong>true</strong> if the MIME parser should stop
154: * parsing, <strong>false</strong> otherwise.
155: * @exception IOException If some IO error occured while reading the
156: * stream.
157: * @exception HttpParserException if parsing failed.
158: */
159: public boolean notifyBeginParsing(MimeParser parser)
160: throws HttpParserException, IOException {
161: // Append the whole reply line in some buffer:
162: HttpBuffer buf = new HttpBuffer();
163: int ch = parser.read();
164: // A present for Netscape !
165: while ((ch == '\r') || (ch == '\n')) {
166: ch = parser.read();
167: }
168: loop: while (true) {
169: switch (ch) {
170: case -1:
171: throw new HttpParserException("End Of File");
172: case '\r':
173: if ((ch = parser.read()) != '\n')
174: parser.unread(ch);
175: break loop;
176: case '\n':
177: break loop;
178: default:
179: buf.append(ch);
180: }
181: ch = parser.read();
182: }
183: // Parse the bufer into HTTP version and status code
184: byte line[] = buf.getByteCopy();
185: ParseState ps = new ParseState();
186: ps.ioff = 0;
187: ps.bufend = line.length;
188: ps.separator = (byte) ' ';
189: // Get the method name:
190: if (HttpParser.nextItem(line, ps) < 0) {
191: throw new RuntimeException("Bad request, no method !");
192: }
193: setMethod(ps.toString(line));
194: // Get the URL path, or full URL
195: if (HttpParser.nextItem(line, ps) < 0) {
196: throw new RuntimeException("Bad request, no URL !");
197: }
198: setTarget(ps.toString(line));
199: // Get the version numbers:
200: HttpParser.skipSpaces(line, ps);
201: if (ps.ioff + 5 < ps.bufend) {
202: ps.ioff += 5;
203: ps.separator = (byte) '.';
204: this .major = (short) HttpParser.parseInt(line, ps);
205: ps.prepare();
206: this .minor = (short) HttpParser.parseInt(line, ps);
207: return false;
208: } else {
209: this .major = 0;
210: this .minor = 9;
211: return true;
212: }
213: }
214:
215: /**
216: * All the headers have been parsed, take any appropriate actions.
217: * Here we will verify that the request is HTTP/1.1 compliant
218: * for the Host header.
219: * @param parser The Mime parser.
220: * @exception MimeParserException if the parsing failed
221: * @exception IOException if an IO error occurs.
222: */
223: public void notifyEndParsing(MimeParser parser)
224: throws HttpParserException, IOException {
225: if (major == 1 && minor == 1) {
226: if (getHost() == null) {
227: throw new HttpParserException("missing Host header",
228: this );
229: }
230: }
231: }
232:
233: // FIXME - I really mean FIXME
234:
235: String target = null;
236:
237: protected void setTarget(String target) {
238: this .target = target;
239: }
240:
241: protected String getTarget() {
242: return target;
243: }
244:
245: /**
246: * Get this request's method.
247: * @return The request method, as a String.
248: */
249:
250: public String getMethod() {
251: return method;
252: }
253:
254: /**
255: * Set this request's method.
256: * @param mth The request method.
257: */
258:
259: public void setMethod(String method) {
260: this .method = method.intern();
261: }
262:
263: /**
264: * Get this request's target URI.
265: * This will only return the absolute path of the requested resource, even
266: * if the actual request came with the full path as an URI.
267: * @return An URL instance, or <strong>null</strong> if undefined.
268: */
269:
270: public URL getURL() {
271: return url;
272: }
273:
274: /**
275: * Set this request URI.
276: * The provided URI should only include the absolute path of the target
277: * request, see the <code>setHost</code> method for how to set the actual
278: * host of the target resource.
279: * @param url The target URL of the request, as an URL instance.
280: */
281:
282: public void setURL(URL url) {
283: this .url = url;
284: }
285:
286: /**
287: * Get the <code>min-fresh</code> directive value of the cache control
288: * header.
289: * @return The min-fresh value, as a number of seconds, or <strong>-1
290: * </strong> if undefined.
291: */
292:
293: public int getMinFresh() {
294: HttpCacheControl cc = getCacheControl();
295: return (cc == null) ? -1 : cc.getMinFresh();
296: }
297:
298: /**
299: * Set the <code>min-fresh</code> directive value of the cache control
300: * header.
301: * @param minfresh The min-fresh value, in seconds, or <strong>-1</strong>
302: * to reset value.
303: */
304:
305: public void setMinFresh(int minfresh) {
306: HttpCacheControl cc = getCacheControl();
307: if (cc == null) {
308: if (minfresh == -1) {
309: return;
310: }
311: setCacheControl(cc = new HttpCacheControl(true));
312: }
313: cc.setMinFresh(minfresh);
314: }
315:
316: /**
317: * Get the <code>max-stale</code> directive value of the cache control
318: * header.
319: * @return The max-stale value, as a number of seconds, or <strong>-1
320: * </strong> if undefined.
321: */
322:
323: public int getMaxStale() {
324: HttpCacheControl cc = getCacheControl();
325: return (cc == null) ? -1 : cc.getMaxStale();
326: }
327:
328: /**
329: * Set the <code>max-stale</code> directive value.
330: * @param maxstale A number of seconds giving the allowed drift for
331: * a resource that is no more valid, or <strong>-1</strong> to reset
332: * the value.
333: */
334:
335: public void setMaxStale(int maxstale) {
336: HttpCacheControl cc = getCacheControl();
337: if (cc == null) {
338: if (maxstale == -1) {
339: return;
340: }
341: setCacheControl(cc = new HttpCacheControl(true));
342: }
343: cc.setMaxStale(maxstale);
344: }
345:
346: // FIXME more cache control accessors
347:
348: /**
349: * Get this request accept list.
350: * @return A list of Accept clauses encoded as an array of HttpAccept
351: * instances, or <strong>null</strong> if undefined.
352: */
353:
354: public HttpAccept[] getAccept() {
355: HeaderValue value = getHeaderValue(H_ACCEPT);
356: return (value != null) ? (HttpAccept[]) value.getValue() : null;
357: }
358:
359: /**
360: * Set the list of accept clauses attached to this request.
361: * @param accepts The list of accept clauses encoded as an array
362: * of HttpAccept instances, or <strong>null</strong> to reset the value.
363: */
364:
365: public void setAccept(HttpAccept accepts[]) {
366: setHeaderValue(H_ACCEPT, ((accepts == null) ? null
367: : new HttpAcceptList(accepts)));
368: }
369:
370: /**
371: * Get the list of accepted charsets for this request.
372: * @return The list of accepted languages encoded as an array of
373: * instances of HttpAcceptCharset, or <strong>null</strong> if undefined.
374: */
375:
376: public HttpAcceptCharset[] getAcceptCharset() {
377: HeaderValue value = getHeaderValue(H_ACCEPT_CHARSET);
378: return (value != null) ? (HttpAcceptCharset[]) value.getValue()
379: : null;
380: }
381:
382: /**
383: * Set the list of accepted charsets for this request.
384: * @param charsets The list of accepted charsets, encoded as an array
385: * of HttpAcceptCharset instances, or <strong>null</strong> to reset
386: * the value.
387: */
388:
389: public void setAcceptCharset(HttpAcceptCharset charsets[]) {
390: setHeaderValue(H_ACCEPT_CHARSET, ((charsets == null) ? null
391: : new HttpAcceptCharsetList(charsets)));
392: }
393:
394: /**
395: * Get the list of accepted encodings.
396: * @return A list of token describing the accepted encodings, or <strong>
397: * null</strong> if undefined.
398: */
399:
400: public HttpAcceptEncoding[] getAcceptEncoding() {
401: HeaderValue value = getHeaderValue(H_ACCEPT_ENCODING);
402: return (value != null) ? (HttpAcceptEncoding[]) value
403: .getValue() : null;
404: }
405:
406: /**
407: * Set the list of accepted encodings.
408: * @param encodings The list of accepted encodings, as an array,
409: * of HttpAcceptEncoding or <strong>null</strong> to reset the value.
410: */
411:
412: public void setAcceptEncoding(HttpAcceptEncoding encoding[]) {
413: setHeaderValue(H_ACCEPT_ENCODING, ((encoding == null) ? null
414: : new HttpAcceptEncodingList(encoding)));
415: }
416:
417: /**
418: * Get the list of accepted languages for this request.
419: * @return The list of accepted languages encoded as an array of
420: * instances of HttpAcceptLanguage, or <strong>null</strong> if
421: * undefined.
422: */
423:
424: public HttpAcceptLanguage[] getAcceptLanguage() {
425: HeaderValue value = getHeaderValue(H_ACCEPT_LANGUAGE);
426: return ((value != null) ? (HttpAcceptLanguage[]) value
427: .getValue() : null);
428: }
429:
430: /**
431: * Set the list of accepted languages for this request.
432: * @param langs The list of accepted languages, encoded as an array
433: * of HttpAcceptLanguage instances, or <strong>null</strong> to reset
434: * value.
435: */
436:
437: public void setAcceptLanguage(HttpAcceptCharset langs[]) {
438: setHeaderValue(H_ACCEPT_LANGUAGE, ((langs == null) ? null
439: : new HttpAcceptCharsetList(langs)));
440: }
441:
442: /**
443: * Get the authorization associated with this request.
444: * @return An instance of HttpCredential of <strong>null</strong>
445: * if undefined.
446: */
447:
448: public HttpCredential getAuthorization() {
449: HeaderValue value = getHeaderValue(H_AUTHORIZATION);
450: return (value != null) ? (HttpCredential) value.getValue()
451: : null;
452: }
453:
454: /**
455: * Set the authorization associated with this request.
456: * @param credentials The credentials to attach to this request, or
457: * <strong>null</strong> to reset the value.
458: */
459:
460: public void setAuthorization(HttpCredential credentials) {
461: setHeaderValue(H_AUTHORIZATION, credentials);
462: }
463:
464: /**
465: * Does this request has some specific authorization infos.
466: * @return A boolean.
467: */
468:
469: public boolean hasAuthorization() {
470: return hasHeader(H_AUTHORIZATION);
471: }
472:
473: /**
474: * Get the originator (from header value) of the request.
475: * @return The originator description, as a String, or <strong>null
476: * </strong> if undefined.
477: */
478:
479: public String getFrom() {
480: HeaderValue value = getHeaderValue(H_FROM);
481: return (value != null) ? (String) value.getValue() : null;
482: }
483:
484: /**
485: * Set the originator of this request.
486: * @param from The description of the originator, as an email address,
487: * or <strong>null</strong> to reset the value.
488: */
489:
490: public void setFrom(String from) {
491: setHeaderValue(H_FROM, ((from == null) ? null : new HttpString(
492: true, from)));
493: }
494:
495: /**
496: * Get the host header.
497: * @return The host header, encoded as a String, or <strong>null</strong>
498: * if undefined.
499: */
500:
501: public String getHost() {
502: HeaderValue value = getHeaderValue(H_HOST);
503: return (value != null) ? (String) value.getValue() : null;
504: }
505:
506: /**
507: * Set the host header value.
508: * @param host The String representing the target host of the request,
509: * or <strong>null</strong> to reset the value.
510: */
511:
512: public void setHost(String host) {
513: setHeaderValue(H_HOST, ((host == null) ? null : new HttpString(
514: true, host)));
515: }
516:
517: /**
518: * Get the if-modified-since conditional.
519: * @return A long, giving the If-Modified-Since date value as the number
520: * of milliseconds since Java epoch, or <strong>-1</strong> if undefined.
521: */
522:
523: public long getIfModifiedSince() {
524: HeaderValue value = getHeaderValue(H_IF_MODIFIED_SINCE);
525: return (value != null) ? ((Long) value.getValue()).longValue()
526: : -1;
527: }
528:
529: /**
530: * Set the if-modified-since conditional.
531: * @param ims The date of last modification, as the number of milliseconds
532: * since Java epoch, or <strong>-1</strong> to reset the value.
533: */
534:
535: public void setIfModifiedSince(long ims) {
536: setHeaderValue(H_IF_MODIFIED_SINCE, ((ims == -1) ? null
537: : new HttpDate(true, ims)));
538: }
539:
540: /**
541: * Get the conditional matching set of entity tags.
542: * @return An array of HttpEntityTag instances, or <strong>null</strong>
543: * if undefined.
544: */
545:
546: public HttpEntityTag[] getIfMatch() {
547: HeaderValue value = getHeaderValue(H_IF_MATCH);
548: return (value != null) ? (HttpEntityTag[]) value.getValue()
549: : null;
550: }
551:
552: /**
553: * Set the conditional matching set of entity tags.
554: * @param etags An array of HttpEntityTag, one per item in the set, or
555: * <strong>null</strong> to reset the header value.
556: */
557:
558: public void setIfMatch(HttpEntityTag etags[]) {
559: setHeaderValue(H_IF_MATCH,
560: ((etags != null) ? new HttpEntityTagList(etags) : null));
561: }
562:
563: /**
564: * Get the conditional none matching entity tags.
565: * @return An entity tag list, encoded as an array of HttpEntityTag, or
566: * <strong>null</strong> if undefined.
567: */
568:
569: public HttpEntityTag[] getIfNoneMatch() {
570: HeaderValue value = getHeaderValue(H_IF_NONE_MATCH);
571: return (value != null) ? (HttpEntityTag[]) value.getValue()
572: : null;
573: }
574:
575: /**
576: * Set the conditional none matching entity tags.
577: * @param etags An array of HttpEntityTag, one per item in the set,
578: * or <strong>null</strong> to reset the value.
579: */
580:
581: public void setIfNoneMatch(HttpEntityTag etags[]) {
582: setHeaderValue(H_IF_NONE_MATCH, ((etags == null) ? null
583: : new HttpEntityTagList(etags)));
584: }
585:
586: /**
587: * Get the if-range conditional if any.
588: * Warning: This API doesn't accept <code>If-Range</code> header that
589: * contains date value (if you want to discuss why, send me email)
590: * @return An HttpEntityTag instance, or <strong>null</strong> if
591: * that header is not defined.
592: */
593:
594: public HttpEntityTag getIfRange() {
595: HeaderValue value = getHeaderValue(H_IF_RANGE);
596: return ((value != null) ? (HttpEntityTag) value : null);
597: }
598:
599: /**
600: * Set the if-range header value.
601: * @param etag The contional etag, or <strong>null</strong> to reset
602: * previous setting.
603: */
604:
605: public void setIfRange(HttpEntityTag etag) {
606: setHeaderValue(H_IF_RANGE, etag);
607: }
608:
609: /**
610: * Get the if unmodified since conditional date.
611: * @return The date encoded as a long number of milliseconds since
612: * Java runtime epoch, or <strong>-1</strong> if undefined.
613: */
614:
615: public long getIfUnmodifiedSince() {
616: HeaderValue value = getHeaderValue(H_IF_UNMODIFIED_SINCE);
617: return (value != null) ? ((Long) value.getValue()).longValue()
618: : -1;
619: }
620:
621: /**
622: * Set the if-unmodified-since conditional date.
623: * @param date The date, encoded as the number of milliseconds since
624: * Java epoch, or <strong>-1</strong> to reset value.
625: */
626:
627: public void setIfUnmodifiedSince(long date) {
628: if (date == -1L) {
629: setHeaderValue(H_IF_UNMODIFIED_SINCE, null);
630: } else {
631: setHeaderValue(H_IF_UNMODIFIED_SINCE, new HttpDate(true,
632: date));
633: }
634: }
635:
636: /**
637: * Get the maximum allowed count of hops for the request.
638: * @return An integer giving the number of hops, or <strong>-1</strong>
639: * if undefined.
640: */
641:
642: public int getMaxForwards() {
643: HeaderValue value = getHeaderValue(H_MAX_FORWARDS);
644: return (value != null) ? ((Integer) value.getValue())
645: .intValue() : -1;
646: }
647:
648: /**
649: * Set the maximum allowed count of hops for that request.
650: * @param hops The hops count, or <strong>-1</strong> to reset value.
651: */
652:
653: public void setMaxForwards(int hops) {
654: if (hops == -1) {
655: setHeaderValue(H_MAX_FORWARDS, null);
656: } else {
657: setHeaderValue(H_MAX_FORWARDS, new HttpInteger(true, hops));
658: }
659: }
660:
661: /**
662: * Set the proxy authorization associated with that request.
663: * @param credentials The credentials, or <strong>null</strong> to
664: * reset the value.
665: */
666:
667: public void setProxyAuthorization(HttpCredential credentials) {
668: setHeaderValue(H_PROXY_AUTHORIZATION, credentials);
669: }
670:
671: /**
672: * Get the authorization associated with this request.
673: * @return An instance of HttpCredential of <strong>null</strong>
674: * if undefined.
675: */
676:
677: public HttpCredential getProxyAuthorization() {
678: HeaderValue value = getHeaderValue(H_PROXY_AUTHORIZATION);
679: return (value != null) ? (HttpCredential) value.getValue()
680: : null;
681: }
682:
683: /**
684: * Get the ranges queried by this request.
685: * @return A list of ranges, encoded as an array of HttpRange instance
686: * or <strong>null</strong> if undefined.
687: */
688:
689: public HttpRange[] getRange() {
690: HeaderValue value = getHeaderValue(H_RANGE);
691: return (value != null) ? (HttpRange[]) value.getValue() : null;
692: }
693:
694: /**
695: * Set the ranges queried by this request.
696: * @param ranges The list of ranges, encoded as an array of instances
697: * of HttpRange, or <strong>null</strong> to reset the value.
698: */
699:
700: public void setRange(HttpRange ranges[]) {
701: setHeaderValue(H_RANGE, ((ranges == null) ? null
702: : new HttpRangeList(ranges)));
703: }
704:
705: /**
706: * Get the referer of the request.
707: * @return A String encoding the referer (generally an URL), or
708: * <strong>null</strong> if undefined.
709: */
710:
711: public String getReferer() {
712: HeaderValue value = getHeaderValue(H_REFERER);
713: return (value != null) ? (String) value.getValue() : null;
714: }
715:
716: /**
717: * Set the referer of this request.
718: * @param referer The referer of the request, or <strong>null</strong>
719: * to reset the value.
720: */
721:
722: public void setReferer(String referer) {
723: setHeaderValue(H_REFERER, ((referer == null) ? null
724: : new HttpString(true, referer)));
725: }
726:
727: /**
728: * Get the user agent String.
729: * @return The user agent description, as a String, or <strong>null
730: * </strong> if undefined.
731: */
732:
733: public String getUserAgent() {
734: HeaderValue value = getHeaderValue(H_USER_AGENT);
735: return (value != null) ? (String) value.getValue() : null;
736: }
737:
738: /**
739: * Set the user agent description header.
740: * @param ua The description of the user agent emiting the request, or
741: * <strong>null</strong> to reset the value.
742: */
743:
744: public void setUserAgent(String ua) {
745: setHeaderValue(H_USER_AGENT, ((ua == null) ? null
746: : new HttpString(true, ua)));
747: }
748:
749: /**
750: * Get this request's Expect header value.
751: * @return This header as a String.
752: */
753:
754: public String getExpect() {
755: HeaderValue value = getHeaderValue(H_EXPECT);
756: return (value != null) ? (String) value.getValue() : null;
757:
758: }
759:
760: /**
761: * Set this request's Expect header.
762: * @param exp The value of the header (ex: "100-continue".
763: */
764:
765: public void setExpect(String expect) {
766: setHeaderValue(H_EXPECT, ((expect == null) ? null
767: : new HttpString(true, expect)));
768: }
769:
770: /**
771: * Get the list of restricted transfer encodings.
772: * @return A list of token describing the restreicted TE, or <strong>
773: * null</strong> if undefined.
774: */
775:
776: public HttpAcceptEncoding[] getTE() {
777: HeaderValue value = getHeaderValue(H_TE);
778: return (value != null) ? (HttpAcceptEncoding[]) value
779: .getValue() : null;
780: }
781:
782: /**
783: * Set the list of restricted transfer encodings
784: * @param encodings The list of accepted encodings, as an array,
785: * of HttpAcceptEncoding or <strong>null</strong> to reset the value.
786: */
787:
788: public void setTE(HttpAcceptEncoding encoding[]) {
789: setHeaderValue(H_TE, ((encoding == null) ? null
790: : new HttpAcceptEncodingList(encoding)));
791: }
792:
793: /**
794: * Set the proxy to use for that request.
795: * @param proxy The proxy's URL, or <strong>null</strong> to reset value.
796: */
797:
798: public void setProxy(URL proxy) {
799: this .proxy = proxy;
800: this .sProxy = (proxy == null) ? null : proxy.toExternalForm();
801: }
802:
803: /**
804: * Get the proxy to use for that request.
805: * @return The proxy's URL, or <strong>null</strong> if none is set.
806: */
807:
808: public URL getProxy() {
809: return proxy;
810: }
811:
812: /**
813: * Will this request use a proxy when executed ?
814: * @return A boolean.
815: */
816:
817: public boolean hasProxy() {
818: return proxy != null;
819: }
820:
821: public HttpRequestMessage(MimeParser parser) {
822: super (parser);
823: }
824:
825: public HttpRequestMessage() {
826: super ();
827: }
828:
829: public static void main(String args[]) {
830: try {
831: HttpRequestMessage r = new HttpRequestMessage();
832: // Set general request headers:
833: r.setURL(new URL("http://www.w3.org/"));
834: r.setHost("http://www.w3.org");
835: r.setFrom("abaird@w3.org");
836: r.setReferer("http://abaird.w3.org/");
837: // Set the cache control directive:
838: HttpCacheControl c = new HttpCacheControl(true);
839: c.setMaxAge(10);
840: c.setNoStore(true);
841: r.setCacheControl(c);
842: // Emit the request:
843: r.emit(System.out);
844: } catch (Exception ex) {
845: ex.printStackTrace();
846: }
847: }
848:
849: }
|