View Javadoc

1   package com.atlassian.httpclient.apache.httpcomponents;
2   
3   import com.atlassian.httpclient.api.Buildable;
4   import com.google.common.base.Preconditions;
5   import org.apache.http.protocol.HTTP;
6   
7   import java.nio.charset.Charset;
8   import java.util.Collections;
9   import java.util.Map;
10  
11  import static com.google.common.collect.Maps.newHashMap;
12  
13  public class Headers {
14      private final Map<String, String> headers;
15      private final String contentCharset;
16      private final String contentType;
17  
18      private Headers(Map<String, String> headers, String contentCharset, String contentType) {
19          this.headers = headers;
20          this.contentCharset = contentCharset;
21          this.contentType = contentType;
22      }
23  
24      public String getContentCharset() {
25          return contentCharset;
26      }
27  
28      public String getContentType() {
29          return contentType;
30      }
31  
32      public Map<String, String> getHeaders() {
33          Map<String, String> headers = newHashMap(this.headers);
34          if (contentType != null) {
35              headers.put(Names.CONTENT_TYPE, buildContentType());
36          }
37          return Collections.unmodifiableMap(headers);
38      }
39  
40      public String getHeader(final String name) {
41          String value;
42          if (name.equalsIgnoreCase(Names.CONTENT_TYPE)) {
43              value = buildContentType();
44          } else {
45              value = headers.get(name);
46          }
47          return value;
48      }
49  
50      private String buildContentType() {
51          String value = contentType != null ? contentType : "text/plain";
52          if (contentCharset != null) {
53              value += "; charset=" + contentCharset;
54          }
55          return value;
56      }
57  
58      public static class Builder implements Buildable<Headers> {
59          private final Map<String, String> headers = newHashMap();
60          private String contentType;
61          private String contentCharset;
62  
63          public Builder setHeaders(Map<String, String> headers) {
64              this.headers.clear();
65              for (Map.Entry<String, String> entry : headers.entrySet()) {
66                  setHeader(entry.getKey(), entry.getValue());
67              }
68              return this;
69          }
70  
71          public Builder setHeader(String name, String value) {
72              if (name.equalsIgnoreCase("Content-Type")) {
73                  parseContentType(value);
74              } else {
75                  headers.put(name, value);
76              }
77              return this;
78          }
79  
80          public Builder setContentLength(long contentLength) {
81              Preconditions.checkArgument(contentLength >= 0, "Content-Length must be greater than or equal to 0");
82              setHeader(Names.CONTENT_LENGTH, Long.toString(contentLength));
83              return this;
84          }
85  
86          public Builder setContentCharset(String contentCharset) {
87              this.contentCharset = contentCharset != null ? Charset.forName(contentCharset).name() : null;
88              return this;
89          }
90  
91          public Builder setContentType(String contentType) {
92              parseContentType(contentType);
93              return this;
94          }
95  
96          private void parseContentType(String value) {
97              if (value != null) {
98                  String[] parts = value.split(";");
99                  if (parts.length >= 1) {
100                     contentType = parts[0].trim();
101                 }
102                 if (parts.length >= 2) {
103                     String subtype = parts[1].trim();
104                     if (subtype.startsWith("charset=")) {
105                         setContentCharset(subtype.substring(8));
106                     } else if (subtype.startsWith("boundary=")) {
107                         contentType = contentType.concat(';' + subtype);
108                     }
109                 }
110             } else {
111                 contentType = null;
112             }
113         }
114 
115         @Override
116         public Headers build() {
117             return new Headers(headers, contentCharset, contentType);
118         }
119     }
120 
121     public static class Names {
122         public static final String CONTENT_LENGTH = HTTP.CONTENT_LEN;
123         public static final String CONTENT_TYPE = HTTP.CONTENT_TYPE;
124 
125         private Names() {
126         }
127     }
128 }