📄 viewmetadataservlet.java
字号:
package com.esri.solutions.jitk.common.csw;
import com.esri.solutions.jitk.web.tasks.search.csw.CswSearchTaskBean;
import org.apache.log4j.Logger;
import java.io.IOException;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This class handles client requests to view metadata document
* via HTML or XML by extending {@link HttpServlet}.
*/
@SuppressWarnings("serial")
public class ViewMetadataServlet extends HttpServlet {
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger LOG = Logger.getLogger(ViewMetadataServlet.class);
/*
* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ViewMetadataHelper helper = null;
String metadataGuid = null;
String catalogId = null;
String htmlContent = null;
String outputType = null;
String taskBeanName = null;
CswSearchTaskBean taskBean = null;
String taskBeanInitParamName = "TaskBeanName";
FacesContext fc = null;
taskBeanName = getServletConfig().getInitParameter(taskBeanInitParamName);
if (taskBeanName == null) {
resp.getWriter()
.write(generateErrorHtml(
"An error has occurred viewing metadata document. Possible configuration problem."));
LOG.debug("Missing servlet initialization parameter '" +
taskBeanInitParamName + "' for servlet: " +
getServletConfig().getServletName() +
" - cannot view metadata document");
resp.getWriter().flush();
resp.getWriter().close();
return;
}
fc = getFacesContext(req, resp);
if (!(fc.getApplication().getVariableResolver()
.resolveVariable(fc, taskBeanName) instanceof CswSearchTaskBean)) {
resp.getWriter()
.write(generateErrorHtml(
"An error has occurred viewing metadata document. Possible configuration problem."));
LOG.debug("CS/W task bean is not of type '" +
CswSearchTaskBean.class.getName() +
"' - cannot view metadata document");
resp.getWriter().flush();
resp.getWriter().close();
return;
}
taskBean = (CswSearchTaskBean) fc.getApplication().getVariableResolver()
.resolveVariable(fc, taskBeanName);
helper = taskBean.getViewMetadataHelper();
metadataGuid = req.getParameter("guid");
catalogId = req.getParameter("catalogId");
if ((req.getParameter("output") == null) ||
(req.getParameter("output").length() <= 0)) {
outputType = ViewMetadataHelper.OUTPUT_TYPE_HTML;
} else if (req.getParameter("output")
.equalsIgnoreCase(ViewMetadataHelper.OUTPUT_TYPE_XML)) {
outputType = ViewMetadataHelper.OUTPUT_TYPE_XML;
} else {
outputType = ViewMetadataHelper.OUTPUT_TYPE_HTML;
}
// replace the placeholder for with the '&'
catalogId = catalogId.replace("*/", "&");
if ((metadataGuid == null) || (metadataGuid.length() <= 0)) {
LOG.warn("Invalid Metadata GUID");
htmlContent = generateErrorHtml("Invalid Metadata document GUID.");
}
if ((catalogId == null) || (catalogId.length() <= 0)) {
LOG.warn("Invalid Catalog ID");
htmlContent = generateErrorHtml("Invalid Catalog Service ID.");
}
// if the html content is not null then no error has occurred
if (htmlContent == null) {
try {
htmlContent = helper.generateHtmlForMetadataDoc(metadataGuid,
catalogId, outputType);
} catch (Exception ex) {
LOG.warn(
"An exception occurred viewing metadata document with ID: [" +
metadataGuid + "] on catalog service: [" + catalogId + "]",
ex);
htmlContent = generateErrorHtml(
"An error has occurred trying to view Metadata document with GUID " +
metadataGuid + "on Catalog Service " + catalogId);
}
}
if (htmlContent == null) {
htmlContent = generateErrorHtml(
"An error has occurred trying to view Metadata document with GUID " +
metadataGuid + "on Catalog Service " + catalogId);
}
resp.getWriter().write(htmlContent);
resp.getWriter().flush();
resp.getWriter().close();
}
/**
* Utility method to generate an HTML message that can be
* sent back to the client.
*
* @param message {@link String} message.
* @return HTML message that can be viewed in a browser.
*/
private String generateErrorHtml(String message) {
StringBuilder errorHtml = null;
errorHtml = new StringBuilder();
errorHtml.append("<html>");
errorHtml.append("<head>");
errorHtml.append("<title>");
errorHtml.append("Error Viewing Metadata Document");
errorHtml.append("</title>");
errorHtml.append("</head>");
errorHtml.append("<body>");
errorHtml.append(message);
errorHtml.append("</body>");
errorHtml.append("</html>");
return errorHtml.toString();
}
/**
* Utility method to get the {@link FacesContext} object via
* the {@link FacesContextFactory#getFacesContext(Object, Object, Object, Lifecycle)}.
*
* @param request Reference to the {@link HttpServletRequest} needed for getting the
* {@link FacesContext}.
* @param response Reference to the {@link HttpServletResponse} needed for getting the
* {@link FacesContext}.
* @return {@link FacesContext} reference.
*/
private FacesContext getFacesContext(HttpServletRequest request,
HttpServletResponse response) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
facesContext = contextFactory.getFacesContext(getServletContext(),
request, response, lifecycle);
}
return facesContext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -