Murain的笔记

Murain的笔记

记录遇到的一些问题和解决方法

我以前在项目中导出word文档需要使用Freemarker编写xml,非常麻烦。直到我发现了Poi-tl,在Poi-tl中使用Word文档就能创建文档模板。

创建模板

直接新建template.docx

{{title}}

写入内容

XWPFTemplate template = XWPFTemplate.compile("template.docx").render(
  new HashMap<String, Object>(){{
    put("title", "Hi, poi-tl Word模板引擎");
}});  
template.writeAndClose(new FileOutputStream("output.docx")); 

具体使用在官方文档中有非常详细的教程


Shapefile文件结构

Shapefile(SHP文件)通常由多个文件组成,每个文件都具有特定的后缀名并承载不同类型的数据。
以下是Shapefile各个文件后缀名的解释:

  1. .shp:主文件,它包含了地理要素的几何形状信息。每个几何对象(如点、线、多边形)都存储在此文件中。
  2. .shx:索引文件,它包含了地理要素的索引信息。索引文件加快了要素的读取和查询速度,可以通过索引快速定位到指定要素的位置。
  3. .dbf:属性表文件,它包含了地理要素的属性信息。属性表文件以dBase格式存储,每个记录代表一个要素,每个字段存储一个属性值。
  4. .prj:投影文件,它定义了地理数据的坐标系和投影信息。投影文件描述了地理数据如何映射到地图的平面坐标系统,以便正确显示和分析。
  5. .sbn.sbx:空间索引文件和空间索引索引文件的补充文件。这些文件存储了Shapefile中的要素的空间索引信息,用于快速查找空间关系和邻近要素。
  6. .cpg:代码页文件,它指定了属性表文件中文本字段的字符编码格式。它用于确保文本字段的正确显示和解释。

这些文件的后缀名和对应的文件内容结合在一起,构成了Shapefile的完整数据集。在使用Shapefile时,通常需要将这些文件一起保持在同一目录中,以便正确读取和使用地理数据。

使用Java解析

在Java中使用geotools解析shp

POM 依赖

<project>
    <repositories>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-referencing</artifactId>
            <version>24.7</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>24.7</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-extension</artifactId>
            <version>24.7</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-jts-wrapper</artifactId>
            <version>24.7</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>24.7</version>
        </dependency>
    </dependencies>
</project>

示例代码

// 请注意此代码示例仅作参考
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.geometry.Geometry;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Shapefile {

    public static void main(String[] args) throws IOException, FactoryException {
        File shapefile = new File("xxx.shp");
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> params = new HashMap<>();
        params.put("url", shapefile.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);
        params.put("charset", "UTF-8");
        ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(params);
        // 获取数据集名称
        for (String typeName : dataStore.getTypeNames()) {
            // 获取数据集的元数据信息
            SimpleFeatureType schema = dataStore.getSchema(typeName);
            // 获取坐标系
            CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
            // 获取坐标系的EPSG代码
            Integer srid = CRS.lookupEpsgCode(crs, true);
            SimpleFeatureSource featureSource = dataStore.getFeatureSource();
            SimpleFeatureCollection features = featureSource.getFeatures();
            try (SimpleFeatureIterator iterator = features.features()) {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    Geometry geometry = (Geometry) feature.getDefaultGeometry();
                }
            }
        }
    }
}

在项目中遇到了需要在实现HTTP反向代理的同时作为一个网关进行鉴权。我选择了 Smiley's HTTP Proxy Servlet 来实现。

原始ProxyServlet类

package org.mitre.dsmiley.httpproxy;

import javax.servlet.http.HttpServlet;

/**
 * An HTTP reverse proxy/gateway servlet. It is designed to be extended for customization
 * if desired. Most of the work is handled by
 * <a href="http://hc.apache.org/httpcomponents-client-ga/">Apache HttpClient</a>.
 * <p>
 *   There are alternatives to a servlet based proxy such as Apache mod_proxy if that is available to you. However
 *   this servlet is easily customizable by Java, secure-able by your web application's security (e.g. spring-security),
 *   portable across servlet engines, and is embeddable into another web application.
 * </p>
 * <p>
 *   Inspiration: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
 * </p>
 *
 * @author David Smiley dsmiley@apache.org
 */
@SuppressWarnings({"deprecation", "serial", "WeakerAccess"})
public class ProxyServlet extends HttpServlet {
    
  @Override
  protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
      throws ServletException, IOException {
    //...
  }
    // ...
}

对这个类进行一些重写

自定义异常处理

/**
 * 重写service方法,处理内部的异常
 */
override fun service(servletRequest: HttpServletRequest?, servletResponse: HttpServletResponse?) {
    logger.info("========= 重写请求 =========")
    try {
        super.service(servletRequest, servletResponse)
    } catch (e: ZZException) {
        logger.error("ZZException", e)
        handleZZException(e, servletRequest!!, servletResponse!!)
    }
}

/**
 * 处理Servlet的service异常部分,
 */
private fun handleZZException(ex: ZZException, request: HttpServletRequest, response: HttpServletResponse) {
    logger.info("RequestURI:{}", request.requestURI)
    // 设置HTTP状态码为403 Forbidden
    response.status = HttpStatus.FORBIDDEN.value()

    // 设置响应类型为JSON
    response.contentType = "application/json;charset=UTF-8"

    // 创建一个包含错误信息的map
    val errorDetails = mapOf(
        "code" to ex.code,
        "msg" to ex.message
    )
    // 将错误信息map转换为JSON字符串
    val json = jacksonObjectMapper().writeValueAsString(errorDetails)

    // 写入JSON字符串到响应体
    response.writer.write(json)

    // 记录日志或其他处理...
}

重写请求的query参数

/**
 * 重写请求的query参数
 */
override fun rewriteQueryStringFromRequest(servletRequest: HttpServletRequest, queryString: String?): String {
    if (queryString.isNullOrBlank()) {
        return ""
    }
    logger.debug("请求参数: $queryString")

    val queryStrBuffer = StringBuffer(queryString.length)
    queryString.split("&".toRegex()).filter { it.isNotBlank() }.forEach {
        processQueryParam(it, queryStrBuffer)
    }
    // 去掉最后一个&
    if (queryStrBuffer.endsWith("&")) {
        val removeRange = queryStrBuffer.removeRange(queryStrBuffer.length - 1, queryStrBuffer.length)
        return removeRange.toString()
    }
    return queryStrBuffer.toString()
}

/**
 * 处理请求参数
 */
protected open fun processQueryParam(param: String, queryStrBuffer: StringBuffer) {
    // tk
    if (param.lowercase().contains("tk=")) {
        val tkStr = param.substring(3)
        if (StrUtil.isBlank(tkStr)) {
            throw ZZException("403", "令牌不能为空")
        }
        tkTL.set(tkStr)
        return
    }
    queryStrBuffer.append("$param&")
}

重写请求的url

protected fun superRewriteUrlFromRequest(servletRequest: HttpServletRequest): String =
    super.rewriteUrlFromRequest(servletRequest)

/**
 * 重写请求的url
 */
override fun rewriteUrlFromRequest(servletRequest: HttpServletRequest): String {

    logger.debug("========= 重写请求url =========")
    // 制作请求 URI
    val uri = StringBuilder(500)

    // 目标uri
    uri.append(getTargetUri(servletRequest))

    // 处理给servlet的路径
    val pathInfo = rewritePathInfoFromRequest(servletRequest)
    uri.append(URLUtil.encode(pathInfo))

    // 处理 query string & fragment
    var queryString = URLUtil.decode(servletRequest.queryString)
    var fragment: String? = null
    // 从 queryString 中分离出 fragment,如果找到则更新 queryString
    val fragIdx = queryString?.indexOf('#')
    if (fragIdx != null) {
        if (fragIdx >= 0) {
            fragment = queryString.substring(fragIdx + 1)
            queryString = queryString.substring(0, fragIdx)
        }
    }

    queryString = rewriteQueryStringFromRequest(servletRequest, queryString)

    // 获取请求的ip
    val ip = getIpAddr(servletRequest)

    // 处理令牌
    val (appInfo, layerInfo) = checkAuth(tkTL.get(), ip, layerIdTL.get())
    // 记录日志
    processLogInfo(appInfo, layerInfo)

    uri.append('?')
    uri.append(queryString)
    if (doSendUrlFragment && fragment != null) {
        uri.append('#')
        uri.append(encodeUriQuery(fragment, false))
    }
    logger.info("重写后的url: $uri")
    return uri.toString()
}

鉴权处理令牌

protected open fun checkAuth(
    tk: String,
    ip: String,
    layerId: String
): Pair<Map<String, Any>, Map<String, Any?>> {
    logger.debug("tk:$tk\nlayerId:$layerId")
    val tokenMap = redisTool.getMap<String, Map<String, Any?>>("$platformName:platform:app:token")
    val appInfo = tokenMap.getMap(tk)
    if (appInfo.isEmpty()) {
        throw ZZException("403", "令牌无效")
    }
    //校验ip是否在白名单内
    checkIp(appInfo.getString("appIpWhitelist"), ip)

    val layers: List<Map<String, Any?>> = appInfo.getList("layers")
    val layerInfo = layers.find { StrUtil.equals(it.getString("lRealLayerId"), layerId) }
        ?: throw ZZException("403", "令牌与图层不匹配")
    logger.info("图层信息: $layerInfo")
    return Pair(appInfo, layerInfo)
}

注入Servlet

/**
 * OGC标准服务代理
 * WMTS
 */
@Bean
fun ogcServletRegistrationBean(proxyServlet: OGCServerProxyServlet): ServletRegistrationBean<OGCServerProxyServlet> {
    val servletRegistrationBean = ServletRegistrationBean(proxyServlet, "/geoserver/*")
    servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, "http://127.0.0.1:8080/geowebcache/service")
    servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true")
    return servletRegistrationBean
}