/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package neembuu.uploader.accounts; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import neembuu.uploader.translation.Translation; import neembuu.uploader.exceptions.NUException; import neembuu.uploader.exceptions.accounts.NUInvalidLoginException; import neembuu.uploader.httpclient.NUHttpClient; import neembuu.uploader.httpclient.httprequest.NUHttpGet; import neembuu.uploader.httpclient.httprequest.NUHttpPost; import neembuu.uploader.interfaces.abstractimpl.AbstractAccount; import neembuu.uploader.utils.NULogger; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * * @author dinesh * @author davidepastore */ public class SlingFileAccount extends AbstractAccount { private HttpClient httpclient = NUHttpClient.getHttpClient(); private HttpResponse httpResponse; private NUHttpPost httpPost; private NUHttpGet httpGet; private CookieStore cookieStore; private String stringResponse; public SlingFileAccount() { KEY_USERNAME = "sfusername"; KEY_PASSWORD = "sfpassword"; HOSTNAME = "SlingFile.com"; } @Override public void disableLogin() { resetLogin(); NULogger.getLogger().log(Level.INFO, "{0} account disabled", getHOSTNAME()); } @Override public void login() { loginsuccessful = false; try { initialize(); NULogger.getLogger().info("Trying to log in to slingfile.com"); httpPost = new NUHttpPost("http://www.slingfile.com/login"); //httpPost.setHeader("Cookie", slingfilecookie.toString() + ";signupreferrerurl=http%3A%2F%2Fwww.slingfile.com%2F;"); //httpPost.setHeader("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("f_user", getUsername())); formparams.add(new BasicNameValuePair("f_password", getPassword())); formparams.add(new BasicNameValuePair("submit", "Login ยป")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPost.setEntity(entity); httpResponse = httpclient.execute(httpPost, httpContext); NULogger.getLogger().info(httpResponse.getStatusLine().toString()); Header lastHeader = httpResponse.getLastHeader("Location"); if (lastHeader != null && lastHeader.getValue().contains("dashboard")) { EntityUtils.consume(httpResponse.getEntity()); loginsuccessful = true; username = getUsername(); password = getPassword(); NULogger.getLogger().info("SlingFile login successful!"); } else { //Get error message stringResponse = EntityUtils.toString(httpResponse.getEntity()); //FileUtils.saveInFile("SlingFileAccount.html", stringResponse); Document doc = Jsoup.parse(stringResponse); String error = doc.select("div#container div#mainContent div.errorbox p strong").first().text(); if("This username / password does not exist!".equals(error)){ throw new NUInvalidLoginException(getUsername(), getHOSTNAME()); } //Generic exception throw new Exception("Login error: "+error); } } catch(NUException ex){ resetLogin(); ex.printError(); accountUIShow().setVisible(true); } catch (Exception e) { resetLogin(); NULogger.getLogger().log(Level.SEVERE, "{0}: {1}", new Object[]{getClass().getName(), e}); showWarningMessage( Translation.T().loginerror(), HOSTNAME); accountUIShow().setVisible(true); } } private void initialize() throws Exception { httpContext = new BasicHttpContext(); cookieStore = new BasicCookieStore(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); NULogger.getLogger().info("Getting startup cookies & link from slingfile.com"); httpGet = new NUHttpGet("http://www.slingfile.com/"); httpResponse = httpclient.execute(httpGet, httpContext); stringResponse = EntityUtils.toString(httpResponse.getEntity()); } private void resetLogin(){ loginsuccessful = false; username = ""; password = ""; } }