View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.exchange.auth;
21  
22  import org.apache.log4j.Logger;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.net.HttpURLConnection;
28  import java.net.ProtocolException;
29  import java.net.URL;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Wrapper for HttpURLConnection to fix missing content type and add logging.
35   */
36  public class HttpURLConnectionWrapper extends HttpURLConnection {
37      private static final Logger LOGGER = Logger.getLogger(HttpURLConnectionWrapper.class);
38      HttpURLConnection httpURLConnection;
39  
40      HttpURLConnectionWrapper(HttpURLConnection httpURLConnection, URL url) {
41          super(url);
42          this.httpURLConnection = httpURLConnection;
43      }
44  
45      @Override
46      public void setRequestMethod(String method) throws ProtocolException {
47          httpURLConnection.setRequestMethod(method);
48      }
49  
50      @Override
51      public void setInstanceFollowRedirects(boolean followRedirects) {
52          httpURLConnection.setInstanceFollowRedirects(followRedirects);
53      }
54  
55      @Override
56      public boolean getInstanceFollowRedirects() {
57          return httpURLConnection.getInstanceFollowRedirects();
58      }
59  
60      @Override
61      public String getRequestMethod() {
62          return httpURLConnection.getRequestMethod();
63      }
64  
65      @Override
66      public int getResponseCode() throws IOException {
67          return httpURLConnection.getResponseCode();
68      }
69  
70      @Override
71      public String getResponseMessage() throws IOException {
72          return httpURLConnection.getResponseMessage();
73      }
74  
75      @Override
76      public Map<String, List<String>> getHeaderFields() {
77          LOGGER.debug(httpURLConnection.getHeaderFields());
78          return httpURLConnection.getHeaderFields();
79      }
80  
81      @Override
82      public String getHeaderField(String name) {
83          return httpURLConnection.getHeaderField(name);
84      }
85  
86      @Override
87      public String getHeaderField(int n) {
88          return httpURLConnection.getHeaderField(n);
89      }
90  
91      @Override
92      public void disconnect() {
93          httpURLConnection.disconnect();
94      }
95  
96      @Override
97      public void setDoOutput(boolean dooutput) {
98          httpURLConnection.setDoOutput(dooutput);
99      }
100 
101     @Override
102     public boolean usingProxy() {
103         return httpURLConnection.usingProxy();
104     }
105 
106     @Override
107     public void connect() throws IOException {
108         try {
109             httpURLConnection.connect();
110         } catch (IOException e) {
111             LOGGER.error(e.getMessage());
112             throw e;
113         }
114     }
115 
116     @Override
117     public InputStream getInputStream() throws IOException {
118         return httpURLConnection.getInputStream();
119     }
120 
121     @Override
122     public OutputStream getOutputStream() throws IOException {
123         return httpURLConnection.getOutputStream();
124     }
125 
126     @Override
127     public InputStream getErrorStream() {
128         return httpURLConnection.getErrorStream();
129     }
130 
131     @Override
132     public void setRequestProperty(String key, String value) {
133         httpURLConnection.setRequestProperty(key, value);
134     }
135 
136     @Override
137     public void addRequestProperty(String key, String value) {
138         httpURLConnection.setRequestProperty(key, value);
139     }
140 
141     @Override
142     public Map<String, List<String>> getRequestProperties() {
143         return httpURLConnection.getRequestProperties();
144     }
145 
146     @Override
147     public String getRequestProperty(String key) {
148         return httpURLConnection.getRequestProperty(key);
149     }
150 
151     /**
152      * Fix missing content type
153      * @return content type or text/html if missing
154      */
155     @Override
156     public String getContentType() {
157         final String contentType = httpURLConnection.getContentType();
158         // workaround for missing content type
159         if (contentType == null && getContentLength() > 0) {
160             LOGGER.debug("Fix missing content-type at "+url.toString());
161             return "text/html";
162         }
163         return contentType;
164     }
165 }