001: package com.quadcap.app.dbimage;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042: import java.io.IOException;
043:
044: import java.sql.Connection;
045: import java.sql.SQLException;
046:
047: import javax.servlet.RequestDispatcher;
048: import javax.servlet.ServletConfig;
049: import javax.servlet.ServletContext;
050: import javax.servlet.ServletException;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054:
055: /**
056: * Implementation of the 'load' action. We load files into the database
057: * from a directory specified in the request. When we're done, we forward
058: * to either the "error" or the "show" page.
059: *
060: * @author Stan Bailes
061: */
062: public class ActionLoad implements Action {
063: ServletConfig config;
064:
065: /**
066: * We've hard-coded the names of the result JSP pages; if we wanted
067: * more flexibility, we could make these configuration parameters.
068: */
069: static final String errorPage = "/error.jsp";
070: static final String successPage = "/show.jsp";
071:
072: public ActionLoad() {
073: }
074:
075: public void init(ServletConfig config) {
076: this .config = config;
077: }
078:
079: public void service(HttpServletRequest request,
080: HttpServletResponse response) throws Exception {
081: String root = request.getParameter("root");
082: if (root == null || root.length() == 0) {
083: request.setAttribute("error-message",
084: "no 'root' parameter specified");
085: forward(request, response, errorPage);
086: } else {
087: File f = new File(root);
088:
089: if (!f.exists()) {
090: request.setAttribute("error-message", "Can't read: "
091: + root);
092: forward(request, response, errorPage);
093: return;
094: }
095:
096: DbImageLoader loader = new DbImageLoader();
097: try {
098: Connection conn = getConnection();
099: try {
100: loader.loadImages(conn, root);
101: } finally {
102: conn.close();
103: }
104: forward(request, response, successPage);
105: } catch (Throwable th) {
106: request.setAttribute("exception", th);
107: forward(request, response, errorPage);
108: }
109: }
110: }
111:
112: public Connection getConnection() throws SQLException {
113: ServletContext application = config.getServletContext();
114: DbImageServlet s = (DbImageServlet) application
115: .getAttribute("servlet");
116: return s.getConnection();
117: }
118:
119: public void forward(HttpServletRequest request,
120: HttpServletResponse response, String page)
121: throws ServletException, IOException {
122: ServletContext context = config.getServletContext();
123: RequestDispatcher rd = context.getRequestDispatcher(page);
124: rd.forward(request, response);
125: }
126: }
|