/**
* Project Wonderland
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
*
* Redistributions in source code form must reproduce the above
* copyright and this condition.
*
* The contents of this file are subject to the GNU General Public
* License, Version 2 (the "License"); you may not use this file
* except in compliance with the License. A copy of the License is
* available at http://www.opensource.org/licenses/gpl-license.php.
*
* Sun designates this particular file as subject to the "Classpath"
* exception as provided by Sun in the License file that accompanied
* this code.
*/
package org.jdesktop.wonderland.modules.service.resources;
import java.io.StringWriter;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.jdesktop.wonderland.modules.Module;
import org.jdesktop.wonderland.common.modules.ModuleInfo;
import org.jdesktop.wonderland.modules.service.ModuleManager;
/**
* The ModuleInfoResource class is a Jersey RESTful service that returns the
* basic information about a module given its name encoding into a request
* URI. The getModuleInfo() method handles the HTTP GET request.
* <p>
* @author Jordan Slott <jslott@dev.java.net>
*/
@Path("/{modulename}/info")
public class ModuleInfoResource {
/**
* Returns the basic information about a module, given its module name
* encoded into the URI. The format of the URI is:
* <p>
* /module/{modulename}/info
* <p>
* where {modulename} is the name of the module. All spaces in the module
* name must be encoded to %20. Returns BAD_REQUEST to the HTTP connection if
* the module name is invalid or if there was an error encoding the module's
* information.
*
* @param moduleName The unique name of the module
* @return An XML encoding of the module's basic information
*/
@GET
@Produces("text/plain")
public Response getModuleInfo(@PathParam("modulename") String moduleName) {
/* Fetch thhe error logger for use in this method */
Logger logger = ModuleManager.getLogger();
/* Fetch the module from the module manager */
ModuleManager manager = ModuleManager.getModuleManager();
Module module = manager.getInstalledModules().get(moduleName);
if (module == null) {
/* Log an error and return an error response */
logger.warning("ModuleManager: unable to locate module " + moduleName);
ResponseBuilder rb = Response.status(Response.Status.BAD_REQUEST);
return rb.build();
}
/* Check to see that the module info exists -- it's really bad if it doesn't */
ModuleInfo moduleInfo = module.getInfo();
if (moduleInfo == null) {
/* Log an error and return an error response */
logger.warning("ModuleManager: unable to locate module info: " + moduleName);
ResponseBuilder rb = Response.status(Response.Status.BAD_REQUEST);
return rb.build();
}
/* Write the XML encoding to a writer and return it */
StringWriter sw = new StringWriter();
try {
moduleInfo.encode(sw);
ResponseBuilder rb = Response.ok(sw.toString());
return rb.build();
} catch (javax.xml.bind.JAXBException excp) {
/* Log an error and return an error response */
logger.warning("ModuleManager: unable to encode module info " + moduleName);
ResponseBuilder rb = Response.status(Response.Status.BAD_REQUEST);
return rb.build();
}
}
}