Package org.apache.cocoon.servlet.multipart

Examples of org.apache.cocoon.servlet.multipart.Part


                                    a.getType(src.getMimeType()),
                                    a.getName(name.substring(name.lastIndexOf('/') + 1)));
                        }
                    } else {
                        if (a.getObject() instanceof Part) {
                            Part part = (Part) a.getObject();
                            ds = new FilePartDataSource(
                                    part,
                                    a.getType(part.getMimeType()),
                                    a.getName(part.getUploadName()));
                        } else {
                            // TODO: other classes?
                            throw new AddressException("Not yet supported: " + a.getObject());
                        }
                    }
View Full Code Here


                File asciiFile = (File) value;
                asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
                length = (int) asciiFile.length();
                clob = new ClobHelper(asciiStream, length);
            } else if (value instanceof Part) {
                Part anyFile = (Part) value;
                asciiStream = new BufferedInputStream(anyFile.getInputStream());
                length = (int) anyFile.getSize();
                clob = new ClobHelper(asciiStream, length);
            } else if (value instanceof JDBCxlobHelper) {
                asciiStream = ((JDBCxlobHelper) value).inputStream;
                length = ((JDBCxlobHelper) value).length;
                clob = new ClobHelper(asciiStream, length);
            } else if (value instanceof Source) {
                asciiStream = ((Source) value).getInputStream();
                length = (int)((Source) value).getContentLength();
                clob = new ClobHelper(asciiStream, length);
            } else {
                String asciiText = (String) value;
                asciiStream = new ByteArrayInputStream(asciiText.getBytes());
                length = asciiText.length();
                clob = new ClobHelper(asciiStream, length);
            }
           
            statement.setClob(position, clob);
            break;
        case Types.CHAR:
            // simple large object, e.g. Informix's TEXT
            //System.out.println("CHAR");
           
            if (value instanceof File) {
                File asciiFile = (File) value;
                asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
                length = (int) asciiFile.length();
            } else if (value instanceof JDBCxlobHelper) {
                asciiStream = ((JDBCxlobHelper) value).inputStream;
                length = ((JDBCxlobHelper) value).length;
            } else if (value instanceof Source) {
                asciiStream = ((Source) value).getInputStream();
                length = (int)((Source) value).getContentLength();
            } else if (value instanceof Part) {
                Part anyFile = (Part) value;
                asciiStream = new BufferedInputStream(anyFile.getInputStream());
                length = (int) anyFile.getSize();
                clob = new ClobHelper(asciiStream, length);
            } else {
                String asciiText = (String) value;
                asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes()));
                length = asciiText.length();
            }
           
            statement.setAsciiStream(position, asciiStream, length);
            break;
        case Types.BIGINT:
            //System.out.println("BIGINT");
            BigDecimal bd = null;
           
            if (value instanceof BigDecimal) {
                bd = (BigDecimal) value;
            } else if (value instanceof Number) {
                bd = BigDecimal.valueOf(((Number)value).longValue());
            } else {
                bd = new BigDecimal((String) value);
            }
           
            statement.setBigDecimal(position, bd);
            break;
        case Types.TINYINT:
            //System.out.println("TINYINT");
            Byte b = null;
           
            if (value instanceof Byte) {
                b = (Byte) value;
            } else if (value instanceof Number) {
                b = new Byte(((Number) value).byteValue());
            } else {
                b = new Byte((String) value);
            }
           
            statement.setByte(position, b.byteValue());
            break;
        case Types.DATE:
            //System.out.println("DATE");
            Date d = null;
           
            if (value instanceof Date) {
                d = (Date) value;
            } else if (value instanceof java.util.Date) {
                d = new Date(((java.util.Date) value).getTime());
            } else if (value instanceof Calendar) {
                d = new Date(((Calendar) value).getTime().getTime());
            } else {
                d = Date.valueOf(String.valueOf(value));
            }
           
            statement.setDate(position, d);
            break;
        case Types.DOUBLE:
            //System.out.println("DOUBLE");
            double db;
           
            if (value instanceof Number) {
                db = (((Number) value).doubleValue());
            } else {
                db = Double.parseDouble(String.valueOf(value));
            }
            statement.setDouble(position, db);
            break;
        case Types.FLOAT:
            //System.out.println("FLOAT");
            float f;
           
            if (value instanceof Number) {
                f = (((Number) value).floatValue());
            } else {
                f = Float.parseFloat(String.valueOf(value));
            }
            statement.setFloat(position, f);
            break;
        case Types.NUMERIC:
            //System.out.println("NUMERIC");
            long l;
           
            if (value instanceof Number) {
                l = (((Number) value).longValue());
            } else {
                l = Long.parseLong(String.valueOf(value));
            }
           
            statement.setLong(position, l);
            break;
        case Types.SMALLINT:
            //System.out.println("SMALLINT");
            Short s = null;
           
            if (value instanceof Short) {
                s = (Short) value;
            } else if (value instanceof Number) {
                s = new Short(((Number) value).shortValue());
            } else {
                s = new Short((String) value);
            }
           
            statement.setShort(position, s.shortValue());
            break;
        case Types.TIME:
            //System.out.println("TIME");
            Time t = null;
           
            if (value instanceof Time) {
                t = (Time) value;
            } else if (value instanceof java.util.Date){
                t = new Time(((java.util.Date) value).getTime());
            } else {
                t = Time.valueOf(value.toString());
            }
           
            statement.setTime(position, t);
            break;
        case Types.TIMESTAMP:
            //System.out.println("TIMESTAMP");
            Timestamp ts = null;
           
            if (value instanceof Time) {
                ts = (Timestamp) value;
            } else if (value instanceof java.util.Date) {
                ts = new Timestamp(((java.util.Date) value).getTime());
            } else {
                ts = Timestamp.valueOf(value.toString());
            }
           
            statement.setTimestamp(position, ts);
            break;
        case Types.ARRAY:
            //System.out.println("ARRAY");
            statement.setArray(position, (Array) value); // no way to convert string to array
            break;
        case Types.STRUCT:
            //System.out.println("STRUCT");
        case Types.OTHER:
            //System.out.println("OTHER");
            statement.setObject(position, value);
            break;
        case Types.LONGVARBINARY:
            //System.out.println("LONGVARBINARY");
            statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime()));
            break;
        case Types.VARCHAR:
            //System.out.println("VARCHAR");
            statement.setString(position, (String) value);
            break;
        case Types.BLOB:
            //System.out.println("BLOB");
            if (value instanceof JDBCxlobHelper) {
                statement.setBinaryStream(position, ((JDBCxlobHelper)value).inputStream, ((JDBCxlobHelper)value).length);
            } else if (value instanceof Source){
                statement.setBinaryStream(position, ((Source)value).getInputStream(), (int)((Source)value).getContentLength());
            } else {
                Blob blob = null;
                if (value instanceof Blob) {
                    blob = (Blob) value;
                } else if( value instanceof File) {
                    file = (File)value;
                    blob = new BlobHelper(new FileInputStream(file), (int) file.length());
                } else if (value instanceof String) {
                    file = new File((String)value);
                    blob = new BlobHelper(new FileInputStream(file), (int) file.length());
                } else if (value instanceof Part) {
                    Part anyFile = (Part) value;
                    blob = new BlobHelper(new BufferedInputStream(anyFile.getInputStream()), anyFile.getSize());
                } else {
                    throw new SQLException("Invalid type for blob: "+value.getClass().getName());
                }
                //InputStream input = new BufferedInputStream(new FileInputStream(file));
                statement.setBlob(position, blob);
View Full Code Here

        Map dublinCoreParams = getDublinCoreParameters(request);
        Map lenyaMetaParams = new HashMap();

        // upload the file to the uploadDir
        Part part = (Part) request.get(UPLOADASSET_PARAM_NAME);

        String fileName = part.getFileName();
        if (!fileName.matches(FILE_NAME_REGEXP) || FilenameUtils.getExtension(fileName).equals("")) {
            // the file name contains characters which mean trouble
            // and are therefore not allowed.
            getLogger().warn("The filename [" + fileName + "] is not valid for an asset.");
            return null;
        }
        // convert spaces in the file name to underscores
        fileName = fileName.replace(' ', '_');
        String mimeType = part.getMimeType();
        int fileSize = part.getSize();

        results.put(UPLOADASSET_RETURN_MIMETYPE, mimeType);
        results.put(UPLOADASSET_RETURN_FILESIZE, new Integer(fileSize));
       
        ResourcesManager resourcesMgr = new ResourcesManager(document);
View Full Code Here

     * @return The saved file or <code>null</code> if the upload was not successful.
     * @throws Exception when something went wrong.
     */
    public File save(Request request, String requestParameter) throws Exception {

        Part part = (Part) request.get(requestParameter);

        File file = null;

        boolean success = save(part);
        if (success) {
            file = new File(directory, part.getFileName());
        }

        return file;
    }
View Full Code Here

                sources.add(src);
                if (src.exists()) {
                    ds = new SourceDataSource(src, getType(), getName());
                }
            } else if (getObject() instanceof Part) {
                Part part = (Part) getObject();
                ds = new FilePartDataSource(part, getType(), getName());
            } else if (getObject() instanceof InputStream) {
                InputStream in = (InputStream) getObject();
                ds = new InputStreamDataSource(in, getType(), getName());
            } else if (getObject() instanceof byte[]) {
View Full Code Here

     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
     */
    protected void doExecute() throws Exception {
        Request request = ContextHelper.getRequest(this.context);
       
        Part jcrImport = (Part)request.get("jcrcontent");
       
        // Get name of first JCR node ('lenya' or publication name).
        String firstNodeName;
        try {
            firstNodeName = getFirstNodeName(new InputSource(jcrImport.getInputStream()));
        } catch (Exception e) {
            throw new JCRImportException("Error getting first node name of import data");
        }
        if (firstNodeName == null) {
            throw new JCRImportException("Reading repository import data failed");
        }

        Repository repo = null;
        try {
            repo = (Repository)manager.lookup(Repository.class.getName());
        } catch (Exception e) {
            throw new CascadingRuntimeException("Cannot lookup repository", e);
        }
       
        try {
            Session session;
            try {
                session = repo.login();
            } catch (LoginException e1) {
                throw new JCRImportException("Login to repository failed", e1);
            } catch (RepositoryException e1) {
                throw new JCRImportException("Cannot access repository", e1);
            }
           
            Workspace ws = session.getWorkspace();
   
            String importTarget = request.getParameter(IMPORT_TARGET_PARAM);
            if (IMPORT_TARGET_REPOSITORY.equals(importTarget)) {
                // Import Lenya repository
                getLogger().debug("Importing Lenya repository into JCR");
                if (!JCR_LENYA_BASE_NAME.equals(firstNodeName)) {
                    throw new JCRImportException("Corrupt Lenya repository data file");
                }
                if (!session.itemExists(JCR_LENYA_ROOT)) {
                    throw new JCRImportException("Lenya JCR root not found [" + JCR_LENYA_ROOT + "]");
                }
                // Remove existing Lenya repository.
                String lenyaBasePath = JCR_LENYA_ROOT +
                    (JCR_LENYA_ROOT.endsWith("/") ? JCR_LENYA_BASE_NAME : "/" + JCR_LENYA_BASE_NAME);
                if (session.itemExists(lenyaBasePath)) {
                    Item jcrLenyaBase = session.getItem(lenyaBasePath);
                    jcrLenyaBase.remove();
                    session.save();
                }
   
                // Import Lenya repository. Use workspace instead of session because of performance.
                try {
                    ws.importXML(JCR_LENYA_ROOT, jcrImport.getInputStream(),
                            ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
                } catch (Exception e) {
                    throw new JCRImportException("Error importing data into workspace");
                }
            } else if (IMPORT_TARGET_PUBLICATION.equals(importTarget)) {
                // Import Lenya publication
                getLogger().debug("Importing Lenya publication into JCR");
                if (!session.itemExists(JCR_LENYA_PUBLICATON_ROOT)) {
                    throw new JCRImportException("Lenya JCR root not found [" + JCR_LENYA_ROOT + "]");
                    // TODO: Create JCR_LENYA_PUBLICATON_ROOT
                }
               
                // Remove existing Lenya repository.
                String lenyaPublicationPath = JCR_LENYA_PUBLICATON_ROOT +
                    (JCR_LENYA_PUBLICATON_ROOT.endsWith("/") ? firstNodeName : "/" + firstNodeName);
                if (session.itemExists(lenyaPublicationPath)) {
                    Item jcrPublicationBase = session.getItem(lenyaPublicationPath);
                    jcrPublicationBase.remove();
                    session.save();
                }
   
                // Import Lenya publication. Use workspace instead of session because of performance.
                try {
                    ws.importXML(JCR_LENYA_PUBLICATON_ROOT, jcrImport.getInputStream(),
                            ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
                } catch (Exception e) {
                    throw new JCRImportException("Error importing data into workspace");
                }
            }
View Full Code Here

        }

        Map dublinCoreParams = getDublinCoreParameters(request);

        // upload the file to the uploadDir
        Part part = (Part) request.get(UPLOADASSET_PARAM_NAME);

        String fileName = part.getFileName();
        if (!fileName.matches(FILE_NAME_REGEXP) || FileUtil.getExtension(fileName).equals("")) {
            // the file name contains characters which mean trouble
            // and are therefore not allowed.
            getLogger().warn("The filename [" + fileName + "] is not valid for an asset.");
            return null;
        }
        // convert spaces in the file name to underscores
        fileName = fileName.replace(' ', '_');
        String mimeType = part.getMimeType();
        int fileSize = part.getSize();

        results.put(UPLOADASSET_RETURN_MIMETYPE, mimeType);
        results.put(UPLOADASSET_RETURN_FILESIZE, new Integer(fileSize));

        dublinCoreParams.put("format", mimeType);
View Full Code Here

        Object obj = formContext.getRequest().get(getRequestParameterName());

        // If the request object is a Part, keep it
        if (obj instanceof Part) {
            Part requestPart = (Part)obj;
            if (this.part != null) {
                // Replace the current part
                this.part.dispose();
            }

            // Keep the request part
            requestPart.setDisposeWithRequest(false);
            this.part = requestPart;
            this.validationError = null;

        // If it's not a part and not null, clear any existing value
        // We also check if we're the submit widget, as a result of clicking the "..." button
View Full Code Here

        final Iterator i = paramNames.iterator();
        while ( i.hasNext() ) {
            final String name = (String)i.next();
            final Object o = req.get(name);
            if ( o != null && o instanceof Part) {
                final Part file = (Part)o;
                try {
                    byte[] c = IOUtil.toByteArray(file.getInputStream());
                    ContentItem ci = new ContentItem(file.getFileName(), true);
                    ci.setContent(c);
                    store.addItem(ci);
                } catch (Exception ignore) {
                    // ignore the exception
                }
View Full Code Here

        }

        Map dublinCoreParams = getDublinCoreParameters(request);

        // upload the file to the uploadDir
        Part part = (Part) request.get(UPLOADASSET_PARAM_NAME);

        String fileName = part.getFileName();
        if (!fileName.matches(FILE_NAME_REGEXP) || FileUtil.getExtension(fileName).equals("")) {
            // the file name contains characters which mean trouble
            // and are therefore not allowed.
            getLogger().warn("The filename [" + fileName + "] is not valid for an asset.");
            return null;
        }
        // convert spaces in the file name to underscores
        fileName = fileName.replace(' ', '_');
        String mimeType = part.getMimeType();
        int fileSize = part.getSize();

        results.put(UPLOADASSET_RETURN_MIMETYPE, mimeType);
        results.put(UPLOADASSET_RETURN_FILESIZE, new Integer(fileSize));

        dublinCoreParams.put("format", mimeType);
View Full Code Here

TOP

Related Classes of org.apache.cocoon.servlet.multipart.Part

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.