package org.webcat.core.webapi; import java.io.IOException; import java.io.Writer; import org.webcat.core.Application; import com.webobjects.appserver.WOApplication; import com.webobjects.foundation.NSMutableDictionary; //------------------------------------------------------------------------- /** * The base class for result formatters in the web API. Web-CAT currently * provides two fixed formatters: JSON ({@link JSONResponseFormatter}, the * default), and XML ({@link XmlResponseFormatter}). * * @author Tony Allevato * @author Last changed by $Author: aallowat $ * @version $Revision: 1.1 $, $Date: 2012/06/22 16:23:17 $ */ public abstract class ResponseFormatter { //~ Methods ............................................................... // ---------------------------------------------------------- /** * Takes the session identifier and the result of the action for * formatting. * * @param sessionId the session identifier * @param result the result of the action */ public void setResult(Object sessionId, Object result) { root = new NSMutableDictionary<String, Object>(); root.setObjectForKey(sessionId, SESSION_ID_KEY); if (result instanceof WebAPIError) { root.setObjectForKey(result, "error"); } else if (result != null) { root.setObjectForKey(result, "result"); } } // ---------------------------------------------------------- /** * Formats the stored result to the specified writer. * * @param writer the writer * @throws IOException if an I/O error occurs */ public void formatToWriter(Writer writer) throws IOException { format(root); } // ---------------------------------------------------------- /** * Gets the MIME type of the response generated by this formatter. * * @return the MIME type of the response */ public abstract String mimeType(); // ---------------------------------------------------------- /** * Subclasses should override this method to provide the logic for * formatting an object. * * @param object the object to format * @throws IOException if an I/O error occurs */ protected abstract void format(Object object) throws IOException; //~ Instance/static variables ............................................. private static final String SESSION_ID_KEY = "sessionId"; private NSMutableDictionary<String, Object> root; }