📄 flowdispatcherservlet.java
字号:
/*
* Copyright (c) 2003, 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.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
/**
* The FlowDispatcherServlet executes the specified flow that is given in the url
* path info. Before execution all request parameters are transformed to the new
* dictionary and afterwords the parameters are transformed from the dictionary
* back to the request.
* The url to call the servlet is of the form:
* <pre>
* http://server:port/context/flow/SomeFlow-Start?arg1=1&...
* </pre>
* <code>context</code> is the context of the webapplication<br>
* <code>flow</code> is mapping to the FlowDispatcherServlet or an overwridden servlet<br>
* <code>SomeFlow-Start</code> is the name of the specific flow and its start flowlet,
* separated by a hyphen.
* @author agreif
*/
public class FlowDispatcherServlet extends HttpServlet {
/**
* 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);
}
}
/**
* Handle the get-request
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGetOrPost(req, res);
}
/**
* Handle the post-request
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGetOrPost(req, res);
}
/**
* Dispatches to the specific flow and executes it.
* Creates a WebFlowDictionary that also looks in the request and the session
* scope.
* Transfers all request parameters to the dictionary
* before call and transfers them back after flow execution.
*/
protected void doGetOrPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// ceate dictionary
WebFlowDictionary dictionary = new WebFlowDictionary();
String flowStart = getFlowStart(req);
// execute flow
preFlowExecution(req, res, dictionary);
executeFlow(req, res, flowStart, dictionary);
postFlowExecution(req, res, dictionary);
}
/**
* 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.
* @param req the user request
* @param res the user response
* @param dictionary the dictionary to fill
* @throws Flow4JServletException if errors ocure
*/
protected void preFlowExecution(HttpServletRequest req, HttpServletResponse res, WebFlowDictionary dictionary) throws Flow4JServletException {
Flow4JRuntimeWebUtils.transferParameters(req, res, this, dictionary);
}
/**
* Transfers dictionary contents to the <code>HttpServletRequest</code>
* @param req the user request
* @param res the user response
* @param dictionary the dictionary to fill
* @throws Flow4JServletException if errors ocure
*/
protected void postFlowExecution(HttpServletRequest req, HttpServletResponse res, 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
* @param res
* @param flowStart
* @param dictionary
*/
protected void executeFlow(
HttpServletRequest req,
HttpServletResponse res,
String flowStart,
FlowDictionary dictionary) {
FlowManager.executeFlow(flowStart, dictionary);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -