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

📄 wcsdispatcher.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org.  All rights reserved.
 * This code is licensed under the GPL 2.0 license, availible at the root
 * application directory.
 */
package org.vfny.geoserver.wcs.servlets;

import org.geoserver.ows.util.EncodingInfo;
import org.geoserver.ows.util.XmlCharsetDetector;
import org.vfny.geoserver.ServiceException;
import org.vfny.geoserver.global.GeoServer;
import org.vfny.geoserver.servlets.AbstractService;
import org.vfny.geoserver.servlets.Dispatcher;
import org.vfny.geoserver.util.requests.readers.DispatcherKvpReader;
import org.vfny.geoserver.util.requests.readers.DispatcherXmlReader;
import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
import org.vfny.geoserver.wcs.WcsException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Routes requests made at the top-level URI to appropriate interface servlet.
 * Note that the logic of this method could be generously described as
 * 'loose.' It is not checking for request validity in any way (this is done
 * by the reqeust- specific servlets).  Rather, it is attempting to make a
 * reasonable guess as to what servlet to call, given that the client is
 * routing to the top level URI as opposed to the request-specific URI, as
 * specified in the GetCapabilities response. Thus, this is a convenience
 * method, which allows for some slight client laziness and helps explain to
 * lost souls/spiders what lives at the URL. Due to the string parsing, it is
 * much faster (and recommended) to use the URIs specified in the
 * GetCapabablities response.  Currently does not support post requests, but
 * most requests for this will likely come with get.
 *
 * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
 * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
 * @version $Id: WcsDispatcher.java 7746 2007-11-13 15:38:35Z aaime $
 */
public class WcsDispatcher extends Dispatcher {
    /**
         * Comment for <code>serialVersionUID</code>
         */
    private static final long serialVersionUID = 3977857384599203894L;

    /** Class logger */
    private static Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.servlets.wcs");
    private static int sequence = 123;
    private static final String DEFAULT_ENCODING = "UTF-8";
    private static final String ENCODING_HEADER_ARG = "Content-Type";
    private static final Pattern ENCODING_PATTERN = Pattern.compile(
            "encoding\\s*\\=\\s*\"([^\"]+)\"");

    /** Temporary file used to store the request */
    private File temp;

    /**
     * This figures out a dispatched post request.  It writes the request to a
     * temp file, and then reads it in twice from there.  I found no other way
     * to do this, but this solution doesn't seem to bad.  Obviously it is
     * better to use the servlets directly, without having to go through this
     * random file writing and reading.  If our xml handlers were written more
     * dynamically this probably wouldn't be necessary, but it is.  Hopefully
     * this should help GeoServer's interoperability with clients, since most
     * of them are kinda stupid, and can't handle different url locations for
     * the different requests.
     *
     * @param request The servlet request object.
     * @param response The servlet response object.
     *
     * @throws ServletException For any servlet problems.
     * @throws IOException For any io problems.
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        int targetRequest = 0;

        try {
            InputStream is = new BufferedInputStream(request.getInputStream());

            // REVISIT: Should do more than sequence here
            // (In case we are running two GeoServers at once)
            // - Could we use response.getHandle() in the filename?
            // - ProcessID is traditional, I don't know how to find that in Java
            sequence++;
            temp = File.createTempFile("wcsdispatch" + sequence, "tmp");

            FileOutputStream fos = new FileOutputStream(temp);
            BufferedOutputStream out = new BufferedOutputStream(fos);

            int c;

            while (-1 != (c = is.read())) {
                out.write(c);
            }

            is.close();
            out.flush();
            out.close();

            //JD: GEOS-323, Adding char encoding support
            EncodingInfo encInfo = new EncodingInfo();

            BufferedReader disReader;
            BufferedReader requestReader;

            try {
                disReader = new BufferedReader(XmlCharsetDetector.getCharsetAwareReader(
                            new FileInputStream(temp), encInfo));

                requestReader = new BufferedReader(XmlCharsetDetector.createReader(
                            new FileInputStream(temp), encInfo));
            } catch (Exception e) {
                /*
                 * Any exception other than WcsException will "hang up" the
                 * process - no client output, no log entries, only "Internal
                 * server error". So this is a little trick to make detector's
                 * exceptions "visible".
                 */
                throw new WcsException(e);
            }

            //          
            //          String req_enc = guessRequestEncoding(request);
            //          BufferedReader disReader = new BufferedReader(
            //                                      new InputStreamReader(
            //                                       new FileInputStream(temp), req_enc));
            //
            //          BufferedReader requestReader = new BufferedReader(
            //                                       new InputStreamReader(
            //                                        new FileInputStream(temp), req_enc));

            //JD: GEOS-323, Adding char encoding support
            if (disReader != null) {
                DispatcherXmlReader requestTypeAnalyzer = new DispatcherXmlReader();

                try {
                    requestTypeAnalyzer.read(disReader, request);
                } catch (ServiceException e) {
                    throw new WcsException(e);
                }

                //targetRequest = requestTypeAnalyzer.getRequestType();
            } else {
                targetRequest = UNKNOWN;
            }

            LOGGER.fine("post got request " + targetRequest);

            doResponse(requestReader, request, response, targetRequest);
        } catch (ServiceException wcs) {
            HttpSession session = request.getSession();
            ServletContext context = session.getServletContext();
            GeoServer geoServer = (GeoServer) context.getAttribute(GeoServer.WEB_CONTAINER_KEY);
            String tempResponse = ((WcsException) wcs).getXmlResponse(geoServer.isVerboseExceptions(),
                    request, geoServer);

            response.setContentType(geoServer.getCharSet().toString());
            response.getWriter().write(tempResponse);
        }
    }

    /**
     * Handles all Get requests.  This method implements the main matching

⌨️ 快捷键说明

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