diff --git a/config/alfresco/messages/webscripts-test-help.txt b/config/alfresco/messages/webscripts-test-help.txt index 1ca522abfa..bdee6ca945 100644 --- a/config/alfresco/messages/webscripts-test-help.txt +++ b/config/alfresco/messages/webscripts-test-help.txt @@ -13,7 +13,9 @@ ok> r ok> user [] Switch to specified . If is omitted, the currently - selected user is shown. + selected user is shown. + + A ticket may be specified instead of username. ok> quit | exit @@ -31,6 +33,27 @@ ok> get URL relative to /alfresco/service e.g. get /blog/category?c=Web20 + +ok> put + + Issue a HTTP PUT request to the Web Script located at . The response + is dumped to the console. + + URL relative to /alfresco/service + +ok> post + + Issue a HTTP POST request to the Web Script located at . The response + is dumped to the console. + + URL relative to /alfresco/service + +ok> delete + + Issue a HTTP DELETE request to the Web Script located at . The response + is dumped to the console. + + URL relative to /alfresco/service ## ## end diff --git a/config/alfresco/templates/webscripts/org/alfresco/index.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/index.get.desc.xml index 1e2cd4cca3..70638bdc72 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/index.get.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/index.get.desc.xml @@ -1,6 +1,5 @@ - Web Script Index - Retrieve an index of all Web Scripts + Web Scripts Documentation + Web Scripts Documentation - \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/index.get.html.ftl b/config/alfresco/templates/webscripts/org/alfresco/index.get.html.ftl index e503d6ab17..457b7a2234 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/index.get.html.ftl +++ b/config/alfresco/templates/webscripts/org/alfresco/index.get.html.ftl @@ -2,16 +2,16 @@ - Alfresco Web Scripts + Web Scripts Documentation -
+ - +
AlfrescoAlfresco Web ScriptsWeb Scripts Documentation
Alfresco ${server.edition} v${server.version}
${webscripts?size} Web Scripts - Online documentation. @@ -19,24 +19,34 @@

- <#list webscripts as webscript> - <#assign desc = webscript.description> - ${desc.shortName} - <#list desc.URIs as uri> -
${desc.method} ${url.serviceContext}${uri.URI} => ${uri.format}<#if uri.format = desc.defaultFormat> (default) - -
-
- - <#if desc.description??>
Description:${desc.description}<#else> -
Authentication:${desc.requiredAuthentication} -
Transaction:${desc.requiredTransaction} -
-
Id:${desc.id} -
Description:${desc.storePath}/${desc.descPath} +
Browse all Web Scripts

- + <#macro recurseuri uri> + <#if uri.scripts?size > 0> +
${uri.path} + + <#list uri.children as childpath> + <@recurseuri uri=childpath/> + + + Web Scripts by URL + + <@recurseuri uri=rooturl/> +
+
+ <#macro recursepackage package> + <#if package.scripts?size > 0> +
${package.path} + + <#list package.children as childpath> + <@recursepackage package=childpath/> + + + Web Scripts by Package + + <@recursepackage package=rootpackage/> +
\ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexall.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.desc.xml new file mode 100644 index 0000000000..237c2f6274 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.desc.xml @@ -0,0 +1,6 @@ + + All Web Scripts Index + Retrieve an index of all Web Scripts + + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexall.get.html.ftl b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.html.ftl new file mode 100644 index 0000000000..1409cd417b --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.html.ftl @@ -0,0 +1,53 @@ + + + + + Index of All Web Scripts + + + + + + + + +
AlfrescoIndex of All Web Scripts
Alfresco ${server.edition} v${server.version} +
${webscripts?size} Web Scripts +
+
+ +
Back to Web Scripts Home +
+
+ <#macro recursepackage package> + <#if package.scripts?size > 0> + Package: ${package.path}

+ <#list package.scripts as webscript> + <#assign desc = webscript.description> + ${desc.shortName} + + <#list desc.URIs as uri> +
${desc.method} ${url.serviceContext}${uri.URI} => ${uri.format}<#if uri.format = desc.defaultFormat> (default) + +
+
+ + <#if desc.description??>
Description:${desc.description}<#else> +
Authentication:${desc.requiredAuthentication} +
Transaction:${desc.requiredTransaction} +
Format Style:${desc.formatStyle} +
+
Id:${desc.id} +
Description:${desc.storePath}/${desc.descPath} +
+
+ + + <#list package.children as childpath> + <@recursepackage package=childpath/> + + + + <@recursepackage package=rootpackage/> + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/index.get.mediawiki.ftl b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.mediawiki.ftl similarity index 63% rename from config/alfresco/templates/webscripts/org/alfresco/index.get.mediawiki.ftl rename to config/alfresco/templates/webscripts/org/alfresco/indexall.get.mediawiki.ftl index b1808d5ce0..89924a9b7b 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/index.get.mediawiki.ftl +++ b/config/alfresco/templates/webscripts/org/alfresco/indexall.get.mediawiki.ftl @@ -12,13 +12,18 @@ This page provides reference documentation for the Alfresco [[HTTP API]]. Generated on ${date?datetime} from - GET http://:/alfresco/service/index?format=wiki + GET http://:/alfresco/service/indexall.mediawiki = API Reference = -<#list webscripts as webscript> + +The following reference provides a list of all available Web Scripts organised by Web Script package. +<#macro recursepackage package> +<#if package.scripts?size > 0> +== Package: ${package.path} == +<#list package.scripts as webscript> <#assign desc = webscript.description> -== ${desc.shortName} == +=== ${desc.shortName} === <#if desc.description??><#if desc.description?ends_with(".")>${desc.description}<#else>${desc.description}.<#else> @@ -29,9 +34,16 @@ Generated on ${date?datetime} from Requirements: * Authentication: ${desc.requiredAuthentication} * Transaction: ${desc.requiredTransaction} +* Format Style: ${desc.formatStyle} Definition: * Id: ${desc.id} -* Store: ${desc.storePath}/${desc.descPath} - - \ No newline at end of file +* Description: ${desc.storePath}/${desc.descPath} + + +<#list package.children as childpath> + <@recursepackage package=childpath/> + + + +<@recursepackage package=rootpackage/> diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.desc.xml new file mode 100644 index 0000000000..acf14e1a17 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.desc.xml @@ -0,0 +1,5 @@ + + Web Script Package Index + Provide an index of Web Scripts for the specified Web Script package + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.html.ftl b/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.html.ftl new file mode 100644 index 0000000000..ec09465dc5 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexpackage.get.html.ftl @@ -0,0 +1,60 @@ + + + + + Index of Web Scripts Package '${package.path}' + + + + + + + + +
AlfrescoIndex of Web Scripts Package '${package.path}'
Alfresco ${server.edition} v${server.version} +
${package.scripts?size} Web Scripts +
+
+ +
Back to Web Scripts Home + <#if package.parent?exists> +
Up to ${package.parent.path} + +
+
+ <#if package.children?size > 0> + + <@recurseuri package=package/> +
+
+ + <#macro recurseuri package> + <#list package.children as childpath> + <#if childpath.scripts?size > 0> +
${childpath.path} + + <@recurseuri package=childpath/> + + + <#list package.scripts as webscript> + <#assign desc = webscript.description> + ${desc.shortName} + + <#list desc.URIs as uri> +
${desc.method} ${url.serviceContext}${uri.URI} => ${uri.format}<#if uri.format = desc.defaultFormat> (default) + +
+
+ + <#if desc.description??>
Description:${desc.description}<#else> +
Authentication:${desc.requiredAuthentication} +
Transaction:${desc.requiredTransaction} +
Format Style:${desc.formatStyle} +
+
Id:${desc.id} +
Description:${desc.storePath}/${desc.descPath} +
+
+ + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.desc.xml new file mode 100644 index 0000000000..bb252805f9 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.desc.xml @@ -0,0 +1,6 @@ + + Web Script URI Index + Provide an index of Web Scripts for the specified Web Script URI + + argument + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.html.ftl b/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.html.ftl new file mode 100644 index 0000000000..3d868eb476 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/indexuri.get.html.ftl @@ -0,0 +1,60 @@ + + + + + Index of Web Scripts URI '${uri.path}' + + + + + + + + +
AlfrescoIndex of Web Scripts URI '${uri.path}'
Alfresco ${server.edition} v${server.version} +
${uri.scripts?size} Web Scripts +
+
+ +
Back to Web Scripts Home + <#if uri.parent?exists> +
Up to ${uri.parent.path} + +
+
+ <#if uri.children?size > 0> + + <@recurseuri uri=uri/> +
+
+ + <#macro recurseuri uri> + <#list uri.children as childpath> + <#if childpath.scripts?size > 0> +
${childpath.path} + + <@recurseuri uri=childpath/> + + + <#list uri.scripts as webscript> + <#assign desc = webscript.description> + ${desc.shortName} + + <#list desc.URIs as uri> +
${desc.method} ${url.serviceContext}${uri.URI} => ${uri.format}<#if uri.format = desc.defaultFormat> (default) + +
+
+ + <#if desc.description??>
Description:${desc.description}<#else> +
Authentication:${desc.requiredAuthentication} +
Transaction:${desc.requiredTransaction} +
Format Style:${desc.formatStyle} +
+
Id:${desc.id} +
Description:${desc.storePath}/${desc.descPath} +
+
+ + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.desc.xml new file mode 100644 index 0000000000..accec45b7a --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.desc.xml @@ -0,0 +1,7 @@ + + Login + Establish a ticket + + none + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.xml.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.xml.ftl new file mode 100644 index 0000000000..d311e170fd --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/login.get.xml.ftl @@ -0,0 +1,2 @@ + +${ticket} diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.desc.xml new file mode 100644 index 0000000000..5b3f922443 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.desc.xml @@ -0,0 +1,7 @@ + + Delete Login Ticket + Delete Login Ticket + + user + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.xml.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.xml.ftl new file mode 100644 index 0000000000..d311e170fd --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.delete.xml.ftl @@ -0,0 +1,2 @@ + +${ticket} diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.desc.xml new file mode 100644 index 0000000000..84384ebd12 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.desc.xml @@ -0,0 +1,7 @@ + + Login Ticket + Login Ticket + + user + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.xml.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.xml.ftl new file mode 100644 index 0000000000..d311e170fd --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/loginticket.get.xml.ftl @@ -0,0 +1,2 @@ + +${ticket} diff --git a/config/alfresco/web-scripts-application-context-test.xml b/config/alfresco/web-scripts-application-context-test.xml index fa83bca578..fe7be45526 100644 --- a/config/alfresco/web-scripts-application-context-test.xml +++ b/config/alfresco/web-scripts-application-context-test.xml @@ -9,6 +9,7 @@ + diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 0372569cac..c8d896cf4c 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -157,7 +157,7 @@ - + @@ -165,11 +165,26 @@ - + + + + + + + + + + + + + + + + diff --git a/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java b/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java index 25fd1434bf..6a6c0ecafe 100644 --- a/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java +++ b/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java @@ -83,7 +83,13 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean // NOTE: The map is sorted by url (descending order) private Map webscriptsByURL = new TreeMap(Collections.reverseOrder()); + // map of web script packages by path + private Map packageByPath = new TreeMap(); + // map of web script uris by path + private Map uriByPath = new TreeMap(); + + // // Initialisation // @@ -180,6 +186,10 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean // clear currently registered services webscriptsById.clear(); webscriptsByURL.clear(); + packageByPath.clear(); + packageByPath.put("/", new Path("/")); + uriByPath.clear(); + uriByPath.put("/", new Path("/")); // register services for (WebScriptStore apiStore : storage.getStores()) @@ -287,6 +297,10 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean logger.debug("Registered Web Script URL '" + uriIdx + "'"); } } + + // build path indexes to web script + registerPackage(serviceImpl); + registerURIs(serviceImpl); } } @@ -294,6 +308,63 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean logger.debug("Registered " + webscriptsById.size() + " Web Scripts; " + webscriptsByURL.size() + " URLs"); } + /** + * Register a Web Script Package + * + * @param script + */ + private void registerPackage(WebScript script) + { + WebScriptDescription desc = script.getDescription(); + Path path = packageByPath.get("/"); + String[] parts = desc.getScriptPath().split("/"); + for (String part : parts) + { + Path subpath = packageByPath.get(Path.concatPath(path.getPath(), part)); + if (subpath == null) + { + subpath = path.createChildPath(part); + packageByPath.put(subpath.getPath(), subpath); + if (logger.isDebugEnabled()) + logger.debug("Registered Web Script package " + subpath.getPath()); + } + path = subpath; + } + path.addScript(script); + } + + /** + * Register a Web Script URI + * + * @param script + */ + private void registerURIs(WebScript script) + { + WebScriptDescription desc = script.getDescription(); + for (URI uri : desc.getURIs()) + { + Path path = uriByPath.get("/"); + String[] parts = uri.getURI().split("/"); + for (String part : parts) + { + if (part.indexOf("?") != -1) + { + part = part.substring(0, part.indexOf("?")); + } + Path subpath = uriByPath.get(Path.concatPath(path.getPath(), part)); + if (subpath == null) + { + subpath = path.createChildPath(part); + uriByPath.put(subpath.getPath(), subpath); + if (logger.isDebugEnabled()) + logger.debug("Registered Web Script URI " + subpath.getPath()); + } + path = subpath; + } + path.addScript(script); + } + } + /** * Create an Web Script Description * @@ -461,6 +532,22 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean } } + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptRegistry#getPackage(java.lang.String) + */ + public WebScriptPath getPackage(String scriptPackage) + { + return packageByPath.get(scriptPackage); + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptRegistry#getUri(java.lang.String) + */ + public WebScriptPath getUri(String scriptUri) + { + return uriByPath.get(scriptUri); + } + /* (non-Javadoc) * @see org.alfresco.web.scripts.WebScriptRegistry#getWebScripts() */ @@ -576,5 +663,5 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean return service; } } - + } diff --git a/source/java/org/alfresco/web/scripts/Path.java b/source/java/org/alfresco/web/scripts/Path.java new file mode 100644 index 0000000000..50f9d56628 --- /dev/null +++ b/source/java/org/alfresco/web/scripts/Path.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts; + +import java.util.Map; +import java.util.TreeMap; + + +/** + * Basic implementation of a Web Script Path + * + * Used for package & url trees. + * + * @author davidc + */ +public class Path implements WebScriptPath +{ + private String path; + private Path parent = null; + private Map children = new TreeMap(); + private Map scripts = new TreeMap(); + + + /** + * Helper to concatenate paths + * + * @param path1 + * @param path2 + * @return concatenated path + */ + public static String concatPath(String path1, String path2) + { + return path1.equals("/") ? path1 + path2 : path1 + "/" + path2; + } + + + /** + * Construct + * + * @param path + */ + public Path(String path) + { + this.path = path; + } + + /** + * Create a Child Path + * + * @param path child path name + * @return child path + */ + public Path createChildPath(String path) + { + Path child = new Path(concatPath(this.path, path)); + child.parent = this; + children.put(child.path, child); + return child; + } + + /** + * Associate Web Script with Path + * + * @param script + */ + public void addScript(WebScript script) + { + scripts.put(script.getDescription().getId(), script); + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptPath#getChildren() + */ + public WebScriptPath[] getChildren() + { + WebScriptPath[] childrenArray = new WebScriptPath[children.size()]; + return children.values().toArray(childrenArray); + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptPath#getScripts() + */ + public WebScript[] getScripts() + { + WebScript[] scriptsArray = new WebScript[scripts.size()]; + return scripts.values().toArray(scriptsArray); + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptPath#getName() + */ + public String getName() + { + String name = ""; + int i = path.lastIndexOf("/"); + if (i != -1 && i != (path.length() -1)) + { + name = path.substring(i + 1); + } + return name; + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptPath#getParent() + */ + public WebScriptPath getParent() + { + return parent; + } + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptPath#getPath() + */ + public String getPath() + { + return path; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() + { + return path; + } + +} diff --git a/source/java/org/alfresco/web/scripts/TestWebScriptServer.java b/source/java/org/alfresco/web/scripts/TestWebScriptServer.java index d7dbe98605..77a5da20c5 100644 --- a/source/java/org/alfresco/web/scripts/TestWebScriptServer.java +++ b/source/java/org/alfresco/web/scripts/TestWebScriptServer.java @@ -35,8 +35,11 @@ import java.io.StringWriter; import org.alfresco.config.Config; import org.alfresco.config.ConfigService; +import org.alfresco.repo.security.authentication.AuthenticationException; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; +import org.alfresco.repo.transaction.TransactionUtil; +import org.alfresco.service.cmr.security.AuthenticationService; import org.alfresco.service.transaction.TransactionService; import org.alfresco.web.config.ServerConfigElement; import org.springframework.context.ApplicationContext; @@ -55,6 +58,7 @@ import org.springframework.mock.web.MockHttpServletResponse; public class TestWebScriptServer { // dependencies + protected AuthenticationService authenticationService; protected TransactionService transactionService; protected DeclarativeWebScriptRegistry registry; protected ConfigService configService; @@ -105,6 +109,13 @@ public class TestWebScriptServer this.configService = configService; } + /** + * @param authenticationService + */ + public void setAuthenticationService(AuthenticationService authenticationService) + { + this.authenticationService = authenticationService; + } /** * Sets the Messages resource bundle @@ -184,7 +195,7 @@ public class TestWebScriptServer public MockHttpServletResponse submitRequest(String method, String uri) throws IOException { - MockHttpServletRequest req = createRequest("get", uri); + MockHttpServletRequest req = createRequest(method, uri); MockHttpServletResponse res = new MockHttpServletResponse(); WebScriptRuntime runtime = new WebScriptServletRuntime(registry, transactionService, null, req, res, serverConfig); @@ -211,7 +222,7 @@ public class TestWebScriptServer { return; } - + // execute command in context of currently selected user long startms = System.currentTimeMillis(); System.out.print(interpretCommand(line)); @@ -234,6 +245,33 @@ public class TestWebScriptServer private String interpretCommand(final String line) throws IOException { + try + { + if (username.startsWith("TICKET_")) + { + try + { + TransactionUtil.executeInUserTransaction(transactionService, new TransactionUtil.TransactionWork() + { + public Object doWork() throws Throwable + { + authenticationService.validate(username); + return null; + } + }); + return executeCommand(line); + } + finally + { + authenticationService.clearCurrentSecurityContext(); + } + } + } + catch(AuthenticationException e) + { + executeCommand("user admin"); + } + // execute command in context of currently selected user return AuthenticationUtil.runAs(new RunAsWork() { @@ -310,7 +348,10 @@ public class TestWebScriptServer out.println("using user " + username); } - else if (command[0].equals("get")) + else if (command[0].equals("get") || + command[0].equals("put") || + command[0].equals("post") || + command[0].equals("delete")) { if (command.length < 2) { @@ -318,11 +359,11 @@ public class TestWebScriptServer } String uri = command[1]; - MockHttpServletResponse res = submitRequest("get", uri); + MockHttpServletResponse res = submitRequest(command[0], uri); bout.write(res.getContentAsByteArray()); out.println(); } - + else if (command[0].equals("reset")) { registry.reset(); @@ -349,7 +390,7 @@ public class TestWebScriptServer */ private MockHttpServletRequest createRequest(String method, String uri) { - MockHttpServletRequest req = new MockHttpServletRequest("get", uri); + MockHttpServletRequest req = new MockHttpServletRequest(method, uri); // set parameters int iArgIndex = uri.indexOf('?'); diff --git a/source/java/org/alfresco/web/scripts/WebScriptPath.java b/source/java/org/alfresco/web/scripts/WebScriptPath.java new file mode 100644 index 0000000000..821497ea1f --- /dev/null +++ b/source/java/org/alfresco/web/scripts/WebScriptPath.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts; + + +/** + * Web Script Path + * + * @author davidc + */ +public interface WebScriptPath +{ + /** + * Gets the full path + * + * @return path + */ + public String getPath(); + + /** + * Gets the name of the path (last path segment) + * + * @return name + */ + public String getName(); + + /** + * Gets the parent path + * + * @return path + */ + public WebScriptPath getParent(); + + /** + * Gets the child paths + * + * @return child paths + */ + public WebScriptPath[] getChildren(); + + /** + * Gets Web Scripts associated with this path + * + * @return web scripts + */ + public WebScript[] getScripts(); + +} diff --git a/source/java/org/alfresco/web/scripts/WebScriptRegistry.java b/source/java/org/alfresco/web/scripts/WebScriptRegistry.java index d856c67d89..310653cff0 100644 --- a/source/java/org/alfresco/web/scripts/WebScriptRegistry.java +++ b/source/java/org/alfresco/web/scripts/WebScriptRegistry.java @@ -38,6 +38,22 @@ import org.alfresco.service.cmr.repository.TemplateImageResolver; */ public interface WebScriptRegistry { + /** + * Gets a Web Script Package + * + * @param packagePath + * @return web script path representing package + */ + public WebScriptPath getPackage(String packagePath); + + /** + * Gets a Web Script URL + * + * @param uriPath + * @return web script path representing uri + */ + public WebScriptPath getUri(String uriPath); + /** * Gets all Web Scripts * diff --git a/source/java/org/alfresco/web/scripts/bean/Index.java b/source/java/org/alfresco/web/scripts/bean/Index.java index c631d8acc2..1b163aa6d2 100644 --- a/source/java/org/alfresco/web/scripts/bean/Index.java +++ b/source/java/org/alfresco/web/scripts/bean/Index.java @@ -33,7 +33,7 @@ import org.alfresco.web.scripts.WebScriptResponse; /** - * Retrieves the list of available Web Scripts + * Index of all Web Scripts * * @author davidc */ @@ -48,6 +48,8 @@ public class Index extends DeclarativeWebScript { Map model = new HashMap(7, 1.0f); model.put("webscripts", getWebScriptRegistry().getWebScripts()); + model.put("rooturl", getWebScriptRegistry().getUri("/")); + model.put("rootpackage", getWebScriptRegistry().getPackage("/")); return model; } diff --git a/source/java/org/alfresco/web/scripts/bean/IndexPackage.java b/source/java/org/alfresco/web/scripts/bean/IndexPackage.java new file mode 100644 index 0000000000..1f39c380a5 --- /dev/null +++ b/source/java/org/alfresco/web/scripts/bean/IndexPackage.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts.bean; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.web.scripts.DeclarativeWebScript; +import org.alfresco.web.scripts.WebScriptException; +import org.alfresco.web.scripts.WebScriptPath; +import org.alfresco.web.scripts.WebScriptRequest; +import org.alfresco.web.scripts.WebScriptResponse; + + +/** + * Index of a Web Script Package + * + * @author davidc + */ +public class IndexPackage extends DeclarativeWebScript +{ + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) + */ + @Override + protected Map executeImpl(WebScriptRequest req, WebScriptResponse res) + { + // extract web script package + String packagePath = req.getExtensionPath(); + if (packagePath == null || packagePath.length() == 0) + { + packagePath = "/"; + } + if (!packagePath.startsWith("/")) + { + packagePath = "/" + packagePath; + } + + // locate web script package + WebScriptPath path = getWebScriptRegistry().getPackage(packagePath); + if (path == null) + { + throw new WebScriptException("Web Script Package '" + packagePath + "' not found"); + } + + Map model = new HashMap(7, 1.0f); + model.put("package", path); + return model; + } + +} diff --git a/source/java/org/alfresco/web/scripts/bean/IndexURI.java b/source/java/org/alfresco/web/scripts/bean/IndexURI.java new file mode 100644 index 0000000000..f5e360d848 --- /dev/null +++ b/source/java/org/alfresco/web/scripts/bean/IndexURI.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts.bean; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.web.scripts.DeclarativeWebScript; +import org.alfresco.web.scripts.WebScriptException; +import org.alfresco.web.scripts.WebScriptPath; +import org.alfresco.web.scripts.WebScriptRequest; +import org.alfresco.web.scripts.WebScriptResponse; + + +/** + * Index of a Web Script URI + * + * @author davidc + */ +public class IndexURI extends DeclarativeWebScript +{ + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) + */ + @Override + protected Map executeImpl(WebScriptRequest req, WebScriptResponse res) + { + // extract web script package + String uriPath = req.getExtensionPath(); + if (uriPath == null || uriPath.length() == 0) + { + uriPath = "/"; + } + if (!uriPath.startsWith("/")) + { + uriPath = "/" + uriPath; + } + + // locate web script package + WebScriptPath path = getWebScriptRegistry().getUri(uriPath); + if (path == null) + { + throw new WebScriptException("Web Script URI '" + uriPath + "' not found"); + } + + Map model = new HashMap(7, 1.0f); + model.put("uri", path); + return model; + } + +} diff --git a/source/java/org/alfresco/web/scripts/bean/Login.java b/source/java/org/alfresco/web/scripts/bean/Login.java new file mode 100644 index 0000000000..ddcb9d54ce --- /dev/null +++ b/source/java/org/alfresco/web/scripts/bean/Login.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts.bean; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.service.cmr.security.AuthenticationService; +import org.alfresco.web.scripts.DeclarativeWebScript; +import org.alfresco.web.scripts.WebScriptException; +import org.alfresco.web.scripts.WebScriptRequest; +import org.alfresco.web.scripts.WebScriptResponse; + + +/** + * Login and establish a ticket + * + * @author davidc + */ +public class Login extends DeclarativeWebScript +{ + // dependencies + private AuthenticationService authenticationService; + + /** + * @param authenticationService + */ + public void setAuthenticationService(AuthenticationService authenticationService) + { + this.authenticationService = authenticationService; + } + + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) + */ + @Override + protected Map executeImpl(WebScriptRequest req, WebScriptResponse res) + { + // extract username and password + String username = req.getParameter("u"); + if (username == null || username.length() == 0) + { + throw new WebScriptException("Username has not been specified"); + } + String password = req.getParameter("pw"); + + // get ticket + authenticationService.authenticate(username, password == null ? null : password.toCharArray()); + + // add ticket to model for javascript and template access + try + { + Map model = new HashMap(7, 1.0f); + model.put("ticket", authenticationService.getCurrentTicket()); + return model; + } + finally + { + authenticationService.clearCurrentSecurityContext(); + } + } + +} diff --git a/source/java/org/alfresco/web/scripts/bean/LoginTicket.java b/source/java/org/alfresco/web/scripts/bean/LoginTicket.java new file mode 100644 index 0000000000..1515853d8b --- /dev/null +++ b/source/java/org/alfresco/web/scripts/bean/LoginTicket.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts.bean; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.repo.security.authentication.AuthenticationException; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.security.authentication.TicketComponent; +import org.alfresco.web.scripts.DeclarativeWebScript; +import org.alfresco.web.scripts.WebScriptException; +import org.alfresco.web.scripts.WebScriptRequest; +import org.alfresco.web.scripts.WebScriptResponse; + + +/** + * Login Ticket + * + * @author davidc + */ +public class LoginTicket extends DeclarativeWebScript +{ + // dependencies + private TicketComponent ticketComponent; + + /** + * @param ticketComponent + */ + public void setTicketComponent(TicketComponent ticketComponent) + { + this.ticketComponent = ticketComponent; + } + + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) + */ + @Override + protected Map executeImpl(WebScriptRequest req, WebScriptResponse res) + { + // retrieve ticket from request and current ticket + String ticket = req.getExtensionPath(); + if (ticket == null && ticket.length() == 0) + { + throw new WebScriptException("Ticket not specified"); + } + + try + { + String ticketUser = ticketComponent.validateTicket(ticket); + + // do not go any further if tickets are different + if (!AuthenticationUtil.getCurrentUserName().equals(ticketUser)) + { + // TODO: 404 error + throw new WebScriptException("Ticket not found"); + } + } + catch(AuthenticationException e) + { + throw new WebScriptException("Ticket not found"); + } + + Map model = new HashMap(7, 1.0f); + model.put("ticket", ticket); + return model; + } + +} diff --git a/source/java/org/alfresco/web/scripts/bean/LoginTicketDelete.java b/source/java/org/alfresco/web/scripts/bean/LoginTicketDelete.java new file mode 100644 index 0000000000..898e5f335e --- /dev/null +++ b/source/java/org/alfresco/web/scripts/bean/LoginTicketDelete.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts.bean; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.repo.security.authentication.AuthenticationException; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.security.authentication.TicketComponent; +import org.alfresco.service.cmr.security.AuthenticationService; +import org.alfresco.web.scripts.DeclarativeWebScript; +import org.alfresco.web.scripts.WebScriptException; +import org.alfresco.web.scripts.WebScriptRequest; +import org.alfresco.web.scripts.WebScriptResponse; + + +/** + * Delete Login Ticket + * + * @author davidc + */ +public class LoginTicketDelete extends DeclarativeWebScript +{ + // dependencies + private AuthenticationService authenticationService; + private TicketComponent ticketComponent; + + /** + * @param ticketComponent + */ + public void setTicketComponent(TicketComponent ticketComponent) + { + this.ticketComponent = ticketComponent; + } + + /** + * @param authenticationService + */ + public void setAuthenticationService(AuthenticationService authenticationService) + { + this.authenticationService = authenticationService; + } + + + /* (non-Javadoc) + * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) + */ + @Override + protected Map executeImpl(WebScriptRequest req, WebScriptResponse res) + { + // retrieve ticket from request and current ticket + String ticket = req.getExtensionPath(); + if (ticket == null && ticket.length() == 0) + { + throw new WebScriptException("Ticket not specified"); + } + + try + { + String ticketUser = ticketComponent.validateTicket(ticket); + + // do not go any further if tickets are different + if (!AuthenticationUtil.getCurrentUserName().equals(ticketUser)) + { + // TODO: 404 error + throw new WebScriptException("Ticket not found"); + } + } + catch(AuthenticationException e) + { + throw new WebScriptException("Ticket not found"); + } + + // delete the ticket + authenticationService.invalidateTicket(ticket); + + Map model = new HashMap(7, 1.0f); + model.put("ticket", ticket); + return model; + } + +}