📄 velocityflowdispatcherservlet.java
字号:
/*
* Copyright (c) 2003-2004, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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 net.orthanc.flow4j.runtime.connectors.velocity;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.orthanc.flow4j.runtime.Flow4JRuntimeConsts;
import net.orthanc.flow4j.runtime.Flow4JRuntimeException;
import net.orthanc.flow4j.runtime.FlowDictionary;
import net.orthanc.flow4j.runtime.FlowManager;
import net.orthanc.flow4j.runtime.web.Flow4JRuntimeWebConsts;
import net.orthanc.flow4j.runtime.web.Flow4JRuntimeWebUtils;
import net.orthanc.flow4j.runtime.web.WebFlowDictionary;
import net.orthanc.flow4j.runtime.web.servlet.Flow4JServletException;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
/**
* This servlet dispatches to a flow and retreives the
* velocity template name from the FlowDictionary.
* @author greifa
*/
public class VelocityFlowDispatcherServlet extends VelocityServlet {
/**
* Initializes the servlet. Registers the flows and sets some init params.
* The flow registry's classname is supported by the servlet init parameter
* <code>flow-repository-class</code>. If a flow registry is supported, then
* it is used to register the flows at the flowManager.
* @see javax.servlet.GenericServlet#init()
*/
public void init() throws ServletException {
super.init();
try {
// init servlet path
String servletPath = getInitParameter(Flow4JRuntimeWebConsts.INIT_PARAM_SERVLET_PATH);
String flowRepositoryClassName =
getInitParameter(Flow4JRuntimeWebConsts.INIT_PARAM_FLOW_REPOSITORY_CLASS);
Flow4JRuntimeWebUtils.registerFlowsFromRepository(servletPath, flowRepositoryClassName);
} catch (InstantiationException e) {
e.printStackTrace();
throw new ServletException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new ServletException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ServletException(e);
} catch (Throwable e) {
e.printStackTrace();
throw new ServletException(e);
}
}
/**
* Main routine to handle a request. Called by
* VelocityServlet.
* Triggers the specified flow.
* @param ctx a Velocity Context object to be filled with
* data. Will be used for rendering this
* template
* @return Template to be used for request
* @see org.apache.velocity.servlet.VelocityServlet#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
*/
protected Template handleRequest(
HttpServletRequest req,
HttpServletResponse res,
Context ctx)
throws Exception {
// ceate dictionary
WebFlowDictionary dictionary = new VelocityWebFlowDictionary();
String flowStart = getFlowStart(req);
// execute flow
preFlowExecution(req, res, ctx, dictionary);
executeFlow(req, res, ctx, flowStart, dictionary);
postFlowExecution(req, res, ctx, dictionary);
return getTemplate(
dictionary.getString(Flow4JRuntimeConsts.DICT_KEY_TEMPLATE_NAME));
}
/**
* Returns the the flow name and the start flowet's name.
* The format is like "MyFlow-Start".
* @param req the user request
* @return the flow name and the start flowet's name.
* @throws Flow4JServletException if the names cannot be determined
*/
protected String getFlowStart(HttpServletRequest req)
throws Flow4JServletException {
try {
return Flow4JRuntimeWebUtils.getFlowStartDesc(req);
} catch (Flow4JRuntimeException e) {
throw new Flow4JServletException(
"Could not determine the flow and it's start node",
e);
}
}
/**
* Fill the dictionary and put the velocity context into it.
* @param req the user request
* @param res the user response
* @param ctx the velocity context
* @param dictionary the dictionary to fill
* @throws Flow4JServletException if errors ocure
*/
protected void preFlowExecution(
HttpServletRequest req,
HttpServletResponse res,
Context ctx,
WebFlowDictionary dictionary)
throws Flow4JServletException {
Flow4JRuntimeWebUtils.transferParameters(req, res, this, dictionary);
dictionary.put(Flow4JVelocityConsts.DICT_KEY_VELOCITY_CONTEXT, ctx);
}
/**
* Transfers dictionary contents to the <code>HttpServletRequest</code>
* @param req the user request
* @param res the user response
* @param ctx the velocity context
* @param dictionary the dictionary to fill
* @throws Flow4JServletException if errors ocure
*/
protected void postFlowExecution(
HttpServletRequest req,
HttpServletResponse res,
Context ctx,
WebFlowDictionary dictionary)
throws Flow4JServletException {
Flow4JRuntimeWebUtils.transferParameters(dictionary, req);
}
/**
* This method only executes the flow.
* Can be overridden to use a wrapper like a profilig tool
* around the flow execution.
* @param req the user request
* @param res the user response
* @param ctx the velocity context
* @param flowStart
* @param dictionary the dictionary to fill
*/
protected void executeFlow(
HttpServletRequest req,
HttpServletResponse res,
Context ctx,
String flowStart,
FlowDictionary dictionary) {
FlowManager.executeFlow(flowStart, dictionary);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -