https for unsigned certificates in logging and uploading
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / FileDownloader.java
1 package eu.alefzero.owncloud;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.UnknownHostException;
9 import java.util.Date;
10
11 import org.apache.http.HttpHost;
12 import org.apache.http.HttpResponse;
13 import org.apache.http.auth.AuthScope;
14 import org.apache.http.auth.UsernamePasswordCredentials;
15 import org.apache.http.client.ClientProtocolException;
16 import org.apache.http.client.methods.HttpGet;
17 import org.apache.http.impl.auth.BasicScheme;
18 import org.apache.http.impl.client.DefaultHttpClient;
19 import org.apache.http.protocol.BasicHttpContext;
20
21 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
22 import eu.alefzero.webdav.HttpPropFind;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.accounts.AuthenticatorException;
27 import android.accounts.OperationCanceledException;
28 import android.app.Notification;
29 import android.app.NotificationManager;
30 import android.app.PendingIntent;
31 import android.app.Service;
32 import android.content.Intent;
33 import android.net.Uri;
34 import android.os.Environment;
35 import android.os.IBinder;
36 import android.util.Log;
37 import android.widget.FrameLayout;
38
39 public class FileDownloader extends Service {
40 static final String EXTRA_ACCOUNT = "ACCOUNT";
41 static final String EXTRA_FILE_PATH = "FILE_PATH";
42 static final String TAG = "OC_FileDownloader";
43
44 NotificationManager nm;
45
46 @Override
47 public IBinder onBind(Intent arg0) {
48 return null;
49 }
50
51 @Override
52 public int onStartCommand(Intent intent, int flags, int startId) {
53 if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_FILE_PATH)) {
54 Log.e(TAG, "Not enough information provided in intent");
55 return START_NOT_STICKY;
56 }
57
58 nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
59
60 Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
61 String file_path = intent.getStringExtra(EXTRA_FILE_PATH);
62 AccountManager am = (AccountManager)getSystemService(ACCOUNT_SERVICE);
63 Uri oc_url = Uri.parse(am.getUserData(account, AccountAuthenticator.KEY_OC_URL));
64
65 DefaultHttpClient client = new DefaultHttpClient();
66 Log.d(TAG, oc_url.toString());
67 HttpGet query = new HttpGet(oc_url + file_path);
68 query.setHeader("Content-type", "text/xml");
69 query.setHeader("User-Agent", "Android-ownCloud");
70
71 BasicHttpContext httpContext = new BasicHttpContext();
72 BasicScheme basicAuth = new BasicScheme();
73 httpContext.setAttribute("preemptive-auth", basicAuth);
74
75 String username = account.name.split("@")[0];
76 String password = "";
77 try {
78 password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
79 } catch (OperationCanceledException e1) {
80 // TODO Auto-generated catch block
81 e1.printStackTrace();
82 } catch (AuthenticatorException e1) {
83 // TODO Auto-generated catch block
84 e1.printStackTrace();
85 } catch (IOException e1) {
86 // TODO Auto-generated catch block
87 e1.printStackTrace();
88 }
89 if (am.getUserData(account, AccountAuthenticator.KEY_OC_URL) == null) {
90
91 }
92
93 client.getCredentialsProvider().setCredentials(
94 new AuthScope(oc_url.getHost(), oc_url.getPort()==-1?80:oc_url.getPort()),
95 new UsernamePasswordCredentials(username, password)
96 );
97
98 HttpHost host = new HttpHost(oc_url.getHost(), oc_url.getPort()==-1?80:oc_url.getPort());
99
100 Notification n = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
101 PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, OwnCloudMainScreen.class), 0);
102 n.setLatestEventInfo(this, "A", "B", pi);
103 nm.notify(1, n);
104
105 HttpResponse response = null;
106 try {
107 response = client.execute(host, query, httpContext);
108 } catch (ClientProtocolException e) {
109 // TODO Auto-generated catch block
110 e.printStackTrace();
111 } catch (IOException e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 }
115
116 File sdCard = Environment.getExternalStorageDirectory();
117 File dir = new File (sdCard.getAbsolutePath() + "/owncloud");
118 dir.mkdirs();
119 File file = new File(dir, "filename");
120
121 try {
122 FileOutputStream f = new FileOutputStream(file);
123 byte[] b = new byte[(int)response.getEntity().getContentLength()];
124 response.getEntity().getContent().read(b);
125 f.write(b);
126 } catch (FileNotFoundException e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 } catch (IllegalStateException e) {
130 // TODO Auto-generated catch block
131 e.printStackTrace();
132 } catch (IOException e) {
133 // TODO Auto-generated catch block
134 e.printStackTrace();
135 }
136
137
138 return START_NOT_STICKY;
139 }
140
141 }