📄 flow4jruntimewebutils.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.web;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import javax.servlet.GenericServlet;
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.IFlowRepository;
/**
* Class Flow4JRuntimeWebUtils TODO
* @author agreif
*
*/
public class Flow4JRuntimeWebUtils {
private static String servletPath;
/**
*
* TODO
* @param path
*/
static public void setServletPath(String path) {
servletPath = path;
}
/**
* Returns the servlet path that was supported by the init parameters.
* It is needed by the flow4j:link tag.
* @return
*/
public static String getServletPath() {
return servletPath;
}
/** Returns the flow's and the startnode's name in the form
* <code>flowname-startflowletname</code>
*/
static public String getFlowStartDesc(HttpServletRequest req)
throws Flow4JRuntimeException
{
String pathInfo = req.getPathInfo();
if (pathInfo == null)
throw new Flow4JRuntimeException("no flow and entry specified (pathInfo: null)");
StringTokenizer tokens = new StringTokenizer(pathInfo,"/");
if (tokens.hasMoreTokens())
return tokens.nextToken();
throw new Flow4JRuntimeException("no flow and entry specified (pathInfo: " + pathInfo);
}
/**
* Transfers request parameters into the dictionary:
* the request itself, the session if exists, the response and the servlet
* to the dictionary
*/
static public void transferParameters(
HttpServletRequest req,
HttpServletResponse res,
GenericServlet servlet,
FlowDictionary dictionary) {
for (Enumeration enum = req.getParameterNames();
enum.hasMoreElements();
) {
String name = (String) enum.nextElement();
String[] value = req.getParameterValues(name);
if (value.length == 1)
dictionary.put(name, value[0]);
else
dictionary.put(name, value);
}
dictionary.put(Flow4JRuntimeWebConsts.DICT_KEY_SERVLET_REQUEST, req);
dictionary.put(
Flow4JRuntimeWebConsts.DICT_KEY_HTTP_SESSION,
req.getSession(false));
dictionary.put(Flow4JRuntimeWebConsts.DICT_KEY_SERVLET_RESPONSE, res);
dictionary.put(Flow4JRuntimeWebConsts.DICT_KEY_SERVLET, servlet);
}
/**
* Transfers dictionary parameters as attributes to the request.
*/
static public void transferParameters(
FlowDictionary dictionary,
HttpServletRequest req) {
for (Iterator iter = dictionary.entrySet().iterator();
iter.hasNext();
) {
Map.Entry entry = (Map.Entry) iter.next();
req.setAttribute(entry.getKey().toString(), entry.getValue());
}
}
/**
* 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.
*/
static public void registerFlowsFromRepository(String servletPath, String flowRepositoryClassName) throws Exception {
servletPath =
servletPath == null ? Flow4JRuntimeWebConsts.DEFAULT_SERVLET_PATH : servletPath;
Flow4JRuntimeWebUtils.setServletPath(servletPath);
// register flows
if (flowRepositoryClassName != null) {
IFlowRepository flowRepository =
(IFlowRepository) Class
.forName(flowRepositoryClassName)
.newInstance();
FlowManager.registerFlows(flowRepository);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -