Search in sources :

Example 1 with FileFolderLoader

use of org.alfresco.repo.model.filefolder.FileFolderLoader 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)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 FileFolderLoader (org.alfresco.repo.model.filefolder.FileFolderLoader)1 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1