BrightSide Workbench Full Report + Source Code
AbstractDirectQuery.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.json.Json;
26 import javax.json.JsonArrayBuilder;
27 import javax.json.JsonObjectBuilder;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
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 
40 public abstract class AbstractDirectQuery implements IDirectContent {
41 
42  public String createQueryURL(String values) {
45  "?" + Actions.createRightNowAction("ds=true;" + values) + "&q={query}";
46  }
47 
48  /* IDirectContent */
49 
50  @Override
51  public boolean itsMe(String id) {
52  return getIdentifier().equals(id);
53  }
54 
55  @Override
56  public boolean myTurn(HttpServletRequest request) {
57  return DirectContents.isYourTurn(request, getIdentifier());
58  }
59 
60  @Override
61  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
62  execute(ElephantContext.getConstructor(request, response));
63  }
64 
65  public void execute(IConstructor constructor) {
66  KeyValueMap map = Actions.isRightNowAction(constructor) ?
67  Actions.getRightNowAction(constructor) :
68  Actions.getAction(constructor);
69  if(map != null && "true".equals(map.get("ds"))) {
70  if(map.containsKey("sroot") && map.containsKey("sitem")) {
71  writeJsonResponse(constructor.getResponse(),
72  generateJson(map.get("sroot"), map.get("sitem"), doSearch(map, constructor.getParameter("q"))));
73  } else {
74  doSearch(map, constructor.getParameter("q"));
75  }
76  }
77  }
78 
79  protected abstract String getIdentifier();
80  protected abstract List doSearch(KeyValueMap map, String query);
81 
82  /* Utility methods */
83 
84  protected String generateJson(String rootElement, String itemElement, List results) {
85  JsonObjectBuilder builder = Json.createObjectBuilder();
86  JsonArrayBuilder array = Json.createArrayBuilder();
87  results.forEach(i -> array.add(createJsonItem(itemElement, i)));
88  builder.add(rootElement, array);
89  return builder.build().toString();
90  }
91 
92  protected JsonObjectBuilder createJsonItem(String itemElement, Object item) {
93  if(item instanceof String) {
94  return Json.createObjectBuilder().add(itemElement, (String) item);
95  } else if(item instanceof Double) {
96  return Json.createObjectBuilder().add(itemElement, (Double) item);
97  } else if(item instanceof Integer) {
98  return Json.createObjectBuilder().add(itemElement, (Integer) item);
99  } else if(item instanceof Boolean) {
100  return Json.createObjectBuilder().add(itemElement, (Boolean) item);
101  } else {
102  return Json.createObjectBuilder().add(itemElement, item.toString());
103  }
104  }
105 
106  protected void writeJsonResponse(HttpServletResponse response, String json) {
107  try {
108  response.setContentType("application/json");
109  //response.setCharacterEncoding(ElephantContext.getEncoding());
110  response.getWriter().write(json);
111  } catch (IOException ex) {
112  Logger.getLogger(AbstractDirectQuery.class.getName()).log(Level.SEVERE, null, ex);
113  }
114  }
115 
116 }
static boolean isRightNowAction(IConstructor constructor)
Definition: Actions.java:288
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 IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
String generateJson(String rootElement, String itemElement, List results)
boolean myTurn(HttpServletRequest request)
void writeJsonResponse(HttpServletResponse response, String json)
JsonObjectBuilder createJsonItem(String itemElement, Object item)
abstract List doSearch(KeyValueMap map, String query)
static boolean isYourTurn(HttpServletRequest request, String path)