KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > image > AbstractImageService


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.image;
31
32 import java.io.IOException JavaDoc;
33
34 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
43  * Abstract base service for rendering images sourced from the application
44  * container.
45  */

46 public abstract class AbstractImageService
47 implements Service {
48
49     private static final String JavaDoc PARAMETER_IMAGE_UID = "imageuid";
50
51     private static final String JavaDoc[] URL_PARAMETERS = new String JavaDoc[]{PARAMETER_IMAGE_UID};
52     
53     /**
54      * Creates a URI to retrieve a specific image for a specific component
55      * from the server.
56      *
57      * @param containerInstance the relevant application container instance.
58      * @param imageId the unique id to retrieve the image from the
59      * <code>ContainerInstance</code>
60      */

61     public String JavaDoc createUri(ContainerInstance containerInstance, String JavaDoc imageId) {
62         return containerInstance.getServiceUri(this, URL_PARAMETERS, new String JavaDoc[]{imageId});
63     }
64
65     /**
66      * Renders the specified image to the given connection.
67      * Implementations should set the response content type, and write image
68      * data to the response <code>OutputStream</code>.
69      *
70      * @param conn the <code>Connection</code> on which to render the image
71      * @param imageReference the image to be rendered
72      * @throws IOException if the image cannot be rendered
73      */

74     public abstract void renderImage(Connection conn, ImageReference imageReference)
75     throws IOException JavaDoc;
76     
77     /**
78      * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
79      */

80     public void service(Connection conn)
81     throws IOException JavaDoc {
82         ContainerInstance containerInstance = (ContainerInstance) conn.getUserInstance();
83         if (containerInstance == null) {
84             serviceBadRequest(conn, "No container available.");
85             return;
86         }
87         String JavaDoc 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 JavaDoc 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