use of com.android.mms.service_alt.exception.MmsHttpException in project qksms by moezbhatti.
the class MmsHttpClient method execute.
/**
* Execute an MMS HTTP request, either a POST (sending) or a GET (downloading)
*
* @param urlString The request URL, for sending it is usually the MMSC, and for downloading
* it is the message URL
* @param pdu For POST (sending) only, the PDU to send
* @param method HTTP method, POST for sending and GET for downloading
* @param isProxySet Is there a proxy for the MMSC
* @param proxyHost The proxy host
* @param proxyPort The proxy port
* @param mmsConfig The MMS config to use
* @return The HTTP response body
* @throws MmsHttpException For any failures
*/
public byte[] execute(String urlString, byte[] pdu, String method, boolean isProxySet, String proxyHost, int proxyPort, MmsConfig.Overridden mmsConfig) throws MmsHttpException {
Timber.d("HTTP: " + method + " " + urlString + (isProxySet ? (", proxy=" + proxyHost + ":" + proxyPort) : "") + ", PDU size=" + (pdu != null ? pdu.length : 0));
checkMethod(method);
HttpURLConnection connection = null;
try {
Proxy proxy = null;
if (isProxySet) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
}
final URL url = new URL(urlString);
// Now get the connection
connection = openConnection(url, proxy);
connection.setDoInput(true);
connection.setConnectTimeout(mmsConfig.getHttpSocketTimeout());
// ------- COMMON HEADERS ---------
// Header: Accept
connection.setRequestProperty(HEADER_ACCEPT, HEADER_VALUE_ACCEPT);
// Header: Accept-Language
connection.setRequestProperty(HEADER_ACCEPT_LANGUAGE, getCurrentAcceptLanguage(Locale.getDefault()));
// Header: User-Agent
final String userAgent = mmsConfig.getUserAgent();
Timber.i("HTTP: User-Agent=" + userAgent);
connection.setRequestProperty(HEADER_USER_AGENT, userAgent);
// Header: x-wap-profile
final String uaProfUrlTagName = mmsConfig.getUaProfTagName();
final String uaProfUrl = mmsConfig.getUaProfUrl();
if (uaProfUrl != null) {
Timber.i("HTTP: UaProfUrl=" + uaProfUrl);
connection.setRequestProperty(uaProfUrlTagName, uaProfUrl);
}
// Add extra headers specified by mms_config.xml's httpparams
addExtraHeaders(connection, mmsConfig);
// Different stuff for GET and POST
if (METHOD_POST.equals(method)) {
if (pdu == null || pdu.length < 1) {
Timber.e("HTTP: empty pdu");
throw new MmsHttpException(0, /*statusCode*/
"Sending empty PDU");
}
connection.setDoOutput(true);
connection.setRequestMethod(METHOD_POST);
if (mmsConfig.getSupportHttpCharsetHeader()) {
connection.setRequestProperty(HEADER_CONTENT_TYPE, HEADER_VALUE_CONTENT_TYPE_WITH_CHARSET);
} else {
connection.setRequestProperty(HEADER_CONTENT_TYPE, HEADER_VALUE_CONTENT_TYPE_WITHOUT_CHARSET);
}
logHttpHeaders(connection.getRequestProperties());
connection.setFixedLengthStreamingMode(pdu.length);
// Sending request body
final OutputStream out = new BufferedOutputStream(connection.getOutputStream());
out.write(pdu);
out.flush();
out.close();
} else if (METHOD_GET.equals(method)) {
logHttpHeaders(connection.getRequestProperties());
connection.setRequestMethod(METHOD_GET);
}
// Get response
final int responseCode = connection.getResponseCode();
final String responseMessage = connection.getResponseMessage();
Timber.d("HTTP: " + responseCode + " " + responseMessage);
logHttpHeaders(connection.getHeaderFields());
if (responseCode / 100 != 2) {
throw new MmsHttpException(responseCode, responseMessage);
}
final InputStream in = new BufferedInputStream(connection.getInputStream());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
final byte[] buf = new byte[4096];
int count = 0;
while ((count = in.read(buf)) > 0) {
byteOut.write(buf, 0, count);
}
in.close();
final byte[] responseBody = byteOut.toByteArray();
Timber.d("HTTP: response size=" + (responseBody != null ? responseBody.length : 0));
return responseBody;
} catch (MalformedURLException e) {
Timber.e(e, "HTTP: invalid URL " + urlString);
throw new MmsHttpException(0, /*statusCode*/
"Invalid URL " + urlString, e);
} catch (ProtocolException e) {
Timber.e(e, "HTTP: invalid URL protocol " + urlString);
throw new MmsHttpException(0, /*statusCode*/
"Invalid URL protocol " + urlString, e);
} catch (IOException e) {
Timber.e(e, "HTTP: IO failure");
throw new MmsHttpException(0, /*statusCode*/
e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
use of com.android.mms.service_alt.exception.MmsHttpException in project qksms by moezbhatti.
the class MmsRequest method execute.
/**
* Execute the request
*
* @param context The context
* @param networkManager The network manager to use
*/
public void execute(Context context, MmsNetworkManager networkManager) {
int result = SmsManager.MMS_ERROR_UNSPECIFIED;
int httpStatusCode = 0;
byte[] response = null;
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
boolean isWifiEnabled = wifi.isWifiEnabled();
if (!useWifi(context)) {
wifi.setWifiEnabled(false);
}
mobileDataEnabled = Utils.isMobileDataEnabled(context);
Timber.v("mobile data enabled: " + mobileDataEnabled);
if (!mobileDataEnabled && !useWifi(context)) {
Timber.v("mobile data not enabled, so forcing it to enable");
Utils.setMobileDataEnabled(context, true);
}
if (!ensureMmsConfigLoaded()) {
// Check mms config
Timber.e("MmsRequest: mms config is not loaded yet");
result = SmsManager.MMS_ERROR_CONFIGURATION_ERROR;
} else if (!prepareForHttpRequest()) {
// Prepare request, like reading pdu data from user
Timber.e("MmsRequest: failed to prepare for request");
result = SmsManager.MMS_ERROR_IO_ERROR;
} else if (!isDataNetworkAvailable(context, mSubId)) {
Timber.e("MmsRequest: in airplane mode or mobile data disabled");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
result = SmsManager.MMS_ERROR_NO_DATA_NETWORK;
} else {
result = 8;
}
} else {
// Execute
long retryDelaySecs = 2;
// Try multiple times of MMS HTTP request
for (int i = 0; i < RETRY_TIMES; i++) {
try {
try {
networkManager.acquireNetwork();
} catch (Exception e) {
Timber.e(e, "error acquiring network");
}
final String apnName = networkManager.getApnName();
try {
ApnSettings apn = null;
try {
apn = ApnSettings.load(context, apnName, mSubId);
} catch (ApnException e) {
// If no APN could be found, fall back to trying without the APN name
if (apnName == null) {
// If the APN name was already null then don't need to retry
throw (e);
}
Timber.i("MmsRequest: No match with APN name:" + apnName + ", try with no name");
apn = ApnSettings.load(context, null, mSubId);
}
Timber.i("MmsRequest: using " + apn.toString());
response = doHttp(context, networkManager, apn);
result = Activity.RESULT_OK;
// Success
break;
} finally {
networkManager.releaseNetwork();
}
} catch (ApnException e) {
Timber.e(e, "MmsRequest: APN failure");
result = SmsManager.MMS_ERROR_INVALID_APN;
break;
// } catch (MmsNetworkException e) {
// Timber.e(e, "MmsRequest: MMS network acquiring failure");
// result = SmsManager.MMS_ERROR_UNABLE_CONNECT_MMS;
// // Retry
} catch (MmsHttpException e) {
Timber.e(e, "MmsRequest: HTTP or network I/O failure");
result = SmsManager.MMS_ERROR_HTTP_FAILURE;
httpStatusCode = e.getStatusCode();
// Retry
} catch (Exception e) {
Timber.e(e, "MmsRequest: unexpected failure");
result = SmsManager.MMS_ERROR_UNSPECIFIED;
break;
}
try {
Thread.sleep(retryDelaySecs * 1000, 0);
} catch (InterruptedException e) {
}
retryDelaySecs <<= 1;
}
}
if (!mobileDataEnabled) {
Timber.v("setting mobile data back to disabled");
Utils.setMobileDataEnabled(context, false);
}
if (!useWifi(context)) {
wifi.setWifiEnabled(isWifiEnabled);
}
processResult(context, result, response, httpStatusCode);
}
Aggregations