]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
Upgrade to the latest server protocol.
[fonbot.git] / src / ro / ieval / fonbot / SendHttpMessageAsyncTask.java
index e27848159817262d3d9462947b6c7b01fa80170f..c05dd05d1d8b58dcf4e6aeb284a357d8158c072f 100644 (file)
@@ -1,7 +1,6 @@
 package ro.ieval.fonbot;
 
 import static ro.ieval.fonbot.R.string.*;
-
 import static ro.ieval.fonbot.Utils.toNonNull;
 
 import java.io.OutputStream;
@@ -15,6 +14,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.preference.PreferenceManager;
+import android.util.Base64;
 import android.util.Log;
 
 /*
@@ -67,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;
@@ -89,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;
@@ -104,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;
@@ -122,25 +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 String hostname=PreferenceManager.getDefaultSharedPreferences(context).getString("hostname", "ieval.ro");
-                       final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "7777"));
-                       final URL url=new URL("http", hostname, port, "/");
+                       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();
@@ -149,6 +170,7 @@ public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, Stri
                                return toNonNull(context.getString(no_response_returned_from_server));
                        return Utils.parseHttpMessage(message);
                } catch (Exception e) {
+                       e.printStackTrace();
                        return toNonNull(context.getString(connection_error));
                }
        }
This page took 0.021069 seconds and 4 git commands to generate.