1 29 30 package nextapp.echo2.webcontainer.image; 31 32 import java.io.IOException ; 33 34 import javax.servlet.http.HttpServletResponse ; 35 36 import nextapp.echo2.app.ImageReference; 37 import nextapp.echo2.webcontainer.ContainerInstance; 38 import nextapp.echo2.webrender.Connection; 39 import nextapp.echo2.webrender.ContentType; 40 import nextapp.echo2.webrender.Service; 41 42 46 public abstract class AbstractImageService 47 implements Service { 48 49 private static final String PARAMETER_IMAGE_UID = "imageuid"; 50 51 private static final String [] URL_PARAMETERS = new String []{PARAMETER_IMAGE_UID}; 52 53 61 public String createUri(ContainerInstance containerInstance, String imageId) { 62 return containerInstance.getServiceUri(this, URL_PARAMETERS, new String []{imageId}); 63 } 64 65 74 public abstract void renderImage(Connection conn, ImageReference imageReference) 75 throws IOException ; 76 77 80 public void service(Connection conn) 81 throws IOException { 82 ContainerInstance containerInstance = (ContainerInstance) conn.getUserInstance(); 83 if (containerInstance == null) { 84 serviceBadRequest(conn, "No container available."); 85 return; 86 } 87 String imageId = conn.getRequest().getParameter(PARAMETER_IMAGE_UID); 88 if (imageId == null) { 89 serviceBadRequest(conn, "Image UID not specified."); 90 return; 91 } 92 ImageReference imageReference = (ImageReference) containerInstance.getIdTable().getObject(imageId); 93 94 if (imageReference == null) { 95 serviceBadRequest(conn, "Image UID is not valid."); 96 return; 97 } 98 renderImage(conn, imageReference); 99 } 100 101 public void serviceBadRequest(Connection conn, String message) { 102 conn.getResponse().setStatus(HttpServletResponse.SC_BAD_REQUEST); 103 conn.setContentType(ContentType.TEXT_PLAIN); 104 conn.getWriter().write(message); 105 } 106 } 107 | Popular Tags |