BrightSide Workbench Full Report + Source Code
AbstractDirectContentCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.Map;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.turro.collections.parser.ParserException;
30 import org.turro.string.Strings;
31 import org.turro.action.Actions;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.security.IUser;
36 import org.turro.marker.ElephantMarker;
37 import org.turro.plugin.contacts.IContact;
38 import org.turro.util.IdGenerator;
39 
44 public abstract class AbstractDirectContentCtrl implements IDirectContent {
45 
46  private final String tmplRoot;
47  private boolean needsUser = false;
48  protected String template = "full";
49 
50  public AbstractDirectContentCtrl(String tmplRoot) {
51  this.tmplRoot = tmplRoot;
52  }
53 
54  public String createFormAction() {
56  }
57 
58  public String createPOST(String values) {
60  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })";
61  }
62 
73  public String getAjaxSubmitUrl(String containerId) {
75  "$('#form_" + containerId + "').serialize())" +
76  ".done(function(data) { $('#" + containerId + "').html(data); });" +
77  "return false;";
78  }
79 
88  public String getAjaxUrl(String containerId, String values) {
89  try {
90  return getAjaxUrl(containerId, new KeyValueMap(values));
91  } catch (ParserException ex) {
92  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(values), ex);
93  }
94  return "";
95  }
96 
105  public String getAjaxUrl(String containerId, KeyValueMap values) {
106  values.put("domid", containerId);
108  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })" +
109  ".done(function(data) { $('#" + containerId + "').html(data); });";
110  }
111 
112  public String getAjaxEvalUrl(KeyValueMap values) {
114  "{ exrino : '" + Actions.createRightNowParameter(values) + "' })" +
115  ".done(function(data) { eval(data); });";
116  }
117 
118  public String createRightNowURL(String values) {
119  try {
120  return createRightNowURL(new KeyValueMap(values));
121  } catch (ParserException ex) {
122  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, null, ex);
123  }
124  return null;
125  }
126 
127  public String createRightNowURL(KeyValueMap values) {
130  "?" + Actions.createRightNowAction(values);
131  }
132 
133  public String createLinkTo(String link, KeyValueMap values) {
135  link + "?" + Actions.createAction(values);
136  }
137 
138  public String createURL() {
141  }
142 
143  public String createURL(IConstructor constructor, String values) {
144  return createURL(constructor, values, 7, false);
145  }
146 
147  public String createURL(IConstructor constructor, String values, int daysValid) {
148  return createURL(constructor, values, daysValid, false);
149  }
150 
151  public String createURL(IConstructor constructor, String values, int daysValid, boolean withDomain) {
152  return createURL(constructor, (IContact) constructor.getUser(), values, daysValid, withDomain);
153 // String exrino = null;
154 // try {
155 // KeyValueMap kvm = new KeyValueMap(values);
156 // kvm.put(Actions.USER_PAR, ((IContact) constructor.getUser()).getConnector(IUser.CONNECTOR_EMAIL));
157 // kvm.put(Actions.REDIR_PAR, getRedirContext(withDomain));
158 // exrino = Actions.createAction(kvm, daysValid, false);
159 // } catch (ParserException ex) {
160 // Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, null, ex);
161 // }
162 // return (withDomain ? ElephantContext.getServerBase("http") : "") +
163 // ElephantContext.getRootWebPath() +
164 // DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
165 // "?" + exrino;
166  }
167 
168  public String createURL(IConstructor constructor, IContact recipe, String values, int daysValid, boolean withDomain) {
169  try {
170  return createURL(constructor, recipe, new KeyValueMap(values), daysValid, withDomain);
171  } catch (ParserException ex) {
172  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
173  }
174  return null;
175 // String exrino = null;
176 // try {
177 // KeyValueMap kvm =
178 // kvm.put(Actions.USER_PAR, recipe.getConnector(IUser.CONNECTOR_EMAIL));
179 // kvm.put(Actions.REDIR_PAR, getRedirContext(withDomain));
180 // exrino = Actions.createAction(kvm, daysValid, false);
181 // } catch (ParserException ex) {
182 // Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, null, ex);
183 // }
184 // return (withDomain ? ElephantContext.getServerBase("http") : "") +
185 // ElephantContext.getRootWebPath() +
186 // DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
187 // "?" + exrino;
188  }
189 
190  public String createURL(IConstructor constructor, IContact recipe, KeyValueMap values, int daysValid, boolean withDomain) {
191  values.put(Actions.USER_PAR, recipe.getConnector(IUser.CONNECTOR_EMAIL));
192  values.put(Actions.REDIR_PAR, getRedirContext(constructor, withDomain));
193  return (withDomain ? ElephantContext.getServerBase("http") : "") +
196  "?" + Actions.createAction(values, daysValid, false);
197  }
198 
199  protected String getRedirContext(IConstructor constructor, boolean withDomain) {
200  if(withDomain) {
201  return ElephantContext.getServerBase("http") + constructor.getCurrentContext().getWebPath();
202  } else {
203  return constructor.getCurrentContext().getPath();
204  }
205  }
206 
207  protected ElephantMarker getMarker(IConstructor constructor) {
208  ElephantMarker marker = getCleanMarkerFrom(constructor);
209  prepareMarker(marker);
210  return marker;
211  }
212 
214  ElephantMarker marker = new ElephantMarker(constructor);
215  marker.put("actions", this);
216  return marker;
217  }
218 
219  protected void addContainerId(ElephantMarker marker, boolean container) {
220  marker.put("container", container);
221  String domid = (String) marker.get("domid");
222  if(Strings.isBlank(domid)) domid = marker.getConstructor().getParameter("domid");
223  marker.put("containerId", container ? "cic_" + IdGenerator.generate() : domid);
224  }
225 
226  protected String getTmplRoot() {
227  return tmplRoot;
228  }
229 
230  protected String getTemplate() {
231  return template;
232  }
233 
234  protected void writeMarkerToResponse(IConstructor constructor, KeyValueMap map) {
235  writeMarkerToResponse(constructor, map, null);
236  }
237 
238  protected void writeMarkerToResponse(IConstructor constructor, KeyValueMap map, Map extra) {
239  ElephantMarker marker = getCleanMarkerFrom(constructor);
240  prepareCleanMarker(marker, map);
241  addContainerId(marker, false);
242  if(extra != null) marker.putAll(extra);
243  try {
244  constructor.getResponse().setContentType("text/html");
245  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
246  marker.process(getTmplRoot(), getTemplate(), constructor.getResponse().getWriter());
247  } catch (IOException ex) {
248  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
249  }
250  }
251 
252  protected void setNeedsUser(boolean needsUser) {
253  this.needsUser = needsUser;
254  }
255 
256  public void setTemplate(String template) {
257  if(template != null) this.template = template;
258  }
259 
260  public void render(IConstructor constructor) {
261  if(!needsUser || hasUser(constructor)) {
262  getMarker(constructor).process(tmplRoot, template);
263  }
264  }
265 
266  public String parse(IConstructor constructor) {
267  if(!needsUser || hasUser(constructor)) {
268  return getMarker(constructor).parse(tmplRoot, template);
269  }
270  return "";
271  }
272 
273  private boolean hasUser(IConstructor constructor) {
274  return (constructor != null) && (constructor.getUser() != null);
275  }
276 
277  /* IDirectContent */
278 
279  @Override
280  public boolean itsMe(String id) {
281  return getIdentifier().equals(id);
282  }
283 
284  @Override
285  public boolean myTurn(HttpServletRequest request) {
286  return DirectContents.isYourTurn(request, getIdentifier());
287  }
288 
289  @Override
290  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
291  execute(ElephantContext.getConstructor(request, response));
292  }
293 
294  public void execute(IConstructor constructor) {
295  KeyValueMap map = Actions.isRightNowAction(constructor) ?
296  Actions.getRightNowAction(constructor) :
297  Actions.getAction(constructor);
298  if(map == null && "POST".equals(constructor.getRequest().getMethod())) {
299  map = new KeyValueMap(Collections.EMPTY_MAP);
300  for(String key : constructor.getRequest().getParameterMap().keySet()) {
301  map.put(key, constructor.getRequest().getParameter(key));
302  }
303  }
304  if(map != null) {
305  doExecute(constructor, map);
306  String redirect = map.get(Actions.REDIR_PAR);
307  if(!Strings.isBlank(redirect)) {
308  try {
309  constructor.redirect(redirect);
310  } catch (IOException ex) {
311  Logger.getLogger(AbstractDirectContentCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
312  }
313  }
314  }
315  }
316 
317  protected abstract String getIdentifier();
318  protected abstract void prepareMarker(ElephantMarker marker);
319  protected abstract void prepareCleanMarker(ElephantMarker marker, KeyValueMap map);
320  protected abstract void doExecute(IConstructor constructor, KeyValueMap map);
321 
322 }
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)
String createURL(IConstructor constructor, String values, int daysValid, boolean withDomain)
abstract void doExecute(IConstructor constructor, KeyValueMap map)
ElephantMarker getCleanMarkerFrom(IConstructor constructor)
void writeMarkerToResponse(IConstructor constructor, KeyValueMap map)
String createURL(IConstructor constructor, IContact recipe, KeyValueMap values, int daysValid, boolean withDomain)
String createURL(IConstructor constructor, String values, int daysValid)
String createURL(IConstructor constructor, String values)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
String getRedirContext(IConstructor constructor, boolean withDomain)
String createURL(IConstructor constructor, IContact recipe, String values, int daysValid, boolean withDomain)
String getAjaxUrl(String containerId, String values)
abstract void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
String getAjaxUrl(String containerId, KeyValueMap values)
void writeMarkerToResponse(IConstructor constructor, KeyValueMap map, Map extra)
void addContainerId(ElephantMarker marker, boolean container)
abstract void prepareMarker(ElephantMarker marker)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27