BrightSide Workbench Full Report + Source Code
EntityInfoContent.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.action;
20 
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.direct.DirectContent;
30 import org.turro.elephant.direct.DirectContents;
31 import org.turro.elephant.direct.IDirectContent;
32 
37 @DirectContent(identifier="entity-info")
38 public class EntityInfoContent implements IDirectContent {
39 
40  public static String createPopup(EntityInfoType type, LinkType link, String path) {
41  return "popupInfo('+Info', '" +
42  createURL(type, link) + "&path=" + path +
43  "')";
44  }
45 
46  public static String createURL(EntityInfoType type, LinkType link) {
47  if(type == null) {
48  type = EntityInfoType.SUMMARY;
49  }
50  if(link == null) {
51  link = LinkType.WEB;
52  }
54  getIdentifier() + "?type=" + type.toString() + "&link=" + link.toString();
55  }
56 
57  public static String createURL(String path, EntityInfoType type, LinkType link) {
58  return createURL(type, link) + "&path=" + path;
59  }
60 
61  public static String getIdentifier() {
62  return EntityInfoContent.class.getAnnotation(DirectContent.class).identifier();
63  }
64 
65  @Override
66  public boolean itsMe(String id) {
67  return getIdentifier().equals(id);
68  }
69 
70  @Override
71  public boolean myTurn(HttpServletRequest request) {
72  return DirectContents.isYourTurn(request, getIdentifier());
73  }
74 
75  @Override
76  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
77  try {
78  Map<String, String[]> pars = request.getParameterMap();
79  EntityInfoType type = pars.containsKey("type") ? EntityInfoType.valueOf(pars.get("type")[0]) : null;
80  LinkType link = pars.containsKey("link") ? LinkType.valueOf(pars.get("link")[0]) : null;
81  response.setContentType("text/html;charset=" + ElephantContext.getEncoding());
82  EntitiesInfo.writeEntityInfo(response.getWriter(), pars.get("path")[0], type, link);
83  } catch (IOException ex) {
84  Logger.getLogger(EntityInfoContent.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
85  }
86  }
87 
88 }
static boolean writeEntityInfo(Writer out, String path, EntityInfoType type, LinkType link)
static String createURL(EntityInfoType type, LinkType link)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String createURL(String path, EntityInfoType type, LinkType link)
static String createPopup(EntityInfoType type, LinkType link, String path)
boolean myTurn(HttpServletRequest request)
static boolean isYourTurn(HttpServletRequest request, String path)