2014-06-17 06:34:30 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Serval Project Inc.
|
|
|
|
*
|
|
|
|
* This file is part of Serval Software (http://www.servalproject.org)
|
|
|
|
*
|
|
|
|
* Serval Software 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.
|
|
|
|
*
|
|
|
|
* This source code 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 this source code; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.servalproject.servaldna.meshms;
|
|
|
|
|
2014-09-16 01:53:31 +00:00
|
|
|
import org.servalproject.json.JSONInputException;
|
|
|
|
import org.servalproject.json.JSONTokeniser;
|
|
|
|
import org.servalproject.servaldna.ServalDFailureException;
|
|
|
|
import org.servalproject.servaldna.ServalDHttpConnectionFactory;
|
|
|
|
import org.servalproject.servaldna.ServalDInterfaceException;
|
|
|
|
import org.servalproject.servaldna.SubscriberId;
|
|
|
|
|
2014-06-17 06:34:30 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2014-06-19 07:57:05 +00:00
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PrintStream;
|
2014-06-17 06:34:30 +00:00
|
|
|
import java.net.HttpURLConnection;
|
2014-09-16 01:53:31 +00:00
|
|
|
import java.net.URL;
|
2014-06-17 06:34:30 +00:00
|
|
|
|
2014-06-19 07:57:05 +00:00
|
|
|
public class MeshMSCommon
|
2014-06-17 06:34:30 +00:00
|
|
|
{
|
2014-06-19 07:57:05 +00:00
|
|
|
protected static JSONTokeniser receiveRestfulResponse(HttpURLConnection conn, int expected_response_code) throws IOException, ServalDInterfaceException, MeshMSException
|
2014-06-24 02:42:48 +00:00
|
|
|
{
|
|
|
|
int[] expected_response_codes = { expected_response_code };
|
|
|
|
return receiveRestfulResponse(conn, expected_response_codes);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static JSONTokeniser receiveRestfulResponse(HttpURLConnection conn, int[] expected_response_codes) throws IOException, ServalDInterfaceException, MeshMSException
|
2014-06-17 06:34:30 +00:00
|
|
|
{
|
2014-09-16 01:53:31 +00:00
|
|
|
if (!"application/json".equals(conn.getContentType()))
|
2014-06-17 06:34:30 +00:00
|
|
|
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
|
|
|
|
if (conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
|
|
|
|
JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getErrorStream(), "US-ASCII"));
|
2014-06-19 07:57:05 +00:00
|
|
|
Status status = decodeRestfulStatus(json);
|
|
|
|
throwRestfulResponseExceptions(status, conn.getURL());
|
2014-07-04 02:38:49 +00:00
|
|
|
throw new ServalDInterfaceException("unexpected MeshMS status = " + status.meshms_status_code + ", \"" + status.meshms_status_message + "\"");
|
2014-06-17 06:34:30 +00:00
|
|
|
}
|
2014-06-24 02:42:48 +00:00
|
|
|
for (int code: expected_response_codes) {
|
|
|
|
if (conn.getResponseCode() == code) {
|
|
|
|
JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getInputStream(), "US-ASCII"));
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new ServalDInterfaceException("unexpected HTTP response code: " + conn.getResponseCode());
|
2014-06-17 06:34:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 07:57:05 +00:00
|
|
|
private static class Status {
|
2014-07-04 02:38:49 +00:00
|
|
|
public int http_status_code;
|
|
|
|
public String http_status_message;
|
|
|
|
public MeshMSStatus meshms_status_code;
|
|
|
|
public String meshms_status_message;
|
2014-06-19 07:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static Status decodeRestfulStatus(JSONTokeniser json) throws IOException, ServalDInterfaceException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Status status = new Status();
|
|
|
|
json.consume(JSONTokeniser.Token.START_OBJECT);
|
|
|
|
json.consume("http_status_code");
|
|
|
|
json.consume(JSONTokeniser.Token.COLON);
|
2014-07-04 02:38:49 +00:00
|
|
|
status.http_status_code = json.consume(Integer.class);
|
2014-06-19 07:57:05 +00:00
|
|
|
json.consume(JSONTokeniser.Token.COMMA);
|
2014-07-04 02:38:49 +00:00
|
|
|
json.consume("http_status_message");
|
2014-06-19 07:57:05 +00:00
|
|
|
json.consume(JSONTokeniser.Token.COLON);
|
2014-07-04 02:38:49 +00:00
|
|
|
status.http_status_message = json.consume(String.class);
|
2014-06-19 07:57:05 +00:00
|
|
|
json.consume(JSONTokeniser.Token.COMMA);
|
|
|
|
json.consume("meshms_status_code");
|
|
|
|
json.consume(JSONTokeniser.Token.COLON);
|
2014-07-04 02:38:49 +00:00
|
|
|
status.meshms_status_code = MeshMSStatus.fromCode(json.consume(Integer.class));
|
|
|
|
json.consume(JSONTokeniser.Token.COMMA);
|
|
|
|
json.consume("meshms_status_message");
|
|
|
|
json.consume(JSONTokeniser.Token.COLON);
|
|
|
|
status.meshms_status_message = json.consume(String.class);
|
2014-06-19 07:57:05 +00:00
|
|
|
json.consume(JSONTokeniser.Token.END_OBJECT);
|
|
|
|
json.consume(JSONTokeniser.Token.EOF);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
catch (JSONInputException e) {
|
|
|
|
throw new ServalDInterfaceException("malformed JSON status response", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static void throwRestfulResponseExceptions(Status status, URL url) throws MeshMSException, ServalDFailureException
|
|
|
|
{
|
2014-07-04 02:38:49 +00:00
|
|
|
switch (status.meshms_status_code) {
|
2014-06-19 07:57:05 +00:00
|
|
|
case OK:
|
|
|
|
case UPDATED:
|
|
|
|
break;
|
|
|
|
case SID_LOCKED:
|
|
|
|
throw new MeshMSUnknownIdentityException(url);
|
|
|
|
case PROTOCOL_FAULT:
|
|
|
|
throw new MeshMSProtocolFaultException(url);
|
|
|
|
case ERROR:
|
2014-07-04 02:38:49 +00:00
|
|
|
throw new ServalDFailureException("received meshms_status_code=ERROR(-1) from " + url);
|
2014-06-19 07:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static MeshMSStatus sendMessage(ServalDHttpConnectionFactory connector, SubscriberId sid1, SubscriberId sid2, String text) throws IOException, ServalDInterfaceException, MeshMSException
|
|
|
|
{
|
|
|
|
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshms/" + sid1.toHex() + "/" + sid2.toHex() + "/sendmessage");
|
|
|
|
String boundary = Long.toHexString(System.currentTimeMillis());
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
|
|
conn.connect();
|
|
|
|
OutputStream ost = conn.getOutputStream();
|
|
|
|
PrintStream wr = new PrintStream(ost, false, "US-ASCII");
|
|
|
|
wr.print("--" + boundary + "\r\n");
|
|
|
|
wr.print("Content-Disposition: form-data; name=\"message\"\r\n");
|
|
|
|
wr.print("Content-Type: text/plain; charset=utf-8\r\n");
|
|
|
|
wr.print("\r\n");
|
|
|
|
wr.print(text);
|
|
|
|
wr.print("\r\n--" + boundary + "--\r\n");
|
|
|
|
wr.close();
|
|
|
|
JSONTokeniser json = MeshMSCommon.receiveRestfulResponse(conn, HttpURLConnection.HTTP_CREATED);
|
|
|
|
Status status = decodeRestfulStatus(json);
|
|
|
|
throwRestfulResponseExceptions(status, conn.getURL());
|
2014-07-04 02:38:49 +00:00
|
|
|
return status.meshms_status_code;
|
2014-06-19 07:57:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 02:42:48 +00:00
|
|
|
public static MeshMSStatus markAllConversationsRead(ServalDHttpConnectionFactory connector, SubscriberId sid1) throws IOException, ServalDInterfaceException, MeshMSException
|
|
|
|
{
|
|
|
|
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshms/" + sid1.toHex() + "/readall");
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.connect();
|
|
|
|
int[] expected_response_codes = { HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_CREATED };
|
|
|
|
JSONTokeniser json = MeshMSCommon.receiveRestfulResponse(conn, expected_response_codes);
|
|
|
|
Status status = decodeRestfulStatus(json);
|
|
|
|
throwRestfulResponseExceptions(status, conn.getURL());
|
2014-07-04 02:38:49 +00:00
|
|
|
return status.meshms_status_code;
|
2014-06-24 02:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static MeshMSStatus markAllMessagesRead(ServalDHttpConnectionFactory connector, SubscriberId sid1, SubscriberId sid2) throws IOException, ServalDInterfaceException, MeshMSException
|
|
|
|
{
|
|
|
|
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshms/" + sid1.toHex() + "/" + sid2.toHex() + "/readall");
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.connect();
|
|
|
|
int[] expected_response_codes = { HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_CREATED };
|
|
|
|
JSONTokeniser json = MeshMSCommon.receiveRestfulResponse(conn, expected_response_codes);
|
|
|
|
Status status = decodeRestfulStatus(json);
|
|
|
|
throwRestfulResponseExceptions(status, conn.getURL());
|
2014-07-04 02:38:49 +00:00
|
|
|
return status.meshms_status_code;
|
2014-06-24 02:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static MeshMSStatus advanceReadOffset(ServalDHttpConnectionFactory connector, SubscriberId sid1, SubscriberId sid2, long offset) throws IOException, ServalDInterfaceException, MeshMSException
|
|
|
|
{
|
|
|
|
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshms/" + sid1.toHex() + "/" + sid2.toHex() + "/recv/" + offset + "/read");
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.connect();
|
|
|
|
int[] expected_response_codes = { HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_CREATED };
|
|
|
|
JSONTokeniser json = MeshMSCommon.receiveRestfulResponse(conn, expected_response_codes);
|
|
|
|
Status status = decodeRestfulStatus(json);
|
|
|
|
throwRestfulResponseExceptions(status, conn.getURL());
|
2014-07-04 02:38:49 +00:00
|
|
|
return status.meshms_status_code;
|
2014-06-24 02:42:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 06:34:30 +00:00
|
|
|
}
|