Search in sources :

Example 16 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class FileFolderLoaderPost method execute.

public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    FileFolderLoader loader = (FileFolderLoader) applicationContext.getBean("fileFolderLoader");
    int count = 0;
    String folderPath = "";
    try {
        JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        folderPath = json.getString(KEY_FOLDER_PATH);
        if (folderPath == null) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, KEY_FOLDER_PATH + " not supplied.");
        }
        int fileCount = 100;
        if (json.has(KEY_FILE_COUNT)) {
            fileCount = json.getInt(KEY_FILE_COUNT);
        }
        int filesPerTxn = DEFAULT_FILES_PER_TXN;
        if (json.has(KEY_FILES_PER_TXN)) {
            filesPerTxn = json.getInt(KEY_FILES_PER_TXN);
        }
        long minFileSize = DEFAULT_MIN_FILE_SIZE;
        if (json.has(KEY_MIN_FILE_SIZE)) {
            minFileSize = json.getInt(KEY_MIN_FILE_SIZE);
        }
        long maxFileSize = DEFAULT_MAX_FILE_SIZE;
        if (json.has(KEY_MAX_FILE_SIZE)) {
            maxFileSize = json.getInt(KEY_MAX_FILE_SIZE);
        }
        long maxUniqueDocuments = DEFAULT_MAX_UNIQUE_DOCUMENTS;
        if (json.has(KEY_MAX_UNIQUE_DOCUMENTS)) {
            maxUniqueDocuments = json.getInt(KEY_MAX_UNIQUE_DOCUMENTS);
        }
        boolean forceBinaryStorage = DEFAULT_FORCE_BINARY_STORAGE;
        if (json.has(KEY_FORCE_BINARY_STORAGE)) {
            forceBinaryStorage = json.getBoolean(KEY_FORCE_BINARY_STORAGE);
        }
        int descriptionCount = DEFAULT_DESCRIPTION_COUNT;
        if (json.has(KEY_DESCRIPTION_COUNT)) {
            descriptionCount = json.getInt(KEY_DESCRIPTION_COUNT);
        }
        long descriptionSize = DEFAULT_DESCRIPTION_SIZE;
        if (json.has(KEY_DESCRIPTION_SIZE)) {
            descriptionSize = json.getLong(KEY_DESCRIPTION_SIZE);
        }
        // Perform the load
        count = loader.createFiles(folderPath, fileCount, filesPerTxn, minFileSize, maxFileSize, maxUniqueDocuments, forceBinaryStorage, descriptionCount, descriptionSize);
    } catch (FileNotFoundException e) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Folder not found: ", folderPath);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    }
    // Write the response
    OutputStream os = res.getOutputStream();
    try {
        JSONObject json = new JSONObject();
        json.put(KEY_COUNT, count);
        os.write(json.toString().getBytes("UTF-8"));
    } catch (JSONException e) {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Failed to write JSON", e);
    } finally {
        os.close();
    }
}
Also used : JSONTokener(org.json.JSONTokener) FileFolderLoader(org.alfresco.repo.model.filefolder.FileFolderLoader) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) OutputStream(java.io.OutputStream) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 17 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class QuickShareMetaDataGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, Status status, Cache cache) {
    if (!isEnabled()) {
        throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "QuickShare is disabled system-wide");
    }
    // create map of params (template vars)
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    final String sharedId = params.get("shared_id");
    if (sharedId == null) {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "A valid sharedId must be specified !");
    }
    try {
        return quickShareService.getMetaData(sharedId);
    } catch (InvalidSharedIdException ex) {
        logger.error("Unable to find: " + sharedId);
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find: " + sharedId);
    } catch (InvalidNodeRefException inre) {
        logger.error("Unable to find: " + sharedId + " [" + inre.getNodeRef() + "]");
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find: " + sharedId);
    }
}
Also used : InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 18 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class ReadGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, Status status, Cache cache) {
    if (!isEnabled()) {
        throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "QuickShare is disabled system-wide");
    }
    // create map of params (template vars)
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    final String sharedId = params.get("shared_id");
    if (sharedId == null) {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "A valid sharedId must be specified !");
    }
    try {
        boolean canRead = quickShareService.canRead(sharedId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("canRead", canRead);
        return result;
    } catch (InvalidSharedIdException ex) {
        logger.error("Unable to find: " + sharedId);
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find: " + sharedId);
    } catch (InvalidNodeRefException inre) {
        logger.error("Unable to find: " + sharedId + " [" + inre.getNodeRef() + "]");
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find: " + sharedId);
    }
}
Also used : InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 19 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class ShareContentGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    if (!isEnabled()) {
        throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "QuickShare is disabled system-wide");
    }
    // create map of params (template vars)
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    final String sharedId = params.get("shared_id");
    if (sharedId == null) {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "A valid sharedId must be specified !");
    }
    try {
        Pair<String, NodeRef> pair = quickShareService.getTenantNodeRefFromSharedId(sharedId);
        final String tenantDomain = pair.getFirst();
        final NodeRef nodeRef = pair.getSecond();
        String siteId = siteService.getSiteShortName(nodeRef);
        Map<String, Object> model = new HashMap<String, Object>(3);
        model.put("sharedId", sharedId);
        model.put("nodeRef", nodeRef.toString());
        model.put("siteId", siteId);
        model.put("tenantDomain", tenantDomain);
        if (logger.isInfoEnabled()) {
            logger.info("QuickShare - get shared context: " + sharedId + " [" + model + "]");
        }
        return model;
    } catch (InvalidNodeRefException inre) {
        logger.error("Unable to find: " + sharedId + " [" + inre.getNodeRef() + "]");
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find: " + sharedId);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 20 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class RatingDelete method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    NodeRef nodeRef = parseRequestForNodeRef(req);
    String ratingSchemeName = parseRequestForScheme(req);
    Rating deletedRating = ratingService.removeRatingByCurrentUser(nodeRef, ratingSchemeName);
    if (deletedRating == null) {
        // There was no rating in the specified scheme to delete.
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Unable to delete non-existent rating: " + ratingSchemeName + " from " + nodeRef.toString());
    }
    model.put(NODE_REF, nodeRef.toString());
    model.put(AVERAGE_RATING, ratingService.getAverageRating(nodeRef, ratingSchemeName));
    model.put(RATINGS_TOTAL, ratingService.getTotalRating(nodeRef, ratingSchemeName));
    model.put(RATINGS_COUNT, ratingService.getRatingsCount(nodeRef, ratingSchemeName));
    return model;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) Rating(org.alfresco.service.cmr.rating.Rating)

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