BrightSide Workbench Full Report + Source Code
ContextAction.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.elephant.impl.context.action;
19 
20 import com.sun.syndication.feed.synd.SyndFeed;
21 import com.sun.syndication.feed.synd.SyndFeedImpl;
22 import com.sun.syndication.io.FeedException;
23 import com.sun.syndication.io.SyndFeedOutput;
24 import java.io.IOException;
25 import javax.servlet.ServletException;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.context.IAction;
28 import org.turro.elephant.context.IElement;
29 import org.turro.elephant.feed.IRSSable;
30 import org.turro.elephant.impl.abstracts.AbstractAction;
31 import org.turro.elephant.web.ElContext;
32 import org.turro.elephant.web.ElContextMap;
33 
38 public class ContextAction extends AbstractAction implements IAction {
39 
40  public ContextAction() {
41  }
42 
43  @Override
44  public int execute() throws Exception {
45  String ctx = getParameter("context"),
46  action = getActionParameter(),
47  ctx_x = "";
48 
49  boolean isAJAX = false;
50 
51  ElContext context = ElContextMap.getContext(ctx);
52 
53  if(context != null) {
54  if ("print".equals(action)) {
55  //ensures loading metas & headers
58  printContext(context, getIntParameter("level", 10), !"false".equals(getParameter("title")));
61  // not true but prevents from redirecting
62  isAJAX = true;
63  } else if ("read".equals(action)) {
65  constructor.getResponse().setContentType("text/html");
66  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
67  constructor.setStopper(getBooleanParameter("stopper") ? "<end/>" : null);
68  printContext(context, 1, false);
70  // not true but prevents from redirecting
71  isAJAX = true;
72  } else if ("rss".equals(action)) {
73  rssContext(context);
75  // not true but prevents from redirecting
76  isAJAX = true;
77  }
78  }
79 
80  if (!isAJAX) {
81  constructor.redirect(ctx + ctx_x);
82  }
83 
84  return IAction.DONE;
85  }
86 
87  private void printContext(ElContext context, int level, boolean title) throws ServletException, IOException {
88  if(level <= 0) return;
89  if(context.canPrint() && context.getWebContext().isWikiPage()) {
91  if(title) constructor.getOut().print("<div class='context-title'>" + context.getName() + "</div>");
92  context.getElement().startConstruction();
93  for(ElContext ctx : context.getChildren()) {
94  printContext(ctx, level-1, title);
95  }
96  }
97  }
98 
99  private void rssContext(ElContext context) throws IOException, FeedException {
100  constructor.getResponse().setContentType("application/rss+xml");
102  IElement el = context.getElement();
103  if(el instanceof IRSSable) {
104  SyndFeed feed = new SyndFeedImpl();
105  feed.setFeedType("rss_1.0");
106  feed.setEncoding(ElephantContext.getEncoding());
107  feed.setTitle(ElephantContext.getSiteName() + " - " + context.getName());
108  feed.setLink(ElephantContext.getServerUrl("http"));
109  feed.setDescription(context.getName());
110  feed.setEntries(((IRSSable) el).getSyndEntries());
111  SyndFeedOutput output = new SyndFeedOutput();
112  output.output(feed, constructor.getOut());
113  }
114  }
115 
116 }
static String getServerUrl(String scheme)
static ElContext getContext(IConstructor constructor)
TreeSet< ElContext > getChildren()
Definition: ElContext.java:196
String getParameter(String param)
void setRenderingContext(ElContext context)
void forceIsAContext(ElContext context)