summaryrefslogtreecommitdiff
path: root/src/plugins/common/src/com/motorola/studio/android/common/utilities/HttpUtils.java
blob: 8fb7238b8d9f3730174244534876a050181d67cd (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.motorola.studio.android.common.utilities;

import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.eclipse.core.internal.net.ProxyManager;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.ui.internal.net.auth.NetAuthenticator;

import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.i18n.UtilitiesNLS;
import com.motorola.studio.android.common.utilities.ui.LoginPasswordDialogCreator;

/**
 * Class for opening an input stream with the given URL.
 */
@SuppressWarnings("restriction")
public class HttpUtils
{
    /**
     * 1 second if the unit is milliseconds.
     */
    private static final int ONE_SECOND = 1000;

    // map of credentials authentication so the user is not repeatedly asked for them
    private static final Map<String, Credentials> authenticationRealmCache =
            new HashMap<String, Credentials>();

    private GetMethod getMethod;

    /**
     * Retrieves an open InputStream with the contents of the file pointed by the given url.
     * 
     * @param url The address from where to retrieve the InputStream
     * @param monitor The monitor to progress while accessing the file
     * 
     * @return The open InputStream object, or <code>null</code> if no file was found
     * 
     * @throws IOException if some error occurs with the network communication
     */
    public InputStream getInputStreamForUrl(String url, IProgressMonitor monitor)
            throws IOException
    {
        return getInputStreamForUrl(url, monitor, true);
    }

    private InputStream getInputStreamForUrl(String url, IProgressMonitor monitor,
            boolean returnStream) throws IOException
    {
        SubMonitor subMonitor = SubMonitor.convert(monitor);

        subMonitor.beginTask(UtilitiesNLS.HttpUtils_MonitorTask_PreparingConnection, 300);

        StudioLogger.debug(HttpUtils.class, "Verifying proxy usage for opening http connection"); //$NON-NLS-1$

        // Try to retrieve proxy configuration to use if necessary
        IProxyService proxyService = ProxyManager.getProxyManager();
        IProxyData proxyData = null;
        if (proxyService.isProxiesEnabled() || proxyService.isSystemProxiesEnabled())
        {
            Authenticator.setDefault(new NetAuthenticator());
            if (url.startsWith("https"))
            {
                proxyData = proxyService.getProxyData(IProxyData.HTTPS_PROXY_TYPE);
                StudioLogger.debug(HttpUtils.class, "Using https proxy"); //$NON-NLS-1$
            }
            else if (url.startsWith("http"))
            {
                proxyData = proxyService.getProxyData(IProxyData.HTTP_PROXY_TYPE);
                StudioLogger.debug(HttpUtils.class, "Using http proxy"); //$NON-NLS-1$
            }
            else
            {
                StudioLogger.debug(HttpUtils.class, "Not using any proxy"); //$NON-NLS-1$
            }
        }

        // Creates the http client and the method to be executed
        HttpClient client = null;
        client = new HttpClient();

        // If there is proxy data, work with it
        if (proxyData != null)
        {
            if (proxyData.getHost() != null)
            {
                // Sets proxy host and port, if any
                client.getHostConfiguration().setProxy(proxyData.getHost(), proxyData.getPort());
            }

            if ((proxyData.getUserId() != null) && (proxyData.getUserId().trim().length() > 0))
            {
                // Sets proxy user and password, if any
                Credentials cred =
                        new UsernamePasswordCredentials(proxyData.getUserId(),
                                proxyData.getPassword() == null ? "" : proxyData.getPassword()); //$NON-NLS-1$
                client.getState().setProxyCredentials(AuthScope.ANY, cred);
            }
        }

        InputStream streamForUrl = null;
        getMethod = new GetMethod(url);
        getMethod.setFollowRedirects(true);

        // set a 30 seconds timeout
        HttpMethodParams params = getMethod.getParams();
        params.setSoTimeout(15 * ONE_SECOND);
        getMethod.setParams(params);

        boolean trying = true;
        Credentials credentials = null;
        subMonitor.worked(100);
        subMonitor.setTaskName(UtilitiesNLS.HttpUtils_MonitorTask_ContactingSite);
        do
        {
            StudioLogger.info(HttpUtils.class, "Attempting to make a connection"); //$NON-NLS-1$

            // retry to connect to the site once, also set the timeout for 5 seconds
            HttpClientParams clientParams = client.getParams();
            clientParams.setIntParameter(HttpClientParams.MAX_REDIRECTS, 1);
            clientParams.setSoTimeout(5 * ONE_SECOND);
            client.setParams(clientParams);

            client.executeMethod(getMethod);
            if (subMonitor.isCanceled())
            {
                break;
            }
            else
            {
                AuthState authorizationState = getMethod.getHostAuthState();
                String authenticationRealm = authorizationState.getRealm();

                if (getMethod.getStatusCode() == HttpStatus.SC_UNAUTHORIZED)
                {
                    StudioLogger.debug(HttpUtils.class,
                            "Client requested authentication; retrieving credentials"); //$NON-NLS-1$

                    credentials = authenticationRealmCache.get(authenticationRealm);

                    if (credentials == null)
                    {
                        StudioLogger.debug(HttpUtils.class,
                                "Credentials not found; prompting user for login/password"); //$NON-NLS-1$

                        subMonitor
                                .setTaskName(UtilitiesNLS.HttpUtils_MonitorTask_WaitingAuthentication);

                        LoginPasswordDialogCreator dialogCreator =
                                new LoginPasswordDialogCreator(url);
                        if (dialogCreator.openLoginPasswordDialog() == LoginPasswordDialogCreator.OK)
                        {

                            credentials =
                                    new UsernamePasswordCredentials(dialogCreator.getTypedLogin(),
                                            dialogCreator.getTypedPassword());
                        }
                        else
                        {
                            // cancel pressed; stop trying
                            trying = false;

                            // set the monitor canceled to be able to stop process
                            subMonitor.setCanceled(true);
                        }

                    }

                    if (credentials != null)
                    {
                        AuthScope scope = new AuthScope(null, -1, authenticationRealm);
                        client.getState().setCredentials(scope, credentials);
                    }

                    subMonitor.worked(100);
                }
                else if (getMethod.getStatusCode() == HttpStatus.SC_OK)
                {
                    StudioLogger.debug(HttpUtils.class, "Http connection suceeded"); //$NON-NLS-1$

                    subMonitor
                            .setTaskName(UtilitiesNLS.HttpUtils_MonitorTask_RetrievingSiteContent);
                    if ((authenticationRealm != null) && (credentials != null))
                    {
                        authenticationRealmCache.put(authenticationRealm, credentials);
                    }
                    else
                    {
                        // no authentication was necessary, just work the monitor
                        subMonitor.worked(100);
                    }

                    StudioLogger.info(HttpUtils.class, "Retrieving site content"); //$NON-NLS-1$

                    // if the stream should not be returned (ex: only testing the connection is
                    // possible), then null will be returned
                    if (returnStream)
                    {
                        streamForUrl = getMethod.getResponseBodyAsStream();
                    }

                    // succeeded; stop trying
                    trying = false;

                    subMonitor.worked(100);
                }
                else
                {
                    // unhandled return status code
                    trying = false;

                    subMonitor.worked(200);
                }
            }
        }
        while (trying);

        subMonitor.done();

        return streamForUrl;
    }

    /**
     * Check if a connection with the given URL can be established.
     * 
     * @param url The URL to test the connection.
     * 
     * @return <code>true</code> if the connection can be established; <code>false</code> otherwise 
     */
    public boolean isConnectionOk(String url)
    {
        try
        {
            getInputStreamForUrl(url, null, false);
            // no need to release connection since the stream has not been retrieved
            // if the code above does not throw any exception, the connection is fine 
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    /**
     * Release the http connection after users finished reading the InputStream
     * provided by the {@link #getInputStreamForUrl(String, IProgressMonitor)}
     * method.
     */
    public void releaseConnection()
    {
        if (getMethod != null)
        {
            Thread t = new Thread()
            {
                /* (non-Javadoc)
                 * @see java.lang.Thread#run()
                 */
                @Override
                public void run()
                {
                    getMethod.releaseConnection();
                }
            };
            t.start();

        }
    }
}