001 /*
002 * This file is part of the Echo Point Project. This project is a
003 * collection of Components that have extended the Echo Web Application
004 * Framework Version 3.
005 *
006 * Version: MPL 1.1
007 *
008 * The contents of this file are subject to the Mozilla Public License Version
009 * 1.1 (the "License"); you may not use this file except in compliance with
010 * the License. You may obtain a copy of the License at
011 * http://www.mozilla.org/MPL/
012 *
013 * Software distributed under the License is distributed on an "AS IS" basis,
014 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015 * for the specific language governing rights and limitations under the
016 * License.
017 */
018 package echopoint.tucana;
019
020 import nextapp.echo.webcontainer.Connection;
021 import nextapp.echo.webcontainer.ContentType;
022 import nextapp.echo.webcontainer.Service;
023 import nextapp.echo.webcontainer.UserInstance;
024
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027 import java.io.IOException;
028
029 /**
030 * Base service for file uploads.
031 *
032 * @author Echo File Transfer Library
033 * @version $Id: BaseUploadService.java 90 2008-11-11 01:41:05Z sptrakesh $
034 */
035 public abstract class BaseUploadService implements Service
036 {
037 /**
038 * Validates if the request contains a valid FileUploadSelector render id
039 * and index.
040 *
041 * @see Service#service(Connection)
042 */
043 public void service( Connection conn ) throws IOException
044 {
045 UserInstance userInstance = conn.getUserInstance();
046 if ( userInstance == null )
047 {
048 serviceBadRequest( conn, "No user instance available." );
049 return;
050 }
051 HttpServletRequest request = conn.getRequest();
052 String renderId = request.getParameter( "i" );
053 if ( renderId == null )
054 {
055 serviceBadRequest( conn, "FileUploadSelector id not specified." );
056 return;
057 }
058 FileUploadSelector uploadSelect = (FileUploadSelector)
059 userInstance.getComponentByClientRenderId( renderId );
060 if ( uploadSelect == null )
061 {
062 serviceBadRequest( conn,
063 "FileUploadSelector id is not valid: " + renderId );
064 return;
065 }
066 String uploadIndexParam = request.getParameter( "x" );
067 if ( uploadIndexParam == null )
068 {
069 serviceBadRequest( conn,
070 "FileUploadSelector upload index not specified." );
071 return;
072 }
073 service( conn, uploadSelect, uploadIndexParam );
074 }
075
076 /**
077 * Performs the actual service of the request.
078 *
079 * @param conn The connection to the application.
080 * @param uploadSelect The file upload selector instance.
081 * @param uploadIndex The unique index of the current upload for the instance.
082 * @throws java.io.IOException If errors are encountered.
083 */
084 public abstract void service( final Connection conn,
085 final FileUploadSelector uploadSelect, final String uploadIndex )
086 throws IOException;
087
088 /**
089 * Serves a bad request message.
090 *
091 * @param conn The connection to the application.
092 * @param message The message to display for the bad request.
093 */
094 protected static void serviceBadRequest( final Connection conn,
095 final String message )
096 {
097 conn.getResponse().setStatus( HttpServletResponse.SC_BAD_REQUEST );
098 conn.setContentType( ContentType.TEXT_PLAIN );
099 conn.getWriter().write( message );
100 }
101 }