[gwtupload] Example of using new GAE libs

Showing 1-4 of 4 messages
[gwtupload] Example of using new GAE libs wesn 5/19/10 12:20 PM
Hello

Please could you include a code sample of how to use your new GAE
libraries. It would be the most rockingest if you could include a
multipart example too.

Wes

--
--
http://groups.google.com/group/gwtupload
[gwtupload] Re: Example of using new GAE libs dindeman 5/20/10 8:58 AM
I recently started using Manuel's library to upload images onto the
GAE datastore.
Here is the way I extended the AppEngineUploadAction servlet to do so
(pay attention at replacing <you_package_name_here> with your own
servlet's package name):

package <your_package_name_here>;

import gwtupload.server.exceptions.UploadActionException;
import gwtupload.server.gae.AppEngineUploadAction;

import java.io.InputStream;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;

import com.google.appengine.api.datastore.Blob;

public class DatastoreUploadAction extends AppEngineUploadAction {

        @Override
        public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
                String out = super.executeAction(request, sessionFiles);

                for(FileItem imgItem : sessionFiles) {
                        PersistenceManager pm = ContentServiceImpl.getPersistenceManager();
                        Transaction tx = pm.currentTransaction();
                        try {
                            // Start the transaction
                            tx.begin();

                            InputStream imgStream = imgItem.getInputStream();
                                Blob blob = new Blob(IOUtils.toByteArray(imgStream));
                                ImageBlob imageBlob = new ImageBlob(imgItem.getName(), blob);

                            pm.makePersistent(imageBlob);

                            // Commit the transaction, flushing the object to the datastore
                            tx.commit();
                        }
                        catch(Exception e) {
                                e.printStackTrace();
                        }
                        finally {
                            if(tx.isActive()) {
                                tx.rollback();
                            }
                            pm.close();
                        }
                }
            return out;
        }
}

And of course don't forget to map the servlet in web.xml:

  <servlet>
    <servlet-name>uploadServlet</servlet-name>
    <servlet-class><your_package_name_here>.DatastoreUploadAction</
servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>*.gupld</url-pattern>
  </servlet-mapping>

I hope this helps.
Luc
--
--
http://groups.google.com/group/gwtupload
Re: [gwtupload] Re: Example of using new GAE libs Michael Witt 5/27/11 6:48 PM
That was quite helpful ... thanks!
Re: [gwtupload] Re: Example of using new GAE libs Juan Manuel Vicente 4/2/12 5:33 PM
Can you attach ImageBlob code?

Thanks!
JM