BrightSide Workbench Full Report + Source Code
DirectContents.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.util.Map;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import javax.servlet.ServletContext;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.turro.string.Strings;
28 import org.turro.action.Actions;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.http.URLUtil;
33 import org.turro.reflection.Instances;
34 
39 public class DirectContents {
40 
41  public static final String
42  DIRECT_CONTENT_PATH = "/_reqxdc_/";
43 
44  public static String getIdentifier(Class<? extends IDirectContent> control) {
45  return control.getAnnotation(DirectContent.class).identifier();
46  }
47 
48  public static String createURL(String server, String id) {
49  String url = URLUtil.checkAppServer(server, ElephantContext.getUseSSL());
50  if(!Strings.isBlank(url)) {
51  return url + DIRECT_CONTENT_PATH + id;
52  }
53  return null;
54  }
55 
56  public static String createRelativeURL(String id) {
58  }
59 
60  public static boolean isDirectContent(HttpServletRequest request) {
61  return request.getServletPath().startsWith(DIRECT_CONTENT_PATH);
62  }
63 
64  public static boolean getContent(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
65  return executeDirectContents(context, request, response);
66  }
67 
68  public static boolean isYourTurn(HttpServletRequest request, String path) {
69  return request.getServletPath().startsWith(DIRECT_CONTENT_PATH + path);
70  }
71 
72  /* URL Helpers */
73 
74  public static String doCreatePOST(String id, String values) {
76  if(dc != null) {
77  return "$.post(\"" + ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + id + "\"," +
78  "{ exrino : \"" + Actions.createRightNowParameter(values) + "\" })";
79  }
80  return null;
81  }
82 
83  public static String doCreatePOST(String id, String domId, String values) {
85  if(dc != null) {
86  return "$.post(\"" + ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + id + "\"," +
87  "{ exrino : \"" + Actions.createRightNowParameter(values) + "\", domid: \"" + domId + "\" })" +
88  ".done(function(data) { $(\"#" + domId + "\").html(data); });";
89  }
90  return null;
91  }
92 
93  public static String doCreateURL(String id, String values) {
95  if(dc != null) {
96  String exrino = Actions.createRightNowAction(values);
98  "?" + exrino;
99  }
100  return null;
101  }
102 
103  public static String doCreateParameter(String id, String parameter, Map<String, String> values) {
105  if(dc != null) {
106  try {
108  "?" + parameter + "=" + Actions.createParameter(values);
109  } catch (Exception ex) {
110  Logger.getLogger(DirectContents.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113  return null;
114  }
115 
116  public static KeyValueMap getParameters(HttpServletRequest request, HttpServletResponse response) {
117  IConstructor constructor = ElephantContext.getConstructor(request, response);
118  return Actions.getRightNowAction(constructor);
119  }
120 
121  /* Direct contents */
122 
123  public static synchronized boolean executeDirectContents(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
124  for(IDirectContent iDirect : Instances.cached().byAnnotation(DirectContent.class, IDirectContent.class)) {
125  if(iDirect.myTurn(request)) {
126  iDirect.execute(context, request, response);
127  return true;
128  }
129  }
130  return false;
131  }
132 
133  public static IDirectContent getDirectContent(String id) {
134  for(IDirectContent iDirect : Instances.cached().byAnnotation(DirectContent.class, IDirectContent.class)) {
135  if(iDirect.itsMe(id)) {
136  return iDirect;
137  }
138  }
139  return null;
140  }
141 
142  private DirectContents() {
143  }
144 
145 }
static String createRightNowParameter(String values)
Definition: Actions.java:333
static String createParameter(String email, String redir)
Definition: Actions.java:122
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static String createRightNowAction(String values)
Definition: Actions.java:312
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String doCreateURL(String id, String values)
static IDirectContent getDirectContent(String id)
static boolean isDirectContent(HttpServletRequest request)
static boolean isYourTurn(HttpServletRequest request, String path)
static String doCreatePOST(String id, String values)
static String createURL(String server, String id)
static String createRelativeURL(String id)
static boolean getContent(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static KeyValueMap getParameters(HttpServletRequest request, HttpServletResponse response)
static String doCreatePOST(String id, String domId, String values)
static String doCreateParameter(String id, String parameter, Map< String, String > values)
static synchronized boolean executeDirectContents(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String getIdentifier(Class<? extends IDirectContent > control)
static String checkAppServer(String server, boolean secured)
Definition: URLUtil.java:37