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