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:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.interfaces.KernelService;
039:
040: import org.libresource.web.Controller;
041: import org.libresource.web.config.Config;
042:
043: import java.net.URI;
044:
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047:
048: public class CreateResource extends BaseServlet {
049: protected void process(HttpServletRequest request,
050: HttpServletResponse response) throws Exception {
051: // cancel
052: if (request.getParameter("cancel") != null) {
053: if ((request.getParameter("originalUri") != null)
054: && (request.getParameter("originalUri").trim()
055: .length() != 0)) {
056: response.sendRedirect((new URI(request
057: .getParameter("originalUri")).getPath())
058: .substring(1));
059: } else {
060: response.sendRedirect("/projects");
061: }
062:
063: return;
064: }
065:
066: // originalUri
067: String uri = request.getParameter("uri");
068: String originalUri = request.getParameter("originalUri");
069:
070: if (uri != null) {
071: request.setAttribute("originalUri", uri);
072: request.setAttribute("uri", uri);
073: } else {
074: if (originalUri != null) {
075: request.setAttribute("originalUri", originalUri);
076: request.setAttribute("uri", originalUri);
077: } else {
078: if (originalUri != null) {
079: request.setAttribute("originalUri", "/projects");
080: request.setAttribute("uri", "/projects");
081: }
082: }
083: }
084:
085: // get param
086: String uriToCreate = request.getParameter("newUri");
087: String type = request.getParameter("type");
088:
089: // go to page
090: if (uriToCreate == null) {
091: request.setAttribute("types", Config.getInstance(
092: getServletContext()).listResourceTypeForCreation());
093:
094: request.getRequestDispatcher("/pages/createResource.jsp")
095: .forward(request, response);
096:
097: return;
098: }
099:
100: if (type == null) {
101: request.setAttribute("types", Config.getInstance(null)
102: .listResourceTypeForCreation());
103: request.setAttribute("uri", uriToCreate);
104:
105: request.setAttribute("creationError",
106: "Please choose a resource type !");
107: request.getRequestDispatcher("/pages/createResource.jsp")
108: .forward(request, response);
109:
110: return;
111: }
112:
113: if (uriToCreate.trim().length() == 0) {
114: request.setAttribute("types", Config.getInstance(null)
115: .listResourceTypeForCreation());
116: request.setAttribute("choosenType", type);
117:
118: request.setAttribute("creationError",
119: "Please choose a name for the new node !");
120: request.getRequestDispatcher("/pages/createResource.jsp")
121: .forward(request, response);
122:
123: return;
124: }
125:
126: if (!new URI(uriToCreate).getPath().matches("[A-Za-z0-9-_/]+")) {
127: request.setAttribute("types", Config.getInstance(null)
128: .listResourceTypeForCreation());
129: request.setAttribute("choosenType", type);
130: request.setAttribute("uri", uriToCreate);
131:
132: request.setAttribute("creationError",
133: "The URI contains forbidden character(s) !");
134: request.getRequestDispatcher("/pages/createResource.jsp")
135: .forward(request, response);
136:
137: return;
138: }
139:
140: Controller controller = Config.getInstance(getServletContext())
141: .getCreateController(request.getParameter("type"));
142: KernelService kernelService = (KernelService) Libresource
143: .getService(KernelConstants.SERVICE);
144: URI newUri = new URI(uriToCreate);
145: kernelService.createURI(newUri);
146:
147: Object forward = controller.process(newUri, request, response);
148:
149: if (forward instanceof String) {
150: request.getRequestDispatcher(
151: forward.toString() + "?action=edit").forward(
152: request, response);
153: } else if (forward instanceof URI) {
154: response.sendRedirect(request.getContextPath()
155: + ((URI) forward).getPath() + "?action=edit");
156: }
157:
158: return;
159: }
160: }
|