001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.wiki.macros;
034:
035: import com.opensymphony.module.sitemesh.Page;
036: import com.opensymphony.module.sitemesh.parser.HTMLPageParser;
037:
038: import org.libresource.Libresource;
039:
040: import org.libresource.kernel.KernelConstants;
041: import org.libresource.kernel.interfaces.KernelService;
042:
043: import org.libresource.web.SessionPropagation;
044: import org.libresource.web.config.Config;
045: import org.libresource.web.wiki.LibresourceRenderContext;
046:
047: import org.radeox.macro.Preserved;
048: import org.radeox.macro.parameter.MacroParameter;
049:
050: import java.io.BufferedReader;
051: import java.io.IOException;
052: import java.io.InputStreamReader;
053: import java.io.Writer;
054:
055: import java.net.URI;
056: import java.net.URL;
057:
058: import java.util.StringTokenizer;
059: import java.util.Vector;
060:
061: public class ResourceMacro extends Preserved {
062: private String scheme;
063: private String host;
064: private KernelService kernelService;
065:
066: public ResourceMacro() throws Exception {
067: super ();
068: kernelService = (KernelService) Libresource
069: .getService(KernelConstants.SERVICE);
070: scheme = kernelService.getUriScheme();
071: host = kernelService.getUriHost();
072:
073: addSpecial('[');
074: addSpecial(']');
075: addSpecial('{');
076: addSpecial('}');
077: addSpecial('*');
078: addSpecial('-');
079: addSpecial('\\');
080: }
081:
082: public String getName() {
083: return "resource";
084: }
085:
086: public void execute(Writer writer, MacroParameter params)
087: throws IllegalArgumentException, IOException {
088: try {
089: if (params.getLength() < 1) {
090: throw new IllegalArgumentException(
091: "syntax : URI|view|noaction");
092: }
093:
094: String sessionId = SessionPropagation
095: .getSessionPropagation().getSession().getId();
096:
097: // i assume that it's a LibreSource link
098: String LibresourceLinkPattern = "([^\"'=]|^)("
099: + scheme
100: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
101: URI currentUri = ((LibresourceRenderContext) params
102: .getContext()).getUri();
103:
104: String uri = params.get(0);
105: URI resolvedUri = null;
106: URL url = null;
107:
108: // absolute LibreSource link ??
109: if (uri.matches(LibresourceLinkPattern)) {
110: resolvedUri = kernelService.normalizeURI(new URI(uri));
111: } else {
112: // so, it's an relative LibreSource link
113: URI relativeLink;
114:
115: if (uri.startsWith("/")) {
116: relativeLink = new URI(scheme + "://" + host + uri);
117: } else {
118: relativeLink = new URI(uri);
119: }
120:
121: resolvedUri = kernelService.normalizeURI(currentUri
122: .resolve(relativeLink));
123: }
124:
125: String wikiChain = Config.getInstance(null).getWikiChain();
126: String currentPath = kernelService
127: .normalizeAbsoluteURIPath(currentUri);
128:
129: String target = kernelService.getServerUrl() + "/"
130: + resolvedUri.getPath();
131:
132: // security
133: target += (";jsessionid=" + sessionId);
134:
135: // chain
136: if (wikiChain == null) {
137: wikiChain = "";
138: target += ("?nodecorator&wikichain=" + currentPath);
139: } else {
140: target += ("?nodecorator&wikichain=" + wikiChain + "," + currentPath);
141: }
142:
143: // view
144: String view = (String) params.getParams().get("view");
145:
146: if (view != null) {
147: target += ("&view=" + view);
148: }
149:
150: // action
151: String action = (String) params.getParams().get("action");
152:
153: if (action != null) {
154: target += ("&action=" + action);
155: }
156:
157: // noaction
158: if ("noaction".equals(params.get(1))
159: || "noaction".equals(params.get(2))
160: || "noaction".equals(params.get(3))) {
161: target += "&noaction";
162: }
163:
164: // get the content
165: url = new URL(target);
166:
167: // cycle detection
168: StringTokenizer tokenizer = new StringTokenizer(wikiChain,
169: ",");
170: Vector tokens = new Vector();
171:
172: while (tokenizer.hasMoreTokens()) {
173: tokens.add(tokenizer.nextToken());
174: }
175:
176: // if no cycle
177: if (!tokens.contains(currentPath)) {
178: // get content
179: StringBuffer strbuf = new StringBuffer();
180: BufferedReader is = new BufferedReader(
181: new InputStreamReader(url.openStream(),
182: "ISO-8859-1"));
183:
184: String b = null;
185:
186: while ((b = is.readLine()) != null) {
187: // fix a strange bug...
188: b = b.replaceAll("\\[", "");
189: b = b.replaceAll("\\]", "");
190: strbuf.append(b);
191: }
192:
193: is.close();
194: is.close();
195:
196: Page page = new HTMLPageParser().parse(strbuf
197: .toString().toCharArray());
198: writer.write(page.getBody());
199: } else {
200: // cycle detected -> stop
201: writer
202: .write("<span class=\"error\">Cycle detected in \"resource\" macro !!!</span>");
203: }
204:
205: writer.write("<br/>");
206:
207: // stop chain
208: Config.getInstance(null).setWikiChain(null);
209: } catch (IOException e) {
210: throw e;
211: } catch (IllegalArgumentException e) {
212: throw e;
213: } catch (Exception e) {
214: throw new IllegalArgumentException("error in resource : "
215: + e.getMessage());
216: }
217: }
218:
219: public String getDescription() {
220: return "Include the view of a LibreSource resource.";
221: }
222:
223: public String[] getParamDescription() {
224: return new String[] { "1. the uri of the resource (mandatory)",
225: "2. the view (optional)", "3. the action (optional)",
226: "4. noaction (optional)" };
227: }
228: }
|