Example usage for org.apache.commons.fileupload.disk DiskFileItem toString

List of usage examples for org.apache.commons.fileupload.disk DiskFileItem toString

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.disk DiskFileItem toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this object.

Usage

From source file:com.novartis.opensource.yada.Service.java

/**
 * Obtains list of {@link FileItem} from {@link YADARequest#getUploadItems()}, extracts item metadata and wraps it in json
 * @return a json string containing pertinent metadata about the upload, including where to get it from the file system
 * @throws YADAPluginException when plugin execution fails
 * @throws YADAExecutionException when the upload result configuration fails
 * @see YADARequest#getUploadItems()//from   w w w. ja v a 2 s.  c o  m
 */
private String executeUpload() throws YADAPluginException, YADAExecutionException {
    //TODO move upload item processing to this method from YADARequest
    String result = engageBypass(this.getYADARequest());
    l.debug("Select bypass [result] is [" + result + "]");
    if (result != null) {
        return result;
    }
    engagePreprocess(getYADARequest());
    try {
        /* must return a json object like: 
         * {"files": [
            {
              "name": "picture1.jpg",
              "size": 902604,
              "url": "http:\/\/example.org\/files\/picture1.jpg",
              "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture1.jpg",
              "deleteUrl": "http:\/\/example.org\/files\/picture1.jpg",
              "deleteType": "DELETE"
            },
            {
              "name": "picture2.jpg",
              "size": 841946,
              "url": "http:\/\/example.org\/files\/picture2.jpg",
              "thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture2.jpg",
              "deleteUrl": "http:\/\/example.org\/files\/picture2.jpg",
              "deleteType": "DELETE"
            }
          ]}
         */

        JSONArray files = new JSONArray();
        for (FileItem fItem : getYADARequest().getUploadItems()) {
            JSONObject f = new JSONObject();
            if (fItem.isFormField()) {
                f.put("formField", true);
                f.put("name", fItem.getFieldName());
                f.put("value", fItem.getString());
            } else {
                DiskFileItem item = (DiskFileItem) fItem;
                l.debug(item.toString());
                f.put("name", item.getName());
                f.put("size", item.getSize());
                f.put("path", item.getStoreLocation().getAbsolutePath());
                f.put("url", "");
                f.put("thumbnailUrl", "");
                f.put("deleteUrl", "");
                f.put("deleteType", "");
            }
            files.put(f);
        }
        result = new JSONObject().put("files", files).toString();
    } catch (JSONException e) {
        l.error(e.getMessage());
        e.printStackTrace();
        throw new YADAExecutionException(e.getMessage());
    }

    result = engagePostprocess(result);
    l.debug("result:" + result);
    return result;
}