繁体   English   中英

WSO2 Api Manager 3.2 - 自定义处理程序,没有登录文件,处理程序不工作

[英]WSO2 Api Manager 3.2 - Custom Handler, no log in file, handler not working

我正在尝试为 WSO2 Api Manager v3.2 开发自定义处理程序。 我使用了 WSO2AM 官方文档中提供的教程: https ://apim.docs.wso2.com/en/latest/develop/extending-api-manager/extending-gateway/writing-custom-handlers/

我试图让处理程序在发送到示例 API (PizzaShack) 的请求中添加一个“X-Request-ID”标头,但甚至找不到日志。 我不确定这是我的代码还是配置的问题。 我通过 osgi 控制台检查了处理程序在 WSO2AM 中被认为是活动的。

我的课:

package com.company.wso2.handlers;

import org.apache.synapse.MessageContext;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Random;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;

public class CustomHeaderHandler extends AbstractHandler {

    private static final String REQUEST_ID_HEADER="X-Request-ID";

    private static final Logger LOG = LoggerFactory.getLogger(CustomHeaderHandler.class);

    public boolean handleRequest(MessageContext messageContext) {
        Map<String, String> headers = (Map<String, String>) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
                getProperty(TRANSPORT_HEADERS);

        if (!headers.containsKey(REQUEST_ID_HEADER)) {
            LOG.info("Request-ID missing, adding new Request-ID");
            System.out.println("Request-ID missing, adding new Request-ID");
            headers.put(REQUEST_ID_HEADER, Integer.toHexString(new Random().nextInt(0x9999999) + 0x9999999));
            ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(TRANSPORT_HEADERS, headers);
            return true;
        }

        LOG.debug("Request-ID header present, continuing");
        System.out.println("Request-ID header present, continuing");

        return true;
    }

    public boolean handleResponse(MessageContext messageContext) {
        Map<String, String> headers = (Map<String, String>) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
                getProperty(TRANSPORT_HEADERS);

        if (!headers.containsKey(REQUEST_ID_HEADER)) {
            LOG.info("Request-ID missing, adding new Request-ID in response");
            System.out.println("Request-ID missing, adding new Request-ID in response");
            headers.put(REQUEST_ID_HEADER, Integer.toHexString(new Random().nextInt(0x9999999) + 0x9999999));
            ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(TRANSPORT_HEADERS, headers);
            return true;
        }

        LOG.debug("Request-ID header present, continuing in response");
        System.out.println("Request-ID header present, continuing in response");

        return true;
    }

}


我的 pom.xml 文件负责使用上面显示的类创建一个 jar:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.wso2.handlers</groupId>
    <artifactId>custom-header-handler</artifactId>
    <version>0.1</version>
    <packaging>bundle</packaging>

    <name>custom-header-handler</name>

    <dependencies>
        <dependency>
            <groupId>org.apache.synapse</groupId>
            <artifactId>synapse-core</artifactId>
            <version>2.1.7-wso2v10</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.snapshots</id>
            <name>Apache Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>

        <pluginRepository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>

        <pluginRepository>
            <id>wso2.snapshots</id>
            <name>WSO2 Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>1.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.company.wso2.handlers</Bundle-SymbolicName>
                        <Bundle-Name>com.company.wso2.handlers</Bundle-Name>
                        <Export-Package>
                             com.company.wso2.handlers.*,
                        </Export-Package>
                        <Import-Package>
                            *; resolution:=optional
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

构建 jar 后,我将其添加到此文件夹中: [WSO2AM_HOME]/repository/components/dropins/我还手动将此处理程序类添加到文件[WSO2AM_HOME]/repository/deployment/server/synapse-configs/default/api/admin--PizzaShackAPI_v1.0.0.xml 'handlers'部分如下:

<handlers>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.common.APIMgtLatencyStatsHandler">
            <property name="apiUUID" value="303aa4cb-fd51-4194-b319-aa0410a853f0"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler">
            <property name="apiImplementationType" value="ENDPOINT"/>
            <property name="AuthorizationHeader" value="Authorization"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler">
            <property name="RemoveOAuthHeadersFromOutMessage" value="true"/>
            <property name="APILevelPolicy" value=""/>
            <property name="AuthorizationHeader" value="Authorization"/>
            <property name="keyManagers" value="all"/>
            <property name="CertificateInformation" value="{}"/>
            <property name="APISecurity" value="oauth2,oauth_basic_auth_api_key_mandatory"/>
            <property name="apiUUID" value="303aa4cb-fd51-4194-b319-aa0410a853f0"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler"/>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtUsageHandler"/>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler">
            <property name="configKey" value="ga-config-key"/>
        </handler>
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
        <!-- logging according to docs -->
        <handler class="org.wso2.carbon.apimgt.gateway.handlers.logging.APILogMessageHandler"/>
        <!-- Custom request ID handler -->
        <handler class="com.company.wso2.handlers.CustomHeaderHandler"/>
    </handlers>

最后是[WSO2AM_HOME]/repository/conf/log4j2.properties文件的配置(剪切版):

(...)

loggers = (all the loggers provided by default in config), custom-log-handler

(...)

logger.custom-log-handler.name = com.company.wso2.handlers.CustomHeaderHandler
logger.custom-log-handler.level = DEBUG
logger.custom-log-handler.appenderRef.CARBON_LOGFILE.ref = CARBON_LOGFILE

如果您还有什么需要我提供的,请随时提出。 感谢您对这个问题的任何帮助。

我对您的自定义处理程序进行了快速检查。 一切正常。

[2020-11-10 19:28:05,138]  INFO - CustomHeaderHandler Request-ID missing, adding new Request-ID in response
Request-ID missing, adding new Request-ID in response
[2020-11-10 19:29:24,408]  INFO - CustomHeaderHandler Request-ID missing, adding new Request-ID
Request-ID missing, adding new Request-ID
[2020-11-10 19:29:24,486]  INFO - CustomHeaderHandler Request-ID missing, adding new Request-ID in response
Request-ID missing, adding new Request-ID in response

您能否从新包中恢复 log4j2.properties 文件并检查行为。

这不会有任何区别,但由于您使用的是 apim 3.2.0,因此请按如下方式更改依赖项

<dependency>
    <groupId>org.apache.synapse</groupId>
    <artifactId>synapse-core</artifactId>
    <version>2.1.7-wso2v183</version>
</dependency>

另外,您可以尝试使用 apache 公共日志记录。

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

private static final Log LOG = LogFactory.getLog(CustomHeaderHandler.class);

尝试以上操作并分享反馈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM