/*--
Copyright (C) 2001 Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "Java and XML" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact brett@newInstance.com.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed for the
'Java and XML' book, by Brett McLaughlin (O'Reilly & Associates)."
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
package javaxml2;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Castor classes
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
// Castor generated classes
import javaxml2.castor.Catalog;
import javaxml2.castor.Item;
import javaxml2.castor.types.LevelType;
/**
* <p>
* <code>AddItemServlet</code> is a servlet that takes in new items
* and adds them to it's file-based video catalog.
* </p>
*/
public class AddItemServlet extends HttpServlet {
/** The XML catalog file to use */
private static final String CATALOG_FILE =
"c:\\java\\tomcat\\webapps\\javaxml2\\catalog.xml";
/**
* <p>
* This accepts information about a new item and adds it to the
* internal file-based catalog, using JAXB.
* </p>
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
// Get input parameters
String id = req.getParameterValues("id")[0];
String levelString = req.getParameterValues("level")[0];
String title = req.getParameterValues("title")[0];
String teacher = req.getParameterValues("teacher")[0];
String[] guests = req.getParameterValues("guest");
String description = req.getParameterValues("description")[0];
// Create new item
Item item = new Item();
item.setId(id);
item.setLevel(LevelType.valueOf(levelString));
item.setTitle(title);
item.setTeacher(teacher);
if (guests != null) {
for (int i=0; i<guests.length; i++) {
if (!guests[i].trim().equals("")) {
item.addGuest(guests[i]);
}
}
}
item.setDescription(description);
try {
// Load current catalog
File catalogFile = new File(CATALOG_FILE);
FileReader reader = new FileReader(catalogFile);
Catalog catalog =
(Catalog)Unmarshaller.unmarshal(Catalog.class, reader);
// Add item
catalog.addItem(item);
// Write back out modified catalog
FileWriter writer = new FileWriter(catalogFile);
Marshaller.marshal(catalog, writer);
out.println("Item added.");
} catch (Exception e) {
out.println("Error loading/writing catalog: " + e.getMessage());
} finally {
out.close();
}
}
/**
* <p>
* Accept GET requests and process through the
* <code>{@link #doPost}</code> method.
* </p>
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req, res);
}
} |