Signal-Android/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java

75 lines
2.7 KiB
Java
Raw Normal View History

/**
2011-12-20 10:20:44 -08:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 10:20:44 -08:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2011-12-20 10:20:44 -08:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;
import android.content.Context;
import android.util.Log;
2011-12-20 10:20:44 -08:00
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import ws.com.google.android.mms.MmsException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
2011-12-20 10:20:44 -08:00
public class MmsDownloadHelper extends MmsCommunication {
private static byte[] makeRequest(MmsConnectionParameters connectionParameters, String url)
throws ClientProtocolException, IOException
{
2011-12-20 10:20:44 -08:00
try {
HttpClient client = constructHttpClient(connectionParameters);
2011-12-20 10:20:44 -08:00
URI hostUrl = new URI(url);
HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
HttpRequest request = new HttpGet(url);
2011-12-20 10:20:44 -08:00
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
2011-12-20 10:20:44 -08:00
HttpResponse response = client.execute(target, request);
StatusLine status = response.getStatusLine();
2011-12-20 10:20:44 -08:00
if (status.getStatusCode() != 200)
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
2011-12-20 10:20:44 -08:00
return parseResponse(response.getEntity());
} catch (URISyntaxException use) {
Log.w("MmsDownlader", use);
throw new IOException("Bad URI syntax");
}
}
public static byte[] retrieveMms(Context context, String url, String apn) throws IOException {
2011-12-20 10:20:44 -08:00
try {
MmsConnectionParameters connectionParameters = getMmsConnectionParameters(context, apn);
2011-12-20 10:20:44 -08:00
checkRouteToHost(context, connectionParameters, url);
return makeRequest(connectionParameters, url);
} catch (MmsException me) {
Log.w("MmsDownloader", me);
throw new IOException("Problem configuring MmsConnectionParameters.");
}
}
}