Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HttpHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.rob57530.smsserver;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.Vector;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.HttpStatus;
  10. import org.apache.http.StatusLine;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.eclipse.jetty.server.Request;
  16. import org.eclipse.jetty.server.handler.AbstractHandler;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19. import android.app.Activity;
  20. import android.app.PendingIntent;
  21. import android.content.BroadcastReceiver;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.content.IntentFilter;
  25. import android.os.AsyncTask;
  26. import android.telephony.SmsManager;
  27. import android.util.Base64;
  28. import android.util.Log;
  29. public class HttpHandler extends AbstractHandler
  30. {
  31. private String m_http = null;
  32. private Context m_context = null;
  33. private Vector<Integer> m_ids = new Vector<Integer>();
  34. class RequestTask extends AsyncTask<String, String, String>
  35. {
  36. @Override
  37. protected String doInBackground(String... uri)
  38. {
  39. HttpClient httpclient = new DefaultHttpClient();
  40. HttpResponse response;
  41. String responseString = null;
  42. try
  43. {
  44. HttpGet get = new HttpGet(uri[0]);
  45. get.setHeader("Host", m_http);
  46. get.setHeader("Authorization", "Basic " + Base64.encodeToString("__gates__:gatespasswd".getBytes(), Base64.NO_WRAP));
  47. response = httpclient.execute(get);
  48. StatusLine statusLine = response.getStatusLine();
  49. if (statusLine.getStatusCode() == HttpStatus.SC_OK)
  50. {
  51. ByteArrayOutputStream out = new ByteArrayOutputStream();
  52. response.getEntity().writeTo(out);
  53. out.close();
  54. responseString = out.toString();
  55. }
  56. else
  57. {
  58. response.getEntity().getContent().close();
  59. throw new IOException(statusLine.getReasonPhrase());
  60. }
  61. }
  62. catch (ClientProtocolException e)
  63. {
  64. e.printStackTrace();
  65. }
  66. catch (IOException e)
  67. {
  68. e.printStackTrace();
  69. }
  70. return responseString;
  71. }
  72. @Override
  73. protected void onPostExecute(String result)
  74. {
  75. super.onPostExecute(result);
  76. }
  77. }
  78. public void setContext(Context c)
  79. {
  80. m_context = c;
  81. }
  82. public void setHttp(String h)
  83. {
  84. m_http = h;
  85. }
  86. @Override
  87. public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  88. {
  89. response.setContentType("application/json;charset=utf-8");
  90. response.setStatus(HttpServletResponse.SC_OK);
  91. JSONObject root = new JSONObject();
  92. if (target.equals("/send.php"))
  93. {
  94. String number = request.getParameter("to");
  95. String text = request.getParameter("text");
  96. int idTmp = -1;
  97. try
  98. {
  99. idTmp = Integer.parseInt(request.getParameter("id"));
  100. }
  101. catch(Exception e)
  102. {
  103. idTmp = m_ids.size();
  104. m_ids.add(0);
  105. }
  106. final int id = idTmp;
  107. if (number == null || number.length() == 0)
  108. {
  109. try
  110. {
  111. root.put("error", "Empty recipient");
  112. }
  113. catch (JSONException e)
  114. {
  115. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  116. }
  117. }
  118. else if (text == null || text.length() == 0)
  119. {
  120. try
  121. {
  122. root.put("error", "Empty text");
  123. }
  124. catch (JSONException e)
  125. {
  126. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  127. }
  128. }
  129. else
  130. {
  131. try
  132. {
  133. PendingIntent sent = null;
  134. if (m_http.length() != 0 && id != -1)
  135. {
  136. String SENT = "SMS_SENT";
  137. sent = PendingIntent.getBroadcast(m_context, 0, new Intent(SENT), 0);
  138. m_context.registerReceiver(new BroadcastReceiver()
  139. {
  140. @Override
  141. public void onReceive(Context arg0, Intent arg1)
  142. {
  143. int r = getResultCode();
  144. if (r == Activity.RESULT_OK)
  145. r = 1;
  146. else if (r == SmsManager.RESULT_ERROR_GENERIC_FAILURE)
  147. r = 3;
  148. else if (r == SmsManager.RESULT_ERROR_NO_SERVICE)
  149. r = 4;
  150. else if (r == SmsManager.RESULT_ERROR_NULL_PDU)
  151. r = 5;
  152. else if (r == SmsManager.RESULT_ERROR_RADIO_OFF)
  153. r = 6;
  154. else
  155. r = 7;
  156. RequestTask re = new RequestTask();
  157. re.execute("http://" + m_http + "/sent.php?id=" + id + "&status=" + r);
  158. }
  159. }, new IntentFilter(SENT));
  160. }
  161. Log.d("SMSServerService", number);
  162. SmsManager.getDefault().sendTextMessage(number, null, text, sent, null);
  163. root.put("id", 0);
  164. root.put("status", 0);
  165. }
  166. catch (Exception e)
  167. {
  168. e.printStackTrace();
  169. try
  170. {
  171. root.put("error", e.getMessage());
  172. }
  173. catch (JSONException e1)
  174. {
  175. e1.printStackTrace();
  176. }
  177. }
  178. }
  179. }
  180. else
  181. return;
  182. baseRequest.setHandled(true);
  183. response.getWriter().println(root.toString());
  184. }
  185. }