⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultpreviewbeancreator.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
package com.esri.solutions.jitk.web.tasks.mapcomp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.esri.solutions.jitk.common.export.image.WebMapImageExporter;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.PreviewType;

/**
 * Default implementation of {@link IPreviewBeanCreator}.  This implementation
 * will export the image to a byte array and then base64 encode the byte array
 * for storage within a {@link PreviewType} bean.  
 */
public class DefaultPreviewBeanCreator implements IPreviewBeanCreator {

	private static final String ERROR_NULL_MIME_TYPE = "Image MIME Type is required, and cannot be null.";
	private static final String ERROR_NULL_WEBMAP_IMAGE_EXPORTER = "WebMap Image Exporter is required, and cannot be null.";

	/**
	 * Logger to use to log messages from this class.
	 */
	private static final Logger LOG = LogManager.getLogger(DefaultPreviewBeanCreator.class);
	
	/**
	 * Reference to WebMap Image Exporter to export the map image.
	 */
	private WebMapImageExporter m_exporter;
	
	/**
	 * MIME Type of the image format, for storage within the PrevewType bean.
	 */
	private String m_mimeType;

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.web.tasks.mapcomp.IPreviewBeanCreator#createPreview()
	 */
	public PreviewType createPreview() {
		if (m_exporter == null) {
			LOG.error(ERROR_NULL_WEBMAP_IMAGE_EXPORTER);
			throw new IllegalStateException(ERROR_NULL_WEBMAP_IMAGE_EXPORTER);
		}
		if (m_mimeType == null) {
			LOG.error(ERROR_NULL_MIME_TYPE);
			throw new IllegalStateException(ERROR_NULL_MIME_TYPE);
		}
		
		int bytesWritten = -1;
		
		PreviewType preview = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bytesWritten = m_exporter.exportImage(baos);
			if (bytesWritten > 0) {
				preview = new ObjectFactory().createPreview();
				preview.setHeight(m_exporter.getImageHeight());
				preview.setWidth(m_exporter.getImageWidth());
				preview.setType(m_mimeType);
				preview.setValue(Base64.encodeBase64(baos.toByteArray()));
			}
		} catch (IOException e) {
			LOG.warn("Unable to generate Preview", e);
		} catch (JAXBException e) {
			LOG.warn("Unable to generate Preview", e);
		}
		return preview;
	}

	/**
	 * Sets the reference to the WebMap Image Exporter object.  This
	 * object will export the map image from a WebMap to a byte array.
	 * 
	 * @param exporter	Reference to WebMap Image Exporter object.
	 */
	public void setWebMapImageExporter (WebMapImageExporter exporter) {
		if (exporter == null) {
			throw new NullPointerException ();
		}
		m_exporter = exporter;
	}
	
	/**
	 * Returns the reference to the WebMap Image Exporter object.  This
	 * object will export the map image from a WebMap to a byte array.
	 * This method will return <code>null</code> if not preceded by
	 * a successful invocation of {@link #setWebMapImageExporter(WebMapImageExporter)}.
	 * 
	 * @return Reference to WebMap Image Exporter object.
	 */
	public WebMapImageExporter getWebMapImageExporter() {
		return m_exporter;
	}
	
	/**
	 * Sets the Image MIME Type that should be used.
	 * 
	 * @param mimeType		Image MIME Type, cannot be <code>null</code>.
	 * @throws NullPointerException	Thrown if the <code>mimeType</code> is
	 * 								<code>null</code>.
	 */
	public void setImageMimeType (String mimeType) {
		if (mimeType == null) {
			throw new NullPointerException ();
		}
		m_mimeType = mimeType;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -