wmsrequesthandler.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 244 行

JAVA
244
字号
/* * $Header: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/image/wms/WmsRequestHandler.java,v 1.1.2.6 2008/02/19 23:34:44 dietrick Exp $ * * Copyright 2001-2005 OBR Centrum Techniki Morskiej, All rights reserved. * */package com.bbn.openmap.image.wms;import java.awt.Paint;import java.awt.Point;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.regex.Pattern;import com.bbn.openmap.LatLonPoint;import com.bbn.openmap.Layer;import com.bbn.openmap.event.ProjectionEvent;import com.bbn.openmap.image.ImageFormatter;import com.bbn.openmap.image.ImageServer;import com.bbn.openmap.image.ImageServerConstants;import com.bbn.openmap.image.ImageServerUtils;import com.bbn.openmap.image.MapRequestFormatException;import com.bbn.openmap.layer.util.http.HttpConnection;import com.bbn.openmap.layer.util.http.IHttpResponse;import com.bbn.openmap.omGraphics.OMColor;import com.bbn.openmap.proj.AspectRatioProjection;import com.bbn.openmap.proj.Proj;import com.bbn.openmap.proj.ProjectionFactory;import com.bbn.openmap.proj.coords.CoordinateReferenceSystem;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PropUtils;/** * @version $Header: *          /cvs/CVS_LEBA/external/openmap/openmap/src/openmap/com/bbn/openmap/wms/WmsRequestHandler.java,v *          1.2 2006/03/27 10:51:13 tomrak Exp $ * @author Adam Dawidowski * @author wachu * @author pitek */public class WmsRequestHandler extends ImageServer implements ImageServerConstants {    /**     */    private CapabilitiesSupport capabilities;    private Map wmsLayers;    private WmsLayerFactory wmsLayerFactory;        private Map imageFormatterByContentType = new HashMap();    /**     * Creates a new WmsRequestHandler object.     *      * @param port     * @param props Properties from openmap.properties     * @param mapLayerHandler     * @param wmsLayersMap     * @throws IOException     */    public WmsRequestHandler(String wmsScheme, String wmsHostName, int wmsPort, String wmsUrlPath,            Properties props) throws IOException, WMSException {        super(props);        setProperties(null, props);                // separate antialias property for wms.        boolean antialias = PropUtils.booleanFromProperties(props, "openmap.wms."                + AntiAliasingProperty, false);        setDoAntiAliasing(antialias);        // for each Openmap Layer created by ImageServer (defined in properties)        // create corresponding IWmsLayer which contains all neccesary        // information required by        // WMS (e.g getCapabilities method)        wmsLayerFactory = new WmsLayerFactory(props);        createWmsLayers();        // create a Map of all formatters by their contentType        for (Iterator it = getFormatters().values().iterator(); it.hasNext();) {            ImageFormatter formatter = (ImageFormatter) it.next();            imageFormatterByContentType.put(formatter.getContentType(), formatter);        }                // read from configuration fixed part of Capabilities Document returned        // in getCapabilities method        capabilities = new CapabilitiesSupport(props, wmsScheme, wmsHostName, wmsPort, wmsUrlPath);        List formatsList = new ArrayList(imageFormatterByContentType.keySet());        capabilities.setFormats(CapabilitiesSupport.FMT_GETMAP, formatsList);    }    /**     * For each layer managed by ImageServer create corresponding IWmsLayer     * which contains additional information for WMS service about given openmap     * layer.     *      * For Layers that already implement IWmsLayer, the instances will be the same.     */    protected void createWmsLayers() {        wmsLayers = new HashMap();        for (int i = 0; i < layers.length; i++) {            Layer layer = layers[i];            IWmsLayer wmsLayer = wmsLayerFactory.createWmsLayer(layer);            wmsLayers.put(wmsLayer.getWmsName(), wmsLayer);        }    }        protected Layer getLayerByName(String layerPropertyPrefix) {        for (int i = 0; i < layers.length; i++) {            Layer layer = layers[i];            if (layerPropertyPrefix.equals(layer.getPropertyPrefix())) {                return layer;            }        }        return null;    }    /**     * @param request     * @param out     * @throws IOException     * @throws MapRequestFormatException     */    public void handleRequest(Properties requestProperties, IHttpResponse httpResponse)            throws IOException, MapRequestFormatException {        try {            String requestType = requestProperties.getProperty(REQUEST);            checkRequest(requestProperties);            if (requestType == null) {                throw new WMSException("Missing REQUEST type parameter");            }            if (requestType.equalsIgnoreCase(GETMAP)) {                Debug.message("ms", "OGCMRH: GetMap request...");                handleGetMapRequest(requestProperties, httpResponse);            } else if (requestType.equals(GETCAPABILITIES)) {                Debug.message("ms", "OGCMRH: GetCapabilities request...");                handleGetCapabilitiesRequest(requestProperties, httpResponse);            } else if (requestType.equalsIgnoreCase(GETFEATUREINFO)) {                Debug.message("ms", "OGCMRH: GetFeatureInfo request...");                handleGetFeatureInfoRequest(requestProperties, httpResponse);            } else {                throw new WMSException("Invalid REQUEST parameter: " + requestType,                        WMSException.OPERATIONNOTSUPPORTED);            }        } catch (WMSException e) {            Debug.output("WMSException(" + e.getCode() + "): " + e.getMessage());            httpResponse.writeHttpResponse("application/vnd.ogc.se_xml", e.getXML());        }    }    /**     * @param requestProperties     * @param out     * @throws IOException     * @throws MapRequestFormatException     * @throws WMSException     */    public void handleGetMapRequest(Properties requestProperties, IHttpResponse httpResponse)            throws IOException, MapRequestFormatException, WMSException {        byte[] image = handleGetMapRequest(requestProperties);        if (Debug.debugging("imageserver")) {            Debug.output("OGCMRH: have completed image, size " + image.length);        }        String contentType = getFormatter().getContentType();        if (contentType == null) {            contentType = HttpConnection.CONTENT_PLAIN;        }        httpResponse.writeHttpResponse(contentType, image);    }    /**     * @param requestProperties     * @return     * @throws IOException     * @throws MapRequestFormatException     * @throws WMSException     */    public byte[] handleGetMapRequest(Properties requestProperties) throws IOException,            MapRequestFormatException, WMSException {        GetMapRequestParameters parameters = new GetMapRequestParameters();        checkFormat(requestProperties, parameters);        setFormatter(parameters.formatter);        checkBackground(requestProperties, parameters);        Paint bgPaint = parameters.background;        checkProjectionType(requestProperties, parameters);        checkBoundingBox(requestProperties, parameters);        Proj projection = createProjection(requestProperties, parameters);                checkLayersAndStyles(requestProperties, parameters);        Debug.message("ms", "handleGetMapRequest: createImage layers:" + parameters.layerNames.toString());        return createImage(projection, parameters.width, parameters.height, parameters.layerNames, bgPaint);    }    /**     * @param requestProperties     * @param out     * @throws IOException     * @throws MapRequestFormatException     * @throws WMSException     */    public void handleGetCapabilitiesRequest(Properties requestProperties,            IHttpResponse httpResponse) throws IOException, MapRequestFormatException, WMSException {        String response = handleGetCapabilitiesRequest(requestProperties);        httpResponse.writeHttpResponse(HttpConnection.CONTENT_XML, response.getBytes("UTF-8"));    }    /**     * @param requestProperties     * @return     * @throws IOException     * @throws MapRequestFormatException     * @throws WMSException     */    public String handleGetCapabilitiesRequest(Properties requestProperties) throws IOException,            MapRequestFormatException, WMSException {        String format = requestProperties.getProperty(FORMAT);        if (format != null && !format.equals("application/vnd.ogc.wms_xml")) {            throw new WMSException("Invalid FORMAT parameter.", WMSException.INVALIDFORMAT);        }        Layer[] layers = getLayers();        for (int i = 0; i < layers.length; i++) {            if (layers[i].getPropertyPrefix() != null) {                capabilities.addLayer(wmsLayerFactory.createWmsLayer(layers[i]));            }        }                try {            return capabilities.generateXMLString();        } catch (Exception e) {            // nie ma takiego kodu b酬du, ale s筪z

⌨️ 快捷键说明

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