Java Reference
In-Depth Information
Listing 8-8. ImageSource.java
package com.kyleroche.gaeservices;
import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ImageSource extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("image/jpeg");
PersistenceManager pm = PMF.get().getPersistenceManager();
resp.getOutputStream().write(pm.getObjectById(ImageObject.class,
Long.valueOf(req.getParameter("id").toString())).getContent().getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();
}
}
Writing the ImageTransform Class
So far, you've created the PersistenceManager class to handle the communication
with the data store, the ImageObject itself, and the servlet to retrieve and render
the image from the data store. The next piece is the most significant. How do you
handle the HTTP POST form that will be sending you the image and apply the
transformation prior to storing the image in the data store? The ImageTransform
servlet that you first added to your project is going to accept the POST parameters
from the HTML form, save the image to the data store, call the App Engine Images
service to transform the image, and display both the original and the transformed
images to the browser.
Copy the code from Listing 8-9 to ImageTransform.java . Pay close attention to the
line of code in bold print. This is where the transformation is defined. You are telling
the Images service what type of transformation you are going to apply to the image
before you commit the changes.
 
Search WWH ::




Custom Search