X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;ds=sidebyside;f=src%2Fro%2Fieval%2Ffonbot%2FSendHttpMessageAsyncTask.java;fp=src%2Fro%2Fieval%2Ffonbot%2FSendHttpMessageAsyncTask.java;h=0000000000000000000000000000000000000000;hb=2e5049c9c08b1989b4bfbbfbf479d83dbe2b75a9;hp=68f7319251be5fbe4d4ab98a332a56a8cbb42be5;hpb=bf70dcc30a9bd5cbcd71aa8481f7171331f9f1b6;p=fonbot.git
diff --git a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java b/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
deleted file mode 100644
index 68f7319..0000000
--- a/src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
+++ /dev/null
@@ -1,190 +0,0 @@
-package ro.ieval.fonbot;
-
-import static ro.ieval.fonbot.R.string.*;
-import static ro.ieval.fonbot.Utils.toNonNull;
-
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.Collection;
-
-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;
-
-import com.google.android.gcm.GCMRegistrar;
-
-/*
- * Copyright © 2013 Marius Gavrilescu
- *
- * This file is part of FonBot.
- *
- * FonBot is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * FonBot is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with FonBot. If not, see .
- */
-
-/**
- * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
- *
- * @author Marius Gavrilescu
- */
-public final class SendHttpMessageAsyncTask extends AsyncTask {
- /**
- * Extra: the response message
- */
- public static final String EXTRA_RESPONSE_MESSAGE="response_message";
- /**
- * Extra: the response code
- */
- public static final String EXTRA_RESPONSE_CODE="response_code";
-
- /**
- * List of extra request headers.
- */
- private final Collection headers;
- /**
- * Action to send back in the response broadcast
- */
- private final String broadcast;
- /**
- * Context instance used by this class
- */
- private final Context context;
- /**
- * 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=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 String path, final Collection headers, final Context context){
- super();
- this.path=path;
- this.headers=headers;
- this.broadcast=null;//NOPMD final field
- this.context=context;
- this.data=null;//NOPMD final field
- }
-
- /**
- * 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 String path, final Collection 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;
- this.data=data;
- }
-
- /**
- * 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 String path, final Collection headers, final String broadcast, final Context context){
- super();
- this.path=path;
- this.headers=headers;
- this.broadcast=broadcast;
- this.context=context;
- this.data=null;//NOPMD final field
- }
-
- @Override
- protected String doInBackground(final @Nullable String... args) {
- if(args==null)
- return "";
-
- final byte[] msg;
- if(data==null && args.length > 0)
- msg=Utils.join(" ", args).getBytes();
- else if(data!=null)
- msg=data;
- else
- msg=null;
-
- try {
- final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
- final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
- if(msg!=null){
- conn.setDoOutput(true);
- conn.setFixedLengthStreamingMode(msg.length);
- }
- conn.setRequestProperty("X-ID", GCMRegistrar.getRegistrationId(context));
- 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();
- 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();
- responseCode=conn.getResponseCode();
- if(message==null)
- return toNonNull(context.getString(no_response_returned_from_server));
- return Utils.parseHttpMessage(message);
- } catch (Exception e) {
- e.printStackTrace();
- return toNonNull(context.getString(connection_error));
- }
- }
-
- @Override
- protected void onPostExecute(final @Nullable String result) {
- super.onPostExecute(result);
- if(broadcast!=null){
- final Intent intent=new Intent(broadcast);
- intent.putExtra(EXTRA_RESPONSE_MESSAGE, result);
- intent.putExtra(EXTRA_RESPONSE_CODE, responseCode);
- context.sendBroadcast(intent);
- }
- }
-}