📄 webmappreviewservlet.java
字号:
package com.esri.solutions.jitk.web.tasks.mapcomp;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.esri.adf.web.data.MapFunctionality;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.WebSession;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.solutions.jitk.common.export.image.WebMapImageExporter;
/**
* Generates the Preview for a Map within a WebContext. 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/maps/current/<Web Context Name>.<format> where
*
* <ul>
* <li>Web Context Name is the name of the Web Context
* <li>format is the image encoding (jpg or png)
* </ul>
*
* In this example, this servlet would need to be mapped to /maps/current/* URL patterns.
*
* <p>
* This servlet also accepts the following optional parameters:
*
* <ul>
* <li>w: width
* <li>h: height
* <li>dpi: Dots Per Inch
* <li>r: ID of GIS Resource to draw.
* </ul>
* </p>
*
* <p>
* This servlet will send a 404 (Not Found) response if any of the conditions are met:
* <ul>
* <li>{@link HttpServletRequest#getPathInfo()} is null or zero-length
* <li>Path Info syntax is invalid.
* <li>No HTTP Session
* <li>{@link WebSession} cannot be found within HTTP Session
* <li>WebContext with specified name cannot be found within WebSession
* <li>WebMap within WebContext is null.
* <li>Any unexpected exceptions occur.
* </ul>
* </p>
*/
public class WebMapPreviewServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private static final int DEFAULT_HEIGHT = 120;
private static final int DEFAULT_WIDTH = 180;
private static final int DEFAULT_DPI = 96;
private static final String VALIDATE_PATH_INFO_RE = "^\\/(\\w[\\w ]*)+\\.(jpg|JPG|png|PNG)$";
private static final long serialVersionUID = 7869517597385668875L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.trim().length() == 0 || !pathInfo.toLowerCase().matches(VALIDATE_PATH_INFO_RE)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String contentType = this.getServletContext().getMimeType(pathInfo);
Pattern pattern = Pattern.compile(VALIDATE_PATH_INFO_RE);
Matcher matcher = pattern.matcher(pathInfo);
if (!matcher.matches()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String webCtxName = matcher.group(1);
String format = matcher.group(2);
String dpiParam = request.getParameter("dpi");
if (dpiParam == null) {
dpiParam = getServletConfig().getInitParameter("dpi");
}
int dpi = DEFAULT_DPI;
if (dpiParam != null) {
try {
dpi = Integer.parseInt(dpiParam);
} catch (NumberFormatException e) {
dpi = DEFAULT_DPI;
}
}
String widthParam = request.getParameter("w");
if (widthParam == null) {
widthParam = getServletConfig().getInitParameter("width");
}
int width = DEFAULT_WIDTH;
if (widthParam != null) {
try {
width = Integer.parseInt(widthParam);
} catch (NumberFormatException e) {
width = DEFAULT_WIDTH;
}
}
String heightParam = request.getParameter("h");
if (heightParam == null) {
heightParam = getServletConfig().getInitParameter("height");
}
int height = DEFAULT_HEIGHT;
if (heightParam != null) {
try {
height = Integer.parseInt(heightParam);
} catch (NumberFormatException e) {
height = DEFAULT_HEIGHT;
}
}
String exportGraphicsParam = request.getParameter("exportGraphics");
boolean exportGraphics = true;
if (exportGraphicsParam == null) {
exportGraphics = true;
} else {
exportGraphics = Boolean.valueOf(exportGraphicsParam).booleanValue();
}
if (request.getSession(false) == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
WebSession webSession = (WebSession) request.getSession(false).getAttribute(WebSession.SESSION_ATTRIBUTE_NAME);
if (webSession == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
WebContext ctx = findWebContext(webSession, webCtxName);
if (ctx == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
WebMap webMap = ctx.getWebMap();
if (webMap == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String resourcesParam = request.getParameter("r");
List<String> resourceIDList = null;
Map<MapFunctionality, Boolean> mapFuncStates = null;
WebExtent extent = null;
if (resourcesParam != null) {
String[] resourceIDs = resourcesParam.split(",");
resourceIDList = java.util.Arrays.asList(resourceIDs);
}
WebMapImageExporter exporter = new WebMapImageExporter();
exporter.setDpi(dpi);
exporter.setImageHeight(height);
exporter.setImageWidth(width);
exporter.setImageFormatAsString(format);
exporter.setWebMap(webMap);
exporter.setExportGraphicsResources(exportGraphics);
if (extent != null) {
exporter.setExtent(extent);
}
if (resourceIDList != null) {
exporter.setResourceIDFilter(resourceIDList);
}
response.setContentType(contentType);
exporter.exportImage(response.getOutputStream());
if (mapFuncStates == null) {
return;
}
for (Entry<MapFunctionality, Boolean> entry : mapFuncStates.entrySet()) {
entry.getKey().setDisabled(entry.getValue());
}
ctx.refresh();
}
/**
* Finds the WebContext in the WebSession with the specified Name.
*
* @param webSession Contains a collection of WebContext objects.
* @param webCtxName Name of WebContext to find.
*/
private WebContext findWebContext (WebSession webSession, String webCtxName) {
List<WebContext> webContexts = webSession.getWebContexts();
for (WebContext ctx : webContexts) {
if (webCtxName.equals(ctx.getName())) {
return ctx;
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -