use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class AbstractCalendarWebScript method parseDate.
/**
* Gets the date from the String, trying the various formats
* (New and Legacy) until one works...
*/
protected Date parseDate(String date) {
// Is there one at all?
if (date == null || date.length() == 0) {
return null;
}
// Today's Date - special case
if (date.equalsIgnoreCase("NOW")) {
// We want all of today, so go back to midnight
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
// Try as ISO8601
try {
return ISO8601DateFormat.parse(date);
} catch (Exception e) {
}
// Try YYYY/MM/DD
SimpleDateFormat slashtime = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat slash = new SimpleDateFormat("yyyy/MM/dd");
try {
return slashtime.parse(date);
} catch (ParseException e) {
}
try {
return slash.parse(date);
} catch (ParseException e) {
}
// Try YYYY-MM-DD
SimpleDateFormat dashtime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat dash = new SimpleDateFormat("yyyy-MM-dd");
try {
return dashtime.parse(date);
} catch (ParseException e) {
}
try {
return dash.parse(date);
} catch (ParseException e) {
}
// We don't know what it is, object
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid date '" + date + "'");
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class AbstractCalendarWebScript method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
if (templateVars == null) {
String error = "No parameters supplied";
if (useJSONErrors()) {
return buildError(error);
} else {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
}
// Parse the JSON, if supplied
JSONObject json = null;
String contentType = req.getContentType();
if (contentType != null && contentType.indexOf(';') != -1) {
contentType = contentType.substring(0, contentType.indexOf(';'));
}
if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
JSONParser parser = new JSONParser();
try {
json = (JSONObject) parser.parse(req.getContent().getContent());
} catch (IOException io) {
return buildError("Invalid JSON: " + io.getMessage());
} catch (org.json.simple.parser.ParseException je) {
return buildError("Invalid JSON: " + je.getMessage());
}
}
// Get the site short name. Try quite hard to do so...
String siteName = templateVars.get("siteid");
if (siteName == null) {
siteName = templateVars.get("site");
}
if (siteName == null) {
siteName = req.getParameter("site");
}
if (siteName == null && json != null) {
if (json.containsKey("siteid")) {
siteName = (String) json.get("siteid");
} else if (json.containsKey("site")) {
siteName = (String) json.get("site");
}
}
if (siteName == null) {
String error = "No site given";
if (useJSONErrors()) {
return buildError("No site given");
} else {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
}
// Grab the requested site
SiteInfo site = siteService.getSite(siteName);
if (site == null) {
String error = "Could not find site: " + siteName;
if (useJSONErrors()) {
return buildError(error);
} else {
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
}
// Event name is optional
String eventName = templateVars.get("eventname");
// Have the real work done
return executeImpl(site, eventName, req, json, status, cache);
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class ContentGet method execute.
/**
* @see org.springframework.extensions.webscripts.WebScript#execute(WebScriptRequest, WebScriptResponse)
*/
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
// create map of args
String[] names = req.getParameterNames();
Map<String, String> args = new HashMap<String, String>(names.length, 1.0f);
for (String name : names) {
args.put(name, req.getParameter(name));
}
// create map of template vars
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
// create object reference from url
ObjectReference reference = createObjectReferenceFromUrl(args, templateVars);
NodeRef nodeRef = reference.getNodeRef();
if (nodeRef == null) {
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find " + reference.toString());
}
// determine attachment
boolean attach = Boolean.valueOf(req.getParameter("a"));
// render content
QName propertyQName = ContentModel.PROP_CONTENT;
String contentPart = templateVars.get("property");
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) {
propertyQName = QName.createQName(propertyName, namespaceService);
}
}
// Stream the content
streamContentLocal(req, res, nodeRef, attach, propertyQName, null);
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class StreamACP method getNodeRefs.
/**
* Converts the given comma delimited string of NodeRefs to an array
* of NodeRefs. If the string is null a WebScriptException is thrown.
*
* @param nodeRefsParam Comma delimited string of NodeRefs
* @return Array of NodeRef objects
*/
protected NodeRef[] getNodeRefs(String nodeRefsParam) {
// check the list of NodeRefs is present
if (nodeRefsParam == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'nodeRefs' parameter was not provided in form data");
}
List<NodeRef> listNodeRefs = new ArrayList<NodeRef>(8);
StringTokenizer tokenizer = new StringTokenizer(nodeRefsParam, ",");
while (tokenizer.hasMoreTokens()) {
listNodeRefs.add(new NodeRef(tokenizer.nextToken().trim()));
}
NodeRef[] nodeRefs = new NodeRef[listNodeRefs.size()];
nodeRefs = listNodeRefs.toArray(nodeRefs);
return nodeRefs;
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class StreamACP method createACP.
/**
* Returns an ACP file containing the nodes represented by the given list of NodeRefs.
*
* @param params The parameters for the ACP exporter
* @param extension The file extenstion to use for the ACP file
* @param keepFolderStructure Determines whether the folder structure is maintained for
* the content inside the ACP file
* @return File object representing the created ACP
*/
protected File createACP(ExporterCrawlerParameters params, String extension, boolean keepFolderStructure) {
try {
// generate temp file and folder name
File dataFile = new File(GUID.generate());
File contentDir = new File(GUID.generate());
// setup export package handler
File acpFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, "." + extension);
ACPExportPackageHandler handler = new ACPExportPackageHandler(new FileOutputStream(acpFile), dataFile, contentDir, this.mimetypeService);
handler.setExportAsFolders(keepFolderStructure);
handler.setNodeService(this.nodeService);
// perform the actual export
this.exporterService.exportView(handler, params, null);
if (logger.isDebugEnabled())
logger.debug("Created temporary archive: " + acpFile.getAbsolutePath());
return acpFile;
} catch (FileNotFoundException fnfe) {
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create archive", fnfe);
}
}
Aggregations