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

📄 agslocalclient.java

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

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.esri.adf.web.ags.data.AGSLocalConnection;
import com.esri.adf.web.ags.data.AGSUser;
import com.esri.arcgis.carto.IImageDescription;
import com.esri.arcgis.carto.IImageDisplay;
import com.esri.arcgis.carto.IImageType;
import com.esri.arcgis.carto.IMapDescription;
import com.esri.arcgis.carto.IMapImage;
import com.esri.arcgis.carto.IMapServer;
import com.esri.arcgis.carto.IMapServerInfo;
import com.esri.arcgis.carto.IMapServerProxy;
import com.esri.arcgis.carto.esriImageFormat;
import com.esri.arcgis.carto.esriImageReturnType;
import com.esri.arcgis.server.IEnumServerObjectConfigurationInfo;
import com.esri.arcgis.server.IEnumServerObjectConfigurationInfoProxy;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectConfigurationInfo;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.solutions.jitk.datasources.ogc.wms.InputStreamConverter;

@SuppressWarnings("deprecation")
public class AGSLocalClient {
	private static Logger _logger = LogManager.getLogger(AGSLocalClient.class.getName());	
	
	public AGSLocalClient() {
	}
	
    public byte[] getAGSMap(String server, String domain, String username, String password, String svrObj, boolean encrypted, int width, int height) {
        AGSLocalConnection ags = null;

        try {
            List<String> host = new ArrayList<String>();
            host.add(server);

            AGSUser agsUser = new AGSUser(domain, username, password, encrypted);
            ags = new AGSLocalConnection("", host, "", "none", agsUser);
            ags.initResource();

            IServerObjectManager mgr = ags.getServerObjectManager();
            IEnumServerObjectConfigurationInfo SOCollection = new IEnumServerObjectConfigurationInfoProxy(mgr.getConfigurationInfos());
            String typeName = "";
            String serverName = "";

            for (int j = 0; j < SOCollection.getCount(); j++) {
                IServerObjectConfigurationInfo srvObj = (IServerObjectConfigurationInfo) SOCollection.next();
                serverName = srvObj.getName();

                if (serverName.equalsIgnoreCase(svrObj)) {
                    typeName = srvObj.getTypeName();
                    break;
                }
            }

            if ((serverName.length() == 0) || (typeName.length() == 0)) {
                return null;
            }

            if (typeName.toUpperCase().indexOf("MAP") < 0) {
                return null;
            }

            IServerContext svrCxt = mgr.createServerContext(svrObj, typeName);
            IMapServer mapSvr = new IMapServerProxy(svrCxt.getServerObject());
            IMapServerInfo mapSvrInfo = mapSvr.getServerInfo(mapSvr.getDefaultMapName());
            IMapDescription mapDesc = mapSvrInfo.getDefaultMapDescription();
            
            IImageDescription imgDesc = (IImageDescription) svrCxt.createObject("esriCarto.ImageDescription");
            IImageDisplay imgDisp = (IImageDisplay) svrCxt.createObject("esriCarto.ImageDisplay");
            imgDisp.setHeight(height);
            imgDisp.setWidth(width);
            imgDisp.setDeviceResolution(96);
            imgDesc.setDisplay(imgDisp);

            IImageType imgType = (IImageType) svrCxt.createObject("esriCarto.ImageType");
            imgType.setFormat(esriImageFormat.esriImagePNG);
            imgType.setReturnType(esriImageReturnType.esriImageReturnURL);
            imgDesc.setType(imgType);

            IMapImage mapImg = mapSvr.exportMapImage(mapDesc, imgDesc);
            String url = mapImg.getURL();
            byte[] data = null;
            if (url == null || url.length() == 0) {
            	data = mapImg.getMimeData();
            } else {
            	data = loadImage(new URL(url));            	
            }
            	 
            svrCxt.releaseContext();
            return data;
            
        } catch(Exception e) {
        	_logger.warn("unable to preview ags local map", e);
            return null;
        
        } finally {
            if(ags != null) {
                ags.passivateResource();
            }
        }
    }
    
    public String[] getAgsServices(String server, String domain, String username, String password, boolean passwordEncrypted) { 
    	AGSLocalConnection ags = null;
        List<String> host = new ArrayList<String>();
        host.add(server);
        String[] agsServices;

        try {
        	
        	ags = new AGSLocalConnection("", host, "", "none", new AGSUser(chkStr(domain, "*"), chkStr(username, null), chkStr(password, null), passwordEncrypted));
            ags.initResource();

            IServerObjectManager mgr = ags.getServerObjectManager();
            IEnumServerObjectConfigurationInfo SOCollection = new IEnumServerObjectConfigurationInfoProxy(mgr.getConfigurationInfos());
            List<String> agsList = new ArrayList<String>();

            for (int j = 0; j < SOCollection.getCount(); j++) {
                IServerObjectConfigurationInfo srvObj = SOCollection.next();

                if (srvObj.getTypeName().toUpperCase().indexOf("MAP") >= 0) {
                    agsList.add(srvObj.getName());
                }
            }

            agsServices = new String[agsList.size()];

            Iterator<String> agsIt = agsList.iterator();
            int i = 0;

            while (agsIt.hasNext()) {
                agsServices[i] = (String) agsIt.next();
                i++;
            }

            return agsServices;
            
        } catch (Exception e) {
			_logger.warn("Unable to get ags service", e);
            return null;
            
        } finally {
            if (ags != null) {
                ags.passivateResource();
            }
        }
    }
    
	private String chkStr(String s, String defaultVal) {
		s = (s == null) ? "" : s.trim();
		return (s.length() == 0) ? defaultVal : s;
	}     
    
    private byte[] loadImage(URL url) throws Exception {
        InputStream is = url.openStream();
        InputStreamConverter isc = new InputStreamConverter();
        return isc.toByteArray(is);
    }      
}

⌨️ 快捷键说明

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