001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.image;
031:
032: import java.io.IOException;
033:
034: import javax.servlet.http.HttpServletResponse;
035:
036: import nextapp.echo2.app.ImageReference;
037: import nextapp.echo2.webcontainer.ContainerInstance;
038: import nextapp.echo2.webrender.Connection;
039: import nextapp.echo2.webrender.ContentType;
040: import nextapp.echo2.webrender.Service;
041:
042: /**
043: * Abstract base service for rendering images sourced from the application
044: * container.
045: */
046: public abstract class AbstractImageService implements Service {
047:
048: private static final String PARAMETER_IMAGE_UID = "imageuid";
049:
050: private static final String[] URL_PARAMETERS = new String[] { PARAMETER_IMAGE_UID };
051:
052: /**
053: * Creates a URI to retrieve a specific image for a specific component
054: * from the server.
055: *
056: * @param containerInstance the relevant application container instance.
057: * @param imageId the unique id to retrieve the image from the
058: * <code>ContainerInstance</code>
059: */
060: public String createUri(ContainerInstance containerInstance,
061: String imageId) {
062: return containerInstance.getServiceUri(this , URL_PARAMETERS,
063: new String[] { imageId });
064: }
065:
066: /**
067: * Renders the specified image to the given connection.
068: * Implementations should set the response content type, and write image
069: * data to the response <code>OutputStream</code>.
070: *
071: * @param conn the <code>Connection</code> on which to render the image
072: * @param imageReference the image to be rendered
073: * @throws IOException if the image cannot be rendered
074: */
075: public abstract void renderImage(Connection conn,
076: ImageReference imageReference) throws IOException;
077:
078: /**
079: * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
080: */
081: public void service(Connection conn) throws IOException {
082: ContainerInstance containerInstance = (ContainerInstance) conn
083: .getUserInstance();
084: if (containerInstance == null) {
085: serviceBadRequest(conn, "No container available.");
086: return;
087: }
088: String imageId = conn.getRequest().getParameter(
089: PARAMETER_IMAGE_UID);
090: if (imageId == null) {
091: serviceBadRequest(conn, "Image UID not specified.");
092: return;
093: }
094: ImageReference imageReference = (ImageReference) containerInstance
095: .getIdTable().getObject(imageId);
096:
097: if (imageReference == null) {
098: serviceBadRequest(conn, "Image UID is not valid.");
099: return;
100: }
101: renderImage(conn, imageReference);
102: }
103:
104: public void serviceBadRequest(Connection conn, String message) {
105: conn.getResponse()
106: .setStatus(HttpServletResponse.SC_BAD_REQUEST);
107: conn.setContentType(ContentType.TEXT_PLAIN);
108: conn.getWriter().write(message);
109: }
110: }
|