1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-07 16:06:18 +00:00

Move request building outside of OCSMSOwncloudClient, adding HTTPRequestBuilder

This commit is contained in:
Loic Blot 2016-12-06 22:50:39 +01:00
parent 65c2b9b99f
commit a3088e7718
2 changed files with 104 additions and 69 deletions

View File

@ -0,0 +1,93 @@
package fr.unix_experience.owncloud_sms.engine;
/*
* Copyright (c) 2014-2016, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import android.content.Context;
import android.net.Uri;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import java.io.IOException;
class HTTPRequestBuilder {
private final OwnCloudClient _ocClient;
// API v1 calls
private static final String OC_GET_VERSION = "/index.php/apps/ocsms/get/apiversion?format=json";
private static final String OC_GET_ALL_SMS_IDS = "/index.php/apps/ocsms/get/smsidlist?format=json";
private static final String OC_GET_LAST_MSG_TIMESTAMP = "/index.php/apps/ocsms/get/lastmsgtime?format=json";
private static final String OC_PUSH_ROUTE = "/index.php/apps/ocsms/push?format=json";
// API v2 calls
private static final String OC_V2_GET_PHONELIST = "/index.php/apps/ocsms/api/v2/phones/list?format=json";
private static final String OC_V2_GET_MESSAGES ="/index.php/apps/ocsms/api/v2/messages/[START]/[LIMIT]?format=json";
private static final String OC_V2_GET_MESSAGES_PHONE ="/index.php/apps/ocsms/api/v2/messages/[PHONENUMBER]/[START]/[LIMIT]?format=json";
private static final String OC_V2_GET_MESSAGES_SENDQUEUE = "/index.php/apps/ocsms/api/v2/messages/sendqueue?format=json";
HTTPRequestBuilder(Context context, Uri serverURI, String accountName, String accountPassword) {
_ocClient = OwnCloudClientFactory.createOwnCloudClient(
serverURI, context, true);
// Set basic credentials
_ocClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(accountName, accountPassword)
);
}
private GetMethod get(String oc_call) {
GetMethod get = new GetMethod(_ocClient.getBaseUri() + oc_call);
get.addRequestHeader("OCS-APIREQUEST", "true");
return get;
}
GetMethod getAllSmsIds() {
return get(HTTPRequestBuilder.OC_GET_ALL_SMS_IDS);
}
public GetMethod getVersion() {
return get(HTTPRequestBuilder.OC_GET_VERSION);
}
PostMethod pushSms(StringRequestEntity ent) {
PostMethod post = new PostMethod(_ocClient.getBaseUri() + HTTPRequestBuilder.OC_PUSH_ROUTE);
post.addRequestHeader("OCS-APIREQUEST", "true");
post.setRequestEntity(ent);
return post;
}
GetMethod getPhoneList() {
return get(HTTPRequestBuilder.OC_V2_GET_PHONELIST);
}
GetMethod getMessages(Integer start, Integer limit) {
return get(HTTPRequestBuilder.OC_V2_GET_MESSAGES.
replace("[START]", start.toString()).replace("[LIMIT]", limit.toString()));
}
int execute(HttpMethod req) throws IOException {
return _ocClient.executeMethod(req);
}
}

View File

@ -1,7 +1,7 @@
package fr.unix_experience.owncloud_sms.engine; package fr.unix_experience.owncloud_sms.engine;
/* /*
* Copyright (c) 2014-2015, Loic Blot <loic.blot@unix-experience.fr> * Copyright (c) 2014-2016, Loic Blot <loic.blot@unix-experience.fr>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -21,13 +21,8 @@ import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -49,23 +44,13 @@ public class OCSMSOwnCloudClient {
public OCSMSOwnCloudClient(Context context, Uri serverURI, String accountName, String accountPassword) { public OCSMSOwnCloudClient(Context context, Uri serverURI, String accountName, String accountPassword) {
_context = context; _context = context;
_ocClient = OwnCloudClientFactory.createOwnCloudClient(
serverURI, _context, true);
// Set basic credentials
_ocClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(accountName, accountPassword)
);
_serverAPIVersion = 1; _serverAPIVersion = 1;
_http = new HTTPRequestBuilder(context, serverURI, accountName, accountPassword);
_connectivityMonitor = new ConnectivityMonitor(_context); _connectivityMonitor = new ConnectivityMonitor(_context);
} }
public Integer getServerAPIVersion() throws OCSyncException { public Integer getServerAPIVersion() throws OCSyncException {
GetMethod get = createGetVersionRequest(); doHttpRequest(_http.getVersion(), true);
doHttpRequest(get, true);
if (_jsonQueryBuffer == null) { if (_jsonQueryBuffer == null) {
// Return default version // Return default version
return 1; return 1;
@ -82,9 +67,8 @@ public class OCSMSOwnCloudClient {
return _serverAPIVersion; return _serverAPIVersion;
} }
public JSONArray getServerPhoneNumbers() throws OCSyncException { JSONArray getServerPhoneNumbers() throws OCSyncException {
GetMethod get = createGetPhoneListRequest(); doHttpRequest(_http.getPhoneList(), true);
doHttpRequest(get, true);
if (_jsonQueryBuffer == null) { if (_jsonQueryBuffer == null) {
return null; return null;
} }
@ -107,13 +91,12 @@ public class OCSMSOwnCloudClient {
} }
} }
public void doPushRequestV1(JSONArray smsList) throws OCSyncException { private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
// We need to save this date as a step for connectivity change // We need to save this date as a step for connectivity change
Long lastMsgDate = (long) 0; Long lastMsgDate = (long) 0;
if (smsList == null) { if (smsList == null) {
GetMethod get = createGetSmsIdListRequest(); doHttpRequest(_http.getAllSmsIds());
doHttpRequest(get);
if (_jsonQueryBuffer == null) { if (_jsonQueryBuffer == null) {
return; return;
} }
@ -195,29 +178,7 @@ public class OCSMSOwnCloudClient {
Log.i(OCSMSOwnCloudClient.TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage); Log.i(OCSMSOwnCloudClient.TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage);
} }
public GetMethod createGetVersionRequest() { private PostMethod createPushRequest(JSONArray smsList) throws OCSyncException {
return createGetRequest(OCSMSOwnCloudClient.OC_GET_VERSION);
}
public GetMethod createGetPhoneListRequest() {
return createGetRequest(OCSMSOwnCloudClient.OC_V2_GET_PHONELIST);
}
public GetMethod createGetSmsIdListRequest() {
return createGetRequest(OCSMSOwnCloudClient.OC_GET_ALL_SMS_IDS);
}
public GetMethod createGetLastSmsTimestampRequest() {
return createGetRequest(OCSMSOwnCloudClient.OC_GET_LAST_MSG_TIMESTAMP);
}
private GetMethod createGetRequest(String oc_call) {
GetMethod get = new GetMethod(_ocClient.getBaseUri() + oc_call);
get.addRequestHeader("OCS-APIREQUEST", "true");
return get;
}
public PostMethod createPushRequest(JSONArray smsList) throws OCSyncException {
JSONObject obj = createPushJSONObject(smsList); JSONObject obj = createPushJSONObject(smsList);
if (obj == null) { if (obj == null) {
return null; return null;
@ -228,11 +189,7 @@ public class OCSMSOwnCloudClient {
return null; return null;
} }
PostMethod post = new PostMethod(_ocClient.getBaseUri() + OCSMSOwnCloudClient.OC_PUSH_ROUTE); return _http.pushSms(ent);
post.addRequestHeader("OCS-APIREQUEST", "true");
post.setRequestEntity(ent);
return post;
} }
private JSONObject createPushJSONObject(JSONArray smsList) throws OCSyncException { private JSONObject createPushJSONObject(JSONArray smsList) throws OCSyncException {
@ -294,8 +251,7 @@ public class OCSMSOwnCloudClient {
} }
try { try {
status = _ocClient.executeMethod(req); status = _http.execute(req);
Log.i(OCSMSOwnCloudClient.TAG, "HTTP Request done at try " + tryNb); Log.i(OCSMSOwnCloudClient.TAG, "HTTP Request done at try " + tryNb);
// Force loop exit // Force loop exit
@ -352,9 +308,7 @@ public class OCSMSOwnCloudClient {
throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
} }
} }
return;
} }
} else if (status == HttpStatus.SC_FORBIDDEN) { } else if (status == HttpStatus.SC_FORBIDDEN) {
// Authentication failed // Authentication failed
throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH); throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH);
@ -380,25 +334,13 @@ public class OCSMSOwnCloudClient {
private static final int maximumHttpReqTries = 3; private static final int maximumHttpReqTries = 3;
private final OwnCloudClient _ocClient; private final HTTPRequestBuilder _http;
private final Context _context; private final Context _context;
private final ConnectivityMonitor _connectivityMonitor; private final ConnectivityMonitor _connectivityMonitor;
private Integer _serverAPIVersion; private Integer _serverAPIVersion;
private JSONObject _jsonQueryBuffer; private JSONObject _jsonQueryBuffer;
// API v1 calls
private static final String OC_GET_VERSION = "/index.php/apps/ocsms/get/apiversion?format=json";
private static final String OC_GET_ALL_SMS_IDS = "/index.php/apps/ocsms/get/smsidlist?format=json";
private static final String OC_GET_LAST_MSG_TIMESTAMP = "/index.php/apps/ocsms/get/lastmsgtime?format=json";
private static final String OC_PUSH_ROUTE = "/index.php/apps/ocsms/push?format=json";
// API v2 calls
private static final String OC_V2_GET_PHONELIST = "/index.php/apps/ocsms/api/v2/phones/list?format=json";
private static final String OC_V2_GET_MESSAGES ="/index.php/apps/ocsms/api/v2/messages/[START]/[LIMIT]?format=json";
private static final String OC_V2_GET_MESSAGES_PHONE ="/index.php/apps/ocsms/api/v2/messages/[PHONENUMBER]/[START]/[LIMIT]?format=json";
private static final String OC_V2_GET_MESSAGES_SENDQUEUE = "/index.php/apps/ocsms/api/v2/messages/sendqueue?format=json";
private static final String TAG = OCSMSOwnCloudClient.class.getSimpleName(); private static final String TAG = OCSMSOwnCloudClient.class.getSimpleName();