com.yworks.yfiles.server.tiles.servlet
Class LoaderServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bycom.yworks.yfiles.server.tiles.servlet.BaseServlet
              extended bycom.yworks.yfiles.server.tiles.servlet.LoaderServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class LoaderServlet
extends BaseServlet

This servlet loads graphs from server side files, caches them and allows the download of cached graphs. See handlePostRequest(HttpServletRequest,HttpServletResponse) for more details.

Subclasses can customize the behavior of this servlet by providing custom implementations of getGraphResourceStream(String), getIoHandler(String), customizeGraphMLIOHandler(GraphMLIOHandler, Graph2D) or loadableGraphs(HttpServletResponse).

See Also:
Serialized Form

Field Summary
protected  String INIT_PARAM_CONFIGURE_REALIZERS
           
protected  String LOADABLE_GRAPHS_LIST
          "/resources/graphs/loadable-graphs.json": resource containing a list of graphs, which can be loaded.
 
Fields inherited from class com.yworks.yfiles.server.tiles.servlet.BaseServlet
EDGE_DESCRIPTION_KEY, EDGE_URL_KEY, NODE_DESCRIPTION_KEY, NODE_URL_KEY
 
Constructor Summary
LoaderServlet()
           
 
Method Summary
protected  void customizeGraphMLIOHandler(GraphMLIOHandler ioh, Graph2D graph)
          customization hook for subclasses.
protected  InputStream getGraphResourceStream(String requestedPath)
          returns an input stream for reading a requested graph.
protected  IOHandler getIoHandler(String format)
          returns an IO handler suitable for handling the requested graph format.
protected  void handleGetRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          calls handlePostRequest(HttpServletRequest,HttpServletResponse).
protected  void handlePostRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           handles "downloadableFormats", "downloadGraph", "loadGraph" and "loadableGraphs" requests.
 void initialize()
          initializes this servlet.
protected  void loadableGraphs(javax.servlet.http.HttpServletResponse response)
           writes the symbolic names of the loadable graphs as a JSON string to the given response.
 
Methods inherited from class com.yworks.yfiles.server.tiles.servlet.BaseServlet
addViewConfigurator, cachedGraphs, cacheGraph, doGet, doPost, getEdgeDescription, getEdgeURL, getForId, getGraph, getGraphBounds, getGraphContentBounds, getId, getId, getId, getId, getNodeDescription, getNodeURL, getReadWriteLock, getSessionMonitor, getViewConfigurators, getWriter, hasDescriptions, hasURLs, init, isUseJSONPrefixing, lookupId, lookupId, lookupId, lookupId, newGraph, readLock, registerLicense, removeViewConfigurator, setEdgeDescription, setEdgeURL, setNodeDescription, setNodeURL, uncacheGraph, unlockRead, unlockWrite, updateEdgePaths, validateIds, writeContentBounds, writeLock, writeWorldBounds
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOADABLE_GRAPHS_LIST

protected String LOADABLE_GRAPHS_LIST
"/resources/graphs/loadable-graphs.json": resource containing a list of graphs, which can be loaded. The list is an array in commented JSON format. The resource is used by loadableGraphs(javax.servlet.http.HttpServletResponse).


INIT_PARAM_CONFIGURE_REALIZERS

protected final String INIT_PARAM_CONFIGURE_REALIZERS
See Also:
Constant Field Values
Constructor Detail

LoaderServlet

public LoaderServlet()
Method Detail

initialize

public void initialize()
                throws javax.servlet.ServletException
initializes this servlet.

Overrides:
initialize in class BaseServlet
Throws:
javax.servlet.ServletException

handlePostRequest

protected void handlePostRequest(javax.servlet.http.HttpServletRequest request,
                                 javax.servlet.http.HttpServletResponse response)
                          throws javax.servlet.ServletException,
                                 IOException

handles "downloadableFormats", "downloadGraph", "loadGraph" and "loadableGraphs" requests.

The "downloadableFormats" request returns a list of available formats for downloading a graph in JSON format.

The "downloadGraph" request has a mandatory parameter "path" and an optional parameter "format". The parameter "path" contains the name of the graph to be downloaded. The parameter "format" specifies the format for the download. It defaults to "ygf". Useful values are filename extensions of yFiles for Java graph formats; "ygf", "gml", "graphml", "graphmlz" and "tgf". Moreover some image formats are supported: "gif", "jpg", "png" and "bmp". The response for this request is a binary stream containing the graph in the requested format.

The "loadableGraphs" request takes no parameters and returns a list of file names of graphs from the "resources/graphs" directory in comment-filtered JSON format.

The "loadGraph" request has a mandatory parameter "path". If the value of the parameter "path" is "foo.ygf", and there is not yet a graph cached for the name "foo.ygf" in the session of the request, then the graph is loaded from the file "foo.ygf" from the directory "resources/graphs" in the web application, to which this servlet belongs, and cached using the name "foo.ygf". Reloading a graph which is already cached can be forced by setting the optional parameter "reload" to "true".

Subclasses can customize the behavior of this servlet by providing custom implementations of getIoHandler(String), customizeGraphMLIOHandler(GraphMLIOHandler, Graph2D), getGraphResourceStream(String) or loadableGraphs(HttpServletResponse).

Overrides:
handlePostRequest in class BaseServlet
Parameters:
request - the request
response - the response to write to, the status is set set to 200 on success and to 404 otherwise
Throws:
javax.servlet.ServletException
IOException

getIoHandler

protected IOHandler getIoHandler(String format)
returns an IO handler suitable for handling the requested graph format. This method is used for the "loadGraph" and "downloadGraph" requests. Subclasses can customize this method. The default format is YGF. A handler for YGF is returned, if the given format is null.

Parameters:
format - the requested format as a filename extension or null
Returns:
an instance of IOHandler suitable for the requested format or null

loadableGraphs

protected void loadableGraphs(javax.servlet.http.HttpServletResponse response)
                       throws IOException

writes the symbolic names of the loadable graphs as a JSON string to the given response. This method handles the "loadableGraphs" request. This implementation returns the list stored in LOADABLE_GRAPHS_LIST. Subclasses can customize this method.

The format of the string written to the response is like in the following example.

 /*[
   "aGraph.ygf",
   "anotherGraph.graphml"
 ]*/
 

Parameters:
response - the response to write to
Throws:
IOException - if there are problems reading input list or writing the response
See Also:
getGraphResourceStream(String)

customizeGraphMLIOHandler

protected void customizeGraphMLIOHandler(GraphMLIOHandler ioh,
                                         Graph2D graph)
customization hook for subclasses. Redefining this method in a subclass enables customizing a GraphMLIOHandler instance before it is used to read or write a given graph. This can be used for adding custom attributes to be read or written, which is not possible in the more general getIoHandler(String) method, since there is no access to the graph. The default implementation adds the string attributes "url" and "description" for nodes and edges, which are also supported by yEd and for which there is further support in the BaseServlet (e.g. BaseServlet.getNodeURL(Graph2D, Node)). It is safe not to call this implementation, if you overwrite this method in a subclass and the "url" and "description" attributes are not needed.

Parameters:
ioh - the handler to be used for reading or writing the graph
graph - the graph to be read or written, after this method has completed
See Also:
getIoHandler(String)

getGraphResourceStream

protected InputStream getGraphResourceStream(String requestedPath)
returns an input stream for reading a requested graph. This method is used in handling "loadGraph" requests, if the graph is not yet cached or should be reloaded. This implementation tries to open a resource in the /resources/graphs directory of the web application using ServletContext.getResourceAsStream(String).

Parameters:
requestedPath - the name for the graph resource
Returns:
an input stream for the requested graph resource or null

handleGetRequest

protected void handleGetRequest(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                         throws javax.servlet.ServletException,
                                IOException
calls handlePostRequest(HttpServletRequest,HttpServletResponse).

Overrides:
handleGetRequest in class BaseServlet
Parameters:
request - the request
response - the response to write to
Throws:
javax.servlet.ServletException
IOException


Copyright © 2006-2013 yWorks GmbH. All rights reserved