OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
OfflineIMServiceRemoteConnector.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.Linq;
31 using System.Reflection;
32 using System.Text;
33 
34 using OpenSim.Framework;
35 using OpenSim.Framework.ServiceAuth;
36 using OpenSim.Server.Base;
37 using OpenSim.Services.Interfaces;
38 
39 using OpenMetaverse;
40 using log4net;
41 using Nini.Config;
42 
43 namespace OpenSim.OfflineIM
44 {
46  {
47  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 
49  private string m_ServerURI = string.Empty;
50  private IServiceAuth m_Auth;
51  private object m_Lock = new object();
52 
54  {
55  m_ServerURI = url;
56  m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: Offline IM server at {0}", m_ServerURI);
57  }
58 
59  public OfflineIMServiceRemoteConnector(IConfigSource config)
60  {
61  IConfig cnf = config.Configs["Messaging"];
62  if (cnf == null)
63  {
64  m_log.WarnFormat("[OfflineIM.V2.RemoteConnector]: Missing Messaging configuration");
65  return;
66  }
67 
68  m_ServerURI = cnf.GetString("OfflineMessageURL", string.Empty);
69 
71  string authType = Util.GetConfigVarFromSections<string>(config, "AuthType", new string[] { "Network", "Messaging" }, "None");
72 
73  switch (authType)
74  {
75  case "BasicHttpAuthentication":
76  m_Auth = new BasicHttpAuthentication(config, "Messaging");
77  break;
78  }
80  m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: Offline IM server at {0} with auth {1}",
81  m_ServerURI, (m_Auth == null ? "None" : m_Auth.GetType().ToString()));
82  }
83 
84  #region IOfflineIMService
85  public List<GridInstantMessage> GetMessages(UUID principalID)
86  {
87  List<GridInstantMessage> ims = new List<GridInstantMessage>();
88 
89  Dictionary<string, object> sendData = new Dictionary<string, object>();
90  sendData["PrincipalID"] = principalID;
91  Dictionary<string, object> ret = MakeRequest("GET", sendData);
92 
93  if (ret == null)
94  return ims;
95 
96  if (!ret.ContainsKey("RESULT"))
97  return ims;
98 
99  string result = ret["RESULT"].ToString();
100  if (result == "NULL" || result.ToLower() == "false")
101  {
102  string reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error";
103  m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: GetMessages for {0} failed: {1}", principalID, reason);
104  return ims;
105  }
106 
107  foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values)
108  {
109  GridInstantMessage m = OfflineIMDataUtils.GridInstantMessage((Dictionary<string, object>)v);
110  ims.Add(m);
111  }
112 
113  return ims;
114  }
115 
116  public bool StoreMessage(GridInstantMessage im, out string reason)
117  {
118  reason = string.Empty;
119  Dictionary<string, object> sendData = OfflineIMDataUtils.GridInstantMessage(im);
120 
121  Dictionary<string, object> ret = MakeRequest("STORE", sendData);
122 
123  if (ret == null)
124  {
125  reason = "Bad response from server";
126  return false;
127  }
128 
129  string result = ret["RESULT"].ToString();
130  if (result == "NULL" || result.ToLower() == "false")
131  {
132  reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error";
133  return false;
134  }
135 
136  return true;
137  }
138 
139  public void DeleteMessages(UUID userID)
140  {
141  Dictionary<string, object> sendData = new Dictionary<string, object>();
142  sendData["UserID"] = userID;
143 
144  MakeRequest("DELETE", sendData);
145  }
146 
147  #endregion
148 
149 
150  #region Make Request
151 
152  private Dictionary<string, object> MakeRequest(string method, Dictionary<string, object> sendData)
153  {
154  sendData["METHOD"] = method;
155 
156  string reply = string.Empty;
157  lock (m_Lock)
158  reply = SynchronousRestFormsRequester.MakeRequest("POST",
159  m_ServerURI + "/offlineim",
160  ServerUtils.BuildQueryString(sendData),
161  m_Auth);
162 
163  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
164  reply);
165 
166  return replyData;
167  }
168  #endregion
169 
170  }
171 }
List< GridInstantMessage > GetMessages(UUID principalID)
void DeleteMessages(UUID userID)
Delete messages to or from this user (or group).
bool StoreMessage(GridInstantMessage im, out string reason)