org.faceless.graph2.tag
Interface RequestHandler


public interface RequestHandler

A RequestHandler is used to process a request delivererd to the GraphServlet. These requests take the form of an InputSource (read from the servlet request), and are converted to a Graph by the process method.

Handlers are supplied to handle SOAP requests and raw XML. They are added to the tag library in the same way as EmbedderFactory, except the service file is META-INF/services/org.faceless.graph2.tag.RequestHandlers

Since:
2.4

Method Summary
 boolean matches(String type)
          Return true if this RequestHandler matches the specified type of request.
 void process(String type, String imagepath, GraphContext context, InputSource source, HttpServletRequest req, HttpServletResponse res)
          Process the input and convert it to a Graph.
 

Method Detail

matches

boolean matches(String type)
Return true if this RequestHandler matches the specified type of request. For instance, requesting servlet/GraphServlet/myhandler will match this RequestHandler if this method returns "myhandler".equals(type)


process

void process(String type,
             String imagepath,
             GraphContext context,
             InputSource source,
             HttpServletRequest req,
             HttpServletResponse res)
             throws IOException,
                    SAXException
Process the input and convert it to a Graph. The Input (normally XML) should be parsed and converted to a Graph using the EmbeddedXMLGraph class and an appropriate Embedder. The generated Graph can be returned immediately, or it can be stored in the GraphContext and a link returned to it for later retrieval.

Here's a simple example showing how to return a graph directly from this request

 EmbeddedXMLGraph xml = new EmbeddedXMLGraph();
 xml.parse(source);
 String format = xml.getRequestedFormat();
 Embedder embedder = context.getEmbedderFactory(format).newEmbedder(format);

 ByteArrayOutputStream bout = new ByteArrayOutputStream();
 xml.doEmbed(embedder, graphid, "", null, new StringWriter(), bout);
 bout.close();
 res.setContentType(embedder.getMIMEType());
 res.setContentLength(bout.size());
 bout.writeTo(res.getOutputStream());
 
and here's an example showing how to store a generated image for later retrieval. Note that we append the pageid (as returned from GraphContext.nextPageId()) and a slash to the image path.
 EmbeddedXMLGraph xml = new EmbeddedXMLGraph();
 xml.parse(source);
 String format = xml.getRequestedFormat();
 Embedder embedder = context.getEmbedderFactory(format).newEmbedder(format);

 String pageid = context.nextPageId();
 ByteArrayOutputStream bout = new ByteArrayOutputStream();

 res.setContentType("text/html; charset="UTF-8");
 imagepath += pageid + "/";
 xml.doEmbed(embedder, "anything", imagepath, null, res.getWriter(), bout);
 bout.close();
 context.putFile(pageid, embedder.getId(), bout.toByteArray(), embedder.getMIMEType());
 

Parameters:
type - the type, as matched by the matches(java.lang.String) method
imagepath - the base URL of the Servlet that will be returning any files generated by this method.
context - the GraphContext object, which may be used to store any generated files
source - the InputSource containing the input
req - the HttpServletRequest, available if any additional information needs to be read
res - the HttpServletResponse, which must be written to to return the necessary data to the browser
Throws:
IOException
SAXException


Copyright © 2001-2011 Big Faceless Organization