mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Reversed incorrect checkin
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7438 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
209
source/java/org/alfresco/web/scripts/bean/ContentGet.java
Normal file
209
source/java/org/alfresco/web/scripts/bean/ContentGet.java
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* 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.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.service.cmr.repository.ContentIOException;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.MimetypeService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.AccessStatus;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.web.scripts.AbstractWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptCache;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptResponse;
|
||||
import org.alfresco.web.scripts.WebScriptServletRequest;
|
||||
import org.alfresco.web.scripts.WebScriptServletResponse;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Content Retrieval Service
|
||||
*
|
||||
* Stream content from the Repository.
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class ContentGet extends AbstractWebScript
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(ContentGet.class);
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScript#execute(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
public void execute(WebScriptRequest req, WebScriptResponse res)
|
||||
throws IOException
|
||||
{
|
||||
// NOTE: This web script must be executed in a HTTP Servlet environment
|
||||
if (!(req instanceof WebScriptServletRequest))
|
||||
{
|
||||
throw new WebScriptException("Content retrieval must be executed in HTTP Servlet environment");
|
||||
}
|
||||
HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
|
||||
HttpServletResponse httpRes = ((WebScriptServletResponse)res).getHttpServletResponse();
|
||||
|
||||
// convert web script URL to node reference in Repository
|
||||
String match = req.getServiceMatch().getPath();
|
||||
String[] matchParts = match.split("/");
|
||||
String extensionPath = req.getExtensionPath();
|
||||
String[] extParts = extensionPath == null ? new String[1] : extensionPath.split("/");
|
||||
String[] path = new String[extParts.length -1];
|
||||
System.arraycopy(extParts, 1, path, 0, extParts.length -1);
|
||||
NodeRef nodeRef = findNodeRef(matchParts[2], path);
|
||||
if (nodeRef == null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find " + matchParts[2] + " reference " + Arrays.toString(path));
|
||||
}
|
||||
|
||||
// determine content property
|
||||
QName propertyQName = ContentModel.PROP_CONTENT;
|
||||
String contentPart = extParts[0];
|
||||
if (contentPart.length() > 0 && contentPart.charAt(0) == ';')
|
||||
{
|
||||
if (contentPart.length() < 2)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Content property malformed");
|
||||
}
|
||||
String propertyName = contentPart.substring(1);
|
||||
if (propertyName.length() > 0)
|
||||
{
|
||||
NamespaceService namespaceService = getServiceRegistry().getNamespaceService();
|
||||
propertyQName = QName.createQName(propertyName, namespaceService);
|
||||
}
|
||||
}
|
||||
|
||||
// determine attachment
|
||||
boolean attach = Boolean.valueOf(req.getParameter("a"));
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Retrieving content from node ref " + nodeRef.toString() + " (property: " + propertyQName.toString() + ") (attach: " + attach + ")");
|
||||
|
||||
// check that the user has at least READ_CONTENT access - else redirect to the login page
|
||||
PermissionService permissionService = getServiceRegistry().getPermissionService();
|
||||
if (permissionService.hasPermission(nodeRef, PermissionService.READ_CONTENT) == AccessStatus.DENIED)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Permission denied");
|
||||
}
|
||||
|
||||
// check If-Modified-Since header and set Last-Modified header as appropriate
|
||||
NodeService nodeService = getServiceRegistry().getNodeService();
|
||||
Date modified = (Date)nodeService.getProperty(nodeRef, ContentModel.PROP_MODIFIED);
|
||||
long modifiedSince = httpReq.getDateHeader("If-Modified-Since");
|
||||
if (modifiedSince > 0L)
|
||||
{
|
||||
// round the date to the ignore millisecond value which is not supplied by header
|
||||
long modDate = (modified.getTime() / 1000L) * 1000L;
|
||||
if (modDate <= modifiedSince)
|
||||
{
|
||||
httpRes.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// handle attachment
|
||||
if (attach == true)
|
||||
{
|
||||
// set header based on filename - will force a Save As from the browse if it doesn't recognize it
|
||||
// this is better than the default response of the browser trying to display the contents
|
||||
httpRes.setHeader("Content-Disposition", "attachment");
|
||||
}
|
||||
|
||||
// get the content reader
|
||||
ContentService contentService = getServiceRegistry().getContentService();
|
||||
ContentReader reader = contentService.getReader(nodeRef, propertyQName);
|
||||
if (reader == null || !reader.exists())
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to locate content for node ref " + nodeRef + " (property: " + propertyQName.toString() + ")");
|
||||
}
|
||||
|
||||
// establish mimetype
|
||||
String mimetype = reader.getMimetype();
|
||||
if (mimetype == null || mimetype.length() == 0)
|
||||
{
|
||||
MimetypeService mimetypeMap = getServiceRegistry().getMimetypeService();
|
||||
mimetype = MimetypeMap.MIMETYPE_BINARY;
|
||||
int extIndex = extensionPath.lastIndexOf('.');
|
||||
if (extIndex != -1)
|
||||
{
|
||||
String ext = extensionPath.substring(extIndex + 1);
|
||||
String mt = mimetypeMap.getMimetypesByExtension().get(ext);
|
||||
if (mt != null)
|
||||
{
|
||||
mimetype = mt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set mimetype for the content and the character encoding + length for the stream
|
||||
httpRes.setContentType(mimetype);
|
||||
httpRes.setCharacterEncoding(reader.getEncoding());
|
||||
httpRes.setHeader("Content-Length", Long.toString(reader.getSize()));
|
||||
|
||||
// set caching
|
||||
WebScriptCache cache = new WebScriptCache();
|
||||
cache.setNeverCache(false);
|
||||
cache.setMustRevalidate(true);
|
||||
cache.setLastModified(modified);
|
||||
res.setCache(cache);
|
||||
|
||||
// get the content and stream directly to the response output stream
|
||||
// assuming the repository is capable of streaming in chunks, this should allow large files
|
||||
// to be streamed directly to the browser response stream.
|
||||
try
|
||||
{
|
||||
reader.getContent(res.getOutputStream());
|
||||
}
|
||||
catch (SocketException e1)
|
||||
{
|
||||
// the client cut the connection - our mission was accomplished apart from a little error message
|
||||
if (logger.isInfoEnabled())
|
||||
logger.info("Client aborted stream read:\n\tnode: " + nodeRef + "\n\tcontent: " + reader);
|
||||
}
|
||||
catch (ContentIOException e2)
|
||||
{
|
||||
if (logger.isInfoEnabled())
|
||||
logger.info("Client aborted stream read:\n\tnode: " + nodeRef + "\n\tcontent: " + reader);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
56
source/java/org/alfresco/web/scripts/bean/Index.java
Normal file
56
source/java/org/alfresco/web/scripts/bean/Index.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* Index of all Web Scripts
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class Index extends DeclarativeWebScript
|
||||
{
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("webscripts", getWebScriptRegistry().getWebScripts());
|
||||
model.put("rooturl", getWebScriptRegistry().getUri("/"));
|
||||
model.put("rootpackage", getWebScriptRegistry().getPackage("/"));
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
74
source/java/org/alfresco/web/scripts/bean/IndexPackage.java
Normal file
74
source/java/org/alfresco/web/scripts/bean/IndexPackage.java
Normal file
@@ -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.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 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<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// 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<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("package", path);
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
74
source/java/org/alfresco/web/scripts/bean/IndexURI.java
Normal file
74
source/java/org/alfresco/web/scripts/bean/IndexURI.java
Normal file
@@ -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.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 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<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// 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<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("uri", path);
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
90
source/java/org/alfresco/web/scripts/bean/IndexUpdate.java
Normal file
90
source/java/org/alfresco/web/scripts/bean/IndexUpdate.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
import org.alfresco.web.scripts.facebook.FacebookService;
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the list of available Web Scripts
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class IndexUpdate extends DeclarativeWebScript
|
||||
{
|
||||
// component dependencies
|
||||
private FacebookService facebookService;
|
||||
|
||||
/**
|
||||
* @param facebookService facebook service
|
||||
*/
|
||||
public void setFacebookService(FacebookService facebookService)
|
||||
{
|
||||
this.facebookService = facebookService;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
List<String> tasks = new ArrayList<String>();
|
||||
|
||||
// reset index
|
||||
String reset = req.getParameter("reset");
|
||||
if (reset != null && reset.equals("on"))
|
||||
{
|
||||
// reset list of web scripts
|
||||
int previousCount = getWebScriptRegistry().getWebScripts().size();
|
||||
getWebScriptRegistry().reset();
|
||||
tasks.add("Reset Web Scripts Registry; found " + getWebScriptRegistry().getWebScripts().size() + " Web Scripts. Previously, there were " + previousCount + ".");
|
||||
|
||||
// reset facebook service
|
||||
// TODO: Determine more appropriate place to put this
|
||||
int appCount = facebookService.getAppModels().size();
|
||||
if (appCount > 0)
|
||||
{
|
||||
facebookService.reset();
|
||||
tasks.add("Reset " + appCount + " Facebook Applications.");
|
||||
}
|
||||
}
|
||||
|
||||
// create model for rendering
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("tasks", tasks);
|
||||
model.put("webscripts", getWebScriptRegistry().getWebScripts());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.jscript.AlfrescoRhinoScriptDebugger;
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* Javascript Debugger
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class JavascriptDebugger extends DeclarativeWebScript
|
||||
{
|
||||
// dependencies
|
||||
private AlfrescoRhinoScriptDebugger debugger;
|
||||
|
||||
/**
|
||||
* @param ticketComponent
|
||||
*/
|
||||
public void setDebugger(AlfrescoRhinoScriptDebugger debugger)
|
||||
{
|
||||
this.debugger = debugger;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// construct model
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("visible", debugger.isVisible());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.jscript.AlfrescoRhinoScriptDebugger;
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* Javascript Debugger
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class JavascriptDebuggerPost extends DeclarativeWebScript
|
||||
{
|
||||
// dependencies
|
||||
private AlfrescoRhinoScriptDebugger debugger;
|
||||
|
||||
/**
|
||||
* @param ticketComponent
|
||||
*/
|
||||
public void setDebugger(AlfrescoRhinoScriptDebugger debugger)
|
||||
{
|
||||
this.debugger = debugger;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
String visibleStr = req.getParameter("visible");
|
||||
boolean visible = Boolean.valueOf(visibleStr);
|
||||
|
||||
if (visible)
|
||||
{
|
||||
debugger.show();
|
||||
}
|
||||
else
|
||||
{
|
||||
debugger.hide();
|
||||
}
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("visible", debugger.isVisible());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
396
source/java/org/alfresco/web/scripts/bean/KeywordSearch.java
Normal file
396
source/java/org/alfresco/web/scripts/bean/KeywordSearch.java
Normal file
@@ -0,0 +1,396 @@
|
||||
/*
|
||||
* 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.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.repo.template.TemplateNode;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.alfresco.util.GUID;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.alfresco.util.URLEncoder;
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Alfresco Keyword (simple) Search Service
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class KeywordSearch extends DeclarativeWebScript
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(KeywordSearch.class);
|
||||
|
||||
// search parameters
|
||||
// TODO: allow configuration of search store
|
||||
protected static final StoreRef SEARCH_STORE = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||
protected static final int DEFAULT_ITEMS_PER_PAGE = 10;
|
||||
protected static final String QUERY_FORMAT = "query_";
|
||||
|
||||
// dependencies
|
||||
protected SearchService searchService;
|
||||
|
||||
/**
|
||||
* @param searchService
|
||||
*/
|
||||
public void setSearchService(SearchService searchService)
|
||||
{
|
||||
this.searchService = searchService;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
//
|
||||
// process arguments
|
||||
//
|
||||
|
||||
String searchTerms = req.getParameter("q");
|
||||
ParameterCheck.mandatoryString("q", searchTerms);
|
||||
String startPageArg = req.getParameter("p");
|
||||
int startPage = 1;
|
||||
try
|
||||
{
|
||||
startPage = new Integer(startPageArg);
|
||||
}
|
||||
catch(NumberFormatException e)
|
||||
{
|
||||
// NOTE: use default startPage
|
||||
}
|
||||
String itemsPerPageArg = req.getParameter("c");
|
||||
int itemsPerPage = DEFAULT_ITEMS_PER_PAGE;
|
||||
try
|
||||
{
|
||||
itemsPerPage = new Integer(itemsPerPageArg);
|
||||
}
|
||||
catch(NumberFormatException e)
|
||||
{
|
||||
// NOTE: use default itemsPerPage
|
||||
}
|
||||
Locale locale = I18NUtil.getLocale();
|
||||
String language = req.getParameter("l");
|
||||
if (language != null && language.length() > 0)
|
||||
{
|
||||
// NOTE: Simple conversion from XML Language Id to Java Locale Id
|
||||
locale = new Locale(language.replace("-", "_"));
|
||||
}
|
||||
|
||||
//
|
||||
// execute the search
|
||||
//
|
||||
|
||||
SearchResult results = search(searchTerms, startPage, itemsPerPage, locale, req);
|
||||
|
||||
//
|
||||
// create model
|
||||
//
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("search", results);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the search
|
||||
*
|
||||
* @param searchTerms
|
||||
* @param startPage
|
||||
* @return
|
||||
*/
|
||||
private SearchResult search(String searchTerms, int startPage, int itemsPerPage, Locale locale, WebScriptRequest req)
|
||||
{
|
||||
SearchResult searchResult = null;
|
||||
ResultSet results = null;
|
||||
|
||||
try
|
||||
{
|
||||
// construct search statement
|
||||
String[] terms = searchTerms.split(" ");
|
||||
Map<String, Object> statementModel = new HashMap<String, Object>(7, 1.0f);
|
||||
statementModel.put("args", createTemplateArgs(req));
|
||||
statementModel.put("terms", terms);
|
||||
Writer queryWriter = new StringWriter(1024);
|
||||
renderFormatTemplate(QUERY_FORMAT, statementModel, queryWriter);
|
||||
String query = queryWriter.toString();
|
||||
|
||||
// execute query
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Search parameters: searchTerms=" + searchTerms + ", startPage=" + startPage + ", itemsPerPage=" + itemsPerPage + ", search locale=" + locale.toString());
|
||||
logger.debug("Issuing lucene search: " + query);
|
||||
}
|
||||
|
||||
SearchParameters parameters = new SearchParameters();
|
||||
parameters.addStore(SEARCH_STORE);
|
||||
parameters.setLanguage(SearchService.LANGUAGE_LUCENE);
|
||||
parameters.setQuery(query);
|
||||
if (locale != null)
|
||||
{
|
||||
parameters.addLocale(locale);
|
||||
}
|
||||
results = searchService.query(parameters);
|
||||
int totalResults = results.length();
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Results: " + totalResults + " rows (limited: " + results.getResultSetMetaData().getLimitedBy() + ")");
|
||||
|
||||
// are we out-of-range
|
||||
int totalPages = (totalResults / itemsPerPage);
|
||||
totalPages += (totalResults % itemsPerPage != 0) ? 1 : 0;
|
||||
if (totalPages != 0 && (startPage < 1 || startPage > totalPages))
|
||||
{
|
||||
throw new WebScriptException("Start page " + startPage + " is outside boundary of " + totalPages + " pages");
|
||||
}
|
||||
|
||||
// construct search result
|
||||
searchResult = new SearchResult();
|
||||
searchResult.setSearchTerms(searchTerms);
|
||||
searchResult.setLocale(locale);
|
||||
searchResult.setItemsPerPage(itemsPerPage);
|
||||
searchResult.setStartPage(startPage);
|
||||
searchResult.setTotalResults(totalResults);
|
||||
if (totalResults == 0)
|
||||
{
|
||||
searchResult.setTotalPages(0);
|
||||
searchResult.setStartIndex(0);
|
||||
searchResult.setTotalPageItems(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
searchResult.setTotalPages(totalPages);
|
||||
searchResult.setStartIndex(((startPage -1) * itemsPerPage) + 1);
|
||||
searchResult.setTotalPageItems(Math.min(itemsPerPage, totalResults - searchResult.getStartIndex() + 1));
|
||||
}
|
||||
SearchTemplateNode[] nodes = new SearchTemplateNode[searchResult.getTotalPageItems()];
|
||||
for (int i = 0; i < searchResult.getTotalPageItems(); i++)
|
||||
{
|
||||
NodeRef node = results.getNodeRef(i + searchResult.getStartIndex() - 1);
|
||||
float score = results.getScore(i + searchResult.getStartIndex() - 1);
|
||||
nodes[i] = new SearchTemplateNode(node, score);
|
||||
}
|
||||
searchResult.setResults(nodes);
|
||||
return searchResult;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (results != null)
|
||||
{
|
||||
results.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Result
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public static class SearchResult
|
||||
{
|
||||
private String id;
|
||||
private String searchTerms;
|
||||
private Locale locale;
|
||||
private int itemsPerPage;
|
||||
private int totalPages;
|
||||
private int totalResults;
|
||||
private int totalPageItems;
|
||||
private int startPage;
|
||||
private int startIndex;
|
||||
private SearchTemplateNode[] results;
|
||||
|
||||
|
||||
public int getItemsPerPage()
|
||||
{
|
||||
return itemsPerPage;
|
||||
}
|
||||
|
||||
/*package*/ void setItemsPerPage(int itemsPerPage)
|
||||
{
|
||||
this.itemsPerPage = itemsPerPage;
|
||||
}
|
||||
|
||||
public TemplateNode[] getResults()
|
||||
{
|
||||
return results;
|
||||
}
|
||||
|
||||
/*package*/ void setResults(SearchTemplateNode[] results)
|
||||
{
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
public int getStartIndex()
|
||||
{
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
/*package*/ void setStartIndex(int startIndex)
|
||||
{
|
||||
this.startIndex = startIndex;
|
||||
}
|
||||
|
||||
public int getStartPage()
|
||||
{
|
||||
return startPage;
|
||||
}
|
||||
|
||||
/*package*/ void setStartPage(int startPage)
|
||||
{
|
||||
this.startPage = startPage;
|
||||
}
|
||||
|
||||
public int getTotalPageItems()
|
||||
{
|
||||
return totalPageItems;
|
||||
}
|
||||
|
||||
/*package*/ void setTotalPageItems(int totalPageItems)
|
||||
{
|
||||
this.totalPageItems = totalPageItems;
|
||||
}
|
||||
|
||||
public int getTotalPages()
|
||||
{
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
/*package*/ void setTotalPages(int totalPages)
|
||||
{
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public int getTotalResults()
|
||||
{
|
||||
return totalResults;
|
||||
}
|
||||
|
||||
/*package*/ void setTotalResults(int totalResults)
|
||||
{
|
||||
this.totalResults = totalResults;
|
||||
}
|
||||
|
||||
public String getSearchTerms()
|
||||
{
|
||||
return searchTerms;
|
||||
}
|
||||
|
||||
/*package*/ void setSearchTerms(String searchTerms)
|
||||
{
|
||||
this.searchTerms = searchTerms;
|
||||
}
|
||||
|
||||
public Locale getLocale()
|
||||
{
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return XML 1.0 Language Identification
|
||||
*/
|
||||
public String getLocaleId()
|
||||
{
|
||||
return locale.toString().replace('_', '-');
|
||||
}
|
||||
|
||||
/*package*/ void setLocale(Locale locale)
|
||||
{
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public String getId()
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
id = GUID.generate();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search result row template node
|
||||
*/
|
||||
public class SearchTemplateNode extends TemplateNode
|
||||
{
|
||||
protected final static String URL = "/api/node/content/{0}/{1}/{2}/{3}";
|
||||
|
||||
private static final long serialVersionUID = -1791913270786140012L;
|
||||
private float score;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param nodeRef
|
||||
* @param score
|
||||
*/
|
||||
public SearchTemplateNode(NodeRef nodeRef, float score)
|
||||
{
|
||||
super(nodeRef, getServiceRegistry(), getWebScriptRegistry().getTemplateImageResolver());
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the result row score
|
||||
*
|
||||
* @return score
|
||||
*/
|
||||
public float getScore()
|
||||
{
|
||||
return score;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.template.BaseContentNode#getUrl()
|
||||
*/
|
||||
@Override
|
||||
public String getUrl()
|
||||
{
|
||||
return MessageFormat.format(URL, new Object[] {
|
||||
getNodeRef().getStoreRef().getProtocol(),
|
||||
getNodeRef().getStoreRef().getIdentifier(),
|
||||
getNodeRef().getId(),
|
||||
URLEncoder.encode(getName()) } );
|
||||
}
|
||||
}
|
||||
}
|
97
source/java/org/alfresco/web/scripts/bean/Login.java
Normal file
97
source/java/org/alfresco/web/scripts/bean/Login.java
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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 javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.repo.security.authentication.AuthenticationException;
|
||||
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.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 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<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// extract username and password
|
||||
String username = req.getParameter("u");
|
||||
if (username == null || username.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Username not specified");
|
||||
}
|
||||
String password = req.getParameter("pw");
|
||||
if (password == null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Password not specified");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// get ticket
|
||||
authenticationService.authenticate(username, password.toCharArray());
|
||||
|
||||
// add ticket to model for javascript and template access
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("ticket", authenticationService.getCurrentTicket());
|
||||
return model;
|
||||
}
|
||||
catch(AuthenticationException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Login failed");
|
||||
}
|
||||
finally
|
||||
{
|
||||
authenticationService.clearCurrentSecurityContext();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
99
source/java/org/alfresco/web/scripts/bean/LoginTicket.java
Normal file
99
source/java/org/alfresco/web/scripts/bean/LoginTicket.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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 javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 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<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// retrieve ticket from request and current ticket
|
||||
String ticket = req.getExtensionPath();
|
||||
if (ticket == null && ticket.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Ticket not specified");
|
||||
}
|
||||
|
||||
// construct model for ticket
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("ticket", ticket);
|
||||
|
||||
try
|
||||
{
|
||||
String ticketUser = ticketComponent.validateTicket(ticket);
|
||||
|
||||
// do not go any further if tickets are different
|
||||
if (!AuthenticationUtil.getCurrentUserName().equals(ticketUser))
|
||||
{
|
||||
status.setRedirect(true);
|
||||
status.setCode(HttpServletResponse.SC_NOT_FOUND);
|
||||
status.setMessage("Ticket not found");
|
||||
}
|
||||
}
|
||||
catch(AuthenticationException e)
|
||||
{
|
||||
status.setRedirect(true);
|
||||
status.setCode(HttpServletResponse.SC_NOT_FOUND);
|
||||
status.setMessage("Ticket not found");
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
114
source/java/org/alfresco/web/scripts/bean/LoginTicketDelete.java
Normal file
114
source/java/org/alfresco/web/scripts/bean/LoginTicketDelete.java
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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 javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.WebScriptStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 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<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// retrieve ticket from request and current ticket
|
||||
String ticket = req.getExtensionPath();
|
||||
if (ticket == null && ticket.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Ticket not specified");
|
||||
}
|
||||
|
||||
// construct model for ticket
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("ticket", ticket);
|
||||
|
||||
try
|
||||
{
|
||||
String ticketUser = ticketComponent.validateTicket(ticket);
|
||||
|
||||
// do not go any further if tickets are different
|
||||
if (!AuthenticationUtil.getCurrentUserName().equals(ticketUser))
|
||||
{
|
||||
status.setCode(HttpServletResponse.SC_NOT_FOUND);
|
||||
status.setMessage("Ticket not found");
|
||||
}
|
||||
else
|
||||
{
|
||||
// delete the ticket
|
||||
authenticationService.invalidateTicket(ticket);
|
||||
status.setMessage("Deleted Ticket " + ticket);
|
||||
}
|
||||
}
|
||||
catch(AuthenticationException e)
|
||||
{
|
||||
status.setCode(HttpServletResponse.SC_NOT_FOUND);
|
||||
status.setMessage("Ticket not found");
|
||||
}
|
||||
|
||||
status.setRedirect(true);
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
209
source/java/org/alfresco/web/scripts/bean/SearchEngines.java
Normal file
209
source/java/org/alfresco/web/scripts/bean/SearchEngines.java
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* 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.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.config.Config;
|
||||
import org.alfresco.config.ConfigService;
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.web.config.OpenSearchConfigElement;
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
/**
|
||||
* List of (server-side) registered Search Engines
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class SearchEngines extends DeclarativeWebScript
|
||||
{
|
||||
// url argument values
|
||||
public static final String URL_ARG_DESCRIPTION = "description";
|
||||
public static final String URL_ARG_TEMPLATE = "template";
|
||||
public static final String URL_ARG_ALL = "all";
|
||||
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(SearchEngines.class);
|
||||
|
||||
// dependencies
|
||||
protected ConfigService configService;
|
||||
protected SearchProxy searchProxy;
|
||||
|
||||
/**
|
||||
* @param configService
|
||||
*/
|
||||
public void setConfigService(ConfigService configService)
|
||||
{
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param searchProxy
|
||||
*/
|
||||
public void setSearchProxy(SearchProxy searchProxy)
|
||||
{
|
||||
this.searchProxy = searchProxy;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
String urlType = req.getParameter("type");
|
||||
if (urlType == null || urlType.length() == 0)
|
||||
{
|
||||
urlType = URL_ARG_DESCRIPTION;
|
||||
}
|
||||
else if (!urlType.equals(URL_ARG_DESCRIPTION) && !urlType.equals(URL_ARG_TEMPLATE) && !urlType.equals(URL_ARG_ALL))
|
||||
{
|
||||
urlType = URL_ARG_DESCRIPTION;
|
||||
}
|
||||
|
||||
//
|
||||
// retrieve open search engines configuration
|
||||
//
|
||||
|
||||
Set<UrlTemplate> urls = getUrls(urlType);
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("urltype", urlType);
|
||||
model.put("engines", urls);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve registered search engines
|
||||
*
|
||||
* @return set of search engines
|
||||
*/
|
||||
private Set<UrlTemplate> getUrls(String urlType)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Search Engine parameters: urltype=" + urlType);
|
||||
|
||||
Set<UrlTemplate> urls = new HashSet<UrlTemplate>();
|
||||
Config config = configService.getConfig("OpenSearch");
|
||||
|
||||
OpenSearchConfigElement searchConfig = (OpenSearchConfigElement)config.getConfigElement(OpenSearchConfigElement.CONFIG_ELEMENT_ID);
|
||||
for (OpenSearchConfigElement.EngineConfig engineConfig : searchConfig.getEngines())
|
||||
{
|
||||
Map<String, String> engineUrls = engineConfig.getUrls();
|
||||
for (Map.Entry<String, String> engineUrl : engineUrls.entrySet())
|
||||
{
|
||||
String type = engineUrl.getKey();
|
||||
String url = searchProxy.createUrl(engineConfig, type);
|
||||
|
||||
if ((urlType.equals(URL_ARG_ALL)) ||
|
||||
(urlType.equals(URL_ARG_DESCRIPTION) && type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION)) ||
|
||||
(urlType.equals(URL_ARG_TEMPLATE) && !type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION)))
|
||||
{
|
||||
String label = engineConfig.getLabel();
|
||||
String labelId = engineConfig.getLabelId();
|
||||
if (labelId != null && labelId.length() > 0)
|
||||
{
|
||||
String i18nLabel = I18NUtil.getMessage(labelId);
|
||||
if (i18nLabel == null && label == null)
|
||||
{
|
||||
label = (i18nLabel == null) ? "$$" + labelId + "$$" : i18nLabel;
|
||||
}
|
||||
}
|
||||
urls.add(new UrlTemplate(label, type, url));
|
||||
}
|
||||
|
||||
// TODO: Extract URL templates from OpenSearch description
|
||||
else if (urlType.equals(URL_ARG_TEMPLATE) &&
|
||||
type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Retrieved " + urls.size() + " engine registrations");
|
||||
|
||||
return urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model object for representing a registered search engine
|
||||
*/
|
||||
public static class UrlTemplate
|
||||
{
|
||||
private String type;
|
||||
private String label;
|
||||
private String url;
|
||||
private UrlTemplate engine;
|
||||
|
||||
public UrlTemplate(String label, String type, String url)
|
||||
{
|
||||
this.label = label;
|
||||
this.type = type;
|
||||
this.url = url;
|
||||
this.engine = null;
|
||||
}
|
||||
|
||||
public UrlTemplate(String label, String type, String url, UrlTemplate engine)
|
||||
{
|
||||
this(label, type, url);
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getUrlType()
|
||||
{
|
||||
return (type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION) ? "description" : "template");
|
||||
}
|
||||
|
||||
public UrlTemplate getEngine()
|
||||
{
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
318
source/java/org/alfresco/web/scripts/bean/SearchProxy.java
Normal file
318
source/java/org/alfresco/web/scripts/bean/SearchProxy.java
Normal file
@@ -0,0 +1,318 @@
|
||||
/*
|
||||
* 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.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.config.Config;
|
||||
import org.alfresco.config.ConfigService;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.web.app.servlet.HTTPProxy;
|
||||
import org.alfresco.web.config.OpenSearchConfigElement;
|
||||
import org.alfresco.web.config.OpenSearchConfigElement.EngineConfig;
|
||||
import org.alfresco.web.config.OpenSearchConfigElement.ProxyConfig;
|
||||
import org.alfresco.web.scripts.AbstractWebScript;
|
||||
import org.alfresco.web.scripts.FormatRegistry;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptResponse;
|
||||
import org.alfresco.web.scripts.WebScriptServletResponse;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.dom4j.Attribute;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.XPath;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
|
||||
/**
|
||||
* Alfresco OpenSearch Proxy Service
|
||||
*
|
||||
* Provides the ability to submit a request to a registered search engine
|
||||
* via the Alfresco server.
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class SearchProxy extends AbstractWebScript implements InitializingBean
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(SearchProxy.class);
|
||||
|
||||
// dependencies
|
||||
protected FormatRegistry formatRegistry;
|
||||
protected ConfigService configService;
|
||||
protected OpenSearchConfigElement searchConfig;
|
||||
protected String proxyPath;
|
||||
|
||||
/**
|
||||
* @param formatRegistry
|
||||
*/
|
||||
public void setFormatRegistry(FormatRegistry formatRegistry)
|
||||
{
|
||||
this.formatRegistry = formatRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configService
|
||||
*/
|
||||
public void setConfigService(ConfigService configService)
|
||||
{
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
Config config = configService.getConfig("OpenSearch");
|
||||
searchConfig = (OpenSearchConfigElement)config.getConfigElement(OpenSearchConfigElement.CONFIG_ELEMENT_ID);
|
||||
if (searchConfig == null)
|
||||
{
|
||||
throw new WebScriptException("OpenSearch configuration not found");
|
||||
}
|
||||
ProxyConfig proxyConfig = searchConfig.getProxy();
|
||||
if (proxyConfig == null)
|
||||
{
|
||||
throw new WebScriptException("OpenSearch proxy configuration not found");
|
||||
}
|
||||
proxyPath = proxyConfig.getUrl();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScript#execute(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
*/
|
||||
public void execute(WebScriptRequest req, WebScriptResponse res)
|
||||
throws IOException
|
||||
{
|
||||
String extensionPath = req.getExtensionPath();
|
||||
String[] extensionPaths = extensionPath.split("/");
|
||||
if (extensionPaths.length != 2)
|
||||
{
|
||||
throw new WebScriptException("OpenSearch engine has not been specified as /{engine}/{format}");
|
||||
}
|
||||
|
||||
// retrieve search engine configuration
|
||||
String engine = extensionPaths[0];
|
||||
EngineConfig engineConfig = searchConfig.getEngine(engine);
|
||||
if (engineConfig == null)
|
||||
{
|
||||
throw new WebScriptException("OpenSearch engine '" + engine + "' does not exist");
|
||||
}
|
||||
|
||||
// retrieve engine url as specified by format
|
||||
String format = extensionPaths[1];
|
||||
String mimetype = formatRegistry.getMimeType(null, format);
|
||||
if (mimetype == null)
|
||||
{
|
||||
throw new WebScriptException("Format '" + format + "' does not map to a registered mimetype");
|
||||
}
|
||||
Map<String, String> engineUrls = engineConfig.getUrls();
|
||||
String engineUrl = engineUrls.get(mimetype);
|
||||
if (engineUrl == null)
|
||||
{
|
||||
throw new WebScriptException("Url mimetype '" + mimetype + "' does not exist for engine '" + engine + "'");
|
||||
}
|
||||
|
||||
// replace template url arguments with actual arguments specified on request
|
||||
int engineUrlArgIdx = engineUrl.indexOf("?");
|
||||
if (engineUrlArgIdx != -1)
|
||||
{
|
||||
engineUrl = engineUrl.substring(0, engineUrlArgIdx);
|
||||
}
|
||||
if (req.getQueryString() != null)
|
||||
{
|
||||
engineUrl += "?" + req.getQueryString();
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Mapping engine '" + engine + "' (mimetype '" + mimetype + "') to url '" + engineUrl + "'");
|
||||
|
||||
// issue request against search engine
|
||||
// NOTE: This web script must be executed in a HTTP servlet environment
|
||||
if (!(res instanceof WebScriptServletResponse))
|
||||
{
|
||||
throw new WebScriptException("Search Proxy must be executed in HTTP Servlet environment");
|
||||
}
|
||||
HttpServletResponse servletRes = ((WebScriptServletResponse)res).getHttpServletResponse();
|
||||
SearchEngineHttpProxy proxy = new SearchEngineHttpProxy(req.getServicePath() + "/" + req.getContextPath(), engine, engineUrl, servletRes);
|
||||
proxy.service();
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenSearch HTTPProxy
|
||||
*
|
||||
* This proxy remaps OpenSearch links (e.g. previous, next) found in search results.
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
private class SearchEngineHttpProxy extends HTTPProxy
|
||||
{
|
||||
private final static String ATOM_NS_URI = "http://www.w3.org/2005/Atom";
|
||||
private final static String ATOM_NS_PREFIX = "atom";
|
||||
private final static String ATOM_LINK_XPATH = "atom:link[@rel=\"first\" or @rel=\"last\" or @rel=\"next\" or @rel=\"previous\" or @rel=\"self\" or @rel=\"alternate\"]";
|
||||
private String engine;
|
||||
private String rootPath;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param requestUrl
|
||||
* @param response
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public SearchEngineHttpProxy(String rootPath, String engine, String engineUrl, HttpServletResponse response)
|
||||
throws MalformedURLException
|
||||
{
|
||||
super(engineUrl.startsWith("/") ? rootPath + engineUrl : engineUrl, response);
|
||||
this.engine = engine;
|
||||
this.rootPath = rootPath;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.app.servlet.HTTPProxy#writeResponse(java.io.InputStream, java.io.OutputStream)
|
||||
*/
|
||||
@Override
|
||||
protected void writeResponse(InputStream input, OutputStream output)
|
||||
throws IOException
|
||||
{
|
||||
if (response.getContentType().startsWith(MimetypeMap.MIMETYPE_ATOM) ||
|
||||
response.getContentType().startsWith(MimetypeMap.MIMETYPE_RSS))
|
||||
{
|
||||
// Only post-process ATOM and RSS feeds
|
||||
// Replace all navigation links with "proxied" versions
|
||||
SAXReader reader = new SAXReader();
|
||||
try
|
||||
{
|
||||
Document document = reader.read(input);
|
||||
Element rootElement = document.getRootElement();
|
||||
|
||||
XPath xpath = rootElement.createXPath(ATOM_LINK_XPATH);
|
||||
Map<String,String> uris = new HashMap<String,String>();
|
||||
uris.put(ATOM_NS_PREFIX, ATOM_NS_URI);
|
||||
xpath.setNamespaceURIs(uris);
|
||||
|
||||
List nodes = xpath.selectNodes(rootElement);
|
||||
Iterator iter = nodes.iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Element element = (Element)iter.next();
|
||||
Attribute hrefAttr = element.attribute("href");
|
||||
String mimetype = element.attributeValue("type");
|
||||
if (mimetype == null || mimetype.length() == 0)
|
||||
{
|
||||
mimetype = MimetypeMap.MIMETYPE_HTML;
|
||||
}
|
||||
String url = createUrl(engine, hrefAttr.getValue(), mimetype);
|
||||
if (url.startsWith("/"))
|
||||
{
|
||||
url = rootPath + url;
|
||||
}
|
||||
hrefAttr.setValue(url);
|
||||
}
|
||||
|
||||
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
|
||||
XMLWriter writer = new XMLWriter(output, outputFormat);
|
||||
writer.write(rootElement);
|
||||
writer.flush();
|
||||
}
|
||||
catch(DocumentException e)
|
||||
{
|
||||
throw new IOException(e.toString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
super.writeResponse(input, output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a "proxied" search engine url
|
||||
*
|
||||
* @param engine engine name (as identified by <engine proxy="<name>">)
|
||||
* @param mimetype url to proxy (as identified by mimetype)
|
||||
* @return "proxied" url
|
||||
*/
|
||||
public String createUrl(OpenSearchConfigElement.EngineConfig engine, String mimetype)
|
||||
{
|
||||
Map<String, String> urls = engine.getUrls();
|
||||
String url = urls.get(mimetype);
|
||||
if (url != null)
|
||||
{
|
||||
String proxy = engine.getProxy();
|
||||
if (proxy != null && !mimetype.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION))
|
||||
{
|
||||
url = createUrl(proxy, url, mimetype);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a "proxied" search engine url
|
||||
*
|
||||
* @param engine engine name (as identified by <engine proxy="<name>">)
|
||||
* @param url engine url
|
||||
* @param mimetype mimetype of url
|
||||
* @return "proxied" url
|
||||
*/
|
||||
public String createUrl(String engine, String url, String mimetype)
|
||||
{
|
||||
String format = formatRegistry.getFormat(null, mimetype);
|
||||
if (format == null)
|
||||
{
|
||||
throw new WebScriptException("Mimetype '" + mimetype + "' is not registered.");
|
||||
}
|
||||
|
||||
String proxyUrl = null;
|
||||
int argIdx = url.indexOf("?");
|
||||
if (argIdx == -1)
|
||||
{
|
||||
proxyUrl = proxyPath + "/" + engine + "/" + format;
|
||||
}
|
||||
else
|
||||
{
|
||||
proxyUrl = proxyPath + "/" + engine + "/" + format + url.substring(argIdx);
|
||||
}
|
||||
return proxyUrl;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.web.scripts.AbstractWebScript;
|
||||
import org.alfresco.web.scripts.WebScript;
|
||||
import org.alfresco.web.scripts.WebScriptDescription;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a Web Script Description Document
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class ServiceDescription extends AbstractWebScript
|
||||
{
|
||||
|
||||
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
|
||||
{
|
||||
// extract web script id
|
||||
String scriptId = req.getExtensionPath();
|
||||
if (scriptId == null || scriptId.length() == 0)
|
||||
{
|
||||
throw new WebScriptException("Web Script Id not provided");
|
||||
}
|
||||
|
||||
// locate web script
|
||||
WebScript script = getWebScriptRegistry().getWebScript(scriptId);
|
||||
if (script == null)
|
||||
{
|
||||
throw new WebScriptException("Web Script Id '" + scriptId + "' not found");
|
||||
}
|
||||
|
||||
// retrieve description document
|
||||
WebScriptDescription desc = script.getDescription();
|
||||
InputStream serviceDescIS = null;
|
||||
try
|
||||
{
|
||||
serviceDescIS = desc.getDescDocument();
|
||||
OutputStream out = res.getOutputStream();
|
||||
res.setContentType(MimetypeMap.MIMETYPE_XML + ";charset=UTF-8");
|
||||
byte[] buffer = new byte[2048];
|
||||
int read = serviceDescIS.read(buffer);
|
||||
while (read != -1)
|
||||
{
|
||||
out.write(buffer, 0, read);
|
||||
read = serviceDescIS.read(buffer);
|
||||
}
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new WebScriptException("Failed to read Web Script description document for '" + scriptId + "'", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (serviceDescIS != null) serviceDescIS.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
// NOTE: ignore close exception
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
192
source/java/org/alfresco/web/scripts/bean/ServiceDump.java
Normal file
192
source/java/org/alfresco/web/scripts/bean/ServiceDump.java
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* 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.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScript;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
import org.alfresco.web.scripts.WebScriptStorage;
|
||||
import org.alfresco.web.scripts.WebScriptStore;
|
||||
|
||||
|
||||
/**
|
||||
* Dumps everything known about the specified Web Script
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class ServiceDump extends DeclarativeWebScript
|
||||
{
|
||||
private WebScriptStorage storage;
|
||||
|
||||
|
||||
public void setStorage(WebScriptStorage storage)
|
||||
{
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
// extract web script id
|
||||
String scriptId = req.getExtensionPath();
|
||||
if (scriptId == null || scriptId.length() == 0)
|
||||
{
|
||||
throw new WebScriptException("Web Script Id not provided");
|
||||
}
|
||||
|
||||
// locate web script
|
||||
WebScript script = getWebScriptRegistry().getWebScript(scriptId);
|
||||
if (script == null)
|
||||
{
|
||||
throw new WebScriptException("Web Script Id '" + scriptId + "' not found");
|
||||
}
|
||||
|
||||
// construct model
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
Map<String, String> implPaths = new HashMap<String, String>();
|
||||
List<Store> modelStores = new ArrayList<Store>();
|
||||
model.put("script", script.getDescription());
|
||||
model.put("script_class", script.getClass().toString());
|
||||
model.put("stores", modelStores);
|
||||
|
||||
// locate web script stores
|
||||
Collection<WebScriptStore> stores = storage.getStores();
|
||||
for (WebScriptStore store : stores)
|
||||
{
|
||||
Store modelStore = new Store();
|
||||
modelStore.path = store.getBasePath();
|
||||
|
||||
// locate script implementation files
|
||||
String[] scriptPaths = store.getScriptDocumentPaths(script);
|
||||
for (String scriptPath : scriptPaths)
|
||||
{
|
||||
Implementation impl = new Implementation();
|
||||
impl.path = scriptPath;
|
||||
impl.overridden = implPaths.containsKey(scriptPath);
|
||||
|
||||
// extract implementation content
|
||||
InputStream documentIS = null;
|
||||
try
|
||||
{
|
||||
documentIS = store.getDocument(scriptPath);
|
||||
InputStreamReader isReader = new InputStreamReader(documentIS);
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
char[] buffer = new char[2048];
|
||||
int read = isReader.read(buffer);
|
||||
while (read != -1)
|
||||
{
|
||||
stringWriter.write(buffer, 0, read);
|
||||
read = isReader.read(buffer);
|
||||
}
|
||||
impl.content = stringWriter.toString();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
impl.throwable = e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (documentIS != null) documentIS.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
// NOTE: ignore close exception
|
||||
}
|
||||
}
|
||||
|
||||
// record web script implementation file against store
|
||||
modelStore.files.add(impl);
|
||||
}
|
||||
|
||||
// record store in list of stores
|
||||
modelStores.add(modelStore);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
public static class Store
|
||||
{
|
||||
private String path;
|
||||
private Collection<Implementation> files = new ArrayList<Implementation>();
|
||||
|
||||
public String getPath()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
public Collection<Implementation> getFiles()
|
||||
{
|
||||
return files;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Implementation
|
||||
{
|
||||
private String path;
|
||||
private boolean overridden;
|
||||
private String content;
|
||||
private Exception throwable;
|
||||
|
||||
public String getPath()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
public String getContent()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
public boolean getOverridden()
|
||||
{
|
||||
return overridden;
|
||||
}
|
||||
|
||||
public Throwable getException()
|
||||
{
|
||||
return throwable;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
245
source/java/org/alfresco/web/scripts/bean/ServiceInstall.java
Normal file
245
source/java/org/alfresco/web/scripts/bean/ServiceInstall.java
Normal file
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* 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.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
||||
import org.alfresco.web.scripts.WebScript;
|
||||
import org.alfresco.web.scripts.WebScriptDescription;
|
||||
import org.alfresco.web.scripts.WebScriptException;
|
||||
import org.alfresco.web.scripts.WebScriptRegistry;
|
||||
import org.alfresco.web.scripts.WebScriptRequest;
|
||||
import org.alfresco.web.scripts.WebScriptServletRequest;
|
||||
import org.alfresco.web.scripts.WebScriptStatus;
|
||||
import org.alfresco.web.scripts.WebScriptStorage;
|
||||
import org.alfresco.web.scripts.WebScriptStore;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.FileItemFactory;
|
||||
import org.apache.commons.fileupload.FileUploadException;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.XPath;
|
||||
import org.dom4j.io.SAXReader;
|
||||
|
||||
|
||||
/**
|
||||
* Install a Web Script
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class ServiceInstall extends DeclarativeWebScript
|
||||
{
|
||||
private WebScriptStorage storage;
|
||||
|
||||
|
||||
public void setStorage(WebScriptStorage storage)
|
||||
{
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status)
|
||||
{
|
||||
if (!(req instanceof WebScriptServletRequest))
|
||||
{
|
||||
throw new WebScriptException("Web Script install only supported via HTTP Servlet");
|
||||
}
|
||||
HttpServletRequest servletReq = ((WebScriptServletRequest)req).getHttpServletRequest();
|
||||
|
||||
// construct model
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
List<InstalledFile> installedFiles = new ArrayList<InstalledFile>();
|
||||
model.put("installedFiles", installedFiles);
|
||||
|
||||
try
|
||||
{
|
||||
// extract uploaded file
|
||||
FileItemFactory factory = new DiskFileItemFactory();
|
||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||
boolean isMultipart = upload.isMultipartContent(servletReq);
|
||||
if (!isMultipart)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Web Script install request is not multi-part");
|
||||
}
|
||||
FileItem part = null;
|
||||
List<FileItem> files = upload.parseRequest(servletReq);
|
||||
for (FileItem file : files)
|
||||
{
|
||||
if (!file.isFormField())
|
||||
{
|
||||
if (part != null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Web Script install request expects only one file upload");
|
||||
}
|
||||
part = file;
|
||||
}
|
||||
}
|
||||
|
||||
// find web script definition
|
||||
Document document = null;
|
||||
InputStream fileIS = part.getInputStream();
|
||||
try
|
||||
{
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileIS));
|
||||
SAXReader reader = new SAXReader();
|
||||
document = reader.read(bufferedReader);
|
||||
}
|
||||
finally
|
||||
{
|
||||
fileIS.close();
|
||||
}
|
||||
Element rootElement = document.getRootElement();
|
||||
XPath xpath = rootElement.createXPath("//ws:webscript");
|
||||
Map<String,String> uris = new HashMap<String,String>();
|
||||
uris.put("ws", "http://www.alfresco.org/webscript/1.0");
|
||||
xpath.setNamespaceURIs(uris);
|
||||
List nodes = xpath.selectNodes(rootElement);
|
||||
if (nodes.size() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Cannot locate Web Script in uploaded file");
|
||||
}
|
||||
|
||||
// extract web script definition
|
||||
Element webscriptElem = (Element)nodes.get(0);
|
||||
String scriptId = webscriptElem.attributeValue("scriptid");
|
||||
if (scriptId == null || scriptId.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Expected scriptid value on webscript element");
|
||||
}
|
||||
Iterator iter = webscriptElem.elementIterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Element fileElem = (Element)iter.next();
|
||||
String webscriptStore = fileElem.attributeValue("store");
|
||||
if (webscriptStore == null || webscriptStore.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Expected store value on webscript element");
|
||||
}
|
||||
String webscriptPath = fileElem.attributeValue("path");
|
||||
if (webscriptPath == null || webscriptPath.length() == 0)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Expected file value on webscript element");
|
||||
}
|
||||
String webscriptContent = fileElem.getText();
|
||||
|
||||
// install web script implementation file
|
||||
installFile(webscriptStore, webscriptPath, webscriptContent);
|
||||
InstalledFile installedFile = new InstalledFile();
|
||||
installedFile.store = webscriptStore;
|
||||
installedFile.path = webscriptPath;
|
||||
installedFiles.add(installedFile);
|
||||
}
|
||||
|
||||
// reset the web script registry
|
||||
WebScriptRegistry registry = getWebScriptRegistry();
|
||||
registry.reset();
|
||||
|
||||
// locate installed web script
|
||||
WebScript webscript = registry.getWebScript(scriptId);
|
||||
if (webscript == null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to install Web Script " + scriptId);
|
||||
}
|
||||
model.put("installedScript", webscript.getDescription());
|
||||
}
|
||||
catch(FileUploadException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
|
||||
}
|
||||
catch(DocumentException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
private void installFile(String storePath, String file, String content)
|
||||
{
|
||||
// retrieve appropriate web script store
|
||||
WebScriptStore store = storage.getStore(storePath);
|
||||
if (store == null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Store path " + storePath + " refers to a store that does not exist");
|
||||
}
|
||||
|
||||
// determine if file already exists in store
|
||||
if (store.hasDocument(file))
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Web Script file " + file + " already exists in store " + storePath);
|
||||
}
|
||||
|
||||
// create the web script file
|
||||
try
|
||||
{
|
||||
store.createDocument(file, content);
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to install Web Script file " + file + " into store" + storePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class InstalledFile
|
||||
{
|
||||
private WebScriptDescription script;
|
||||
private String store;
|
||||
private String path;
|
||||
|
||||
public String getStore()
|
||||
{
|
||||
return store;
|
||||
}
|
||||
|
||||
public String getPath()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user