View Javadoc

1   package de.tivsource.page.user.actions;
2   
3   import java.util.regex.Pattern;
4   
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   import org.apache.struts2.ServletActionContext;
8   import org.apache.struts2.convention.annotation.Action;
9   import org.apache.struts2.convention.annotation.Actions;
10  import org.apache.struts2.convention.annotation.Result;
11  import org.apache.struts2.tiles.annotation.TilesDefinition;
12  import org.apache.struts2.tiles.annotation.TilesDefinitions;
13  import org.apache.struts2.tiles.annotation.TilesPutAttribute;
14  
15  import de.tivsource.ejb3plugin.InjectEJB;
16  import de.tivsource.page.dao.page.PageDaoLocal;
17  import de.tivsource.page.entity.page.Page;
18  
19  /**
20   * 
21   * @author Marc Michele
22   *
23   */
24  @TilesDefinitions({
25    @TilesDefinition(name="page", extend = "userTemplate", putAttributes = {
26      @TilesPutAttribute(name = "meta",       value = "/WEB-INF/tiles/active/meta/content.jsp"),
27      @TilesPutAttribute(name = "twitter",    value = "/WEB-INF/tiles/active/twitter/content.jsp"),
28      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/page/page.jsp")
29    })
30  })
31  public class PageAction extends EmptyAction {
32  
33      /**
34       * Serial Version UID.
35       */
36      private static final long serialVersionUID = 6236431708460575442L;
37  
38      /**
39       * Statischer Logger der Klasse.
40       */
41      private static final Logger LOGGER = LogManager.getLogger(PageAction.class);
42  
43      @InjectEJB(name = "PageDao")
44      private PageDaoLocal pageDaoLocal;
45  
46      /**
47       * Seiten-Name im Pfad (Achtung kann duch den Benutzer manipuliert werden).
48       */
49      private String pageName;
50  
51      private Page page;
52  
53      @Override
54      @Actions({
55          @Action(value = "*/*", results = {
56              @Result(name = "success", type = "tiles", location = "page"),
57              @Result(name = "error", type = "redirectAction", location = "index.html") })
58      })
59      public String execute() throws Exception {
60          LOGGER.info("execute() aufgerufen.");
61  
62          // Hole Action Locale
63          this.getLanguageFromActionContext();
64  
65          pageName = ServletActionContext.getRequest().getServletPath();
66          LOGGER.info("PageName: " + pageName);
67  
68          // /gallery/painting/index.html?page=1&request_locale=de
69          
70          
71          pageName = pageName.replaceAll("/index.html", "");
72          pageName = pageName.replaceAll("/", "");
73              
74          LOGGER.info("PageName: " + pageName);
75  
76          /*
77           * Wenn der Seiten-Name keine nicht erlaubten Zeichen enthält und es
78           * die Seite mit dem Namen gibt dann wird der Block ausgeführt.
79           */
80          if (isValid(pageName) && pageDaoLocal.isPageUrl(pageName)) {
81              page = pageDaoLocal.findByTechnical(pageName);
82              return SUCCESS;
83          }
84  
85          /*
86           * Wenn es die Seite nicht gibt oder es einen Manipulationsversuch
87           * gab.
88           */
89           return ERROR;
90      }// Ende execute()
91  
92      @Override
93      public Page getPage() {
94          return page;
95      }
96  
97      private Boolean isValid(String input) {
98          if (Pattern.matches("[a-z]*", input)) {
99              return true;
100         } else {
101             return false;
102         }
103     }
104 
105 }// Ende class