summaryrefslogtreecommitdiffstats
path: root/src/com/android/exchange/EasResponse.java
blob: 5af80fddc56a0e2aadecde540b7be621300d3163 (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
/*
 * Copyright (C) 2008-2009 Marc Blank
 * Licensed to 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.android.exchange;

import android.net.Uri;

import com.android.emailcommon.utility.EmailClientConnectionManager;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/**
 * Encapsulate a response to an HTTP POST
 */
public class EasResponse {
    // MSFT's custom HTTP result code indicating the need to provision
    static private final int HTTP_NEED_PROVISIONING = 449;

    // Microsoft-defined HTTP response indicating a redirect to a "better" server.
    // Why is this a 4xx instead of 3xx? Because EAS considers this a "Device misconfigured" error.
    static private final int HTTP_REDIRECT = 451;

    private final HttpResponse mResponse;
    private final HttpEntity mEntity;
    private final int mLength;
    private InputStream mInputStream;
    private boolean mClosed;

    private final int mStatus;

    /**
     * Whether or not a certificate was requested by the server and missing.
     * If this is set, it is essentially a 403 whereby the failure was due
     */
    private final boolean mClientCertRequested;

    private EasResponse(final HttpResponse response,
            final EmailClientConnectionManager connManager, final long reqTime) {
        mResponse = response;
        mEntity = (response == null) ? null : mResponse.getEntity();
        if (mEntity !=  null) {
            mLength = (int) mEntity.getContentLength();
        } else {
            mLength = 0;
        }
        int status = response.getStatusLine().getStatusCode();
        mClientCertRequested =
                isAuthError(status) && connManager.hasDetectedUnsatisfiedCertReq(reqTime);
        if (mClientCertRequested) {
            status = HttpStatus.SC_UNAUTHORIZED;
            mClosed = true;
        }
        mStatus = status;
    }

    public static EasResponse fromHttpRequest(
            EmailClientConnectionManager connManager, HttpClient client, HttpUriRequest request)
            throws IOException {
        final long reqTime = System.currentTimeMillis();
        final HttpResponse response = client.execute(request);
        return new EasResponse(response, connManager, reqTime);
    }

    public boolean isSuccess() {
        return mStatus == HttpStatus.SC_OK;
    }

    public boolean isForbidden() {
        return mStatus == HttpStatus.SC_FORBIDDEN;
    }

    /**
     * @return Whether this response indicates an authentication error.
     */
    public boolean isAuthError() {
        return mStatus == HttpStatus.SC_UNAUTHORIZED;
    }

    /**
     * @return Whether this response indicates a provisioning error.
     */
    public boolean isProvisionError() {
        return (mStatus == HTTP_NEED_PROVISIONING) || isForbidden();
    }

    /**
     * @return Whether this response indicates a redirect error.
     */
    public boolean isRedirectError() {
        return mStatus == HTTP_REDIRECT;
    }

    /**
     * Determine whether an HTTP code represents an authentication error
     * @param code the HTTP code returned by the server
     * @return whether or not the code represents an authentication error
     */
    private static boolean isAuthError(int code) {
        return (code == HttpStatus.SC_UNAUTHORIZED) || (code == HttpStatus.SC_FORBIDDEN);
    }

    /**
     * Read the redirect address from this response, if it's present.
     * @return The new host address, or null if it's not there.
     */
    public String getRedirectAddress() {
        final Header locHeader = getHeader("X-MS-Location");
        if (locHeader != null) {
            return Uri.parse(locHeader.getValue()).getHost();
        }
        return null;
    }

    /**
     * Return an appropriate input stream for the response, either a GZIPInputStream, for
     * compressed data, or a generic InputStream otherwise
     * @return the input stream for the response
     */
    public InputStream getInputStream() {
        if (mInputStream != null || mClosed) {
            throw new IllegalStateException("Can't reuse stream or get closed stream");
        } else if (mEntity == null) {
            throw new IllegalStateException("Can't get input stream without entity");
        }
        InputStream is = null;
        try {
            // Get the default input stream for the entity
            is = mEntity.getContent();
            Header ceHeader = mResponse.getFirstHeader("Content-Encoding");
            if (ceHeader != null) {
                String encoding = ceHeader.getValue();
                // If we're gzip encoded, wrap appropriately
                if (encoding.toLowerCase().equals("gzip")) {
                    is = new GZIPInputStream(is);
                }
            }
        } catch (IllegalStateException e1) {
        } catch (IOException e1) {
        }
        mInputStream = is;
        return is;
    }

    public boolean isEmpty() {
        return mLength == 0;
    }

    public int getStatus() {
        return mStatus;
    }

    public boolean isMissingCertificate() {
        return mClientCertRequested;
    }

    public Header getHeader(String name) {
        return (mResponse == null) ? null : mResponse.getFirstHeader(name);
    }

    public int getLength() {
        return mLength;
    }

    public void close() {
        if (!mClosed) {
            if (mEntity != null) {
                try {
                    mEntity.consumeContent();
                } catch (IOException e) {
                    // No harm, no foul
                }
            }
            if (mInputStream instanceof GZIPInputStream) {
                try {
                    mInputStream.close();
                } catch (IOException e) {
                    // We tried
                }
            }
            mClosed = true;
        }
    }
}