otsdaq  v2_05_02_indev
DesktopIconTable_table.cc
1 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq/Macros/TablePluginMacros.h"
3 #include "otsdaq/TablePlugins/DesktopIconTable.h"
4 #include "otsdaq/TablePlugins/XDAQContextTable.h"
5 
6 #include "otsdaq/WebUsersUtilities/WebUsers.h"
7 
8 #include <stdio.h>
9 #include <fstream> // std::fstream
10 #include <iostream>
11 using namespace ots;
12 
13 #define DESKTOP_ICONS_FILE std::string(__ENV__("SERVICE_DATA_PATH")) + "/OtsWizardData/iconList.dat"
14 
15 // DesktopIconTable Column names
16 
17 const std::string DesktopIconTable::COL_NAME = "IconName";
18 const std::string DesktopIconTable::COL_STATUS = TableViewColumnInfo::COL_NAME_STATUS;
19 const std::string DesktopIconTable::COL_CAPTION = "Caption";
20 const std::string DesktopIconTable::COL_ALTERNATE_TEXT = "AlternateText";
21 const std::string DesktopIconTable::COL_FORCE_ONLY_ONE_INSTANCE = "ForceOnlyOneInstance";
22 const std::string DesktopIconTable::COL_PERMISSIONS = "RequiredPermissionLevel";
23 const std::string DesktopIconTable::COL_IMAGE_URL = "ImageURL";
24 const std::string DesktopIconTable::COL_WINDOW_CONTENT_URL = "WindowContentURL";
25 const std::string DesktopIconTable::COL_APP_LINK = "LinkToApplicationTable";
26 const std::string DesktopIconTable::COL_APP_LINK_UID = "LinkToApplicationUID";
27 
28 const std::string DesktopIconTable::COL_PARAMETER_LINK = "LinkToParameterTable";
29 const std::string DesktopIconTable::COL_PARAMETER_LINK_GID = "LinkToParameterGroupID";
30 const std::string DesktopIconTable::COL_FOLDER_PATH = "FolderPath";
31 
32 const std::string DesktopIconTable::COL_PARAMETER_GID = "windowParameterGroupID";
33 const std::string DesktopIconTable::COL_PARAMETER_KEY = "windowParameterKey";
34 const std::string DesktopIconTable::COL_PARAMETER_VALUE = "windowParameterValue";
35 
36 const std::string DesktopIconTable::ICON_TABLE = "DesktopIconTable";
37 const std::string DesktopIconTable::PARAMETER_TABLE = "DesktopWindowParameterTable";
38 
39 //#define COL_NAME "IconName"
40 //#define COL_STATUS TableViewColumnInfo::COL_NAME_STATUS
41 //#define COL_CAPTION "Caption"
42 //#define COL_ALTERNATE_TEXT "AlternateText"
43 //#define COL_FORCE_ONLY_ONE_INSTANCE "ForceOnlyOneInstance"
44 //#define COL_REQUIRED_PERMISSION_LEVEL "RequiredPermissionLevel"
45 //#define COL_IMAGE_URL "ImageURL"
46 //#define COL_WINDOW_CONTENT_URL "WindowContentURL"
47 //#define COL_APP_LINK "LinkToApplicationTable"
48 //#define COL_PARAMETER_LINK "LinkToParameterTable"
49 //#define COL_PARAMETER_KEY "windowParameterKey"
50 //#define COL_PARAMETER_VALUE "windowParameterValue"
51 //#define COL_FOLDER_PATH "FolderPath"
52 
53 // XDAQ App Column names
54 const std::string DesktopIconTable::COL_APP_ID = "Id";
55 //#define COL_APP_ID "Id"
56 
57 //==============================================================================
58 DesktopIconTable::DesktopIconTable(void) : TableBase(DesktopIconTable::ICON_TABLE)
59 {
60  // Icon list no longer passes through file! so delete it from user's $USER_DATA
61  std::system(("rm -rf " + (std::string)DESKTOP_ICONS_FILE).c_str());
62 
64  // WARNING: the names used in C++ MUST match the Table INFO //
66 }
67 
68 //==============================================================================
69 DesktopIconTable::~DesktopIconTable(void) {}
70 
71 //==============================================================================
72 void DesktopIconTable::init(ConfigurationManager* configManager)
73 {
74  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
75  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
76 
77  unsigned int intVal;
78 
79  auto childrenMap = configManager->__SELF_NODE__.getChildren();
80 
81  ConfigurationTree contextTableNode = configManager->getNode(ConfigurationManager::XDAQ_CONTEXT_TABLE_NAME);
82  const XDAQContextTable* contextTable =
83  configManager->getTable<XDAQContextTable>(ConfigurationManager::XDAQ_CONTEXT_TABLE_NAME);
84 
85  //find gateway host origin string, to avoid modifying icons with same host
86  std::string gatewayContextUID = contextTable->getContextOfGateway(configManager);
87 
88 
89 
90  activeDesktopIcons_.clear();
91 
93  bool addedAppId;
94  bool numeric;
95  unsigned int i;
96  for(auto& child : childrenMap)
97  {
98  if(!child.second.getNode(COL_STATUS).getValue<bool>())
99  continue;
100  //__COUTV__(child.first);
101 
102  activeDesktopIcons_.push_back(DesktopIconTable::DesktopIcon());
103  icon = &(activeDesktopIcons_.back());
104 
105  icon->caption_ = child.second.getNode(COL_CAPTION).getValue<std::string>();
106  icon->alternateText_ = child.second.getNode(COL_ALTERNATE_TEXT).getValue<std::string>();
107  icon->enforceOneWindowInstance_ = child.second.getNode(COL_FORCE_ONLY_ONE_INSTANCE).getValue<bool>();
108  icon->permissionThresholdString_ = child.second.getNode(COL_PERMISSIONS).getValue<std::string>();
109  icon->imageURL_ = child.second.getNode(COL_IMAGE_URL).getValue<std::string>();
110  icon->windowContentURL_ = child.second.getNode(COL_WINDOW_CONTENT_URL).getValue<std::string>();
111  icon->folderPath_ = child.second.getNode(COL_FOLDER_PATH).getValue<std::string>();
112 
113  if(icon->folderPath_ == TableViewColumnInfo::DATATYPE_STRING_DEFAULT)
114  icon->folderPath_ = ""; // convert DEFAULT to empty string
115 
116  if(icon->permissionThresholdString_ == TableViewColumnInfo::DATATYPE_STRING_DEFAULT)
117  icon->permissionThresholdString_ = "1"; // convert DEFAULT to standard user allow
118 
119  numeric = true;
120  for(i = 0; i < icon->permissionThresholdString_.size(); ++i)
121  if(!(icon->permissionThresholdString_[i] >= '0' && icon->permissionThresholdString_[i] <= '9'))
122  {
123  numeric = false;
124  break;
125  }
126  // for backwards compatibility, if permissions threshold is a single number
127  // assume it is the threshold intended for the WebUsers::DEFAULT_USER_GROUP group
128  if(numeric)
129  icon->permissionThresholdString_ = WebUsers::DEFAULT_USER_GROUP + ":" + icon->permissionThresholdString_;
130 
131  // remove all commas from member strings because desktop icons are served to
132  // client in comma-separated string
133  icon->caption_ = removeCommas(icon->caption_, false /*andHexReplace*/, true /*andHTMLReplace*/);
134  icon->alternateText_ = removeCommas(icon->alternateText_, false /*andHexReplace*/, true /*andHTMLReplace*/);
135  icon->imageURL_ = removeCommas(icon->imageURL_, true /*andHexReplace*/);
136  icon->windowContentURL_ = removeCommas(icon->windowContentURL_, true /*andHexReplace*/);
137  icon->folderPath_ = removeCommas(icon->folderPath_, false /*andHexReplace*/, true /*andHTMLReplace*/);
138 
139  // add application origin and URN/LID to windowContentURL_, if link is given
140  addedAppId = false;
141  ConfigurationTree appLink = child.second.getNode(COL_APP_LINK);
142  if(!appLink.isDisconnected())
143  {
144 
145  //first check app origin
146  if(icon->windowContentURL_.size() && icon->windowContentURL_[0] == '/')
147  {
148  //if starting with opening slash, then assume app should come from
149  // appLink context's origin (to avoid cross-origin issues communicating
150  // with app/supervisor)
151 
152  std::string contextUID = contextTable->getContextOfApplication(configManager,
153  appLink.getValueAsString());
154 
155 
156  //only prepend address if not same as gateway
157  if(contextUID != gatewayContextUID)
158  {
159  try
160  {
161  //__COUTV__(contextUID);
162  ConfigurationTree contextNode =
163  contextTableNode.getNode(contextUID);
164 
165  std::string contextAddress = contextNode.getNode(
166  XDAQContextTable::colContext_.colAddress_).getValue<std::string>();
167  unsigned int contextPort = contextNode.getNode(
168  XDAQContextTable::colContext_.colPort_).getValue<unsigned int>();
169 
170  //__COUTV__(contextAddress);
171  icon->windowContentURL_ = contextAddress + ":" +
172  std::to_string(contextPort) +
173  icon->windowContentURL_;
174  //__COUTV__(icon->windowContentURL_);
175  }
176  catch(const std::runtime_error& e)
177  {
178  __SS__ << "Error finding App origin which was linked to Desktop Icon '" <<
179  child.first <<
180  "': " << e.what() << __E__;
181  ss << "\n\nPlease fix by disabling the Icon, enabling the App or fixing the link in the Configurate Tree." << __E__;
182  __SS_THROW__;
183  }
184  }
185  } //end app origin check
186 
187 
188  // if last character is not '='
189  // then assume need to add "?urn="
190  if(icon->windowContentURL_[icon->windowContentURL_.size() - 1] != '=')
191  icon->windowContentURL_ += "?urn=";
192 
193  //__COUT__ << "Following Application link." << std::endl;
194  appLink.getNode(COL_APP_ID).getValue(intVal);
195  icon->windowContentURL_ += std::to_string(intVal);
196 
197  //__COUT__ << "URN/LID=" << intVal << std::endl;
198  addedAppId = true;
199  }
200  //__COUTV__(icon->windowContentURL_);
201 
202  // add parameters if link is given
203  if(!child.second.getNode(COL_PARAMETER_LINK).isDisconnected())
204  {
205  // if there is no '?' found
206  // then assume need to add "?"
207  if(icon->windowContentURL_.find('?') == std::string::npos)
208  icon->windowContentURL_ += '?';
209  else if(addedAppId || icon->windowContentURL_[icon->windowContentURL_.size() - 1] != '?') // if not first parameter, add &
210  icon->windowContentURL_ += '&';
211 
212  // now add each paramter separated by &
213  auto paramGroupMap = child.second.getNode(COL_PARAMETER_LINK).getChildren();
214  bool notFirst = false;
215  for(const auto param : paramGroupMap)
216  {
217  if(!param.second.isEnabled())
218  continue;
219 
220  if(notFirst)
221  icon->windowContentURL_ += '&';
222  else
223  notFirst = true;
224  icon->windowContentURL_ += StringMacros::encodeURIComponent(param.second.getNode(COL_PARAMETER_KEY).getValue<std::string>()) + "=" +
225  StringMacros::encodeURIComponent(param.second.getNode(COL_PARAMETER_VALUE).getValue<std::string>());
226  }
227  }
228  } // end main icon extraction loop
229 
230 } // end init()
231 
232 //==============================================================================
233 std::string DesktopIconTable::removeCommas(const std::string& str, bool andHexReplace, bool andHTMLReplace)
234 {
235  std::string retStr = "";
236  retStr.reserve(str.length());
237 
238  for(unsigned int i = 0; i < str.length(); ++i)
239  if(str[i] != ',')
240  retStr += str[i];
241  else if(andHexReplace)
242  retStr += "%2C";
243  else if(andHTMLReplace)
244  retStr += "&#44;";
245 
246  return retStr;
247 } // end removeCommas()
248 
249 //==============================================================================
250 void DesktopIconTable::setAllDesktopIcons(const std::vector<DesktopIconTable::DesktopIcon>& newIcons)
251 {
252  activeDesktopIcons_.clear();
253  for(const auto& newIcon : newIcons)
254  activeDesktopIcons_.push_back(newIcon);
255 
256 } // end setAllDesktopIcons
257 
258 DEFINE_OTS_TABLE(DesktopIconTable)