Upgrade to the latest server protocol.
[fonbot.git] / src / ro / ieval / fonbot / SendHttpMessageAsyncTask.java
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();
This page took 0.01145 seconds and 4 git commands to generate.