mirror of https://github.com/ThmmyNoLife/mTHMMY
Ezerous
5 years ago
8 changed files with 235 additions and 314 deletions
@ -0,0 +1,86 @@ |
|||||
|
package gr.thmmy.mthmmy.session; |
||||
|
|
||||
|
import org.jsoup.nodes.Document; |
||||
|
import org.jsoup.select.Elements; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.base.BaseApplication; |
||||
|
import gr.thmmy.mthmmy.utils.Parcel; |
||||
|
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; |
||||
|
import gr.thmmy.mthmmy.utils.networking.NetworkTask; |
||||
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
||||
|
import okhttp3.Response; |
||||
|
import timber.log.Timber; |
||||
|
|
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.baseLogoutLink; |
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.indexUrl; |
||||
|
|
||||
|
|
||||
|
public class LogoutTask extends NetworkTask<Void> { |
||||
|
private String logoutLink; |
||||
|
|
||||
|
public LogoutTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) { |
||||
|
super(onTaskStartedListener, onParseTaskFinishedListener); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected Parcel<Void> doInBackground(String... input) { |
||||
|
/* Firstly we will find the logout link |
||||
|
Keep in mind, server changes sesc at will over time for a given session! |
||||
|
*/ |
||||
|
Parcel<Void> parcel = executeInBackground(indexUrl.toString()); |
||||
|
if(parcel.getResultCode() == NetworkResultCodes.SUCCESSFUL) |
||||
|
return executeInBackground(logoutLink); // Now we will attempt to logout
|
||||
|
else return parcel; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected Void performTask(Document document, Response response) { |
||||
|
try { |
||||
|
if(logoutLink==null) |
||||
|
logoutLink = extractLogoutLink(document); |
||||
|
else { // Just for logging purposes
|
||||
|
Elements sessionVerificationFailed = document.select("td:containsOwn(Session " + |
||||
|
"verification failed. Please try logging out and back in again, and then try " + |
||||
|
"again.), td:containsOwn(Η επαλήθευση συνόδου απέτυχε. Παρακαλούμε κάντε " + |
||||
|
"αποσύνδεση, επανασύνδεση και ξαναδοκιμάστε.)"); |
||||
|
if(!sessionVerificationFailed.isEmpty()){ |
||||
|
Timber.i("Logout failed (invalid session)"); |
||||
|
throw new InvalidSessionException(); |
||||
|
} |
||||
|
Elements loginButton = document.select("[value=Login]"); //Attempt to find login button
|
||||
|
if (!loginButton.isEmpty()) //If login button exists, logout was successful
|
||||
|
Timber.i("Logout successful!"); |
||||
|
else |
||||
|
Timber.i("Logout failed"); |
||||
|
} |
||||
|
} catch (InvalidSessionException ise) { |
||||
|
throw ise; |
||||
|
} catch (Exception e) { |
||||
|
throw new ParseException("Parsing failed", e); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onPostExecute(Parcel<Void> voidParcel) { |
||||
|
super.onPostExecute(voidParcel); |
||||
|
//All data should always be cleared from device regardless the result of logout
|
||||
|
BaseApplication.getInstance().getSessionManager().logoutCleanup(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected int getResultCode(Response response, Void v) { |
||||
|
return NetworkResultCodes.SUCCESSFUL; |
||||
|
} |
||||
|
|
||||
|
private String extractLogoutLink(Document document){ |
||||
|
Elements logoutLink = document.select("a[href^=" + baseLogoutLink + "]"); |
||||
|
|
||||
|
if (!logoutLink.isEmpty()) { |
||||
|
String link = logoutLink.first().attr("href"); |
||||
|
if (link != null && !link.isEmpty()) |
||||
|
return link; |
||||
|
} |
||||
|
throw new ParseException("Parsing failed (logoutLink extraction)"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
package gr.thmmy.mthmmy.session; |
||||
|
|
||||
|
import org.jsoup.nodes.Document; |
||||
|
import org.jsoup.select.Elements; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.utils.Parcel; |
||||
|
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; |
||||
|
import gr.thmmy.mthmmy.utils.networking.NetworkTask; |
||||
|
import gr.thmmy.mthmmy.utils.parsing.ParseException; |
||||
|
import okhttp3.Response; |
||||
|
|
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.baseMarkAllAsReadLink; |
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.unreadUrl; |
||||
|
|
||||
|
public class MarkAsReadTask extends NetworkTask<Void> { |
||||
|
private String markAsReadLink; |
||||
|
|
||||
|
public MarkAsReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) { |
||||
|
super(onTaskStartedListener, onParseTaskFinishedListener); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected Parcel<Void> doInBackground(String... input) { |
||||
|
Parcel<Void> parcel = executeInBackground(unreadUrl.toString()); |
||||
|
if(parcel.getResultCode() == NetworkResultCodes.SUCCESSFUL) |
||||
|
return executeInBackground(markAsReadLink); |
||||
|
else return parcel; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected Void performTask(Document document, Response response) { |
||||
|
try { |
||||
|
Elements sessionVerificationFailed = document.select("td:containsOwn(Session " + |
||||
|
"verification failed. Please try logging out and back in again, and then try " + |
||||
|
"again.), td:containsOwn(Η επαλήθευση συνόδου απέτυχε. Παρακαλούμε κάντε " + |
||||
|
"αποσύνδεση, επανασύνδεση και ξαναδοκιμάστε.)"); |
||||
|
if(!sessionVerificationFailed.isEmpty()) |
||||
|
throw new InvalidSessionException(); |
||||
|
if(markAsReadLink==null) |
||||
|
markAsReadLink = extractMarkAsReadLink(document); |
||||
|
|
||||
|
} catch (InvalidSessionException ise) { |
||||
|
throw ise; |
||||
|
} catch (Exception e) { |
||||
|
throw new ParseException("Parsing failed", e); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected int getResultCode(Response response, Void v) { |
||||
|
return NetworkResultCodes.SUCCESSFUL; |
||||
|
} |
||||
|
|
||||
|
private String extractMarkAsReadLink(Document document){ |
||||
|
Elements markAllAsReadLink = document.select("a[href^=" + baseMarkAllAsReadLink + "]"); |
||||
|
|
||||
|
if (!markAllAsReadLink.isEmpty()) { |
||||
|
String link = markAllAsReadLink.first().attr("href"); |
||||
|
if (link != null && !link.isEmpty()) |
||||
|
return link; |
||||
|
} |
||||
|
throw new ParseException("Parsing failed (markAllAsReadLink extraction)"); |
||||
|
} |
||||
|
} |
@ -1,18 +0,0 @@ |
|||||
package gr.thmmy.mthmmy.session; |
|
||||
|
|
||||
import android.os.AsyncTask; |
|
||||
|
|
||||
import gr.thmmy.mthmmy.base.BaseApplication; |
|
||||
|
|
||||
|
|
||||
public class ValidateSessionTask extends AsyncTask<String, Void, Void> { |
|
||||
@Override |
|
||||
protected Void doInBackground(String... params) { |
|
||||
BaseApplication.getInstance().getSessionManager().validateSession(); |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public boolean isRunning(){ |
|
||||
return getStatus() == AsyncTask.Status.RUNNING; |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue