19 package org.turro.elephant.direct;
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;
48 private boolean redirectToCurrent =
false;
54 return redirectToCurrent;
58 this.redirectToCurrent = redirectToCurrent;
82 "$('#form_" + containerId +
"').serialize())" +
83 ".done(function(data) { $('#" + containerId +
"').html(data); });" +
95 public String
getAjaxUrl(String containerId, String values) {
97 return getAjaxUrl(containerId,
new KeyValueMap(values));
98 }
catch (ParserException ex) {
112 public String
getAjaxUrl(String containerId, KeyValueMap values) {
113 values.put(
"domid", containerId);
116 ".done(function(data) { $('#" + containerId +
"').html(data); });";
122 ".done(function(data) { eval(data); });";
128 }
catch (ParserException ex) {
129 Logger.getLogger(
AbstractDirect.class.getName()).log(Level.SEVERE,
null, ex);
151 return createURL(constructor, values, 7,
false);
155 return createURL(constructor, values, daysValid,
false);
164 return createURL(constructor, recipe,
new KeyValueMap(values), daysValid, withDomain);
165 }
catch (ParserException ex) {
190 protected String
generateJson(String rootElement, String itemElement, List results) {
191 JsonObjectBuilder builder = Json.createObjectBuilder();
192 JsonArrayBuilder array = Json.createArrayBuilder();
194 builder.add(rootElement, array);
195 return builder.build().toString();
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);
208 return Json.createObjectBuilder().add(itemElement, item.toString());
214 response.setContentType(
"application/json");
216 response.getWriter().write(json);
217 }
catch (IOException ex) {
230 public boolean myTurn(HttpServletRequest request) {
235 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
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"))) {
247 map.put(
"json", IOUtils.toString(constructor.
getRequest().getReader()));
248 }
catch (IOException ex) {
252 for(String key : constructor.
getRequest().getParameterMap().keySet()) {
253 map.put(key, constructor.
getRequest().getParameter(key));
259 String redirect = map.get(
Actions.REDIR_PAR);
260 if(!Strings.isBlank(redirect)) {
263 }
catch (IOException ex) {
static String createAction(String email, String redir)
static String createRightNowParameter(String values)
static boolean isRightNowAction(IConstructor constructor)
static final String USER_PAR
static KeyValueMap getRightNowAction(IConstructor constructor)
static KeyValueMap getAction(IConstructor constructor)
static String createRightNowAction(String values)
static String getServerBase(String scheme)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String getRootWebPath()
static String logMsg(String msg)
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 createPOST(String values)
abstract String getIdentifier()
String generateJson(String rootElement, String itemElement, List results)
String createFormAction()
String getAjaxUrl(String containerId, KeyValueMap values)
String createRightNowURL(String values)
String createURL(IConstructor constructor, String values)
String createRightNowURL(KeyValueMap values)
String createURL(IConstructor constructor, String values, int daysValid, boolean withDomain)
boolean isRedirectToCurrent()
void setRedirectToCurrent(boolean redirectToCurrent)
String getAjaxEvalUrl(KeyValueMap values)
boolean myTurn(HttpServletRequest request)
void execute(IConstructor constructor)
static boolean isYourTurn(HttpServletRequest request, String path)
static final String DIRECT_CONTENT_PATH
static String createRelativeURL(String id)
void redirect(String path)
ElContext getCurrentContext()
HttpServletRequest getRequest()
static final String CONNECTOR_EMAIL