Example usage for org.apache.commons.fileupload.sdata FileItemStream getHeaderNames

List of usage examples for org.apache.commons.fileupload.sdata FileItemStream getHeaderNames

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.sdata FileItemStream getHeaderNames.

Prototype

String[] getHeaderNames();

Source Link

Document

Gets a list of all header names

Usage

From source file:org.sakaiproject.sdata.tool.JCRHandler.java

/**
 * Perform a mime multipart upload into the JCR repository based on a location specified
 * by the rp parameter. The parts of the multipart upload are relative to the current
 * request path//from   w w  w.  j  ava 2  s  . co  m
 *
 * @param request
 *          the request object of the current request.
 * @param response
 *          the response object of the current request
 * @param rp
 *          the resource definition for the current request
 * @throws ServletException
 * @throws IOException
 */
private void doMumtipartUpload(HttpServletRequest request, HttpServletResponse response, ResourceDefinition rp)
        throws ServletException, IOException {
    try {
        try {
            Node n = jcrNodeFactory.createFolder(rp.getRepositoryPath());
            if (n == null) {
                response.reset();
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Unable to uplaod to location " + rp.getRepositoryPath());
                return;
            }
        } catch (Exception ex) {
            sendError(request, response, ex);
            snoopRequest(request);
            LOG.error("Failed  TO service Request ", ex);
            return;
        }

        // Check that we have a file upload request

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
        List<String> errors = new ArrayList<String>();

        // Parse the request
        FileItemIterator iter = upload.getItemIterator(request);
        Map<String, Object> responseMap = new HashMap<String, Object>();
        Map<String, Object> uploads = new HashMap<String, Object>();
        Map<String, List<String>> values = new HashMap<String, List<String>>();
        int uploadNumber = 0;
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            LOG.debug("Got Upload through Uploads");
            String name = item.getName();
            String fieldName = item.getFieldName();
            LOG.info("    Name is " + name + " field Name " + fieldName);
            for (String headerName : item.getHeaderNames()) {
                LOG.info("Header " + headerName + " is " + item.getHeader(headerName));
            }
            InputStream stream = item.openStream();
            if (!item.isFormField()) {
                try {
                    if (name != null && name.trim().length() > 0) {

                        List<String> realNames = values.get(REAL_UPLOAD_NAME);
                        String finalName = name;
                        if (realNames != null && realNames.size() > uploadNumber) {
                            finalName = realNames.get(uploadNumber);
                        }

                        String path = rp.convertToAbsoluteRepositoryPath(finalName);
                        // pooled uploads never overwrite.
                        List<String> pooled = values.get(POOLED);
                        if (pooled != null && pooled.size() > 0 && "1".equals(pooled.get(0))) {
                            path = rp.convertToAbsoluteRepositoryPath(PathUtils.getPoolPrefix(finalName));
                            int i = 0;
                            String basePath = path;
                            try {
                                while (true) {
                                    Node n = jcrNodeFactory.getNode(path);
                                    if (n == null) {
                                        break;
                                    }
                                    int lastStop = basePath.lastIndexOf('.');
                                    path = basePath.substring(0, lastStop) + "_" + i
                                            + basePath.substring(lastStop);
                                    i++;
                                }
                            } catch (JCRNodeFactoryServiceException ex) {
                                // the path does not exist which is good.
                            }
                        }

                        String mimeType = ContentTypes.getContentType(finalName, item.getContentType());
                        Node target = jcrNodeFactory.createFile(path, mimeType);
                        GregorianCalendar lastModified = new GregorianCalendar();
                        lastModified.setTime(new Date());
                        long size = saveStream(target, stream, mimeType, "UTF-8", lastModified);
                        Map<String, Object> uploadMap = new HashMap<String, Object>();
                        if (size > Integer.MAX_VALUE) {
                            uploadMap.put("contentLength", String.valueOf(size));
                        } else {
                            uploadMap.put("contentLength", (int) size);
                        }
                        uploadMap.put("name", finalName);
                        uploadMap.put("url", rp.convertToExternalPath(path));
                        uploadMap.put("mimeType", mimeType);
                        uploadMap.put("lastModified", lastModified.getTime());
                        uploadMap.put("status", "ok");

                        uploads.put(fieldName, uploadMap);
                    }
                } catch (Exception ex) {
                    LOG.error("Failed to Upload Content", ex);
                    Map<String, Object> uploadMap = new HashMap<String, Object>();
                    uploadMap.put("mimeType", "text/plain");
                    uploadMap.put("encoding", "UTF-8");
                    uploadMap.put("contentLength", -1);
                    uploadMap.put("lastModified", 0);
                    uploadMap.put("status", "Failed");
                    uploadMap.put("cause", ex.getMessage());
                    List<String> stackTrace = new ArrayList<String>();
                    for (StackTraceElement ste : ex.getStackTrace()) {
                        stackTrace.add(ste.toString());
                    }
                    uploadMap.put("stacktrace", stackTrace);
                    uploads.put(fieldName, uploadMap);
                    uploadMap = null;

                }

            } else {
                String value = Streams.asString(stream);
                List<String> valueList = values.get(name);
                if (valueList == null) {
                    valueList = new ArrayList<String>();
                    values.put(name, valueList);

                }
                valueList.add(value);
            }
        }

        responseMap.put("success", true);
        responseMap.put("errors", errors.toArray(new String[1]));
        responseMap.put("uploads", uploads);
        sendMap(request, response, responseMap);
        LOG.info("Response Complete Saved to " + rp.getRepositoryPath());
    } catch (Throwable ex) {
        LOG.error("Failed  TO service Request ", ex);
        sendError(request, response, ex);
        return;
    }
}