BrightSide Workbench Full Report + Source Code
AbstractDirect.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.direct;
20 
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.json.Json;
27 import javax.json.JsonArrayBuilder;
28 import javax.json.JsonObjectBuilder;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.turro.collections.parser.ParserException;
33 import org.turro.string.Strings;
34 import org.apache.commons.io.IOUtils;
35 import org.turro.action.Actions;
36 import org.turro.collections.KeyValueMap;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.context.IConstructor;
39 import org.turro.elephant.security.IUser;
40 import org.turro.plugin.contacts.IContact;
41 
46 public abstract class AbstractDirect implements IDirectContent {
47 
48  private boolean redirectToCurrent = false;
49 
50  public AbstractDirect() {
51  }
52 
53  public boolean isRedirectToCurrent() {
54  return redirectToCurrent;
55  }
56 
57  public void setRedirectToCurrent(boolean redirectToCurrent) {
58  this.redirectToCurrent = redirectToCurrent;
59  }
60 
61  public String createFormAction() {
63  }
64 
65  public String createPOST(String values) {
67  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })";
68  }
69 
80  public String getAjaxSubmitUrl(String containerId) {
82  "$('#form_" + containerId + "').serialize())" +
83  ".done(function(data) { $('#" + containerId + "').html(data); });" +
84  "return false;";
85  }
86 
95  public String getAjaxUrl(String containerId, String values) {
96  try {
97  return getAjaxUrl(containerId, new KeyValueMap(values));
98  } catch (ParserException ex) {
99  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(values), ex);
100  }
101  return "";
102  }
103 
112  public String getAjaxUrl(String containerId, KeyValueMap values) {
113  values.put("domid", containerId);
115  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })" +
116  ".done(function(data) { $('#" + containerId + "').html(data); });";
117  }
118 
119  public String getAjaxEvalUrl(KeyValueMap values) {
121  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })" +
122  ".done(function(data) { eval(data); });";
123  }
124 
125  public String createRightNowURL(String values) {
126  try {
127  return createRightNowURL(new KeyValueMap(values));
128  } catch (ParserException ex) {
129  Logger.getLogger(AbstractDirect.class.getName()).log(Level.SEVERE, null, ex);
130  }
131  return null;
132  }
133 
134  public String createRightNowURL(KeyValueMap values) {
137  "?" + Actions.createRightNowAction(values);
138  }
139 
140  public String createLinkTo(String link, KeyValueMap values) {
142  link + "?" + Actions.createAction(values);
143  }
144 
145  public String createURL() {
148  }
149 
150  public String createURL(IConstructor constructor, String values) {
151  return createURL(constructor, values, 7, false);
152  }
153 
154  public String createURL(IConstructor constructor, String values, int daysValid) {
155  return createURL(constructor, values, daysValid, false);
156  }
157 
158  public String createURL(IConstructor constructor, String values, int daysValid, boolean withDomain) {
159  return createURL(constructor, (IContact) constructor.getUser(), values, daysValid, withDomain);
160  }
161 
162  public String createURL(IConstructor constructor, IContact recipe, String values, int daysValid, boolean withDomain) {
163  try {
164  return createURL(constructor, recipe, new KeyValueMap(values), daysValid, withDomain);
165  } catch (ParserException ex) {
166  Logger.getLogger(AbstractDirect.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
167  }
168  return null;
169  }
170 
171  public String createURL(IConstructor constructor, IContact recipe, KeyValueMap values, int daysValid, boolean withDomain) {
172  values.put(Actions.USER_PAR, recipe.getConnector(IUser.CONNECTOR_EMAIL));
173  if(redirectToCurrent) values.put(Actions.REDIR_PAR, getRedirContext(constructor, withDomain));
174  return (withDomain ? ElephantContext.getServerBase("http") : "") +
177  "?" + Actions.createAction(values, daysValid, false);
178  }
179 
180  protected String getRedirContext(IConstructor constructor, boolean withDomain) {
181  if(withDomain) {
182  return ElephantContext.getServerBase("http") + constructor.getCurrentContext().getWebPath();
183  } else {
184  return constructor.getCurrentContext().getPath();
185  }
186  }
187 
188  /* JSON */
189 
190  protected String generateJson(String rootElement, String itemElement, List results) {
191  JsonObjectBuilder builder = Json.createObjectBuilder();
192  JsonArrayBuilder array = Json.createArrayBuilder();
193  results.forEach(i -> array.add(createJsonItem(itemElement, i)));
194  builder.add(rootElement, array);
195  return builder.build().toString();
196  }
197 
198  protected JsonObjectBuilder createJsonItem(String itemElement, Object item) {
199  if(item instanceof String) {
200  return Json.createObjectBuilder().add(itemElement, (String) item);
201  } else if(item instanceof Double) {
202  return Json.createObjectBuilder().add(itemElement, (Double) item);
203  } else if(item instanceof Integer) {
204  return Json.createObjectBuilder().add(itemElement, (Integer) item);
205  } else if(item instanceof Boolean) {
206  return Json.createObjectBuilder().add(itemElement, (Boolean) item);
207  } else {
208  return Json.createObjectBuilder().add(itemElement, item.toString());
209  }
210  }
211 
212  protected void writeJsonResponse(HttpServletResponse response, String json) {
213  try {
214  response.setContentType("application/json");
215  //response.setCharacterEncoding(ElephantContext.getEncoding());
216  response.getWriter().write(json);
217  } catch (IOException ex) {
218  Logger.getLogger(AbstractDirectQuery.class.getName()).log(Level.SEVERE, null, ex);
219  }
220  }
221 
222  /* IDirectContent */
223 
224  @Override
225  public boolean itsMe(String id) {
226  return getIdentifier().equals(id);
227  }
228 
229  @Override
230  public boolean myTurn(HttpServletRequest request) {
231  return DirectContents.isYourTurn(request, getIdentifier());
232  }
233 
234  @Override
235  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
236  execute(ElephantContext.getConstructor(request, response));
237  }
238 
239  public void execute(IConstructor constructor) {
240  KeyValueMap map = Actions.isRightNowAction(constructor) ?
241  Actions.getRightNowAction(constructor) :
242  Actions.getAction(constructor);
243  if(map == null && "POST".equals(constructor.getRequest().getMethod())) {
244  map = new KeyValueMap(Collections.EMPTY_MAP);
245  if("application/json".equals(constructor.getRequest().getHeader("Content-Type"))) {
246  try {
247  map.put("json", IOUtils.toString(constructor.getRequest().getReader()));
248  } catch (IOException ex) {
249  Logger.getLogger(AbstractDirect.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
250  }
251  } else {
252  for(String key : constructor.getRequest().getParameterMap().keySet()) {
253  map.put(key, constructor.getRequest().getParameter(key));
254  }
255  }
256  }
257  if(map != null) {
258  doExecute(constructor, map);
259  String redirect = map.get(Actions.REDIR_PAR);
260  if(!Strings.isBlank(redirect)) {
261  try {
262  constructor.redirect(redirect);
263  } catch (IOException ex) {
264  Logger.getLogger(AbstractDirect.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
265  }
266  }
267  }
268  }
269 
270  protected abstract String getIdentifier();
271  protected abstract void doExecute(IConstructor constructor, KeyValueMap map);
272 
273 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static String createRightNowParameter(String values)
Definition: Actions.java:333
static boolean isRightNowAction(IConstructor constructor)
Definition: Actions.java:288
static final String USER_PAR
Definition: Actions.java:66
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static KeyValueMap getAction(IConstructor constructor)
Definition: Actions.java:252
static String createRightNowAction(String values)
Definition: Actions.java:312
static String getServerBase(String scheme)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
void writeJsonResponse(HttpServletResponse response, String json)
String createLinkTo(String link, KeyValueMap values)
String createURL(IConstructor constructor, IContact recipe, KeyValueMap values, int daysValid, boolean withDomain)
String getAjaxSubmitUrl(String containerId)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
JsonObjectBuilder createJsonItem(String itemElement, Object item)
String createURL(IConstructor constructor, IContact recipe, String values, int daysValid, boolean withDomain)
abstract void doExecute(IConstructor constructor, KeyValueMap map)
String getAjaxUrl(String containerId, String values)
String createURL(IConstructor constructor, String values, int daysValid)
String getRedirContext(IConstructor constructor, boolean withDomain)
String generateJson(String rootElement, String itemElement, List results)
String getAjaxUrl(String containerId, KeyValueMap values)
String createURL(IConstructor constructor, String values)
String createRightNowURL(KeyValueMap values)
String createURL(IConstructor constructor, String values, int daysValid, boolean withDomain)
void setRedirectToCurrent(boolean redirectToCurrent)
String getAjaxEvalUrl(KeyValueMap values)
boolean myTurn(HttpServletRequest request)
void execute(IConstructor constructor)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27