Search in sources :

Example 36 with WebScriptException

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 + "'");
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Calendar(java.util.Calendar) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 37 with WebScriptException

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);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException)

Example 38 with WebScriptException

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);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName)

Example 39 with WebScriptException

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;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) StringTokenizer(java.util.StringTokenizer) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ArrayList(java.util.ArrayList)

Example 40 with WebScriptException

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);
    }
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ACPExportPackageHandler(org.alfresco.repo.exporter.ACPExportPackageHandler) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Aggregations

WebScriptException (org.springframework.extensions.webscripts.WebScriptException)204 HashMap (java.util.HashMap)94 NodeRef (org.alfresco.service.cmr.repository.NodeRef)67 IOException (java.io.IOException)60 JSONException (org.json.JSONException)48 JSONObject (org.json.JSONObject)44 ArrayList (java.util.ArrayList)32 QName (org.alfresco.service.namespace.QName)31 JSONTokener (org.json.JSONTokener)29 JSONObject (org.json.simple.JSONObject)25 JSONArray (org.json.JSONArray)18 Map (java.util.Map)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)11 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)11 StoreRef (org.alfresco.service.cmr.repository.StoreRef)10 File (java.io.File)9 Date (java.util.Date)8 JSONParser (org.json.simple.parser.JSONParser)8 Serializable (java.io.Serializable)7 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)7