1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.client.protocol;
29
30 import java.io.IOException;
31 import java.util.Collection;
32
33 import org.apache.http.Header;
34 import org.apache.http.HttpException;
35 import org.apache.http.HttpRequest;
36 import org.apache.http.HttpRequestInterceptor;
37 import org.apache.http.annotation.Contract;
38 import org.apache.http.annotation.ThreadingBehavior;
39 import org.apache.http.client.params.ClientPNames;
40 import org.apache.http.protocol.HttpContext;
41 import org.apache.http.util.Args;
42
43
44
45
46
47
48 @SuppressWarnings("deprecation")
49 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
50 public class RequestDefaultHeaders implements HttpRequestInterceptor {
51
52 private final Collection<? extends Header> defaultHeaders;
53
54
55
56
57 public RequestDefaultHeaders(final Collection<? extends Header> defaultHeaders) {
58 super();
59 this.defaultHeaders = defaultHeaders;
60 }
61
62 public RequestDefaultHeaders() {
63 this(null);
64 }
65
66 @Override
67 public void process(final HttpRequest request, final HttpContext context)
68 throws HttpException, IOException {
69 Args.notNull(request, "HTTP request");
70
71 final String method = request.getRequestLine().getMethod();
72 if (method.equalsIgnoreCase("CONNECT")) {
73 return;
74 }
75
76
77 @SuppressWarnings("unchecked")
78 Collection<? extends Header> defHeaders = (Collection<? extends Header>)
79 request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS);
80 if (defHeaders == null) {
81 defHeaders = this.defaultHeaders;
82 }
83
84 if (defHeaders != null) {
85 for (final Header defHeader : defHeaders) {
86 request.addHeader(defHeader);
87 }
88 }
89 }
90
91 }