📄 mapcomppreviewservlet.java
字号:
package com.esri.solutions.jitk.web.tasks.mapcomp;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.esri.solutions.jitk.common.personalization.ConstantUserId;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.data.IMapComposition;
import com.esri.solutions.jitk.personalization.data.IPersonalizationData;
import com.esri.solutions.jitk.personalization.data.IPersonalizationDataFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.MapCompositionType;
import com.esri.solutions.jitk.personalization.data.beans.v1.PreviewType;
/**
* Generates the Preview for a specified Map Composition. 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/preview/<username>/<id> where
*
* <ul>
* <li>username is the name of the user that created the Map Composition
* <li>id is the unique identifier for the Map Composition
* </ul>
*
* In this example, this servlet would need to be mapped to /maps/preview/* URL patterns.
*
* <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>Personalization Data cannot be retrieved for the user.
* <li>Map Composition with the specified ID cannot be found.
* <li>ID is not in the correct format.
* <li>Preview cannot be found for the Map Composition
* <li>Any unexpected exceptions occur.
* </ul>
* </p>
*/
public class MapCompPreviewServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private static final String ERROR_INCORRECT_PATH_INFO = "Incorrect Path Information in Request.";
private static final String ERROR_ID_IN_BAD_FORMAT = "ID [{0}] is not in acceptable format";
private static final String ERROR_PERS_EXCEPTION = "Cannot retrieve Map Composition Preview [{0}]";
private static final String ERROR_NO_PREVIEW_IN_MAP_COMP = "Map Composition [{0}] does not contain a Preview.";
private static final String ERROR_MAP_COMP_NOT_FOUND = "Map Composition [{0}] not found.";
private static final String ERROR_PERS_DATA_NOT_FOUND = "Unable to create Personalization Data object.";
private static final long serialVersionUID = 1986336733811201885L;
private static final Logger LOG = LogManager.getLogger(MapCompPreviewServlet.class);
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonalizationDataFactory factory = null;
IPersonalizationData data = null;
IMapComposition mc = null;
MapCompositionType bean = null;
ConstantUserId userId = null;
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.trim().length() == 0) {
LOG.error(ERROR_INCORRECT_PATH_INFO);
log(ERROR_INCORRECT_PATH_INFO);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
File resource = new File(pathInfo);
String username = resource.getParent().replace(File.separator, "");
String id = resource.getName();
userId = new ConstantUserId();
userId.setUsername(username);
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
factory = (IPersonalizationDataFactory) ctx.getBean("dbPersonalizationDataFactory");
try {
data = factory.create(userId);
} catch (PersonalizationException e) {
LOG.error(ERROR_PERS_DATA_NOT_FOUND);
log(ERROR_PERS_DATA_NOT_FOUND);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
try {
UUID mcId = UUID.fromString(id);
mc = data.getMapComposition(mcId);
if (mc == null) {
LOG.error(MessageFormat.format(ERROR_MAP_COMP_NOT_FOUND, id));
log(MessageFormat.format(ERROR_MAP_COMP_NOT_FOUND, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
bean = mc.getMapComposition();
PreviewType preview = bean.getPreview();
if (preview != null) {
byte[] preview64Data = preview.getValue();
byte[] imageData = Base64.decodeBase64(preview64Data);
response.setContentType(preview.getType());
ServletOutputStream out = response.getOutputStream();
out.write(imageData);
} else {
LOG.error(MessageFormat.format(ERROR_NO_PREVIEW_IN_MAP_COMP, id));
log(MessageFormat.format(ERROR_NO_PREVIEW_IN_MAP_COMP, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
} catch (PersonalizationException e) {
LOG.error(MessageFormat.format(ERROR_PERS_EXCEPTION, id));
log(MessageFormat.format(ERROR_PERS_EXCEPTION, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (IllegalArgumentException e) {
LOG.error(MessageFormat.format(ERROR_ID_IN_BAD_FORMAT, id));
log(MessageFormat.format(ERROR_ID_IN_BAD_FORMAT, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (NullPointerException npe) {
LOG.error(MessageFormat.format(ERROR_MAP_COMP_NOT_FOUND, id));
log(MessageFormat.format(ERROR_MAP_COMP_NOT_FOUND, id));
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -