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.servlets;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceResourceIdentifier;
037: import org.libresource.LibresourceResourceValue;
038:
039: import org.libresource.kernel.KernelConstants;
040: import org.libresource.kernel.URINotExistException;
041: import org.libresource.kernel.interfaces.KernelService;
042:
043: import org.libresource.membership.MembershipConstants;
044: import org.libresource.membership.interfaces.MembershipService;
045:
046: import org.libresource.web.Controller;
047: import org.libresource.web.Tab;
048: import org.libresource.web.config.Config;
049: import org.libresource.web.controllers.ServerConfigController;
050: import org.libresource.web.controllers.UsersController;
051:
052: import java.net.URI;
053: import java.util.ArrayList;
054: import java.util.Collection;
055:
056: import javax.jms.Topic;
057: import javax.jms.TopicConnectionFactory;
058:
059: import javax.naming.InitialContext;
060:
061: import javax.servlet.ServletConfig;
062: import javax.servlet.ServletException;
063: import javax.servlet.http.HttpServletRequest;
064: import javax.servlet.http.HttpServletResponse;
065:
066: public class Browse extends BaseServlet {
067: public final static String END_DOJO_PATH = "static/script/dojo-0.4.2-ajax/dojo.js";
068:
069: private static Topic eventsTopic;
070:
071: private static TopicConnectionFactory connectionFactory;
072:
073: private KernelService kernelService;
074:
075: private MembershipService membershipService;
076:
077: private Config config;
078:
079: public void init(ServletConfig config) throws ServletException {
080: super .init(config);
081: try {
082: kernelService = (KernelService) Libresource
083: .getService(KernelConstants.SERVICE);
084: membershipService = (MembershipService) Libresource
085: .getService(MembershipConstants.SERVICE);
086: this .config = Config
087: .getInstance(config.getServletContext());
088: } catch (Exception e) {
089: throw new ServletException(e);
090: }
091: }
092:
093: protected void process(HttpServletRequest request,
094: HttpServletResponse response) throws Exception {
095: String requestedPath = request.getRequestURI();
096: URI object = new URI(requestedPath.substring(
097: request.getContextPath().length())
098: .replaceAll("//", "/"));
099: request.setAttribute("requestedURI", object);
100:
101: try {
102: // from 1.5 to 2.0 migration
103: if (kernelService.getShortName(object) == null
104: || request.getParameter("refreshName") != null) {
105: if (kernelService.isSuperUser()) {
106: if (request.getParameter("depth") == null) {
107: updateShortNameRec(object, 9);
108: } else {
109: updateShortNameRec(object,
110: Integer.parseInt(request
111: .getParameter("depth")));
112: }
113: }
114: }
115:
116: // vieux hack
117: if ((object.getPath().equals("/") || object.getPath()
118: .equals(""))
119: && (request.getParameter("action") == null)) {
120: object = new URI("/" + workingNodePath);
121: }
122:
123: if (!kernelService
124: .exist(kernelService.getServerConfigURI())) {
125: kernelService.systemInitConfig();
126: }
127:
128: // Set resource as attribute
129: LibresourceResourceValue resource = kernelService
130: .getResource(object);
131: request.setAttribute("resource", resource);
132:
133: // Set additional tab info
134: Collection<Tab> additionalTab = config.getAdditionalTab();
135: boolean extratTabSelected = false;
136: String currentURI = resource.getUri().getPath();
137: for (Tab tab : additionalTab) {
138: extratTabSelected = extratTabSelected
139: || currentURI.startsWith(tab.getPath());
140: }
141: request.setAttribute("additionalTab", additionalTab);
142: request.setAttribute("noAdditionalTabSelected", Boolean
143: .valueOf(!extratTabSelected));
144:
145: // dojo path
146: String dojoPath = Browse.getRelativePathToRoot(object)
147: + Browse.END_DOJO_PATH;
148: request.setAttribute("dojoPath", dojoPath);
149:
150: // for wiki cycle detection
151: if (request.getParameter("wikichain") != null) {
152: config.setWikiChain(request.getParameter("wikichain"));
153: }
154: String action = "view";
155: if (request.getParameter("action") != null) {
156:
157: action = request.getParameter("action");
158: }
159:
160: // event
161: // LibresourceEvent event = new LibresourceEvent(object,
162: // "libresourceWeb.display");
163: // try {
164: // TopicConnectionFactory factory = getConnectionFactory();
165: // Topic topic = getEventsTopic();
166: // TopicConnection topicConnection =
167: // factory.createTopicConnection();
168: // TopicSession topicSession =
169: // topicConnection.createTopicSession(false,
170: // javax.jms.Session.AUTO_ACKNOWLEDGE);
171: // TopicPublisher publisher = topicSession.createPublisher(topic);
172: // Message message = topicSession.createMessage();
173: // event.toMessage(message);
174: // publisher.publish(message);
175: // publisher.close();
176: // topicSession.close();
177: // topicConnection.close();
178: // } catch (Exception e) {
179: // }
180: // if uri ends with "/", remove the "/"
181: if (object.getPath().endsWith("/")) {
182: object = new URI(object.getPath().substring(0,
183: object.getPath().length() - 1));
184: }
185:
186: Controller controller = null;
187:
188: if ((object.getPath().compareTo(
189: membershipService.getUsersRootURI().getPath()) == 0)
190: && (action.compareTo("view") == 0)) {
191: controller = new UsersController();
192: } else if ((object.getPath().compareTo(
193: kernelService.getServerConfigURI().getPath()) == 0)
194: && ((action.compareTo("view") == 0) || (action
195: .compareTo("edit") == 0))) {
196: controller = new ServerConfigController();
197: } else {
198: controller = config.getController(resource, action);
199: }
200:
201: Object forward = controller.process(object, request,
202: response);
203:
204: if (forward instanceof String) {
205: request.getRequestDispatcher(forward.toString())
206: .forward(request, response);
207: } else if (forward instanceof URI) {
208: // System.out.println("redirect URI: " + forward);
209: if ((((URI) forward).getScheme() != null)
210: && (((URI) forward).getScheme().equals("http") || ((URI) forward)
211: .getScheme().equals("ftp"))) {
212: response.sendRedirect(forward.toString());
213: } else {
214: // Allow query in uri
215: String pathRedirect = ((URI) forward).getPath();
216: if (((URI) forward).getQuery() != null) {
217: pathRedirect += "?"
218: + ((URI) forward).getQuery();
219: }
220: response.sendRedirect(request.getContextPath()
221: + pathRedirect);
222: }
223: }
224:
225: } catch (URINotExistException e) {
226: if (object.getPath().equals("/" + workingNodePath)) {
227: response.sendRedirect(request.getContextPath()
228: + "/Bootstrap");
229: } else {
230: throw e;
231: }
232: }
233: }
234:
235: public static Topic getEventsTopic() throws Exception {
236: if (eventsTopic == null) {
237: eventsTopic = (Topic) new InitialContext()
238: .lookup(org.libresource.Libresource
239: .getLibresourceConfiguration("event.topic.lookup"));
240: }
241: return eventsTopic;
242: }
243:
244: public static TopicConnectionFactory getConnectionFactory()
245: throws Exception {
246: if (connectionFactory == null) {
247: connectionFactory = (TopicConnectionFactory) new InitialContext()
248: .lookup(org.libresource.Libresource
249: .getLibresourceConfiguration("remote.topic.connection.factory"));
250: }
251:
252: return connectionFactory;
253: }
254:
255: private void updateShortNameRec(URI object, int depth) {
256: try {
257: LibresourceResourceIdentifier resourceIdentifier = kernelService
258: .lookup(object);
259: String shortName;
260:
261: if (resourceIdentifier != null) {
262: shortName = Libresource.getService(
263: resourceIdentifier.getService())
264: .getShortResourceName(resourceIdentifier);
265: } else {
266: shortName = "(Empty Node)";
267: }
268:
269: kernelService.setShortName(object, shortName);
270: } catch (Exception e) {
271: //
272: }
273:
274: if (depth > 0) {
275: try {
276: URI[] children = kernelService.listChildren(object);
277:
278: for (int i = 0; i < children.length; i++) {
279: updateShortNameRec(children[i], depth - 1);
280: }
281: } catch (Exception e) {
282: //
283: }
284: }
285: }
286:
287: public static String getRelativePathToRoot(URI currentURI) {
288: StringBuffer result = new StringBuffer();
289: int nbNode = currentURI.getPath().split("/").length;
290:
291: while (--nbNode > 0) {
292: result.append("../");
293: }
294:
295: return result.toString();
296: }
297: }
|