commit 6e2495180f140cfe5514e3b691b94fbfc211ce85
parent ff9d41bd4c1262c853abe593785195f755babe76
Author: dankert <devnull@localhost>
Date: Mon, 17 Oct 2011 21:35:58 +0200
Client-Request über die Klasse OpenratClient verarbeiten.
Diffstat:
9 files changed, 186 insertions(+), 174 deletions(-)
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
@@ -4,7 +4,7 @@
android:versionName="1.0">
<application android:icon="@drawable/logo" android:label="@string/app_name">
- <activity android:name=".OpenRatBlog" android:label="@string/connect">
+ <activity android:name=".OpenRatBlog" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
@@ -46,9 +46,9 @@
<string name="filename">Dateiname</string>
<string name="template">Vorlage</string>
<string name="save">Speichern</string>
- <string name="project_list">Projetliste</string>
- <string name="folder_content">Ordnerinhalt</string>
+ <string name="project_list">Projekte</string>
+ <string name="folder_content">Ordner</string>
<string name="folder_properties">Eigenschaften</string>
- <string name="page_content">Seiteninhalt</string>
- <string name="editor">Inhalt bearbeiten</string>
+ <string name="page_content">Seite</string>
+ <string name="editor">Inhalt</string>
</resources>
diff --git a/src/de/openrat/android/blog/NewActivity.java b/src/de/openrat/android/blog/NewActivity.java
@@ -5,11 +5,12 @@ import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import de.openrat.client.CMSRequest;
+import de.openrat.client.OpenRatClient;
public class NewActivity extends Activity
{
- private CMSRequest request;
+ private OpenRatClient request;
private int menuid;
/**
@@ -21,7 +22,7 @@ public class NewActivity extends Activity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
- request = (CMSRequest) getIntent().getSerializableExtra("request");
+ request = (OpenRatClient) getIntent().getSerializableExtra("request");
menuid = getIntent().getIntExtra("menuid", 0);
setContentView(R.layout.new1);
diff --git a/src/de/openrat/android/blog/OpenRatBlog.java b/src/de/openrat/android/blog/OpenRatBlog.java
@@ -34,6 +34,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
+import android.widget.Toast;
import de.openrat.client.CMSRequest;
import de.openrat.client.OpenRatClient;
@@ -43,7 +44,7 @@ import de.openrat.client.OpenRatClient;
public class OpenRatBlog extends Activity
{
private static final String PREFS_NAME = "OR_BLOG_PREFS";
- private CMSRequest request;
+ private OpenRatClient client;
/** Called when the activity is first created. */
@Override
@@ -51,82 +52,54 @@ public class OpenRatBlog extends Activity
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
- setTitle(R.string.app_name);
- SharedPreferences prefs = PreferenceManager
+ final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
int port = Integer.parseInt(prefs.getString("port", "80"));
String path = prefs.getString("path", "/");
String host = prefs.getString("hostname", "");
- request = new OpenRatClient(host, path, port);
-
- request.setParameter("action", "index");
- request.setParameter("subaction", "login");
- request.setParameter("dbid", "db1");
- request.setParameter("login_name", prefs.getString("username", ""));
- request.setParameter("login_password", prefs.getString("password", ""));
- String response = null;
- try
- {
- ProgressDialog dialog = ProgressDialog.show(OpenRatBlog.this,
- getResources().getString(R.string.loading), getResources()
- .getString(R.string.waitingforlogin));
- response = request.performRequest();
- dialog.dismiss();
+ client = new OpenRatClient(host, path, port);
- } catch (IOException e)
- {
- response = e.getMessage();
- }
+ @SuppressWarnings("unused")
+ TextView tv = (TextView) findViewById(R.id.hello);
- try
+ View connect = findViewById(R.id.connect);
+ connect.setOnClickListener(new OnClickListener()
{
- JSONObject json = new JSONObject(response);
- JSONObject session = json.getJSONObject("session");
- final String sessionName = session.getString("name");
- final String sessionId = session.getString("id");
-
- final String msgText = json.getJSONArray("notices")
- .getJSONObject(0).getString("text");
- // final String msgText2 = json.getJSONArray("notics")
- // .getJSONObject(0).getString("text");
-
- // TextView text = new TextView(this);
- // text.setText("Sitzung '" + sessionId + "': " + sessionId);
-
- request.setCookie(sessionName, sessionId);
- TextView tv = (TextView) findViewById(R.id.hello);
- // tv.setText(msgText + "\nSitzung '" + sessionName + "': "
- // + sessionId + "\nAusgabe: " + response);
-
- View connect = findViewById(R.id.connect);
- connect.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
+ @Override
+ public void onClick(View v)
+ {
+ try
{
- Intent intent = new Intent(v.getContext(),
- ProjectActivity.class);
- intent.putExtra(ProjectActivity.CLIENT, request);
- startActivity(intent);
- }
- });
+ final ProgressDialog dialog = ProgressDialog.show(
+ OpenRatBlog.this, getResources().getString(
+ R.string.loading), getResources()
+ .getString(R.string.waitingforlogin));
- } catch (Exception e)
- {
- response = e.getMessage();
+ client.login(prefs.getString("username", ""), prefs
+ .getString("password", ""));
- TextView tv = (TextView) findViewById(R.id.hello);
- tv.setText("Fehler: " + response);
+ dialog.dismiss();
- }
+ // Verbindung und Login waren erfolgreich.
+ // Jetzt zur Projekt-Liste wechseln.
+ final Intent intent = new Intent(v.getContext(),
+ ProjectActivity.class);
+ intent.putExtra(ProjectActivity.CLIENT, client);
+ startActivity(intent);
+ }
+ catch (IOException e1)
+ {
+ // Verbindung nicht möglich...
+ Toast.makeText(OpenRatBlog.this, e1.getMessage(),
+ Toast.LENGTH_LONG);
+ }
- // Restore preferences
- // SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
- // boolean silent = settings.getBoolean("silentMode", false);
+ }
+ });
}
@@ -144,8 +117,8 @@ public class OpenRatBlog extends Activity
{
switch (item.getItemId())
{
- case R.id.menu_preferences:
- startActivity(new Intent(this, Configuration.class));
+ case R.id.menu_preferences:
+ startActivity(new Intent(this, Configuration.class));
}
return false;
}
diff --git a/src/de/openrat/android/blog/PublishService.java b/src/de/openrat/android/blog/PublishService.java
@@ -1,18 +0,0 @@
-package de.openrat.android.blog;
-
-import android.app.IntentService;
-import android.content.Intent;
-
-public class PublishService extends IntentService
-{
- public PublishService(String name)
- {
- super(name);
- }
-
- @Override
- protected void onHandleIntent(Intent intent)
- {
- }
-
-}
diff --git a/src/de/openrat/android/blog/service/PublishIntentService.java b/src/de/openrat/android/blog/service/PublishIntentService.java
@@ -5,18 +5,16 @@ package de.openrat.android.blog.service;
import java.io.IOException;
-import org.json.JSONException;
-import org.json.JSONObject;
-
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
+import android.util.Log;
import de.openrat.android.blog.FolderActivity;
import de.openrat.android.blog.R;
-import de.openrat.client.CMSRequest;
+import de.openrat.client.OpenRatClient;
/**
* @author dankert
@@ -45,88 +43,61 @@ public class PublishIntentService extends IntentService
@Override
protected void onHandleIntent(Intent intent)
{
- final CMSRequest request = (CMSRequest) intent
+ final OpenRatClient request = (OpenRatClient) intent
.getSerializableExtra(EXTRA_REQUEST);
String type = intent.getStringExtra(EXTRA_TYPE);
String id = intent.getStringExtra(EXTRA_ID);
String name = intent.getStringExtra(EXTRA_NAME);
- request.clearParameters();
- request.setAction(type );
- request.setActionMethod("pub");
- request.setId(id);
-
// Erstmal alles aktivieren was geht
// TODO: Abfrage der gewünschten Einstellungen über AlertDialog.
request.setParameter("subdirs", "1");
request.setParameter("pages", "1");
request.setParameter("files", "1");
- String response = null;
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- Intent notificationIntent = new Intent(this,
+ final Intent notificationIntent = new Intent(this,
FolderActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this,
+ final PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
- // the next two lines initialize the Notification, using the
- // configurations above
- Notification notification = new Notification(
- R.drawable.publish, getResources().getString(
+ final Notification notification = new Notification(
+ R.drawable.logo, getResources().getString(
R.string.publish), System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(),
getResources().getString(R.string.publish), name,
contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_NO_CLEAR;
-
nm.notify(NOTIFICATION_PUBLISH, notification);
try
{
- try
- {
- Thread.sleep(2000);
- }
- catch (InterruptedException e)
- {
- }
-
- response = request.performRequest();
- JSONObject json = new JSONObject(response);
+ request.publish(type, id);
+ // Alles OK.
notification.setLatestEventInfo(getApplicationContext(),
getResources().getString(R.string.publish_ok),
name, contentIntent);
- notification.flags = 0;
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFICATION_PUBLISH, notification);
}
catch (IOException e)
{
- System.err.println(response);
- System.err.println(e.getMessage());
-
- notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.publish_fail),
- name, contentIntent);
- notification.flags = 0;
- nm.notify(NOTIFICATION_PUBLISH, notification);
- }
- catch (JSONException e)
- {
+ final String msgText = getResources().getString(R.string.publish_fail);
notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.publish_fail),
+ msgText,
name, contentIntent);
- notification.flags = 0;
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFICATION_PUBLISH, notification);
- e.printStackTrace();
+
+ Log.e(this.getClass().getName(), msgText,e);
}
finally
{
}
-
}
}
diff --git a/src/de/openrat/android/blog/service/UploadIntentService.java b/src/de/openrat/android/blog/service/UploadIntentService.java
@@ -12,10 +12,10 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
+import android.util.Log;
import de.openrat.android.blog.FolderActivity;
import de.openrat.android.blog.R;
-import de.openrat.android.blog.util.FileUtils;
-import de.openrat.client.CMSRequest;
+import de.openrat.client.OpenRatClient;
/**
* @author dankert
@@ -43,61 +43,49 @@ public class UploadIntentService extends IntentService
protected void onHandleIntent(Intent intent)
{
final String filePath = intent.getStringExtra(EXTRA_FILENAME);
- final CMSRequest request = (CMSRequest) intent
+ final OpenRatClient client = (OpenRatClient) intent
.getSerializableExtra(EXTRA_REQUEST);
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- Intent notificationIntent = new Intent(this, FolderActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
+ final Intent notificationIntent = new Intent(this, FolderActivity.class);
+ final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
- request.clearParameters();
- request.setAction("folder");
- request.setActionMethod("createnewfile");
- request.setMethod("POST");
- request.trace = true;
-
- final Notification notification = new Notification(R.drawable.upload,
+ final Notification notification = new Notification(R.drawable.logo,
getResources().getString(R.string.upload), System
.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), getResources()
.getString(R.string.upload_ok), filePath, contentIntent);
-
+ notification.flags |= Notification.FLAG_NO_CLEAR;
nm.notify(NOTIFICATION_UPLOAD, notification);
+
try
{
final File file = new File(filePath);
- notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.upload), file.getName(),
- contentIntent);
- notification.flags |= Notification.FLAG_NO_CLEAR;
-
- byte[] fileBytes = FileUtils.getBytesFromFile(file);
- request.setFile(EXTRA_FILENAME, fileBytes, file.getName(),
- "image/jpeg", "binary");
-
- String response = request.performRequest();
-
- System.out.println("nach dem Hochladen" + response);
+ client.uploadFile(EXTRA_FILENAME,file);
+ // Alles OK.
+ final String msgText = getResources().getString(R.string.upload_ok);
notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.upload_ok), file
+ msgText, file
.getName(), contentIntent);
- notification.flags = 0;
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFICATION_UPLOAD, notification);
+ Log.d(this.getClass().getName(), msgText);
}
catch (IOException e)
{
+ // Fehler ist aufgetreten.
+ final String msgText = getResources().getString(R.string.upload_fail);
notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.upload_fail), "",
+ msgText, e.getMessage(),
contentIntent);
- notification.flags = 0;
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFICATION_UPLOAD, notification);
- e.printStackTrace();
- throw new RuntimeException(e);
+ Log.e(this.getClass().getName(), msgText, e);
}
finally
{
diff --git a/src/de/openrat/client/HTTPRequest.java b/src/de/openrat/client/HTTPRequest.java
@@ -27,14 +27,11 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Serializable;
-import java.io.StringReader;
-import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URLEncoder;
-import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -358,6 +355,7 @@ public class HTTPRequest implements Serializable
socket.setKeepAlive(false);
socket.setReuseAddress(false);
+ socket.setSoTimeout(5000);
socket.connect(socketAddress, 5000);
final StringBuffer header = new StringBuffer();
@@ -495,7 +493,6 @@ public class HTTPRequest implements Serializable
{
response.append(bufferedReader.readLine() + "\n");
}
- socket.close();
if (this.trace)
System.out.println("--- response body ---");
diff --git a/src/de/openrat/client/OpenRatClient.java b/src/de/openrat/client/OpenRatClient.java
@@ -1,5 +1,6 @@
package de.openrat.client;
+import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -10,6 +11,7 @@ import org.json.JSONObject;
import de.openrat.android.blog.FolderEntry;
import de.openrat.android.blog.FolderEntry.FType;
+import de.openrat.android.blog.util.FileUtils;
/**
* Komfortabler Zugriff auf das OpenRat-CMS.
@@ -63,7 +65,8 @@ public class OpenRatClient extends CMSRequest
* Rootfolder geladen.
* @return Ordner-Einträge
*/
- public List<FolderEntry> getFolderEntries(String folderid) throws IOException
+ public List<FolderEntry> getFolderEntries(String folderid)
+ throws IOException
{
if (folderid == null)
{
@@ -125,7 +128,6 @@ public class OpenRatClient extends CMSRequest
protected JSONObject readJSON() throws OpenRatClientException
{
-
String response;
try
{
@@ -139,9 +141,21 @@ public class OpenRatClient extends CMSRequest
try
{
- return new JSONObject(response);
- // TODO: Notices und Errors aus dem JSON-Dokument auswerten und ggf.
- // Exception werfen.
+ final JSONObject json = new JSONObject(response);
+
+ try
+ {
+ // Versuchen, die 1. Notice zu lesen. Falls es eine gibt, Exception mit dem Notice-Text werfen.
+ final String msgText = json.getJSONArray("notices")
+ .getJSONObject(0).getString("text");
+ throw new OpenRatClientException(msgText);
+ }
+ catch (JSONException e)
+ {
+ // Keine Notice gefunden. Das deutet auf eine fehlerfreie Ausführung hin :)
+ return json;
+ }
+
}
catch (JSONException e)
{
@@ -204,4 +218,90 @@ public class OpenRatClient extends CMSRequest
}
}
+
+ /**
+ * @throws IOException
+ *
+ */
+ public void uploadFile(String filenName, File file) throws IOException
+ {
+
+ super.clearParameters();
+ super.setAction("folder");
+ super.setActionMethod("createnewfile");
+ super.setMethod("POST");
+
+ byte[] fileBytes;
+ try
+ {
+ fileBytes = FileUtils.getBytesFromFile(file);
+ }
+ catch (IOException e)
+ {
+ throw new OpenRatClientException(e);
+ }
+ super.setFile(filenName, fileBytes, file.getName(), "image/jpeg",
+ "binary");
+
+ @SuppressWarnings("unused")
+ final JSONObject response = readJSON();
+ }
+
+ /**
+ * @param type
+ * Typ
+ * @param id
+ * Id
+ * @throws IOException
+ */
+ public void publish(String type, String id) throws IOException
+ {
+
+ super.clearParameters();
+ super.setAction(type);
+ super.setActionMethod("pub");
+ super.setId(id);
+
+ // Erstmal alles aktivieren was geht
+ // TODO: Abfrage der gewünschten Einstellungen über AlertDialog.
+ super.setParameter("subdirs", "1");
+ super.setParameter("pages", "1");
+ super.setParameter("files", "1");
+
+ @SuppressWarnings("unused")
+ JSONObject response = readJSON();
+ }
+
+ public void login(String login, String password) throws IOException
+ {
+ super.setParameter("action", "index");
+ super.setParameter("subaction", "login");
+ super.setParameter("dbid", "db1");
+ super.setParameter("login_name", login);
+ super.setParameter("login_password", password);
+
+ JSONObject json = readJSON();
+ JSONObject session;
+ try
+ {
+ session = json.getJSONObject("session");
+ final String sessionName = session.getString("name");
+ final String sessionId = session.getString("id");
+ setCookie(sessionName, sessionId);
+ }
+ catch (JSONException e)
+ {
+ try
+ {
+ final String msgText = json.getJSONArray("notices")
+ .getJSONObject(0).getString("text");
+ throw new OpenRatClientException(msgText, e);
+ }
+ catch (JSONException e1)
+ {
+ throw new OpenRatClientException(e);
+ }
+ }
+
+ }
}