BrightSide Workbench Full Report + Source Code
TagsServlet.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.web.tags;
20 
21 import java.io.IOException;
22 import javax.servlet.ServletException;
23 import javax.servlet.annotation.WebServlet;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.turro.collections.KeyValueMap;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.web.ElContext;
31 import org.turro.elephant.web.ElContextMap;
32 import org.turro.marker.MarkerHelper;
33 import org.turro.path.Path;
34 import org.turro.string.Strings;
35 
40 @WebServlet(name = "TagsEl", urlPatterns = {"/tag_/*"})
41 public class TagsServlet extends HttpServlet {
42 
43  private static final String CONTEXT_TO = "elctxnav";
44 
45  public static String navigateFromTo(String fromTag, String toTag) {
46  return ElephantContext.getRootWebPath() + "/tag_/" + toTag + "?" + MarkerHelper.setObfuscatedPars("tag=" + fromTag);
47  }
48 
49  public static String navigateAndSet(String fromTag, String path) {
50  return ElephantContext.getRootWebPath() + "/tag_/" + CONTEXT_TO + "?" +
51  MarkerHelper.setObfuscatedPars("tag=" + fromTag + ";path=" + path);
52  }
53 
54  public static String navigateBack(IConstructor constructor, String path) {
55  if(Strings.isBlank(path)) {
56  String backTag = SessionTags.get(constructor).previous();
57  if(!Strings.isBlank(backTag)) {
58  return ElephantContext.getRootWebPath() + "/tag_/" + backTag + "?" +
59  MarkerHelper.setObfuscatedPars("back=true");
60  }
61  } else {
62  return ElephantContext.getRootWebPath() + "/tag_/" + CONTEXT_TO + "?" +
63  MarkerHelper.setObfuscatedPars("back=true;path=" + path);
64  }
65  return null;
66  }
67 
68  protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters(request);
70  if(kvm != null) {
71  if(kvm.containsKey("tag")) SessionTags.get(request, response).add(kvm.get("tag"));
72  if(kvm.containsKey("back")) SessionTags.get(request, response).removeLast();
73  if(kvm.containsKey("path")) {
74  response.sendRedirect(request.getContextPath() + kvm.get("path"));
75  return;
76  }
77  }
78  Path path = Path.pathFrom(request.getPathInfo());
79  ElContext context = ElContextMap.getContextFromWebTag(path.getRoot());
80  if(context != null) {
81  response.setStatus(HttpServletResponse.SC_FOUND);
82  response.sendRedirect(request.getContextPath() + context.getPath());
83  }
84  }
85 
86  @Override
87  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
88  processRequest(request, response);
89  }
90 
91  @Override
92  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
93  processRequest(request, response);
94  }
95 
96  @Override
97  public String getServletInfo() {
98  return "WebTags for Elephant";
99  }
100 
101 }
static ElContext getContextFromWebTag(String webTag)
void doGet(HttpServletRequest request, HttpServletResponse response)
static String navigateAndSet(String fromTag, String path)
static String navigateFromTo(String fromTag, String toTag)
static String navigateBack(IConstructor constructor, String path)
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
static KeyValueMap getObfuscatedParameters()
static String setObfuscatedPars(String parameters)