Example usage for org.apache.commons.fileupload FileUploadException getStackTrace

List of usage examples for org.apache.commons.fileupload FileUploadException getStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileUploadException getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:org.trianacode.TrianaCloud.Broker.Broker.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    logger.info("Broker received a request.");
    String pathInfo = isolatePath(request);
    String content = "";
    if (!pathInfo.equalsIgnoreCase("")) {
        logger.info("Unknown Endpoint");
        write404Error(response, "Unknown endpoint");
        return;// w  w  w. java  2s.c  om
    }

    try {
        byte[] data = null;
        String r_key = "";
        String fname = "";
        String name = "";
        int numTasks = 0;
        StringBuilder s = new StringBuilder();

        try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                    String fieldname = item.getFieldName();
                    String fieldvalue = item.getString();
                    if (fieldname.equalsIgnoreCase("task")) {
                        s.append(fieldvalue);
                    }
                    if (fieldname.equalsIgnoreCase("routingkey")) {
                        r_key = fieldvalue;
                    }
                    if (fieldname.equalsIgnoreCase("numtasks")) {
                        numTasks = Integer.parseInt(fieldvalue);
                    }
                    if (fieldname.equalsIgnoreCase("name")) {
                        name = fieldvalue;
                    }
                } else {
                    // Process form file field (input type="file").
                    String fieldname = item.getFieldName();
                    String filename = FilenameUtils.getName(item.getName());
                    // ... (do your job here)

                    fname = filename;

                    InputStream is = item.getInputStream();

                    long length = item.getSize();

                    if (length > Integer.MAX_VALUE) {
                        // File is too large
                        throw new Exception("File too large");
                    }

                    // Create the byte array to hold the data
                    byte[] bytes = new byte[(int) length];

                    // Read in the bytes
                    int offset = 0;
                    int numRead = 0;
                    while (offset < bytes.length
                            && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                        offset += numRead;
                    }

                    // Ensure all the bytes have been read in
                    if (offset < bytes.length) {
                        throw new IOException("Could not completely read file " + length);
                    }
                    data = bytes;
                }
            }
        } catch (FileUploadException e) {
            logger.error("Cannot parse multipart request.");
            throw new ServletException("Cannot parse multipart request.", e);
        }

        List<String> UUIDList = new ArrayList<String>();

        log.debug(content);
        for (int i = 0; i < numTasks; i++) {
            Task t = new Task();
            t.setData(data);
            t.setDataMD5(MD5.getMD5Hash(data));
            t.setDataType("binary");
            t.setName(name);
            t.setFileName(fname);
            t.setOrigin("Broker");
            t.setDispatchTime(System.currentTimeMillis());
            t.setRoutingKey(r_key);
            t.setUUID(UUID.randomUUID().toString());
            insertTask(t);
            UUIDList.add(t.getUUID());
        }

        StringBuilder sb = new StringBuilder();
        for (String id : UUIDList) {
            sb.append(id + "\n");
        }
        //String ret = "Ok; ///TODO:do some stuff here
        writeResponse(response, 200, "Success", sb.toString());

        List<Task> t = td.list();

        for (Task ta : t) {
            System.out.println(ta.getDispatchTime() + "  " + ta.getState());
        }

    } catch (Exception e) {
        e.printStackTrace();
        log.error(e);
        StringBuffer stack = new StringBuffer("Error: " + e.getMessage() + "<br/>");
        StackTraceElement[] trace = e.getStackTrace();
        for (StackTraceElement element : trace) {
            stack.append(element.toString()).append("<br/>");
        }
        writeError(response, 500, stack.toString());
    } catch (Throwable t) {
        writeThrowable(response, t);
    }
}