Upgrade to the latest server protocol.
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 11 Mar 2013 15:27:54 +0000 (17:27 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 11 Mar 2013 15:27:54 +0000 (17:27 +0200)
Server protocol changes:
* All server requests use Basic access authentication
* There is no more X-Action. Server paths are used instead:
- /send for sending messages, with the destination address
in the X-Destination header.
- /ok for doing nothing. Used for the main activity login status.

Other minor changes:
* Centered the resultTextView in main.xml.
* Utils#parseHttpMessage can parse messages like "(text)".

res/layout/main.xml
res/values/strings.xml
src/ro/ieval/fonbot/FonBotMainActivity.java
src/ro/ieval/fonbot/PollServerAsyncTask.java
src/ro/ieval/fonbot/RemoteCrashdumpHandler.java
src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
src/ro/ieval/fonbot/Utils.java

index 1fc54d2f20ee7c1291bd7b8eb1fb1a0922e36282..90b6d4ab26a4fbf758361b6f53ab47f9ab8f4d0c 100644 (file)
@@ -9,6 +9,7 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center_horizontal"
+        android:gravity="center_horizontal"
         android:textAppearance="?android:attr/textAppearanceLarge"
         android:textIsSelectable="false" />
 
index 6e7544401d1bb4d5cef09b5ef0dd319ce8e75bb5..bdc90a66d6d7eb490ceb917cb2ba9556db5b0ca2 100644 (file)
@@ -444,4 +444,5 @@ The help command can be used to get a list of commands and help for them. Exampl
     <string name="notification_canceled">Notification canceled</string>
     <string name="notification_shown">Notification shown</string>
     <string name="notify_help">Notify help</string>
+    <string name="user_or_password_not_set">User or password not set</string>
 </resources>
\ No newline at end of file
index 92a81af7ec4c70d57b9a89d53894758f669894cc..7ef45b849c485400cfc83276141a1fdb382ab67a 100644 (file)
@@ -1,25 +1,22 @@
 package ro.ieval.fonbot;
 
-import static ro.ieval.fonbot.R.string.*;
-
+import static ro.ieval.fonbot.R.string.logging_in;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
-import java.util.Arrays;
+import java.util.Collections;
+
 import org.eclipse.jdt.annotation.Nullable;
 
 import ro.ieval.fonbot.FonBotMainService.Binder;
 import ro.ieval.fonbot.Utils.OngoingEvent;
-
 import android.app.ListActivity;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.IBinder;
-import android.preference.PreferenceManager;
 import android.support.v4.content.LocalBroadcastManager;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -204,14 +201,9 @@ public final class FonBotMainActivity extends ListActivity {
        @Override
        protected void onStart() {
                super.onStart();
-               final SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
                resultTextView.setText(logging_in);
-               new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
-                               new Header("X-Action","LOGIN")
-                               )),LOGIN_BROADCAST,this).execute(
-                                               sp.getString("username", null),
-                                               sp.getString("password", null)
-                                               );
+               new SendHttpMessageAsyncTask("/ok", toNonNull(Collections.<Header>emptySet()),
+                               LOGIN_BROADCAST,this).execute();
                connection.refreshAdapter();
        }
 
index 2a4a21d60fe73661bdc9b652f3647eb4a7d791bd..15bf9cc093e6a0932b1bb568d38b2a2c25d074b0 100644 (file)
@@ -17,6 +17,8 @@ import org.json.JSONObject;
 import ro.ieval.fonbot.Address.Protocol;
 import android.content.Context;
 import android.os.AsyncTask;
+import android.preference.PreferenceManager;
+import android.util.Base64;
 import android.util.Log;
 
 /*
@@ -97,6 +99,13 @@ public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollSe
                        final URL url=Utils.getServerURL(toNonNull(context),"/get");
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+                       final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+                       final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+                       if(user == null || password == null)
+                               return commands;
+
+                       conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
+                                       (user+':'+password).getBytes(), Base64.NO_WRAP));
                        conn.connect();
                        final byte[] buf=new byte[4096*1024];
                        Log.d(getClass().getName(), "Server poll got response code "+conn.getResponseCode()+" and message "+conn.getResponseMessage());
index 4f28d843d06bcd4725b972f53a54d31122410095..e228869a5fde4180acd597ab348daf8d1980f52d 100644 (file)
@@ -6,7 +6,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.lang.Thread.UncaughtExceptionHandler;
-import java.util.Arrays;
+import java.util.Collections;
 
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -59,9 +59,8 @@ final class RemoteCrashdumpHandler implements UncaughtExceptionHandler {
                ex.printStackTrace(pw);
                pw.close();
 
-               new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
-                               new Header("X-Action", "CRASHDUMP")
-                               )), toNonNull(context), toNonNull(baos.toByteArray())).execute();
+               new SendHttpMessageAsyncTask("/crashdump", toNonNull(Collections.<Header>emptyList()),
+                               toNonNull(context), toNonNull(baos.toByteArray())).execute();
 
                try {
                        baos.close();
index 5087b0b9762dc46f4de5ba5b0e770159e26f9b0c..c05dd05d1d8b58dcf4e6aeb284a357d8158c072f 100644 (file)
@@ -13,6 +13,8 @@ import org.eclipse.jdt.annotation.Nullable;
 import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncTask;
+import android.preference.PreferenceManager;
+import android.util.Base64;
 import android.util.Log;
 
 /*
@@ -65,19 +67,25 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
         * Data to send to the server
         */
        private final byte[] data;
+       /**
+        * Server URL path
+        */
+       private final String path;
        /**
         * Response code from the server
         */
-       private int responseCode;
+       private int responseCode=0;
 
        /**
         * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response. 
         *
+        * @param path URL path
         * @param headers the extra headers
         * @param context the context instance
         */
-       public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context){
+       public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){
                super();
+               this.path=path;
                this.headers=headers;
                this.broadcast=null;//NOPMD final field
                this.context=context;
@@ -87,12 +95,14 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
        /**
         * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
         *
+        * @param path URL path
         * @param headers the extra headers
         * @param context the context instance
         * @param data the message to send
         */
-       public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
+       public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
                super();
+               this.path=path;
                this.headers=headers;
                this.broadcast=null;//NOPMD final field
                this.context=context;
@@ -102,12 +112,14 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
        /**
         * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
         *
+        * @param path URL path
         * @param headers the extra headers
         * @param broadcast the broadcast to send
         * @param context the context instance
         */
-       public SendHttpMessageAsyncTask(final Collection<Header> headers, final String broadcast, final Context context){
+       public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){
                super();
+               this.path=path;
                this.headers=headers;
                this.broadcast=broadcast;
                this.context=context;
@@ -120,23 +132,36 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
                        return "";
 
                final byte[] msg;
-               if(data==null)
+               if(data==null && args.length > 0)
                        msg=Utils.join(" ", args).getBytes();
-               else
+               else if(data!=null)
                        msg=data;
+               else
+                       msg=null;
 
                try {
-                       final URL url=Utils.getServerURL(toNonNull(context),"/");
+                       final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
-                       conn.setDoOutput(true);
-                       conn.setFixedLengthStreamingMode(msg.length);
+                       if(msg!=null){
+                               conn.setDoOutput(true);
+                               conn.setFixedLengthStreamingMode(msg.length);
+                       }
                        conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+                       final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+                       final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+                       if(user == null || password == null)
+                               return toNonNull(context.getString(user_or_password_not_set));
+
+                       conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
+                                       (user+':'+password).getBytes(), Base64.NO_WRAP));
                        for (Header header : headers)
                                conn.setRequestProperty(header.name, header.value);
                        conn.connect();
-                       final OutputStream stream=conn.getOutputStream();
-                       stream.write(msg);
-                       stream.close();
+                       if(msg!=null){
+                               final OutputStream stream=conn.getOutputStream();
+                               stream.write(msg);
+                               stream.close();
+                       }
                        Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
                        conn.disconnect();
                        final String message=conn.getResponseMessage();
index 117c528f6408d8cea153b5f665544009935329c0..d9060eb662734475026200ef84587260ef931794 100644 (file)
@@ -316,8 +316,8 @@ public final class Utils {
        public static void sendMessage(final Context context, final Address address, final String message){
                switch(address.protocol){
                case HTTP:
-                       new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
-                                       new Header("X-Action", "SEND "+address.data))), context).execute(message);
+                       new SendHttpMessageAsyncTask("/send", toNonNull(Arrays.asList(
+                                       new Header("X-Destination", toNonNull(address.data)))), context).execute(message);
                        break;
 
                case SMS:
@@ -345,6 +345,8 @@ public final class Utils {
                final int indexOfParen=message.indexOf(')');
                if(indexOfParen==-1)
                        return message;
+               if(indexOfParen == message.length()-1)
+                       return toNonNull(message.substring(1, indexOfParen));
                return toNonNull(message.substring(indexOfParen+2));
        }
 
This page took 0.021132 seconds and 4 git commands to generate.