📄 maptemplatepreviewservlet.java
字号:
package com.esri.solutions.jitk.common.templates.map;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.el.ValueBinding;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.media.jai.JAI;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Generates the Map Template Preview for a specified Map Template. This servlet needs
* to be configured with path-style URL mappings and not extension-style mappings. This
* servlet expects the URL to be configured like the following:
*
* <code>http://host/Viewer/maptemplate/<Map Template Repo Bean Name>/<Map Template ID>.<format> where
*
* <ul>
* <li>Map Template Repo Bean Name is the name of the Map Template Repository bean name within the
* bean framework.</li>
* <li>Map Template ID is the ID of the Map Template</li>
* <li>format is the image format, jpg or png.
* </ul>
*
* In this example, this servlet would need to be mapped to /maptemplate/* URL patterns.
*
* <p>
* This servlet will send a 404 (Not Found) response if any of the conditions are met:
* <ul>
* <li>Map Template Repository can't be found.
* <li>Map Template with the specified ID can't be found.
* </ul>
* </p>
*/
public class MapTemplatePreviewServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private static final Logger LOG = LogManager.getLogger(MapTemplatePreviewServlet.class);
private static final String ERROR_CANNOT_FIND_TEMPLATE = "Map Template Repository [{0}] does not contain Map Template [{1}]";
private static final String ERROR_CANNOT_FIND_REPO = "Cannot find Map Template Repository [{0}]";
private static final String WARN_UNRECOGNIZABLE_CONTENT_TYPE = "Unrecognizable Content Type, using default {0}";
private static final String WARN_UNSUPPORTED_ENCODING = "{0} is an unsupported encoding, using default {1}";
private static final String INFO_MAP_TEMPLATE_ID = "Map Template ID: {0}";
private static final String INFO_MAP_TEMPLATE_ID_WITH_EXT = "Map Template ID with Extension: {0}";
private static final String INFO_MAP_TEMPLATE_REPO = "Map Template Repository: {0}";
private static final String INFO_EXTENSION = "Extension: {0}";
private static final String DEFAULT_EXT = "jpg";
private static final String DEFAULT_CONTENT_TYPE = "image/jpg";
private static final String SUPPORTED_ENCODINGS = "jpg,png";
private static final long serialVersionUID = -3637754480378538291L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File pathInfo = new File(request.getPathInfo());
String repoName = pathInfo.getParent().replace(File.separator, "");
String idWithExt = pathInfo.getName().replace(repoName, "");
String id = idWithExt.substring(0, idWithExt.lastIndexOf("."));
String ext = idWithExt.substring(idWithExt.lastIndexOf(".") + 1);
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_MAP_TEMPLATE_REPO, repoName));
LOG.info(MessageFormat.format(INFO_MAP_TEMPLATE_ID_WITH_EXT, idWithExt));
LOG.info(MessageFormat.format(INFO_MAP_TEMPLATE_ID, id));
LOG.info(MessageFormat.format(INFO_EXTENSION, ext));
}
IMapTemplateRepository repo = getMapTemplateRepository(repoName, request, response);
if (repo == null) {
LOG.error(MessageFormat.format(ERROR_CANNOT_FIND_REPO, repoName));
log(MessageFormat.format(ERROR_CANNOT_FIND_REPO, repoName));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
IMapTemplate template = repo.getMapTemplate(id);
if (template == null) {
LOG.error(MessageFormat.format(ERROR_CANNOT_FIND_TEMPLATE, repoName, id));
log(MessageFormat.format(ERROR_CANNOT_FIND_TEMPLATE, repoName, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!SUPPORTED_ENCODINGS.contains(ext)) {
LOG.warn(MessageFormat.format(WARN_UNSUPPORTED_ENCODING, ext, DEFAULT_EXT));
ext = DEFAULT_EXT;
idWithExt = id + "." + DEFAULT_EXT;
}
String contentType = getServletContext().getMimeType(idWithExt);
if (contentType == null) {
LOG.warn(MessageFormat.format(WARN_UNRECOGNIZABLE_CONTENT_TYPE, DEFAULT_CONTENT_TYPE));
contentType = DEFAULT_CONTENT_TYPE;
ext = DEFAULT_EXT;
}
RenderedImage image = template.getPreview();
response.setContentType(contentType);
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add(response.getOutputStream());
pb.add(getJAIEncodingType(ext));
JAI.create("encode", pb);
}
/**
* Returns the Map Template Repository with the specified name in the
* Faces subsystem.
*
* @param repoName Name of Map Template Repository bean.
* @param request Reference to HTTP Request object.
* @param response Reference to HTTP Response object.
* @return Reference to Map Template Repository.
*/
private IMapTemplateRepository getMapTemplateRepository(String repoName,
HttpServletRequest request, HttpServletResponse response) {
FacesContext fc = getFacesContext(request, response);
ValueBinding vb = fc.getApplication().createValueBinding("#{" + repoName + "}");
IMapTemplateRepository repo = (IMapTemplateRepository) vb.getValue(fc);
return repo;
}
/**
* Helper method to retrieve the {@link FacesContext} object for the
* HTTP request. The FacesContext is used to get references to
* managed beans defined in the Faces subsystem.
*
* @param request Reference to HTTP Request object
* @param response Reference to HTTP Response object.
* @return Reference to Faces Context object.
*/
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(this.getServletContext(), request, response, lifecycle);
}
return facesContext;
}
/**
* Helper method to convert between an image file extension to the
* proper encoding type for JAI. Most notably, it converts "jpg" to
* "jpeg".
*
* @param ext Image file extension to convert to JAI encoding type.
* @return JAI Encoding Type for image file extension.
*/
private String getJAIEncodingType (String ext) {
if (ext.equalsIgnoreCase("jpg")) {
return "jpeg";
}
return ext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -